#!/usr/bin/env elixir # Test script to validate unified header processing system # Tests both HeaderProcessor functionality and configuration validation Code.append_path("lib") require Logger defmodule HeaderProcessorTest do @moduledoc """ Test script to validate the unified header processing system. Tests: 1. HeaderProcessor module functionality 2. Configuration validation 3. Integration with existing header infrastructure 4. Both inbound and outbound processing """ def run_tests do IO.puts("\n๐Ÿงช Testing Unified Header Processing System") IO.puts("=" |> String.duplicate(50)) test_configurations() test_header_processing() test_integration_examples() IO.puts("\nโœ… All header processing tests completed!") end defp test_configurations do IO.puts("\n๐Ÿ“‹ Testing Header Configurations") IO.puts("-" |> String.duplicate(30)) # Test example configurations configs = DaProductApp.MercuryISO8583.Headers.HeaderProcessor.example_header_configs() Enum.each(configs, fn {name, config} -> case DaProductApp.MercuryISO8583.Headers.HeaderProcessor.validate_header_config(config) do {:ok, _validated} -> IO.puts("โœ… #{name}: Valid configuration") {:error, reason} -> IO.puts("โŒ #{name}: Invalid - #{inspect(reason)}") end end) end defp test_header_processing do IO.puts("\n๐Ÿ”„ Testing Header Processing") IO.puts("-" |> String.duplicate(30)) # Test sample ISO message iso_message = "0200F23E46D129E0900000000000000000000016374594847123456789012345890=25124012345678901234567890123456789\n" iso_bytes = iso_message |> String.trim() |> String.to_charlist() |> :binary.list_to_bin() # Test with different header configurations test_configs = [ {"Disabled headers", %{enabled: false}}, {"Simple BCD", %{enabled: true, type: :base, encoding: :bcd, pattern: "6000", length: 2}}, {"Visa-style", %{enabled: true, type: :base1, encoding: :bcd, pattern: "6000782000", length: 5}} ] Enum.each(test_configs, fn {name, config} -> test_header_round_trip(name, iso_bytes, config) end) end defp test_header_round_trip(name, iso_bytes, header_config) do IO.puts(" Testing: #{name}") # Test outbound processing (add header) case DaProductApp.MercuryISO8583.Headers.HeaderProcessor.process_message(iso_bytes, header_config, :outbound) do {:ok, framed_message} -> IO.puts(" โœ… Outbound: Added header (#{byte_size(framed_message)} bytes)") # Test inbound processing (extract header) case DaProductApp.MercuryISO8583.Headers.HeaderProcessor.process_message(framed_message, header_config, :inbound) do {:ok, {clean_message, _header_info}} -> if clean_message == iso_bytes do IO.puts(" โœ… Inbound: Header extracted correctly") else IO.puts(" โŒ Inbound: Message mismatch after round-trip") end {:error, reason} -> IO.puts(" โŒ Inbound: Failed to extract header - #{inspect(reason)}") end {:error, reason} -> IO.puts(" โŒ Outbound: Failed to add header - #{inspect(reason)}") end end defp test_integration_examples do IO.puts("\n๐Ÿ”— Testing Integration Examples") IO.puts("-" |> String.duplicate(30)) # Show how this works with upstream network configs upstream_configs = [ {:visa, %{ header_config: %{ enabled: true, type: :base1, encoding: :bcd, pattern: "6000782000", length: 5 } }}, {:amex, %{ header_config: %{enabled: false} }} ] Enum.each(upstream_configs, fn {network, config} -> header_config = Map.get(config, :header_config) case DaProductApp.MercuryISO8583.Headers.HeaderProcessor.validate_header_config(header_config) do {:ok, _} -> IO.puts("โœ… #{network}: Upstream configuration valid") {:error, reason} -> IO.puts("โŒ #{network}: Configuration invalid - #{inspect(reason)}") end end) end end # Run the tests if this file is executed directly if System.argv() |> Enum.any?(&(&1 == "--test")) do HeaderProcessorTest.run_tests() else IO.puts("Run with --test flag to execute tests") IO.puts("Example: elixir test_unified_headers.exs --test") end