This commit is contained in:
Boki 2026-10-22 09:46:29 -04:00
parent e4925ecdfe
commit b7aeca4888
20 changed files with 490 additions and 248 deletions

View file

@ -128,10 +128,7 @@ async function fixture(
);
}
const storage = new StorageService({ logger }, mongodb, null, null);
const engine = new RustBacktestAdapter(
{ logger, custom: { StorageService: storage } },
engineOptions
);
const engine = new RustBacktestAdapter({ logger, storage }, engineOptions);
const repository = new PostgresBacktestRunRepository(postgres);
const runner = new BacktestRunner({
engine,
@ -147,9 +144,9 @@ async function fixture(
return { postgres, mongodb, storage, engine, repository, runner, services, routes };
}
async function typescriptRuntime(
async function composedRuntime(
f: Awaited<ReturnType<typeof fixture>>,
strategyRegistry?: StrategyRegistry
options: { strategyRegistry?: StrategyRegistry; useRustEngine?: boolean } = {}
) {
const di = await new ServiceContainerBuilder()
.withOptions({
@ -166,23 +163,60 @@ async function typescriptRuntime(
logger,
postgres: f.postgres,
mongodb: f.mongodb,
custom: {},
};
di.register({ serviceContainer: asValue(services) });
await registerOrchestratorServices(di, {
initializeMode: false,
useRustEngine: false,
strategyRegistry,
...options,
});
expect(di.resolve('BacktestRunner')).toBe(services.custom?.BacktestRunner);
return services;
}
describe('one backtest execution workflow', () => {
for (const backend of ['native', 'typescript'] as const) {
test(`${backend} composed runtime uses injected peers when application service lookups are unavailable`, async () => {
const f = await fixture();
const services = await composedRuntime(f, { useRustEngine: backend === 'native' });
const {
BacktestRunner: runner,
ModeManager: mode,
MarketDataService: market,
StrategyManager: manager,
} = services.custom!;
cleanup.unshift(async () => {
await runner!.shutdown();
await mode!.shutdown();
await manager!.shutdown();
});
for (const key of Object.keys(services.custom!)) {
Object.defineProperty(services.custom, key, {
get() {
throw new Error(`Unexpected runtime service lookup: ${key}`);
},
});
}
const history = await market!.loadHistoricalData(
config.symbols,
new Date(config.startDate),
new Date(config.endDate),
config.dataFrequency
);
expect(history).toHaveLength(70);
const result = await runner!.runBacktest(config);
expect(result.engine).toBe(backend);
expect(result.trades.length).toBeGreaterThan(0);
expect(market!.listenerCount('marketDataBatch')).toBe(1);
await mode!.shutdown();
await manager!.shutdown();
expect(market!.listenerCount('marketDataBatch')).toBe(0);
});
}
for (const backend of ['native', 'typescript'] as const) {
test(`${backend} API runs separate mixed execution settings and replay the original configuration`, async () => {
const f = await fixture();
const services = backend === 'typescript' ? await typescriptRuntime(f) : f.services;
const services = backend === 'typescript' ? await composedRuntime(f) : f.services;
const runner = services.custom!.BacktestRunner!;
cleanup.unshift(async () => {
await runner.shutdown();
@ -248,7 +282,7 @@ describe('one backtest execution workflow', () => {
return new CounterStrategy(dependencies.execution, definition);
}
);
const services = await typescriptRuntime(f, registry);
const services = await composedRuntime(f, { strategyRegistry: registry });
const runner = services.custom!.BacktestRunner!;
cleanup.unshift(async () => {
await runner.shutdown();
@ -376,7 +410,7 @@ describe('one backtest execution workflow', () => {
test('the TypeScript backend uses the same runner, data policy, and persisted contract', async () => {
const f = await fixture();
const services = await typescriptRuntime(f);
const services = await composedRuntime(f);
try {
const result = await services.custom!.BacktestRunner!.runBacktest(config);
expect(result.engine).toBe('typescript');
@ -402,7 +436,7 @@ describe('one backtest execution workflow', () => {
test('the maintained multi-strategy example runs through the composed runner and persists real results', async () => {
const f = await fixture();
const services = await typescriptRuntime(f);
const services = await composedRuntime(f);
const runner = services.custom!.BacktestRunner!;
cleanup.unshift(async () => {
await runner.shutdown();
@ -432,7 +466,7 @@ describe('one backtest execution workflow', () => {
test('undefined one-timestamp statistics survive normalization, persistence and the public result reader as null', async () => {
const f = await fixture(false, {}, 1);
const services = await typescriptRuntime(f);
const services = await composedRuntime(f);
const runner = services.custom!.BacktestRunner!;
cleanup.unshift(async () => {
await runner.shutdown();
@ -452,7 +486,7 @@ describe('one backtest execution workflow', () => {
test('TypeScript cancellation is persisted without a partial success or completion event', async () => {
const f = await fixture();
const services = await typescriptRuntime(f);
const services = await composedRuntime(f);
const runner = services.custom!.BacktestRunner!;
cleanup.unshift(async () => {
await runner.shutdown();
@ -485,7 +519,7 @@ describe('one backtest execution workflow', () => {
test('TypeScript ledger failures are persisted as failures, never fabricated equity or results', async () => {
const f = await fixture();
const services = await typescriptRuntime(f);
const services = await composedRuntime(f);
const runner = services.custom!.BacktestRunner!;
cleanup.unshift(async () => {
await runner.shutdown();