94 lines
3.1 KiB
TypeScript
94 lines
3.1 KiB
TypeScript
import { describe, expect, test } from 'bun:test';
|
|
import { ModeManager } from '../../apps/stock/orchestrator/src/core/ModeManager';
|
|
import { config, logger } from '../fixtures/execution';
|
|
|
|
function fixture(failAt?: string) {
|
|
const calls: string[] = [];
|
|
const failure = new Error('injected shutdown failure');
|
|
const record = async (step: string) => {
|
|
calls.push(step);
|
|
if (step === failAt) throw failure;
|
|
};
|
|
const mode = new ModeManager({
|
|
logger,
|
|
execution: {
|
|
initialize: () => record('execution.initialize'),
|
|
shutdown: () => record('execution.shutdown'),
|
|
},
|
|
storage: {
|
|
initialize: () => record('storage.initialize'),
|
|
shutdown: () => record('storage.shutdown'),
|
|
},
|
|
strategies: {
|
|
restoreStrategies: () => record('strategies.restore'),
|
|
shutdown: () => record('strategies.shutdown'),
|
|
},
|
|
marketData: {
|
|
initialize: () => record('market.initialize'),
|
|
shutdown: () => record('market.shutdown'),
|
|
},
|
|
});
|
|
return { mode, calls, failure };
|
|
}
|
|
|
|
describe('explicit mode lifecycle dependencies', () => {
|
|
test('paper recovery completes before the feed starts; shutdown drains the feed before strategies and execution', async () => {
|
|
const f = fixture();
|
|
await f.mode.initializeMode({ mode: 'paper', startingCapital: 10000 });
|
|
expect(f.calls).toEqual([
|
|
'execution.initialize',
|
|
'storage.initialize',
|
|
'strategies.restore',
|
|
'market.initialize',
|
|
]);
|
|
await f.mode.shutdown();
|
|
expect(f.calls.slice(4)).toEqual([
|
|
'market.shutdown',
|
|
'strategies.shutdown',
|
|
'execution.shutdown',
|
|
'storage.shutdown',
|
|
]);
|
|
expect(() => f.mode.getTradingEngine()).toThrow('not initialized');
|
|
await f.mode.shutdown();
|
|
expect(f.calls).toHaveLength(8);
|
|
});
|
|
|
|
test('backtest mode replaces the current engine without restoring paper strategies', async () => {
|
|
const f = fixture();
|
|
await f.mode.initializeMode(config);
|
|
const prior = f.mode.getTradingEngine();
|
|
await f.mode.initializeMode(config);
|
|
expect(f.mode.getTradingEngine()).not.toBe(prior);
|
|
expect(f.calls).toEqual([
|
|
'execution.initialize',
|
|
'storage.initialize',
|
|
'market.initialize',
|
|
'market.shutdown',
|
|
'strategies.shutdown',
|
|
'execution.shutdown',
|
|
'storage.shutdown',
|
|
'execution.initialize',
|
|
'storage.initialize',
|
|
'market.initialize',
|
|
]);
|
|
await f.mode.shutdown();
|
|
});
|
|
|
|
test.each(['market.shutdown', 'strategies.shutdown', 'execution.shutdown', 'storage.shutdown'])(
|
|
'%s failure still closes every other dependency and clears the mode engine',
|
|
async failAt => {
|
|
const f = fixture(failAt);
|
|
await f.mode.initializeMode(config);
|
|
await expect(f.mode.shutdown()).rejects.toBe(f.failure);
|
|
expect(f.calls.slice(3)).toEqual([
|
|
'market.shutdown',
|
|
'strategies.shutdown',
|
|
'execution.shutdown',
|
|
'storage.shutdown',
|
|
]);
|
|
expect(() => f.mode.getTradingEngine()).toThrow('not initialized');
|
|
await f.mode.shutdown();
|
|
expect(f.calls).toHaveLength(7);
|
|
}
|
|
);
|
|
});
|