/** * JavaScript Dictionary Receiver Test * Tests the smartreceive function with dictionary payloads */ const NATSBridge = require('../src/natsbridge.js'); 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 Receiver Test ===\n'); // Create a mock NATS message with dictionary payloads const simpleDict = { key1: 'value1', key2: 'value2' }; const nestedDict = { outer: { inner: 'value', number: 42 } }; const arrayDict = { items: [1, 2, 3, 'four', 'five'] }; const mixedDict = { string: 'text', number: 123, boolean: true, null_val: null }; const testData = { correlation_id: 'test-receiver-dict-' + Date.now(), msg_id: 'msg-' + Date.now(), timestamp: new Date().toISOString(), send_to: '/test/dictionary', msg_purpose: 'test', sender_name: 'js-dict-test', sender_id: 'sender-' + Date.now(), receiver_name: 'js-receiver', receiver_id: 'receiver-' + Date.now(), reply_to: '', reply_to_msg_id: '', broker_url: TEST_BROKER_URL, metadata: {}, payloads: [ { id: 'payload-1', dataname: 'simple_dict', payload_type: 'dictionary', transport: 'direct', encoding: 'base64', size: Buffer.from(JSON.stringify(simpleDict)).length, data: Buffer.from(JSON.stringify(simpleDict)).toString('base64'), metadata: { payload_bytes: Buffer.from(JSON.stringify(simpleDict)).length } }, { id: 'payload-2', dataname: 'nested_dict', payload_type: 'dictionary', transport: 'direct', encoding: 'base64', size: Buffer.from(JSON.stringify(nestedDict)).length, data: Buffer.from(JSON.stringify(nestedDict)).toString('base64'), metadata: { payload_bytes: Buffer.from(JSON.stringify(nestedDict)).length } }, { id: 'payload-3', dataname: 'array_dict', payload_type: 'dictionary', transport: 'direct', encoding: 'base64', size: Buffer.from(JSON.stringify(arrayDict)).length, data: Buffer.from(JSON.stringify(arrayDict)).toString('base64'), metadata: { payload_bytes: Buffer.from(JSON.stringify(arrayDict)).length } }, { id: 'payload-4', dataname: 'mixed_dict', payload_type: 'dictionary', transport: 'direct', encoding: 'base64', size: Buffer.from(JSON.stringify(mixedDict)).length, data: Buffer.from(JSON.stringify(mixedDict)).toString('base64'), metadata: { payload_bytes: Buffer.from(JSON.stringify(mixedDict)).length } } ] }; const mockMsg = { payload: JSON.stringify(testData) }; console.log('Mock Message Created:'); console.log(` Correlation ID: ${testData.correlation_id}`); console.log(` Payloads: ${testData.payloads.length}`); console.log(` Payload types: ${testData.payloads.map(p => p.payload_type).join(', ')}\n`); try { // Receive and process the message console.log('Receiving and processing message...'); const env = await NATSBridge.smartreceive( mockMsg, { max_retries: 3, base_delay: 100, max_delay: 1000 } ); console.log('\n=== Received Envelope ==='); 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(`Payloads: ${env.payloads.length}\n`); // Validate received data console.log('=== Validation ==='); let passed = true; if (!env.correlation_id) { console.log('❌ correlation_id is missing'); passed = false; } else { console.log('✅ correlation_id present'); } if (env.payloads.length !== 4) { console.log(`❌ Expected 4 payloads, got ${env.payloads.length}`); passed = false; } else { console.log('✅ Correct number of payloads'); } // Expected data const expectedData = [ ['simple_dict', simpleDict, 'dictionary'], ['nested_dict', nestedDict, 'dictionary'], ['array_dict', arrayDict, 'dictionary'], ['mixed_dict', mixedDict, 'dictionary'] ]; for (let i = 0; i < env.payloads.length; i++) { const payload = env.payloads[i]; const expected = expectedData[i]; if (payload[0] !== expected[0]) { console.log(`❌ Payload ${i + 1}: Expected dataname '${expected[0]}', got '${payload[0]}'`); passed = false; } else { console.log(`✅ Payload ${i + 1}: Correct dataname`); } if (payload[2] !== expected[2]) { console.log(`❌ Payload ${i + 1}: Expected type '${expected[2]}', got '${payload[2]}'`); passed = false; } else { console.log(`✅ Payload ${i + 1}: Correct type`); } const dataMatch = JSON.stringify(payload[1]) === JSON.stringify(expected[1]); if (!dataMatch) { console.log(`❌ Payload ${i + 1}: Data mismatch`); console.log(` Expected: ${JSON.stringify(expected[1])}`); console.log(` Got: ${JSON.stringify(payload[1])}`); passed = false; } else { console.log(`✅ Payload ${i + 1}: Data correctly deserialized`); } } // Test round-trip with receive console.log('\n=== Round-trip Test ==='); const roundTripData = { correlation_id: 'roundtrip-' + Date.now(), msg_id: 'msg-' + Date.now(), timestamp: new Date().toISOString(), send_to: '/test/dictionary', msg_purpose: 'test', sender_name: 'js-test', sender_id: 'sender-' + Date.now(), receiver_name: 'js-receiver', receiver_id: 'receiver-' + Date.now(), reply_to: '', reply_to_msg_id: '', broker_url: TEST_BROKER_URL, metadata: {}, payloads: [ { id: 'payload-rt', dataname: 'roundtrip', payload_type: 'dictionary', transport: 'direct', encoding: 'base64', size: Buffer.from(JSON.stringify({ test: 'data', nested: { a: 1, b: 2 } })).length, data: Buffer.from(JSON.stringify({ test: 'data', nested: { a: 1, b: 2 } })).toString('base64'), metadata: { payload_bytes: Buffer.from(JSON.stringify({ test: 'data', nested: { a: 1, b: 2 } })).length } } ] }; const mockRtMsg = { payload: JSON.stringify(roundTripData) }; const rtEnv = await NATSBridge.smartreceive(mockRtMsg); if (rtEnv.payloads.length === 1 && rtEnv.payloads[0][0] === 'roundtrip' && rtEnv.payloads[0][2] === 'dictionary') { console.log('✅ Round-trip test successful'); } else { console.log('❌ Round-trip test failed'); 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();