Files
smartrust/test/test.sockettransport.node.ts

313 lines
9.0 KiB
TypeScript
Raw Permalink Normal View History

import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as path from 'path';
import * as fs from 'fs';
import * as childProcess from 'child_process';
import * as os from 'os';
import { RustBridge } from '../ts/classes.rustbridge.js';
import type { ICommandDefinition } from '../ts/interfaces/index.js';
const testDir = path.resolve(path.dirname(new URL(import.meta.url).pathname));
const mockServerPath = path.join(testDir, 'helpers/mock-socket-server.mjs');
// Define the command types for our mock binary
type TMockCommands = {
echo: { params: Record<string, any>; result: Record<string, any> };
largeEcho: { params: Record<string, any>; result: Record<string, any> };
error: { params: {}; result: never };
emitEvent: { params: { eventName: string; eventData: any }; result: null };
slow: { params: {}; result: { delayed: boolean } };
exit: { params: {}; result: null };
streamEcho: { params: { count: number }; chunk: { index: number; value: string }; result: { totalChunks: number } };
streamError: { params: {}; chunk: { index: number; value: string }; result: never };
streamEmpty: { params: {}; chunk: never; result: { totalChunks: number } };
};
/**
* Start the mock socket server and return the socket path.
* Returns { proc, socketPath }.
*/
async function startMockServer(testName: string): Promise<{ proc: childProcess.ChildProcess; socketPath: string }> {
const socketPath = path.join(os.tmpdir(), `smartrust-test-${Date.now()}-${testName}.sock`);
const proc = childProcess.spawn('node', [mockServerPath, socketPath], {
stdio: ['pipe', 'pipe', 'pipe'],
});
// Wait for the server to signal readiness via stdout
return new Promise((resolve, reject) => {
let stdoutData = '';
const timeout = setTimeout(() => {
proc.kill();
reject(new Error('Mock server did not start within 5s'));
}, 5000);
proc.stdout!.on('data', (data: Buffer) => {
stdoutData += data.toString();
const lines = stdoutData.split('\n');
for (const line of lines) {
if (line.trim()) {
try {
const parsed = JSON.parse(line.trim());
if (parsed.ready) {
clearTimeout(timeout);
resolve({ proc, socketPath: parsed.socketPath });
return;
}
} catch { /* not JSON yet */ }
}
}
});
proc.on('error', (err) => {
clearTimeout(timeout);
reject(err);
});
proc.on('exit', (code) => {
clearTimeout(timeout);
reject(new Error(`Mock server exited with code ${code}`));
});
});
}
function stopMockServer(proc: childProcess.ChildProcess, socketPath: string) {
try { proc.kill('SIGTERM'); } catch { /* ignore */ }
try { fs.unlinkSync(socketPath); } catch { /* ignore */ }
}
// === Socket Transport Tests via RustBridge.connect() ===
tap.test('socket: should connect and receive ready event', async () => {
const { proc, socketPath } = await startMockServer('connect-ready');
try {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
});
const result = await bridge.connect(socketPath);
expect(result).toBeTrue();
expect(bridge.running).toBeTrue();
bridge.kill();
expect(bridge.running).toBeFalse();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should send command and receive response', async () => {
const { proc, socketPath } = await startMockServer('send-command');
try {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
});
await bridge.connect(socketPath);
const result = await bridge.sendCommand('echo', { hello: 'world', num: 42 });
expect(result).toEqual({ hello: 'world', num: 42 });
bridge.kill();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should handle error responses', async () => {
const { proc, socketPath } = await startMockServer('error-response');
try {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
});
await bridge.connect(socketPath);
let threw = false;
try {
await bridge.sendCommand('error', {});
} catch (err: any) {
threw = true;
expect(err.message).toInclude('Test error message');
}
expect(threw).toBeTrue();
bridge.kill();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should receive custom events', async () => {
const { proc, socketPath } = await startMockServer('custom-events');
try {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
});
await bridge.connect(socketPath);
const eventPromise = new Promise<any>((resolve) => {
bridge.once('management:testEvent', (data) => resolve(data));
});
await bridge.sendCommand('emitEvent', {
eventName: 'testEvent',
eventData: { key: 'value' },
});
const eventData = await eventPromise;
expect(eventData).toEqual({ key: 'value' });
bridge.kill();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should handle multiple concurrent commands', async () => {
const { proc, socketPath } = await startMockServer('concurrent');
try {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
});
await bridge.connect(socketPath);
const results = await Promise.all([
bridge.sendCommand('echo', { id: 1 }),
bridge.sendCommand('echo', { id: 2 }),
bridge.sendCommand('echo', { id: 3 }),
]);
expect(results[0]).toEqual({ id: 1 });
expect(results[1]).toEqual({ id: 2 });
expect(results[2]).toEqual({ id: 3 });
bridge.kill();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should handle 1MB payload round-trip', async () => {
const { proc, socketPath } = await startMockServer('large-payload');
try {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
requestTimeoutMs: 30000,
});
await bridge.connect(socketPath);
const largeString = 'x'.repeat(1024 * 1024);
const result = await bridge.sendCommand('largeEcho', { data: largeString });
expect(result.data).toEqual(largeString);
expect(result.data.length).toEqual(1024 * 1024);
bridge.kill();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should disconnect without killing the daemon', async () => {
const { proc, socketPath } = await startMockServer('no-kill-daemon');
try {
// First connection
const bridge1 = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
});
await bridge1.connect(socketPath);
expect(bridge1.running).toBeTrue();
// Disconnect
bridge1.kill();
expect(bridge1.running).toBeFalse();
// Second connection — daemon should still be alive
const bridge2 = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
});
const result = await bridge2.connect(socketPath);
expect(result).toBeTrue();
expect(bridge2.running).toBeTrue();
// Verify the daemon is functional
const echoResult = await bridge2.sendCommand('echo', { reconnected: true });
expect(echoResult).toEqual({ reconnected: true });
bridge2.kill();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should stream responses via socket', async () => {
const { proc, socketPath } = await startMockServer('streaming');
try {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
requestTimeoutMs: 10000,
});
await bridge.connect(socketPath);
const stream = bridge.sendCommandStreaming('streamEcho', { count: 5 });
const chunks: Array<{ index: number; value: string }> = [];
for await (const chunk of stream) {
chunks.push(chunk);
}
expect(chunks.length).toEqual(5);
for (let i = 0; i < 5; i++) {
expect(chunks[i].index).toEqual(i);
expect(chunks[i].value).toEqual(`chunk_${i}`);
}
const result = await stream.result;
expect(result.totalChunks).toEqual(5);
bridge.kill();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should return false when socket path does not exist', async () => {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 3000,
});
const result = await bridge.connect('/tmp/nonexistent-smartrust-test.sock');
expect(result).toBeFalse();
expect(bridge.running).toBeFalse();
});
tap.test('socket: should throw when sending command while not connected', async () => {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
});
let threw = false;
try {
await bridge.sendCommand('echo', {});
} catch (err: any) {
threw = true;
expect(err.message).toInclude('not running');
}
expect(threw).toBeTrue();
});
export default tap.start();