fix(runtime): isolate legacy research from paper execution
This commit is contained in:
parent
ee670a3bd1
commit
43ffb547be
7 changed files with 485 additions and 9 deletions
|
|
@ -166,6 +166,8 @@ async function composedRuntime(
|
|||
};
|
||||
di.register({ serviceContainer: asValue(services) });
|
||||
await registerOrchestratorServices(di, {
|
||||
role: 'research-only',
|
||||
useRustEngine: false,
|
||||
initializeMode: false,
|
||||
...options,
|
||||
});
|
||||
|
|
|
|||
151
test/integration/research-runtime-isolation.test.ts
Normal file
151
test/integration/research-runtime-isolation.test.ts
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
import { asFunction, asValue, createContainer as createDiContainer } from 'awilix';
|
||||
import { afterEach, describe, expect, test } from 'bun:test';
|
||||
import { ServiceContainerBuilder, type ServiceCradle } from '@stock-bot/di';
|
||||
import type { StockAppConfig } from '@stock-bot/stock-config';
|
||||
import { BacktestEngine } from '../../apps/stock/orchestrator/src/backtest/BacktestEngine';
|
||||
import { RustBacktestAdapter } from '../../apps/stock/orchestrator/src/backtest/RustBacktestAdapter';
|
||||
import { registerOrchestratorServices } from '../../apps/stock/orchestrator/src/container';
|
||||
import { defaultRiskLimits } from '../../apps/stock/orchestrator/src/execution/model';
|
||||
import { createContainer } from '../../apps/stock/orchestrator/src/simple-container';
|
||||
import type { OrchestratorServices } from '../../apps/stock/orchestrator/src/types';
|
||||
import { config, logger, makeExecution } from '../fixtures/execution';
|
||||
|
||||
const cleanup: (() => Promise<void>)[] = [];
|
||||
afterEach(async () => {
|
||||
for (const close of cleanup.splice(0).reverse()) await close();
|
||||
});
|
||||
|
||||
async function compose(options: Parameters<typeof registerOrchestratorServices>[1] = {}) {
|
||||
const container = await new ServiceContainerBuilder()
|
||||
.withOptions({
|
||||
enableCache: false,
|
||||
enableQueue: false,
|
||||
enableMongoDB: false,
|
||||
enablePostgres: false,
|
||||
enableQuestDB: false,
|
||||
enableBrowser: false,
|
||||
enableProxy: false,
|
||||
})
|
||||
.build();
|
||||
const services: OrchestratorServices = { logger };
|
||||
container.register({ serviceContainer: asValue(services) });
|
||||
await registerOrchestratorServices(container, options);
|
||||
const runtime = services.custom!;
|
||||
cleanup.push(async () => {
|
||||
await runtime.BacktestRunner!.shutdown();
|
||||
await runtime.ModeManager!.shutdown();
|
||||
await runtime.StrategyManager!.shutdown();
|
||||
await container.dispose();
|
||||
});
|
||||
return runtime;
|
||||
}
|
||||
|
||||
describe('research runtime composition admission', () => {
|
||||
test.each([true, false])(
|
||||
'legacy_paper_combination_rejected_before_prepare (initializeMode=%s)',
|
||||
async initializeMode => {
|
||||
let resolved = 0;
|
||||
const container = createDiContainer<ServiceCradle>();
|
||||
container.register({
|
||||
serviceContainer: asFunction(() => {
|
||||
resolved++;
|
||||
throw new Error('Runtime services must not be resolved');
|
||||
}),
|
||||
});
|
||||
await expect(
|
||||
registerOrchestratorServices(container, { useRustEngine: false, initializeMode })
|
||||
).rejects.toThrow('TypeScript backtests require a research-only composition');
|
||||
expect(resolved).toBe(0);
|
||||
}
|
||||
);
|
||||
|
||||
test('unsafe application startup fails before connection setup', async () => {
|
||||
const config = {
|
||||
services: { orchestrator: { backtesting: { useRustEngine: false } } },
|
||||
} as StockAppConfig;
|
||||
await expect(createContainer(config)).rejects.toThrow('Unsupported runtime combination');
|
||||
});
|
||||
|
||||
test('unchecked role and backend options fail closed before service resolution', async () => {
|
||||
const container = createDiContainer<ServiceCradle>();
|
||||
for (const options of [
|
||||
{ role: 'research' },
|
||||
{ role: null },
|
||||
{ useRustEngine: 'false' },
|
||||
{ useRustEngine: null },
|
||||
]) {
|
||||
await expect(
|
||||
registerOrchestratorServices(
|
||||
container,
|
||||
options as unknown as Parameters<typeof registerOrchestratorServices>[1]
|
||||
)
|
||||
).rejects.toThrow('role' in options ? 'Unsupported runtime role' : 'must be a boolean');
|
||||
}
|
||||
});
|
||||
|
||||
test('standalone_legacy_research_allowed without automatic paper initialization', async () => {
|
||||
const runtime = await compose({
|
||||
role: 'research-only',
|
||||
useRustEngine: false,
|
||||
allowSyntheticData: true,
|
||||
syntheticSeed: 42,
|
||||
});
|
||||
expect(runtime.BacktestEngine).toBeInstanceOf(BacktestEngine);
|
||||
expect(runtime.ModeManager!.getConfig()).toBeNull();
|
||||
await runtime.ModeManager!.initializeMode(config);
|
||||
const result = await runtime.BacktestEngine!.runBacktest(config);
|
||||
expect(result.status).toBe('completed');
|
||||
expect(result.trades.length).toBeGreaterThan(0);
|
||||
expect(runtime.ModeManager!.getConfig()).toEqual(config);
|
||||
});
|
||||
|
||||
test('research_only_paper_activation_denied through every exposed initializer', async () => {
|
||||
const runtime = await compose({ role: 'research-only', useRustEngine: false });
|
||||
const mode = runtime.ModeManager!;
|
||||
const execution = runtime.ExecutionService!;
|
||||
await mode.initializeMode(config);
|
||||
const engine = mode.getTradingEngine();
|
||||
const account = await execution.getAccount();
|
||||
const paper = { mode: 'paper' as const, startingCapital: 123 };
|
||||
let shutdowns = 0;
|
||||
mode.on('shutdown', () => shutdowns++);
|
||||
|
||||
await expect(mode.initializeMode(paper)).rejects.toThrow('only permits backtest');
|
||||
await expect(mode.transitionMode('backtest', 'paper', paper)).rejects.toThrow(
|
||||
'only permits backtest'
|
||||
);
|
||||
await expect(execution.initialize(paper, engine)).rejects.toThrow('only permits backtest');
|
||||
const live = {
|
||||
mode: 'live' as const,
|
||||
broker: 'ib',
|
||||
accountId: 'not-a-real-account',
|
||||
accountType: 'cash' as const,
|
||||
riskLimits: { ...defaultRiskLimits },
|
||||
};
|
||||
await expect(mode.initializeMode(live)).rejects.toThrow('Live trading is disabled');
|
||||
await expect(execution.initialize(live, engine)).rejects.toThrow('Live trading is disabled');
|
||||
expect(shutdowns).toBe(0);
|
||||
expect(mode.getTradingEngine()).toBe(engine);
|
||||
expect(mode.getConfig()).toEqual(config);
|
||||
expect(await execution.getAccount()).toEqual(account);
|
||||
});
|
||||
|
||||
test('paper composition defaults to native and cannot be switched to a legacy shared mode', async () => {
|
||||
const runtime = await compose({ initializeMode: false });
|
||||
expect(runtime.BacktestEngine).toBeInstanceOf(RustBacktestAdapter);
|
||||
await expect(runtime.ModeManager!.initializeMode(config)).rejects.toThrow('only permits paper');
|
||||
await expect(
|
||||
runtime.ExecutionService!.initialize(config, makeExecution().engine)
|
||||
).rejects.toThrow('only permits paper');
|
||||
expect(runtime.ModeManager!.getConfig()).toBeNull();
|
||||
});
|
||||
|
||||
test('research-only native composition also forbids paper startup and activation', async () => {
|
||||
const runtime = await compose({ role: 'research-only' });
|
||||
expect(runtime.BacktestEngine).toBeInstanceOf(RustBacktestAdapter);
|
||||
expect(runtime.ModeManager!.getConfig()).toBeNull();
|
||||
await expect(
|
||||
runtime.ModeManager!.initializeMode({ mode: 'paper', startingCapital: 10000 })
|
||||
).rejects.toThrow('only permits backtest');
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue