/** * JavaScript Dictionary Sender Test * Tests the smartsend function with dictionary payloads */ const NATSBridge = require('../src/natsbridge.js'); const TEST_SUBJECT = '/test/dictionary'; const TEST_BROKER_URL = process.env.NATS_URL || 'nats://localhost:4222'; const TEST_FILESERVER_URL = process.env.FILESERVER_URL || 'http://localhost:8080'; async function runTest() { console.log('=== JavaScript Dictionary Sender Test ===\n'); const correlationId = NATSBridge.uuidv4(); console.log(`Correlation ID: ${correlationId}`); console.log(`Subject: ${TEST_SUBJECT}`); console.log(`Broker URL: ${TEST_BROKER_URL}\n`); // Test data - various dictionary structures const testData = [ ['simple_dict', { key1: 'value1', key2: 'value2' }, 'dictionary'], ['nested_dict', { outer: { inner: 'value', number: 42 } }, 'dictionary'], ['array_dict', { items: [1, 2, 3, 'four', 'five'] }, 'dictionary'], ['mixed_dict', { string: 'text', number: 123, boolean: true, null_val: null }, 'dictionary'] ]; try { // Send the message console.log('Sending dictionary payloads...'); const [env, envJsonStr] = await NATSBridge.smartsend( TEST_SUBJECT, testData, { broker_url: TEST_BROKER_URL, fileserver_url: TEST_FILESERVER_URL, correlation_id: correlationId, msg_purpose: 'test', sender_name: 'js-dict-test', is_publish: false } ); console.log('\n=== Envelope Created ==='); console.log(`Correlation ID: ${env.correlation_id}`); console.log(`Message ID: ${env.msg_id}`); console.log(`Timestamp: ${env.timestamp}`); console.log(`Subject: ${env.send_to}`); console.log(`Purpose: ${env.msg_purpose}`); console.log(`Sender: ${env.sender_name}`); console.log(`Payloads: ${env.payloads.length}\n`); // Validate envelope structure console.log('=== Validation ==='); let passed = true; if (env.payloads.length !== 4) { console.log(`❌ Expected 4 payloads, got ${env.payloads.length}`); passed = false; } else { console.log('✅ Correct number of payloads'); } // Test each payload const expectedDatanames = ['simple_dict', 'nested_dict', 'array_dict', 'mixed_dict']; const expectedTypes = ['dictionary', 'dictionary', 'dictionary', 'dictionary']; for (let i = 0; i < env.payloads.length; i++) { const payload = env.payloads[i]; if (payload.dataname !== expectedDatanames[i]) { console.log(`❌ Payload ${i + 1}: Expected dataname '${expectedDatanames[i]}', got '${payload.dataname}'`); passed = false; } else { console.log(`✅ Payload ${i + 1}: Correct dataname`); } if (payload.payload_type !== expectedTypes[i]) { console.log(`❌ Payload ${i + 1}: Expected type '${expectedTypes[i]}', got '${payload.payload_type}'`); passed = false; } else { console.log(`✅ Payload ${i + 1}: Correct type`); } if (payload.transport !== 'direct') { console.log(`❌ Payload ${i + 1}: Expected transport 'direct', got '${payload.transport}'`); passed = false; } else { console.log(`✅ Payload ${i + 1}: Correct transport`); } if (payload.encoding !== 'base64') { console.log(`❌ Payload ${i + 1}: Expected encoding 'base64', got '${payload.encoding}'`); passed = false; } else { console.log(`✅ Payload ${i + 1}: Correct encoding`); } // Decode and verify the data const decodedData = JSON.parse(Buffer.from(payload.data, 'base64').toString('utf8')); const originalData = testData[i][1]; const originalJson = JSON.stringify(originalData); const decodedJson = JSON.stringify(decodedData); if (originalJson !== decodedJson) { console.log(`❌ Payload ${i + 1}: Data integrity mismatch`); console.log(` Expected: ${originalJson}`); console.log(` Got: ${decodedJson}`); passed = false; } else { console.log(`✅ Payload ${i + 1}: Data integrity verified`); } console.log(` Size: ${payload.size} bytes\n`); } // Test round-trip serialization console.log('=== Round-trip Serialization Test ==='); const roundTripTestData = [ ['roundtrip', { test: 'data', numbers: [1, 2, 3], nested: { a: 1, b: 2 } }, 'dictionary'] ]; const [rtEnv, rtEnvJsonStr] = await NATSBridge.smartsend( TEST_SUBJECT, roundTripTestData, { broker_url: TEST_BROKER_URL, fileserver_url: TEST_FILESERVER_URL, correlation_id: 'roundtrip-' + correlationId, is_publish: false } ); const rtPayload = rtEnv.payloads[0]; const rtDecoded = JSON.parse(Buffer.from(rtPayload.data, 'base64').toString('utf8')); if (JSON.stringify(rtDecoded) === JSON.stringify(roundTripTestData[0][1])) { console.log('✅ Round-trip serialization successful'); } else { console.log('❌ Round-trip serialization failed'); passed = false; } // Test JSON string output console.log('\n=== JSON String Output Test ==='); try { const parsed = JSON.parse(envJsonStr); if (parsed.correlation_id === env.correlation_id && parsed.payloads.length === env.payloads.length) { console.log('✅ JSON string is valid and matches envelope'); } else { console.log('❌ JSON string does not match envelope'); passed = false; } } catch (e) { console.log('❌ JSON string is invalid:', e.message); passed = false; } // Final result console.log('\n=== Test Result ==='); if (passed) { console.log('✅ ALL TESTS PASSED'); process.exit(0); } else { console.log('❌ SOME TESTS FAILED'); process.exit(1); } } catch (error) { console.error('❌ Test failed with error:', error.message); console.error(error.stack); process.exit(1); } } runTest();