feat(research): add strategy catalog and versioned backtest setup

This commit is contained in:
Boki 2026-10-23 21:11:59 -04:00
parent 8cca0ee588
commit d929db707a
44 changed files with 4198 additions and 209 deletions

View file

@ -3,7 +3,7 @@ import { EventEmitter } from 'node:events';
import { readFileSync } from 'node:fs';
import type { ServerWebSocket } from 'bun';
import { Pool } from 'pg';
import { chromium, type Page } from 'playwright';
import type { Browser, Page } from 'playwright';
import { createHttpSecurity } from '@stock-bot/di';
import { HandlerRegistry } from '@stock-bot/handler-registry';
import type { IServiceContainer } from '@stock-bot/handlers';
@ -27,10 +27,12 @@ import {
handleWebSocketConnection,
} from '../../apps/stock/web-api/src/routes/websocket.routes';
import { BacktestDispatcher } from '../../apps/stock/web-api/src/services/BacktestDispatcher';
import { StrategyCatalogService } from '../../apps/stock/web-api/src/services/strategy-catalog.service';
import {
getWebSocketHandler,
type RunSocketData,
} from '../../apps/stock/web-api/src/websocket/run-updates';
import { seedBuiltinCatalog } from '../../scripts/seed-builtin-strategies';
setLoggerConfig({ logLevel: 'fatal', logConsole: false });
const logger = getLogger('browser-fixture');
@ -214,7 +216,7 @@ class FixtureEngine extends EventEmitter {
}
}
export async function fixture(native = false) {
export async function fixture(browser: Browser, native = false) {
const database = process.env.PIPELINE_TEST_DATABASE_URL;
const redisUrl = process.env.BACKTEST_TEST_REDIS_URL;
if (
@ -285,7 +287,9 @@ export async function fixture(native = false) {
'10-durable-backtest-dispatch.sql',
'12-historical-datasets.sql',
'14-backtest-submission.sql',
'16-strategy-catalog.sql',
'17-research-retention.sql',
'18-backtest-strategy-versions.sql',
]) {
await pool.query(
readFileSync(new URL(`../../database/postgres/init/${name}`, import.meta.url), 'utf8')
@ -399,6 +403,8 @@ export async function fixture(native = false) {
(!pathname.startsWith('/api/v2/backtests') &&
!pathname.startsWith('/api/v2/runs') &&
!pathname.startsWith('/api/v2/datasets') &&
!pathname.startsWith('/api/v2/strategies') &&
!pathname.startsWith('/api/v2/strategy-versions') &&
pathname !== '/api/auth/session') ||
/\/(pause|resume|speed)$/.test(pathname)
) {
@ -458,9 +464,8 @@ export async function fixture(native = false) {
}
});
const origin = `http://127.0.0.1:${server.port}`;
const browser = await chromium.launch({ headless: true });
cleanup.push(() => browser.close());
const context = await browser.newContext({ viewport: { width: 1440, height: 1080 } });
cleanup.push(() => context.close());
await context.route('**/*', route => {
const url = new URL(route.request().url());
if (url.origin !== origin && !['data:', 'blob:'].includes(url.protocol)) {
@ -520,10 +525,19 @@ export async function fixture(native = false) {
await target.getByRole('button', { name: 'Sign in', exact: true }).click();
await visible(target, 'Create and manage backtest configurations');
};
const request = async (path: string, method = 'GET', body?: unknown) => {
const request = async (
path: string,
method = 'GET',
body?: unknown,
headers: Record<string, string> = {}
) => {
const response = await fetch(origin + path, {
method,
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
...headers,
},
body: body === undefined ? undefined : JSON.stringify(body),
});
if (!response.ok) {
@ -546,6 +560,7 @@ export async function fixture(native = false) {
login,
close,
unexpectedNetwork,
seedCatalog: () => seedBuiltinCatalog(new StrategyCatalogService(container)),
};
} catch (error) {
try {
@ -560,7 +575,7 @@ export async function fixture(native = false) {
export async function createInBrowser(page: Page, name: string, run = false) {
await page.getByRole('button', { name: 'New Backtest', exact: true }).click();
await page.getByPlaceholder('My Strategy Backtest').fill(name);
await page.locator('select').first().selectOption('moving-average');
await page.getByLabel('Legacy strategy', { exact: true }).selectOption('moving-average');
await page.locator('input[type="date"]').nth(0).fill('2024-01-01');
await page.locator('input[type="date"]').nth(1).fill('2024-03-15');
await page.getByRole('button', { name: run ? 'Save and run' : 'Save', exact: true }).click();