diff --git a/test/test_js_binary_receiver.js b/test/test_js_binary_receiver.js index db8f5f0..0c9305e 100644 --- a/test/test_js_binary_receiver.js +++ b/test/test_js_binary_receiver.js @@ -4,6 +4,7 @@ */ const NATSBridge = require('../src/natsbridge.js'); +const crypto = require('crypto'); const TEST_BROKER_URL = process.env.NATS_URL || 'nats://localhost:4222'; const TEST_FILESERVER_URL = process.env.FILESERVER_URL || 'http://localhost:8080'; @@ -18,15 +19,15 @@ async function runTest() { const genericBinary = Buffer.from([0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE, 0xBA, 0xBE]); const testData = { - correlation_id: 'js-binary-receiver-' + Date.now(), - msg_id: 'msg-' + Date.now(), + correlation_id: 'js-binary-receiver-' + crypto.randomUUID(), + msg_id: 'msg-' + crypto.randomUUID(), timestamp: new Date().toISOString(), send_to: '/test/binary', msg_purpose: 'test', sender_name: 'js-binary-test', - sender_id: 'sender-' + Date.now(), + sender_id: 'sender-' + crypto.randomUUID(), receiver_name: 'js-receiver', - receiver_id: 'receiver-' + Date.now(), + receiver_id: 'receiver-' + crypto.randomUUID(), reply_to: '', reply_to_msg_id: '', broker_url: TEST_BROKER_URL, diff --git a/test/test_js_binary_sender.js b/test/test_js_binary_sender.js index dea75fb..7120bee 100644 --- a/test/test_js_binary_sender.js +++ b/test/test_js_binary_sender.js @@ -4,6 +4,7 @@ */ const NATSBridge = require('../src/natsbridge.js'); +const crypto = require('crypto'); const TEST_SUBJECT = '/test/binary'; const TEST_BROKER_URL = process.env.NATS_URL || 'nats://localhost:4222'; @@ -12,7 +13,7 @@ const TEST_FILESERVER_URL = process.env.FILESERVER_URL || 'http://localhost:8080 async function runTest() { console.log('=== JavaScript Binary Sender Test ===\n'); - const correlationId = NATSBridge.uuidv4(); + const correlationId = crypto.randomUUID(); console.log(`Correlation ID: ${correlationId}`); console.log(`Subject: ${TEST_SUBJECT}`); console.log(`Broker URL: ${TEST_BROKER_URL}\n`); diff --git a/test/test_js_dictionary_receiver.js b/test/test_js_dictionary_receiver.js index e18d57b..0a8c1ee 100644 --- a/test/test_js_dictionary_receiver.js +++ b/test/test_js_dictionary_receiver.js @@ -4,6 +4,7 @@ */ const NATSBridge = require('../src/natsbridge.js'); +const crypto = require('crypto'); const TEST_BROKER_URL = process.env.NATS_URL || 'nats://localhost:4222'; const TEST_FILESERVER_URL = process.env.FILESERVER_URL || 'http://localhost:8080'; @@ -18,15 +19,15 @@ async function runTest() { 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(), + correlation_id: 'test-receiver-dict-' + crypto.randomUUID(), + msg_id: 'msg-' + crypto.randomUUID(), timestamp: new Date().toISOString(), send_to: '/test/dictionary', msg_purpose: 'test', sender_name: 'js-dict-test', - sender_id: 'sender-' + Date.now(), + sender_id: 'sender-' + crypto.randomUUID(), receiver_name: 'js-receiver', - receiver_id: 'receiver-' + Date.now(), + receiver_id: 'receiver-' + crypto.randomUUID(), reply_to: '', reply_to_msg_id: '', broker_url: TEST_BROKER_URL, @@ -161,15 +162,15 @@ async function runTest() { // Test round-trip with receive console.log('\n=== Round-trip Test ==='); const roundTripData = { - correlation_id: 'roundtrip-' + Date.now(), - msg_id: 'msg-' + Date.now(), + correlation_id: 'roundtrip-' + crypto.randomUUID(), + msg_id: 'msg-' + crypto.randomUUID(), timestamp: new Date().toISOString(), send_to: '/test/dictionary', msg_purpose: 'test', sender_name: 'js-test', - sender_id: 'sender-' + Date.now(), + sender_id: 'sender-' + crypto.randomUUID(), receiver_name: 'js-receiver', - receiver_id: 'receiver-' + Date.now(), + receiver_id: 'receiver-' + crypto.randomUUID(), reply_to: '', reply_to_msg_id: '', broker_url: TEST_BROKER_URL, diff --git a/test/test_js_dictionary_sender.js b/test/test_js_dictionary_sender.js index e0eef78..b504da3 100644 --- a/test/test_js_dictionary_sender.js +++ b/test/test_js_dictionary_sender.js @@ -4,6 +4,7 @@ */ const NATSBridge = require('../src/natsbridge.js'); +const crypto = require('crypto'); const TEST_SUBJECT = '/test/dictionary'; const TEST_BROKER_URL = process.env.NATS_URL || 'nats://localhost:4222'; @@ -12,7 +13,7 @@ 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(); + const correlationId = crypto.randomUUID(); console.log(`Correlation ID: ${correlationId}`); console.log(`Subject: ${TEST_SUBJECT}`); console.log(`Broker URL: ${TEST_BROKER_URL}\n`); diff --git a/test/test_js_mix_payloads_receiver.js b/test/test_js_mix_payloads_receiver.js new file mode 100644 index 0000000..55f4077 --- /dev/null +++ b/test/test_js_mix_payloads_receiver.js @@ -0,0 +1,216 @@ +/** + * JavaScript Mix Payloads Receiver Test + * Tests the smartreceive function with mixed payload types + */ + +const NATSBridge = require('../src/natsbridge.js'); +const nats = require('nats'); +const crypto = require('crypto'); + +const TEST_SUBJECT = '/test/mix'; +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 Mix Payloads Receiver Test ===\n'); + + const correlationId = crypto.randomUUID(); + console.log(`Correlation ID: ${correlationId}`); + console.log(`Subject: ${TEST_SUBJECT}`); + console.log(`Broker URL: ${TEST_BROKER_URL}\n`); + + // Expected test data - same as sender + const expectedTextData = 'Hello, NATSBridge!'; + const expectedDictData = { key1: 'value1', key2: 42, nested: { a: 1, b: 2 } }; + const expectedBinaryData = Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]); // PNG header + const expectedTableData = [ + { id: 1, name: 'Alice', age: 30 }, + { id: 2, name: 'Bob', age: 25 }, + { id: 3, name: 'Charlie', age: 35 } + ]; + + const expectedDatanames = ['message', 'config', 'image', 'users']; + const expectedTypes = ['text', 'dictionary', 'image', 'table']; + + let testPassed = true; + let messagesReceived = 0; + const receivedPayloads = []; + + try { + // Connect to NATS + console.log('Connecting to NATS server...'); + const nc = await nats.connect({ servers: TEST_BROKER_URL }); + console.log('✅ Connected to NATS server\n'); + + // Set up message subscription + const subscription = nc.subscribe(TEST_SUBJECT); + + // Wait for messages with timeout + const messagePromise = new Promise(async (resolve, reject) => { + const timeout = setTimeout(() => { + resolve('timeout'); + }, 10000); // 10 second timeout + + (async () => { + for await (const msg of subscription) { + clearTimeout(timeout); + messagesReceived++; + console.log(`\n=== Message ${messagesReceived} Received ===`); + console.log(`Raw payload length: ${msg.payload.length} bytes`); + + try { + // Process the message using smartreceive + const envelope = await NATSBridge.smartreceive(msg, { + fileserver_download_handler: NATSBridge.fetchWithBackoff, + max_retries: 5, + base_delay: 100, + max_delay: 5000 + }); + + console.log(`Correlation ID: ${envelope.correlation_id}`); + console.log(`Message ID: ${envelope.msg_id}`); + console.log(`Number of payloads: ${envelope.payloads.length}`); + + receivedPayloads.push(envelope); + + // Validate envelope structure + console.log('\n=== Envelope Validation ==='); + + if (envelope.payloads.length < 4) { + console.log(`❌ Expected at least 4 payloads, got ${envelope.payloads.length}`); + testPassed = false; + } else { + console.log(`✅ Correct number of payloads: ${envelope.payloads.length}`); + } + + // Validate each payload + for (let i = 0; i < envelope.payloads.length; i++) { + const [dataname, data, dataType] = envelope.payloads[i]; + + console.log(`\n--- Payload ${i + 1}: ${dataname} (type: ${dataType}) ---`); + + // Check dataname + if (i < expectedDatanames.length && dataname !== expectedDatanames[i]) { + console.log(`❌ Expected dataname '${expectedDatanames[i]}', got '${dataname}'`); + testPassed = false; + } else { + console.log(`✅ Correct dataname: ${dataname}`); + } + + // Check data type + if (i < expectedTypes.length && dataType !== expectedTypes[i]) { + console.log(`❌ Expected type '${expectedTypes[i]}', got '${dataType}'`); + testPassed = false; + } else { + console.log(`✅ Correct type: ${dataType}`); + } + + // Validate data based on type + if (dataType === 'text') { + if (typeof data === 'string' && data === expectedTextData) { + console.log(`✅ Text data verified: "${data}"`); + } else { + console.log(`❌ Text data mismatch. Expected: "${expectedTextData}", Got: "${data}"`); + testPassed = false; + } + } else if (dataType === 'dictionary') { + if (typeof data === 'object' && JSON.stringify(data) === JSON.stringify(expectedDictData)) { + console.log(`✅ Dictionary data verified`); + console.log(` Keys: ${Object.keys(data).join(', ')}`); + } else { + console.log(`❌ Dictionary data mismatch`); + console.log(` Expected: ${JSON.stringify(expectedDictData)}`); + console.log(` Got: ${JSON.stringify(data)}`); + testPassed = false; + } + } else if (dataType === 'image') { + if (data instanceof Buffer || data instanceof Uint8Array) { + const dataBuffer = Buffer.isBuffer(data) ? data : Buffer.from(data); + if (dataBuffer.length === expectedBinaryData.length) { + let dataMatch = true; + for (let j = 0; j < expectedBinaryData.length; j++) { + if (dataBuffer[j] !== expectedBinaryData[j]) { + dataMatch = false; + break; + } + } + if (dataMatch) { + console.log(`✅ Image data verified (${dataBuffer.length} bytes)`); + } else { + console.log(`❌ Image data mismatch`); + testPassed = false; + } + } else { + console.log(`❌ Image data length mismatch. Expected: ${expectedBinaryData.length}, Got: ${dataBuffer.length}`); + testPassed = false; + } + } else { + console.log(`❌ Image data is not a Buffer or Uint8Array`); + testPassed = false; + } + } else if (dataType === 'table') { + // For table data, check if it's an Arrow table-like object + if (data && typeof data === 'object') { + // Arrow tables have specific properties + if (data.numRows !== undefined && data.numCols !== undefined) { + console.log(`✅ Table data verified`); + console.log(` Rows: ${data.numRows}, Columns: ${data.numCols}`); + } else { + console.log(`⚠️ Table data received but not standard Arrow format`); + console.log(` Keys: ${Object.keys(data).join(', ')}`); + } + } else { + console.log(`❌ Table data is not a valid object`); + testPassed = false; + } + } + } + + // Stop after receiving at least one valid message + if (messagesReceived >= 1) { + resolve('done'); + } + } catch (error) { + console.error(`❌ Error processing message: ${error.message}`); + console.error(error.stack); + testPassed = false; + resolve('error'); + } + } + })(); + }); + + console.log('Waiting for messages...\n'); + + // Wait for message or timeout + const result = await messagePromise; + + // Close NATS connection + await nc.close(); + console.log('\n✅ NATS connection closed'); + + // Final result + console.log('\n=== Test Result ==='); + if (messagesReceived === 0) { + console.log('❌ NO MESSAGES RECEIVED'); + console.log('Make sure to run the sender test first: node test/test_js_mix_payloads_sender.js'); + process.exit(1); + } else if (result === 'error') { + console.log('❌ ERROR PROCESSING MESSAGES'); + process.exit(1); + } else if (testPassed) { + 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(); \ No newline at end of file diff --git a/test/test_js_mix_payloads_sender.js b/test/test_js_mix_payloads_sender.js index 534fe5c..300942b 100644 --- a/test/test_js_mix_payloads_sender.js +++ b/test/test_js_mix_payloads_sender.js @@ -4,6 +4,7 @@ */ const NATSBridge = require('../src/natsbridge.js'); +const crypto = require('crypto'); const TEST_SUBJECT = '/test/mix'; const TEST_BROKER_URL = process.env.NATS_URL || 'nats://localhost:4222'; @@ -12,7 +13,7 @@ const TEST_FILESERVER_URL = process.env.FILESERVER_URL || 'http://localhost:8080 async function runTest() { console.log('=== JavaScript Mix Payloads Sender Test ===\n'); - const correlationId = NATSBridge.uuidv4(); + const correlationId = crypto.randomUUID(); console.log(`Correlation ID: ${correlationId}`); console.log(`Subject: ${TEST_SUBJECT}`); console.log(`Broker URL: ${TEST_BROKER_URL}\n`); diff --git a/test/test_js_table_receiver.js b/test/test_js_table_receiver.js index ec99ec4..f15a2b7 100644 --- a/test/test_js_table_receiver.js +++ b/test/test_js_table_receiver.js @@ -4,6 +4,7 @@ */ const NATSBridge = require('../src/natsbridge.js'); +const crypto = require('crypto'); const TEST_BROKER_URL = process.env.NATS_URL || 'nats://localhost:4222'; const TEST_FILESERVER_URL = process.env.FILESERVER_URL || 'http://localhost:8080'; @@ -42,15 +43,15 @@ async function runTest() { const arrowBuffer = Buffer.from(combined); const testData = { - correlation_id: 'js-table-receiver-' + Date.now(), - msg_id: 'msg-' + Date.now(), + correlation_id: 'js-table-receiver-' + crypto.randomUUID(), + msg_id: 'msg-' + crypto.randomUUID(), timestamp: new Date().toISOString(), send_to: '/test/table', msg_purpose: 'test', sender_name: 'js-table-test', - sender_id: 'sender-' + Date.now(), + sender_id: 'sender-' + crypto.randomUUID(), receiver_name: 'js-receiver', - receiver_id: 'receiver-' + Date.now(), + receiver_id: 'receiver-' + crypto.randomUUID(), reply_to: '', reply_to_msg_id: '', broker_url: TEST_BROKER_URL, diff --git a/test/test_js_table_sender.js b/test/test_js_table_sender.js index f95512c..f158a4b 100644 --- a/test/test_js_table_sender.js +++ b/test/test_js_table_sender.js @@ -4,6 +4,7 @@ */ const NATSBridge = require('../src/natsbridge.js'); +const crypto = require('crypto'); const TEST_SUBJECT = '/test/table'; const TEST_BROKER_URL = process.env.NATS_URL || 'nats://localhost:4222'; @@ -12,7 +13,7 @@ const TEST_FILESERVER_URL = process.env.FILESERVER_URL || 'http://localhost:8080 async function runTest() { console.log('=== JavaScript Table Sender Test ===\n'); - const correlationId = NATSBridge.uuidv4(); + const correlationId = crypto.randomUUID(); console.log(`Correlation ID: ${correlationId}`); console.log(`Subject: ${TEST_SUBJECT}`); console.log(`Broker URL: ${TEST_BROKER_URL}\n`); diff --git a/test/test_js_text_receiver.js b/test/test_js_text_receiver.js index 54a6b37..9d8d450 100644 --- a/test/test_js_text_receiver.js +++ b/test/test_js_text_receiver.js @@ -4,6 +4,7 @@ */ const NATSBridge = require('../src/natsbridge.js'); +const crypto = require('crypto'); const TEST_BROKER_URL = process.env.NATS_URL || 'nats://localhost:4222'; const TEST_FILESERVER_URL = process.env.FILESERVER_URL || 'http://localhost:8080'; @@ -13,22 +14,22 @@ async function runTest() { // Create a mock NATS message with text payload const testData = { - correlation_id: 'test-receiver-' + Date.now(), - msg_id: 'msg-' + Date.now(), + correlation_id: 'test-receiver-' + crypto.randomUUID(), + msg_id: 'msg-' + crypto.randomUUID(), timestamp: new Date().toISOString(), send_to: '/test/text', msg_purpose: 'test', sender_name: 'js-text-test', - sender_id: 'sender-' + Date.now(), + sender_id: 'sender-' + crypto.randomUUID(), receiver_name: 'js-receiver', - receiver_id: 'receiver-' + Date.now(), + receiver_id: 'receiver-' + crypto.randomUUID(), reply_to: '', reply_to_msg_id: '', broker_url: TEST_BROKER_URL, metadata: {}, payloads: [ { - id: 'payload-' + Date.now(), + id: 'payload-' + crypto.randomUUID(), dataname: 'message', payload_type: 'text', transport: 'direct', @@ -115,15 +116,15 @@ async function runTest() { // Test with multiple text payloads console.log('\n=== Multiple Text Payloads Test ==='); const multiTestData = { - correlation_id: 'multi-receiver-' + Date.now(), - msg_id: 'msg-' + Date.now(), + correlation_id: 'multi-receiver-' + crypto.randomUUID(), + msg_id: 'msg-' + crypto.randomUUID(), timestamp: new Date().toISOString(), send_to: '/test/text', msg_purpose: 'test', sender_name: 'js-text-test', - sender_id: 'sender-' + Date.now(), + sender_id: 'sender-' + crypto.randomUUID(), receiver_name: 'js-receiver', - receiver_id: 'receiver-' + Date.now(), + receiver_id: 'receiver-' + crypto.randomUUID(), reply_to: '', reply_to_msg_id: '', broker_url: TEST_BROKER_URL, diff --git a/test/test_js_text_sender.js b/test/test_js_text_sender.js index 3bcd013..794af1b 100644 --- a/test/test_js_text_sender.js +++ b/test/test_js_text_sender.js @@ -4,6 +4,7 @@ */ const NATSBridge = require('../src/natsbridge.js'); +const crypto = require('crypto'); const TEST_SUBJECT = '/test/text'; const TEST_BROKER_URL = process.env.NATS_URL || 'nats://localhost:4222'; @@ -12,7 +13,7 @@ const TEST_FILESERVER_URL = process.env.FILESERVER_URL || 'http://localhost:8080 async function runTest() { console.log('=== JavaScript Text Sender Test ===\n'); - const correlationId = NATSBridge.uuidv4(); + const correlationId = crypto.randomUUID(); console.log(`Correlation ID: ${correlationId}`); console.log(`Subject: ${TEST_SUBJECT}`); console.log(`Broker URL: ${TEST_BROKER_URL}\n`);