stock-bot/test/fixtures/backtest-crash-owner.ts

45 lines
1.8 KiB
TypeScript

/** Dedicated fixture process: claim a real SQL run, heartbeat, then await a hard kill. */
import { Pool } from 'pg';
import { getLogger, setLoggerConfig } from '@stock-bot/logger';
import { PostgreSQLClient } from '@stock-bot/postgres';
import { BacktestRunner } from '../../apps/stock/orchestrator/src/backtest/BacktestRunner';
import { PostgresBacktestRunRepository } from '../../apps/stock/orchestrator/src/backtest/PostgresBacktestRunRepository';
import { RustBacktestAdapter } from '../../apps/stock/orchestrator/src/backtest/RustBacktestAdapter';
import { StorageService } from '../../apps/stock/orchestrator/src/services/StorageService';
setLoggerConfig({ logLevel: 'fatal', logConsole: false });
const url = process.env.PIPELINE_TEST_DATABASE_URL;
const schema = process.env.BACKTEST_FIXTURE_SCHEMA;
const runId = process.env.BACKTEST_FIXTURE_RUN_ID;
if (
!url ||
!new URL(url).pathname.startsWith('/stock_bot_safety') ||
!schema ||
!/^stock_bot_safety_[a-f0-9]+$/.test(schema) ||
!runId
) {
throw new Error('Unsafe crash fixture target');
}
const logger = getLogger('crash-owner');
const pool = new Pool({ connectionString: url, options: `-c search_path=${schema}` });
const postgres = new PostgreSQLClient(
{ host: 'unused', port: 5432, database: 'unused', username: 'unused', password: 'unused' },
logger
);
Object.defineProperty(postgres, 'connectionPool', { get: () => pool });
const repository = new PostgresBacktestRunRepository(postgres, 500);
repository.loadCanonicalDataset = async () => {
process.send?.({ type: 'claimed' });
return new Promise(() => {});
};
const runner = new BacktestRunner({
engine: new RustBacktestAdapter({
logger,
storage: new StorageService({ logger }, null, null, null),
}),
engineName: 'native',
repository,
logger,
heartbeatMs: 50,
});
await runner.runQueued(runId);