update
This commit is contained in:
169
test/test_js_text_sender.js
Normal file
169
test/test_js_text_sender.js
Normal file
@@ -0,0 +1,169 @@
|
||||
/**
|
||||
* JavaScript Text Sender Test
|
||||
* Tests the smartsend function with text payloads
|
||||
*/
|
||||
|
||||
const NATSBridge = require('../src/natbridge.js');
|
||||
|
||||
const TEST_SUBJECT = '/test/text';
|
||||
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 Text 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
|
||||
const textData = 'Hello, NATSBridge! This is a test message.';
|
||||
const testData = [
|
||||
['message', textData, 'text']
|
||||
];
|
||||
|
||||
try {
|
||||
// Send the message
|
||||
console.log('Sending text payload...');
|
||||
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-text-test',
|
||||
is_publish: false // Don't actually publish for this test
|
||||
}
|
||||
);
|
||||
|
||||
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.correlation_id) {
|
||||
console.log('❌ correlation_id is missing');
|
||||
passed = false;
|
||||
} else {
|
||||
console.log('✅ correlation_id present');
|
||||
}
|
||||
|
||||
if (!env.msg_id) {
|
||||
console.log('❌ msg_id is missing');
|
||||
passed = false;
|
||||
} else {
|
||||
console.log('✅ msg_id present');
|
||||
}
|
||||
|
||||
if (!env.timestamp) {
|
||||
console.log('❌ timestamp is missing');
|
||||
passed = false;
|
||||
} else {
|
||||
console.log('✅ timestamp present');
|
||||
}
|
||||
|
||||
if (env.payloads.length !== 1) {
|
||||
console.log(`❌ Expected 1 payload, got ${env.payloads.length}`);
|
||||
passed = false;
|
||||
} else {
|
||||
console.log('✅ Correct number of payloads');
|
||||
}
|
||||
|
||||
const payload = env.payloads[0];
|
||||
if (payload.dataname !== 'message') {
|
||||
console.log(`❌ Expected dataname 'message', got '${payload.dataname}'`);
|
||||
passed = false;
|
||||
} else {
|
||||
console.log('✅ Correct dataname');
|
||||
}
|
||||
|
||||
if (payload.payload_type !== 'text') {
|
||||
console.log(`❌ Expected payload_type 'text', got '${payload.payload_type}'`);
|
||||
passed = false;
|
||||
} else {
|
||||
console.log('✅ Correct payload_type');
|
||||
}
|
||||
|
||||
if (payload.transport !== 'direct') {
|
||||
console.log(`❌ Expected transport 'direct', got '${payload.transport}'`);
|
||||
passed = false;
|
||||
} else {
|
||||
console.log('✅ Correct transport');
|
||||
}
|
||||
|
||||
if (payload.encoding !== 'base64') {
|
||||
console.log(`❌ Expected encoding 'base64', got '${payload.encoding}'`);
|
||||
passed = false;
|
||||
} else {
|
||||
console.log('✅ Correct encoding');
|
||||
}
|
||||
|
||||
// Decode and verify the data
|
||||
const decodedData = Buffer.from(payload.data, 'base64').toString('utf8');
|
||||
if (decodedData !== textData) {
|
||||
console.log(`❌ Decoded data mismatch`);
|
||||
console.log(` Expected: ${textData}`);
|
||||
console.log(` Got: ${decodedData}`);
|
||||
passed = false;
|
||||
} else {
|
||||
console.log('✅ Data integrity verified');
|
||||
}
|
||||
|
||||
console.log(`\nPayload size: ${payload.size} bytes`);
|
||||
console.log(`Base64 data length: ${payload.data.length} chars`);
|
||||
|
||||
// Test with multiple text payloads
|
||||
console.log('\n=== Multiple Text Payloads Test ===');
|
||||
const multiTestData = [
|
||||
['msg1', 'First message', 'text'],
|
||||
['msg2', 'Second message', 'text'],
|
||||
['msg3', 'Third message', 'text']
|
||||
];
|
||||
|
||||
const [multiEnv, multiEnvJsonStr] = await NATSBridge.smartsend(
|
||||
TEST_SUBJECT,
|
||||
multiTestData,
|
||||
{
|
||||
broker_url: TEST_BROKER_URL,
|
||||
fileserver_url: TEST_FILESERVER_URL,
|
||||
correlation_id: 'multi-test-' + correlationId,
|
||||
is_publish: false
|
||||
}
|
||||
);
|
||||
|
||||
if (multiEnv.payloads.length === 3) {
|
||||
console.log('✅ Multiple payloads handled correctly');
|
||||
} else {
|
||||
console.log(`❌ Expected 3 payloads, got ${multiEnv.payloads.length}`);
|
||||
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