56 lines
2 KiB
TypeScript
56 lines
2 KiB
TypeScript
/** Disposable process used only against an isolated stock_bot_safety schema. */
|
|
import { Pool } from 'pg';
|
|
import { PostgreSQLClient } from '@stock-bot/postgres';
|
|
import { PostgresExecutionRepository } from '../../apps/stock/orchestrator/src/execution/PostgresExecutionRepository';
|
|
import { logger, makeExecution, start } from './execution';
|
|
import { attachCounter, CounterStrategy, FaultRepository } from './strategy-recovery';
|
|
|
|
const url = process.env.PIPELINE_TEST_DATABASE_URL,
|
|
schema = process.env.STRATEGY_FIXTURE_SCHEMA,
|
|
phase = process.env.STRATEGY_FIXTURE_PHASE;
|
|
if (
|
|
!url ||
|
|
!new URL(url).pathname.startsWith('/stock_bot_safety') ||
|
|
!schema ||
|
|
!/^stock_bot_safety_[a-f0-9]+$/.test(schema) ||
|
|
!['decision', 'checkpoint', 'dispatch'].includes(phase ?? '')
|
|
) {
|
|
throw new Error('Unsafe strategy crash fixture');
|
|
}
|
|
const pool = new Pool({ connectionString: url, options: '-c search_path=' + schema });
|
|
const postgres = new PostgreSQLClient(
|
|
{ host: 'unused', port: 5432, database: 'unused', user: 'unused', password: 'unused' },
|
|
logger
|
|
);
|
|
Object.defineProperty(postgres, 'connectionPool', { get: () => pool });
|
|
const repository = new FaultRepository(new PostgresExecutionRepository(postgres));
|
|
const { execution, engine } = makeExecution(repository, () => start + 100);
|
|
await execution.initialize(
|
|
{
|
|
mode: 'paper',
|
|
accountId: 'fixture',
|
|
startingCapital: 10000,
|
|
commission: 0.001,
|
|
slippage: 0,
|
|
maxQuoteAgeMs: 1000,
|
|
},
|
|
engine
|
|
);
|
|
const blocked = async () => {
|
|
process.send?.({ phase });
|
|
await new Promise<void>(() => {});
|
|
};
|
|
const strategy = new CounterStrategy(execution);
|
|
if (phase === 'decision') {
|
|
strategy.onDecision = blocked;
|
|
} else {
|
|
repository.fault = {
|
|
method: 'putIntent',
|
|
stage: 'commit',
|
|
matches: value =>
|
|
(value as { status: string }).status === (phase === 'checkpoint' ? 'pending' : 'dispatched'),
|
|
hook: blocked,
|
|
};
|
|
}
|
|
await attachCounter(execution, strategy);
|
|
throw new Error('Crash fixture did not reach its requested boundary');
|