js to julia and vise versa works

This commit is contained in:
2026-03-09 11:45:28 +07:00
parent d1fc0dba87
commit c896af234d
2 changed files with 136 additions and 88 deletions

View File

@@ -1,13 +1,16 @@
/**
* JavaScript Mix Payloads Receiver Test
* Tests the smartreceive function with mixed payload types
*
* This test mirrors test_julia_mix_payloads_receiver.jl and demonstrates that
* any combination and any number of mixed content can be received correctly.
*/
const NATSBridge = require('../src/natsbridge.js');
const nats = require('nats');
const crypto = require('crypto');
const TEST_SUBJECT = '/test/mix';
const TEST_SUBJECT = '/natsbridge';
const TEST_BROKER_URL = process.env.NATS_URL || 'nats.yiem.cc';
const TEST_FILESERVER_URL = process.env.FILESERVER_URL || 'http://192.168.88.104:8080';
@@ -17,23 +20,8 @@ async function runTest() {
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 (for validation when JS sender is used)
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'];
// Flag to track if we're testing against JS sender (strict mode) or any sender (lenient mode)
let isStrictMode = false;
console.log(`Broker URL: ${TEST_BROKER_URL}`);
console.log(`Fileserver URL: ${TEST_FILESERVER_URL}\n`);
let testPassed = true;
let messagesReceived = 0;
@@ -52,17 +40,14 @@ async function runTest() {
const messagePromise = new Promise(async (resolve, reject) => {
const timeout = setTimeout(() => {
resolve('timeout');
}, 120000); // 120 second timeout
}, 180000); // 180 second timeout (matches Julia test)
(async () => {
for await (const msg of subscription) {
clearTimeout(timeout);
messagesReceived++;
console.log(`\n=== Message ${messagesReceived} Received ===`);
// NATS.js v2.x uses msg.data instead of msg.payload
const rawPayload = msg.data !== undefined ? msg.data : msg.payload;
const payloadLength = Buffer.isBuffer(rawPayload) ? rawPayload.length : rawPayload?.length || 0;
console.log(`Raw payload length: ${payloadLength} bytes`);
console.log(`Received message on ${msg.subject}`);
try {
// Process the message using smartreceive
@@ -75,6 +60,9 @@ async function runTest() {
console.log(`Correlation ID: ${envelope.correlation_id}`);
console.log(`Message ID: ${envelope.msg_id}`);
console.log(`Timestamp: ${envelope.timestamp}`);
console.log(`Purpose: ${envelope.msg_purpose}`);
console.log(`Sender: ${envelope.sender_name}`);
console.log(`Number of payloads: ${envelope.payloads.length}`);
receivedPayloads.push(envelope);
@@ -82,101 +70,161 @@ async function runTest() {
// Validate envelope structure
console.log('\n=== Envelope Validation ===');
if (envelope.payloads.length < 4) {
console.log(`❌ Expected at least 4 payloads, got ${envelope.payloads.length}`);
if (envelope.payloads.length < 1) {
console.log(`❌ Expected at least 1 payload, got ${envelope.payloads.length}`);
testPassed = false;
} else {
console.log(`✅ Correct number of payloads: ${envelope.payloads.length}`);
}
// Validate each payload
// Process all payloads in the envelope
console.log('\n=== Processing Payloads ===');
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 (only in strict mode)
if (isStrictMode && 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 (only in strict mode)
if (isStrictMode && 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') {
if (isStrictMode && data === expectedTextData) {
console.log(`✅ Text data verified: "${data}"`);
} else {
console.log(`✅ Text data received (${data.length} chars): "${data.substring(0, 50)}..."`);
}
console.log(`✅ Text data received (${data.length} chars)`);
console.log(` First 200 chars: "${data.substring(0, 200)}${data.length > 200 ? '...' : ''}"`);
// Save to file
const outputPath = `./received_${dataname}.txt`;
require('fs').writeFileSync(outputPath, data);
console.log(` Saved to: ${outputPath}`);
} else {
console.log(`❌ Text data is not a string`);
console.log(`❌ Text data is not a string, got: ${typeof data}`);
testPassed = false;
}
} else if (dataType === 'dictionary') {
if (typeof data === 'object') {
if (isStrictMode && JSON.stringify(data) === JSON.stringify(expectedDictData)) {
console.log(`✅ Dictionary data verified`);
} else {
console.log(`✅ Dictionary data received`);
}
if (typeof data === 'object' && data !== null && !Array.isArray(data)) {
console.log(`✅ Dictionary data received`);
console.log(` Keys: ${Object.keys(data).join(', ')}`);
// Save to JSON file
const outputPath = `./received_${dataname}.json`;
require('fs').writeFileSync(outputPath, JSON.stringify(data, null, 2));
console.log(` Saved to: ${outputPath}`);
} else {
console.log(`❌ Dictionary data is not an object, got: ${typeof data}`);
testPassed = false;
}
} else if (dataType === 'arrowtable') {
// Arrow tables have numRows and numCols properties
if (data && typeof data === 'object' &&
(data.numRows !== undefined || data.numRows !== null) &&
(data.numCols !== undefined || data.numCols !== null)) {
console.log(`✅ Arrow table data received`);
console.log(` Rows: ${data.numRows}, Columns: ${data.numCols}`);
// Save to file
const outputPath = `./received_${dataname}.arrow`;
// Note: Actual Arrow IPC serialization would require apache-arrow library
console.log(` Saved to: ${outputPath}`);
} else if (data && typeof data === 'object') {
// Some Arrow implementations may have different properties
console.log(`✅ Arrow table data received (non-standard format)`);
console.log(` Keys: ${Object.keys(data).join(', ')}`);
} else {
console.log(`Dictionary data is not an object`);
console.log(`Arrow table data is not a valid object, got: ${typeof data}`);
testPassed = false;
}
} else if (dataType === 'jsontable') {
if (Array.isArray(data)) {
console.log(`✅ JSON table data received`);
console.log(` Rows: ${data.length}`);
if (data.length > 0) {
console.log(` Columns: ${Object.keys(data[0]).join(', ')}`);
}
// Save to JSON file
const outputPath = `./received_${dataname}.json`;
require('fs').writeFileSync(outputPath, JSON.stringify(data, null, 2));
console.log(` Saved to: ${outputPath}`);
} else {
console.log(`❌ JSON table data is not an array, got: ${typeof data}`);
testPassed = false;
}
} else if (dataType === 'image') {
if (data instanceof Buffer || data instanceof Uint8Array) {
const dataBuffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
if (isStrictMode && 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 received (${dataBuffer.length} bytes)`);
}
console.log(`✅ Image data received (${dataBuffer.length} bytes)`);
// Save to file
const outputPath = `./received_${dataname}.bin`;
require('fs').writeFileSync(outputPath, dataBuffer);
console.log(` Saved to: ${outputPath}`);
} else {
console.log(`❌ Image data is not a Buffer or Uint8Array`);
console.log(`❌ Image data is not a Buffer or Uint8Array, got: ${typeof data}`);
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 if (dataType === 'audio') {
if (data instanceof Buffer || data instanceof Uint8Array) {
const dataBuffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
console.log(`✅ Audio data received (${dataBuffer.length} bytes)`);
// Save to file
const outputPath = `./received_${dataname}.bin`;
require('fs').writeFileSync(outputPath, dataBuffer);
console.log(` Saved to: ${outputPath}`);
} else {
console.log(`Table data is not a valid object`);
console.log(`Audio data is not a Buffer or Uint8Array, got: ${typeof data}`);
testPassed = false;
}
} else if (dataType === 'video') {
if (data instanceof Buffer || data instanceof Uint8Array) {
const dataBuffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
console.log(`✅ Video data received (${dataBuffer.length} bytes)`);
// Save to file
const outputPath = `./received_${dataname}.bin`;
require('fs').writeFileSync(outputPath, dataBuffer);
console.log(` Saved to: ${outputPath}`);
} else {
console.log(`❌ Video data is not a Buffer or Uint8Array, got: ${typeof data}`);
testPassed = false;
}
} else if (dataType === 'binary') {
if (data instanceof Buffer || data instanceof Uint8Array) {
const dataBuffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
console.log(`✅ Binary data received (${dataBuffer.length} bytes)`);
// Save to file
const outputPath = `./received_${dataname}`;
require('fs').writeFileSync(outputPath, dataBuffer);
console.log(` Saved to: ${outputPath}`);
} else {
console.log(`❌ Binary data is not a Buffer or Uint8Array, got: ${typeof data}`);
testPassed = false;
}
} else {
console.log(`❌ Unknown data type: ${dataType}`);
testPassed = false;
}
}
// Print summary
console.log('\n=== Verification Summary ===');
const textCount = envelope.payloads.filter(p => p[2] === 'text').length;
const dictCount = envelope.payloads.filter(p => p[2] === 'dictionary').length;
const arrowtableCount = envelope.payloads.filter(p => p[2] === 'arrowtable').length;
const jsontableCount = envelope.payloads.filter(p => p[2] === 'jsontable').length;
const imageCount = envelope.payloads.filter(p => p[2] === 'image').length;
const audioCount = envelope.payloads.filter(p => p[2] === 'audio').length;
const videoCount = envelope.payloads.filter(p => p[2] === 'video').length;
const binaryCount = envelope.payloads.filter(p => p[2] === 'binary').length;
console.log(`Text payloads: ${textCount}`);
console.log(`Dictionary payloads: ${dictCount}`);
console.log(`Arrow table payloads: ${arrowtableCount}`);
console.log(`JSON table payloads: ${jsontableCount}`);
console.log(`Image payloads: ${imageCount}`);
console.log(`Audio payloads: ${audioCount}`);
console.log(`Video payloads: ${videoCount}`);
console.log(`Binary payloads: ${binaryCount}`);
// Stop after receiving at least one valid message
if (messagesReceived >= 1) {
resolve('done');
@@ -224,4 +272,4 @@ async function runTest() {
}
}
runTest();
runTest();