update
This commit is contained in:
215
test/test_js_binary_receiver.js
Normal file
215
test/test_js_binary_receiver.js
Normal file
@@ -0,0 +1,215 @@
|
||||
/**
|
||||
* JavaScript Binary Receiver Test
|
||||
* Tests the smartreceive function with binary/image/audio/video payloads
|
||||
*/
|
||||
|
||||
const NATSBridge = require('../src/natbridge.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 Binary Receiver Test ===\n');
|
||||
|
||||
// Create mock NATS message with binary payloads
|
||||
const binaryData = Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]); // PNG header
|
||||
const audioData = Buffer.from([0x46, 0x4C, 0x41, 0x43, 0x00, 0x00, 0x00, 0x00]); // FLAC header
|
||||
const videoData = Buffer.from([0x00, 0x00, 0x00, 0x18, 0x66, 0x74, 0x79, 0x70]); // MP4 header
|
||||
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(),
|
||||
timestamp: new Date().toISOString(),
|
||||
send_to: '/test/binary',
|
||||
msg_purpose: 'test',
|
||||
sender_name: 'js-binary-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: 'image',
|
||||
payload_type: 'image',
|
||||
transport: 'direct',
|
||||
encoding: 'base64',
|
||||
size: binaryData.length,
|
||||
data: binaryData.toString('base64'),
|
||||
metadata: { payload_bytes: binaryData.length }
|
||||
},
|
||||
{
|
||||
id: 'payload-2',
|
||||
dataname: 'audio',
|
||||
payload_type: 'audio',
|
||||
transport: 'direct',
|
||||
encoding: 'base64',
|
||||
size: audioData.length,
|
||||
data: audioData.toString('base64'),
|
||||
metadata: { payload_bytes: audioData.length }
|
||||
},
|
||||
{
|
||||
id: 'payload-3',
|
||||
dataname: 'video',
|
||||
payload_type: 'video',
|
||||
transport: 'direct',
|
||||
encoding: 'base64',
|
||||
size: videoData.length,
|
||||
data: videoData.toString('base64'),
|
||||
metadata: { payload_bytes: videoData.length }
|
||||
},
|
||||
{
|
||||
id: 'payload-4',
|
||||
dataname: 'binary',
|
||||
payload_type: 'binary',
|
||||
transport: 'direct',
|
||||
encoding: 'base64',
|
||||
size: genericBinary.length,
|
||||
data: genericBinary.toString('base64'),
|
||||
metadata: { payload_bytes: genericBinary.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 = [
|
||||
['image', binaryData, 'image'],
|
||||
['audio', audioData, 'audio'],
|
||||
['video', videoData, 'video'],
|
||||
['binary', genericBinary, 'binary']
|
||||
];
|
||||
|
||||
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`);
|
||||
}
|
||||
|
||||
// Verify binary data integrity
|
||||
const receivedData = payload[1];
|
||||
if (!(receivedData instanceof Buffer || receivedData instanceof Uint8Array)) {
|
||||
console.log(`❌ Payload ${i + 1}: Expected Buffer/Uint8Array, got ${typeof receivedData}`);
|
||||
passed = false;
|
||||
} else if (Buffer.isBuffer(receivedData)) {
|
||||
if (receivedData.length !== expected[1].length) {
|
||||
console.log(`❌ Payload ${i + 1}: Length mismatch`);
|
||||
passed = false;
|
||||
} else {
|
||||
let dataMatch = true;
|
||||
for (let j = 0; j < expected[1].length; j++) {
|
||||
if (receivedData[j] !== expected[1][j]) {
|
||||
dataMatch = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (dataMatch) {
|
||||
console.log(`✅ Payload ${i + 1}: Data correctly deserialized`);
|
||||
} else {
|
||||
console.log(`❌ Payload ${i + 1}: Data mismatch`);
|
||||
passed = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Uint8Array comparison
|
||||
const receivedBuffer = Buffer.from(receivedData);
|
||||
if (receivedBuffer.length !== expected[1].length) {
|
||||
console.log(`❌ Payload ${i + 1}: Length mismatch`);
|
||||
passed = false;
|
||||
} else {
|
||||
let dataMatch = true;
|
||||
for (let j = 0; j < expected[1].length; j++) {
|
||||
if (receivedBuffer[j] !== expected[1][j]) {
|
||||
dataMatch = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (dataMatch) {
|
||||
console.log(`✅ Payload ${i + 1}: Data correctly deserialized`);
|
||||
} else {
|
||||
console.log(`❌ Payload ${i + 1}: Data mismatch`);
|
||||
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();
|
||||
Reference in New Issue
Block a user