alias DaProductApp.MercuryISO8583.Packagers.{ISO87BPackager, ISOMsg, ISOComponent} IO.puts("=== Complete ISO8583 Packing Test ===") # Test 1: Create a complete ISO8583 message IO.puts("\n1. Creating complete ISO8583 message:") msg = ISOMsg.new("0200", ISO87BPackager) msg = ISOMsg.set(msg, 4, 123456) # Transaction Amount msg = ISOMsg.set(msg, 11, 123456) # STAN msg = ISOMsg.set(msg, 41, "TERMID01") # Terminal ID msg = ISOMsg.set(msg, 3, "000000") # Processing Code IO.puts("Message created with #{map_size(msg.fields)} fields") # Test 2: Verify all field packagers are assigned IO.puts("\n2. Verifying field packagers:") for {field_num, component} <- msg.fields do packager_type = if component.packager do component.packager.__struct__ |> to_string() |> String.split(".") |> List.last() else "nil" end IO.puts("Field #{field_num}: #{packager_type}") end # Test 3: Test individual field packing IO.puts("\n3. Testing individual field packing:") field_4 = ISOMsg.get_field(msg, 4) case ISOComponent.pack(field_4) do {:ok, packed_data} -> IO.puts("✅ Field 4 packed successfully: #{inspect(packed_data, limit: :infinity)}") IO.puts(" Packed length: #{byte_size(packed_data)} bytes") {:error, reason} -> IO.puts("❌ Field 4 pack failed: #{reason}") end # Test 4: Test complete message packing IO.puts("\n4. Testing complete message packing:") case ISO87BPackager.pack(msg) do {:ok, packed_message} -> IO.puts("✅ Complete message packed successfully!") IO.puts(" Total message length: #{byte_size(packed_message)} bytes") IO.puts(" First 50 bytes: #{inspect(binary_part(packed_message, 0, min(50, byte_size(packed_message))), limit: :infinity)}") {:error, reason} -> IO.puts("❌ Complete message pack failed: #{reason}") end # Test 5: Test message unpacking IO.puts("\n5. Testing message unpacking:") case ISO87BPackager.pack(msg) do {:ok, packed_message} -> case ISO87BPackager.unpack(packed_message) do {:ok, unpacked_msg} -> IO.puts("✅ Message unpacked successfully!") IO.puts(" MTI: #{unpacked_msg.mti}") IO.puts(" Fields: #{map_size(unpacked_msg.fields)}") # Verify unpacked field values for field_num <- [3, 4, 11, 41] do value = ISOMsg.getString(unpacked_msg, field_num) IO.puts(" Field #{field_num}: #{value}") end {:error, reason} -> IO.puts("❌ Message unpack failed: #{reason}") end {:error, reason} -> IO.puts("❌ Cannot test unpack, pack failed: #{reason}") end # Test 6: Round-trip test IO.puts("\n6. Round-trip integrity test:") original_fields = %{ 3 => ISOMsg.getString(msg, 3), 4 => ISOMsg.getString(msg, 4), 11 => ISOMsg.getString(msg, 11), 41 => ISOMsg.getString(msg, 41) } case ISO87BPackager.pack(msg) do {:ok, packed} -> case ISO87BPackager.unpack(packed) do {:ok, unpacked} -> unpacked_fields = %{ 3 => ISOMsg.getString(unpacked, 3), 4 => ISOMsg.getString(unpacked, 4), 11 => ISOMsg.getString(unpacked, 11), 41 => ISOMsg.getString(unpacked, 41) } if original_fields == unpacked_fields do IO.puts("✅ Round-trip test passed! All fields preserved.") else IO.puts("❌ Round-trip test failed!") IO.puts("Original: #{inspect(original_fields)}") IO.puts("Unpacked: #{inspect(unpacked_fields)}") end {:error, reason} -> IO.puts("❌ Round-trip failed at unpack: #{reason}") end {:error, reason} -> IO.puts("❌ Round-trip failed at pack: #{reason}") end IO.puts("\n=== Test Complete ===")