stock-bot/test/integration/strategy-registry.test.ts

110 lines
4.8 KiB
TypeScript

import { afterEach, describe, expect, test } from 'bun:test';
import type { ModeManager } from '../../apps/stock/orchestrator/src/core/ModeManager';
import { createBuiltinStrategyRegistry } from '../../apps/stock/orchestrator/src/strategies/builtin-strategies';
import { StrategyManager } from '../../apps/stock/orchestrator/src/strategies/StrategyManager';
import { StrategyRegistry } from '../../apps/stock/orchestrator/src/strategies/StrategyRegistry';
import { config, logger, makeExecution, quote } from '../fixtures/execution';
import { CounterStrategy, recoveryConfig } from '../fixtures/strategy-recovery';
const cleanup: (() => Promise<void>)[] = [];
afterEach(async () => {
for (const close of cleanup.splice(0).reverse()) {
await close();
}
});
async function fixture() {
const { execution, engine } = makeExecution();
await execution.initialize(config, engine);
cleanup.push(() => execution.shutdown());
const modeManager = { getTradingEngine: () => engine } as ModeManager;
return {
execution,
dependencies: { modeManager, execution },
services: { logger, custom: { ModeManager: modeManager, ExecutionService: execution } },
};
}
describe('strategy registration and expansion', () => {
test('custom lazy factories integrate with ordinary initialization, recovery and restart', async () => {
const f = await fixture();
let created = 0;
const registry = new StrategyRegistry().register(
['fixture', 'custom-alias'],
(config, dependencies) => {
created++;
expect(dependencies.execution).toBe(f.execution);
return new CounterStrategy(dependencies.execution, config);
}
);
expect(created).toBe(0);
const manager = new StrategyManager(f.services, registry);
cleanup.push(() => manager.shutdown());
await manager.initializeStrategies([{ ...recoveryConfig, name: ' Custom-Alias ' }]);
await manager.onMarketData(quote('first'));
expect((manager.getStrategy('counter') as CounterStrategy).count).toBe(1);
await manager.shutdown();
const restarted = new StrategyManager(f.services, registry);
cleanup.push(() => restarted.shutdown());
await restarted.restoreStrategies();
expect(created).toBe(2);
expect((restarted.getStrategy('counter') as CounterStrategy).count).toBe(1);
await restarted.onMarketData(quote('second'));
expect((restarted.getStrategy('counter') as CounterStrategy).count).toBe(2);
});
test('aliases are normalized and registration conflicts fail atomically', async () => {
const f = await fixture();
const registry = new StrategyRegistry().register(
['fixture'],
config => new CounterStrategy(f.execution, config)
);
const factory = () => new CounterStrategy(f.execution);
expect(() => registry.register(['new', 'FIXTURE'], factory)).toThrow('already registered');
await expect(
registry.create({ ...recoveryConfig, name: 'new' }, f.dependencies)
).rejects.toThrow('Unknown strategy');
for (const aliases of [[], [''], [' '], ['duplicate', 'DUPLICATE']]) {
expect(() => registry.register(aliases, factory)).toThrow('nonempty and distinct');
}
expect(
(await registry.create({ ...recoveryConfig, name: ' FIXTURE ' }, f.dependencies)).getConfig()
.id
).toBe('counter');
});
test('built-in aliases keep their implementations and unknown names fail closed', async () => {
const f = await fixture();
const registry = createBuiltinStrategyRegistry();
for (const name of ['meanreversion', 'mean-reversion']) {
expect(
(await registry.create({ ...recoveryConfig, name }, f.dependencies)).getCheckpointVersion()
).toBe('mean-reversion/v2');
}
for (const name of ['smacrossover', 'sma-crossover', 'moving-average']) {
expect(
(await registry.create({ ...recoveryConfig, name }, f.dependencies)).getCheckpointVersion()
).toBe('sma-crossover-fixed/v2');
}
for (const name of ['mlenhanced', 'ml-enhanced']) {
expect(
(await registry.create({ ...recoveryConfig, name }, f.dependencies)).getCheckpointVersion()
).toBeUndefined();
}
await expect(
registry.create({ ...recoveryConfig, name: 'missing' }, f.dependencies)
).rejects.toThrow('Unknown strategy: missing');
});
test('factory failures propagate before an invalid strategy can be activated', async () => {
const f = await fixture();
const registry = new StrategyRegistry().register(['fixture'], async () => {
throw new Error('factory failed');
});
const manager = new StrategyManager(f.services, registry);
cleanup.push(() => manager.shutdown());
await expect(manager.initializeStrategies([recoveryConfig])).rejects.toThrow('factory failed');
expect(manager.getActiveStrategies()).toEqual([]);
expect(await f.execution.registeredStrategies()).toEqual([]);
});
});