#!/usr/bin/env elixir # Standalone test for header system validation defmodule HeaderValidationTest do @moduledoc """ Simple validation test for header system components. Tests basic functionality without complex mocking. """ def run_tests do IO.puts("๐Ÿงช Running Header System Validation Tests...") IO.puts("=" <> String.duplicate("=", 50)) test_hex_validation() test_bcd_validation() test_header_config_validation() IO.puts("โœ… All basic validation tests completed!") end defp test_hex_validation do IO.puts("\n๐Ÿ“ฆ Testing Hex String Validation...") # Test valid hex strings valid_hex_strings = [ "6000782000", "123456", "ABCDEF", "abcdef", "00FF00", "A" # Should be padded to "0A" ] invalid_hex_strings = [ "ZZZZ", "12G4", "hello", "" ] IO.puts(" Testing valid hex strings...") Enum.each(valid_hex_strings, fn hex -> case validate_hex_string(hex) do {:ok, _} -> IO.puts(" โœ… '#{hex}' - Valid") {:error, reason} -> IO.puts(" โŒ '#{hex}' - Failed: #{reason}") end end) IO.puts(" Testing invalid hex strings...") Enum.each(invalid_hex_strings, fn hex -> case validate_hex_string(hex) do {:error, _} -> IO.puts(" โœ… '#{hex}' - Correctly rejected") {:ok, _} -> IO.puts(" โŒ '#{hex}' - Should have been rejected") end end) end defp test_bcd_validation do IO.puts("\n๐Ÿ”ข Testing BCD String Validation...") valid_bcd_strings = [ {"123456", 3}, {"000001", 3}, {"999999", 3}, {"12", 2} ] invalid_bcd_strings = [ {"12345A", 3}, # Contains non-digit {"", 3}, # Empty {"123", 0} # Zero length ] IO.puts(" Testing valid BCD strings...") Enum.each(valid_bcd_strings, fn {bcd, length} -> case validate_bcd_string(bcd, length) do {:ok, _} -> IO.puts(" โœ… '#{bcd}' (#{length} bytes) - Valid") {:error, reason} -> IO.puts(" โŒ '#{bcd}' (#{length} bytes) - Failed: #{reason}") end end) IO.puts(" Testing invalid BCD strings...") Enum.each(invalid_bcd_strings, fn {bcd, length} -> case validate_bcd_string(bcd, length) do {:error, _} -> IO.puts(" โœ… '#{bcd}' (#{length} bytes) - Correctly rejected") {:ok, _} -> IO.puts(" โŒ '#{bcd}' (#{length} bytes) - Should have been rejected") end end) end defp test_header_config_validation do IO.puts("\nโš™๏ธ Testing Header Configuration Validation...") # Valid configuration valid_config = %{ enabled: true, type: :base1, encoding: :hex, pattern: "6000782000", length: 5, source_address_offset: 2, source_address_length: 2, dest_address_offset: 4, dest_address_length: 2 } # Invalid configurations invalid_configs = [ %{enabled: true}, # Missing required fields %{enabled: true, type: :invalid_type, encoding: :hex}, # Invalid type %{enabled: true, type: :base1, encoding: :invalid_encoding}, # Invalid encoding %{enabled: true, type: :base1, encoding: :hex, pattern: "ZZZZ"} # Invalid pattern ] IO.puts(" Testing valid configuration...") case validate_header_config(valid_config) do {:ok, _} -> IO.puts(" โœ… Valid config - Accepted") {:error, reason} -> IO.puts(" โŒ Valid config - Failed: #{reason}") end IO.puts(" Testing invalid configurations...") Enum.each(invalid_configs, fn config -> case validate_header_config(config) do {:error, _} -> IO.puts(" โœ… Invalid config - Correctly rejected") {:ok, _} -> IO.puts(" โŒ Invalid config - Should have been rejected") end end) end # Simple validation functions (mimicking the real ones) defp validate_hex_string(hex_string) when is_binary(hex_string) do clean_hex = String.replace(hex_string, ~r/[^0-9A-Fa-f]/, "") if clean_hex == "" do {:error, :empty_string} else # Pad to even length padded_hex = if rem(String.length(clean_hex), 2) == 1 do "0" <> clean_hex else clean_hex end case Base.decode16(padded_hex) do {:ok, binary} -> {:ok, binary} :error -> {:error, :invalid_hex} end end end defp validate_bcd_string(bcd_string, byte_length) when is_binary(bcd_string) and is_integer(byte_length) do cond do byte_length <= 0 -> {:error, :invalid_length} !Regex.match?(~r/^[0-9]+$/, bcd_string) -> {:error, :invalid_bcd} String.length(bcd_string) == 0 -> {:error, :empty_string} true -> {:ok, :valid_bcd} end end defp validate_header_config(config) when is_map(config) do required_fields = [:enabled, :type, :encoding] cond do !Enum.all?(required_fields, &Map.has_key?(config, &1)) -> {:error, :missing_required_fields} config[:type] not in [:base1, :custom] -> {:error, :invalid_type} config[:encoding] not in [:hex, :bcd, :ascii] -> {:error, :invalid_encoding} Map.has_key?(config, :pattern) and config[:encoding] == :hex -> case validate_hex_string(config[:pattern]) do {:ok, _} -> {:ok, :valid} {:error, _} -> {:error, :invalid_pattern} end true -> {:ok, :valid} end end end # Run the tests HeaderValidationTest.run_tests()