#!/usr/bin/env elixir # Start the application dependencies {:ok, _} = Application.ensure_all_started(:logger) # Start the Mix project to load dependencies Mix.start() Mix.env(:dev) Code.eval_file("mix.exs") # Load dependencies {:ok, _} = Application.ensure_all_started(:iso_8583) # Compile the modules we need Code.compile_file("lib/da_product_app/switch/message_handler.ex") alias DaProductApp.Switch.MessageHandler defmodule MessageHandlerTest do require Logger def test_mercury_tpdu_parsing do Logger.info("Testing Mercury TPDU message parsing...") # Test data: Mercury TPDU (6000782000) + binary ISO8583 message # TPDU: 6000782000 (5 bytes) # ISO8583: Binary format starting with MTI 0400 message_payload = << 0x60, 0x00, 0x78, 0x20, 0x00, # Mercury TPDU 0x04, 0x00, # MTI 0400 in BCD 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # Primary bitmap 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, # Fields data 0x89, 0xAB, 0xCD, 0xEF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 >> # Create proper length header length = byte_size(message_payload) test_data = <> <> message_payload Logger.info("Test data (#{byte_size(test_data)} bytes): #{Base.encode16(test_data)}") Logger.info("Message payload length: #{length} bytes") case MessageHandler.parse(test_data) do {:ok, result, _remaining} -> Logger.info("✅ Parse successful!") Logger.info("Result: #{inspect(result)}") # Check if TPDU was preserved case Map.get(result, :tpdu) do nil -> Logger.info("❌ TPDU context not preserved") tpdu -> Logger.info("✅ TPDU preserved: #{Base.encode16(tpdu)}") end {:error, reason} -> Logger.error("❌ Parse failed: #{inspect(reason)}") {:incomplete, _} -> Logger.info("⚠️ Message incomplete") end end def test_binary_data_analysis do Logger.info("Testing binary data analysis...") # Just the ISO8583 part without TPDU (for analysis) iso8583_binary = << 0x04, 0x00, # MTI 0400 in BCD 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # Primary bitmap 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, # Fields data 0x89, 0xAB, 0xCD, 0xEF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 >> Logger.info("Binary ISO8583 data: #{Base.encode16(iso8583_binary)}") # Test each decode strategy individually <> = iso8583_binary Logger.info("MTI bytes: #{Base.encode16(mti_bytes)} (#{inspect(mti_bytes)})") # BCD conversion test case mti_bytes do <> -> ascii_mti = "#{high_nibble}#{low_nibble}#{high_nibble2}#{low_nibble2}" Logger.info("BCD to ASCII MTI: #{ascii_mti}") _ -> Logger.info("❌ BCD conversion failed") end # Full hex conversion test hex_string = Base.encode16(iso8583_binary) Logger.info("Full hex conversion: #{String.slice(hex_string, 0, 50)}...") end def run_all_tests do Logger.info("=== Starting Message Handler Tests ===") test_binary_data_analysis() Logger.info("") test_mercury_tpdu_parsing() Logger.info("=== Tests Complete ===") end end MessageHandlerTest.run_all_tests()