Share backtest request contracts and isolate stored result mapping

This commit is contained in:
Boki 2026-10-21 23:57:48 -04:00
parent 007e35c747
commit 420c47704e
21 changed files with 777 additions and 244 deletions

View file

@ -8,6 +8,10 @@ import { HandlerRegistry } from '@stock-bot/handler-registry';
import { getLogger, setLoggerConfig } from '@stock-bot/logger';
import { PostgreSQLClient } from '@stock-bot/postgres';
import { Queue } from '@stock-bot/queue';
import {
backtestRequestFromDefinition,
parseBacktestRequest,
} from '@stock-bot/stock-config/backtest';
import { BacktestRunner } from '../../apps/stock/orchestrator/src/backtest/BacktestRunner';
import { BacktestCancelledError } from '../../apps/stock/orchestrator/src/backtest/execution-engine';
import { PostgresBacktestRunRepository } from '../../apps/stock/orchestrator/src/backtest/PostgresBacktestRunRepository';
@ -360,6 +364,38 @@ describe.skipIf(!url || !redisUrl)('durable backtests on real PostgreSQL and Red
})
).rejects.toThrow('does not match');
});
test('API acceptance uses the runner contract before writing a run or dispatch intent', async () => {
const f = await fixture();
await expect(
f.api.updateBacktest(f.definition.id, { config: { commission: -1 } })
).rejects.toThrow('commission');
expect((await f.api.getBacktest(f.definition.id))?.config).toEqual(f.definition.config);
// Model malformed historical configuration that bypassed today's API validator.
await f.pool.query('UPDATE backtests SET config = $2::jsonb WHERE id = $1', [
f.definition.id,
JSON.stringify({ ...f.definition.config, slippage: 0.2 }),
]);
await expect(f.api.createRun({ backtestId: f.definition.id })).rejects.toThrow('slippage');
expect((await f.pool.query('SELECT * FROM runs')).rows).toHaveLength(0);
expect((await f.pool.query('SELECT * FROM backtest_dispatch')).rows).toHaveLength(0);
await f.pool.query('UPDATE backtests SET config = $2::jsonb WHERE id = $1', [
f.definition.id,
JSON.stringify(f.definition.config),
]);
const run = await f.api.createRun({ backtestId: f.definition.id, speedMultiplier: 7 });
const snapshot = (
await f.pool.query('SELECT request_snapshot FROM runs WHERE id = $1', [run.id])
).rows[0].request_snapshot;
const expected = backtestRequestFromDefinition(f.definition, {
runId: run.id,
backtestId: f.definition.id,
speedMultiplier: 7,
});
expect(parseBacktestRequest(snapshot)).toEqual(expected);
expect(snapshot.speed).toBe('5x');
expect((await f.pool.query('SELECT * FROM backtest_dispatch')).rows).toHaveLength(1);
});
test('acceptance commits run + immutable snapshot + intent atomically, with no Redis/HTTP dependency', async () => {
const f = await fixture();
await f.pool