stock-bot/test/fixtures/native-backtest-worker.ts
2026-10-20 16:57:59 -04:00

49 lines
1.6 KiB
TypeScript

import {
nativeWorkerRequestSchema,
type NativeWorkerMessage,
} from '../../apps/stock/orchestrator/src/backtest/native-worker-protocol';
function send(message: NativeWorkerMessage | { invalid: boolean }): Promise<void> {
return new Promise((resolve, reject) => {
process.send!(message, (error: Error | null) => (error ? reject(error) : resolve()));
});
}
process.once('message', async (input: unknown) => {
const { config } = nativeWorkerRequestSchema.parse(input);
const behavior = config.config?.fixtureBehavior;
if (!behavior) {
// The same adapter can recover and run the actual Rust engine after a failed worker.
await import('../../apps/stock/orchestrator/src/backtest/native-worker');
process.emit('message', input);
return;
}
await send({ type: 'progress', progress: 0, message: `Fixture worker ready:${process.pid}` });
switch (behavior) {
case 'block':
// Intentionally block this process. Messages, timers, and cooperative stop cannot run.
while (true) {
Math.sqrt(Math.random());
}
case 'crash':
process.kill(process.pid, 'SIGKILL');
break;
case 'malformed':
await send({ invalid: true });
break;
case 'invalid-result':
await send({ type: 'result', result: '{"not":"a backtest result"}' });
process.disconnect?.();
break;
case 'reported-error':
await send({ type: 'error', message: 'Fixture native failure' });
break;
case 'no-result':
process.exit(0);
case 'result-then-block':
await send({ type: 'result', result: '{}' });
while (true) {
Math.sqrt(Math.random());
}
}
});