Files
NATSBridge/test/test_js_binary_sender.js
2026-03-05 20:17:36 +07:00

173 lines
6.6 KiB
JavaScript

/**
* JavaScript Binary Sender Test
* Tests the smartsend function with binary/image/audio/video payloads
*/
const NATSBridge = require('../src/natbridge.js');
const TEST_SUBJECT = '/test/binary';
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 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 - binary data for different types
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 = [
['image', binaryData, 'image'],
['audio', audioData, 'audio'],
['video', videoData, 'video'],
['binary', genericBinary, 'binary']
];
try {
// Send the message
console.log('Sending binary 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-binary-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 = ['image', 'audio', 'video', 'binary'];
const expectedTypes = ['image', 'audio', 'video', 'binary'];
const expectedData = [binaryData, audioData, videoData, genericBinary];
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 = Buffer.from(payload.data, 'base64');
const originalData = expectedData[i];
if (decodedData.length !== originalData.length) {
console.log(`❌ Payload ${i + 1}: Length mismatch (${decodedData.length} vs ${originalData.length})`);
passed = false;
} else {
let dataMatch = true;
for (let j = 0; j < originalData.length; j++) {
if (decodedData[j] !== originalData[j]) {
dataMatch = false;
break;
}
}
if (dataMatch) {
console.log(`✅ Payload ${i + 1}: Data integrity verified`);
} else {
console.log(`❌ Payload ${i + 1}: Data integrity mismatch`);
passed = false;
}
}
console.log(` Size: ${payload.size} bytes\n`);
}
// Test with larger binary data (simulating file upload scenario)
console.log('=== Large Binary Data Test ===');
const largeData = Buffer.alloc(10000, 0xFF); // 10KB of binary data
const largeTestData = [
['large_binary', largeData, 'binary']
];
const [largeEnv, _] = await NATSBridge.smartsend(
TEST_SUBJECT,
largeTestData,
{
broker_url: TEST_BROKER_URL,
fileserver_url: TEST_FILESERVER_URL,
correlation_id: 'large-' + correlationId,
is_publish: false
}
);
if (largeEnv.payloads.length === 1 && largeEnv.payloads[0].size === 10000) {
console.log('✅ Large binary data handled correctly');
} else {
console.log('❌ Large binary data handling 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();