feat(research): select and reuse frozen datasets in workbench

This commit is contained in:
Boki 2026-10-23 18:13:37 -04:00
parent 6a67ae8683
commit b106c1ca85
29 changed files with 2295 additions and 426 deletions

View file

@ -103,6 +103,26 @@ const storage: Pick<StorageService, 'getHistoricalBars'> = {
},
};
/** Retain a real hash-validated v1 fixture through the same preparation function as a run. */
export async function retainDataset(pool: Pool, overrides: Partial<BacktestConfig> = {}) {
const dataset = await loadDataset(storage, {
mode: 'backtest',
strategy: 'moving-average',
symbols: ['AAPL.US'],
startDate: '2024-01-01T00:00:00.000Z',
endDate: '2024-03-15T00:00:00.000Z',
dataFrequency: '1d',
initialCapital: 100000,
speed: 'max',
...overrides,
});
await pool.query('INSERT INTO backtest_datasets(hash,payload) VALUES ($1,$2)', [
dataset.manifest.hash,
JSON.stringify(dataset),
]);
return dataset;
}
/** Fixed UI lifecycle data, NOT a financial simulator or correctness oracle. */
class FixtureEngine extends EventEmitter {
completion = gate();
@ -377,6 +397,7 @@ export async function fixture(native = false) {
if (
(!pathname.startsWith('/api/v2/backtests') &&
!pathname.startsWith('/api/v2/runs') &&
!pathname.startsWith('/api/v2/datasets') &&
pathname !== '/api/auth/session') ||
/\/(pause|resume|speed)$/.test(pathname)
) {

View file

@ -1,7 +1,15 @@
import { randomUUID } from 'node:crypto';
import { expect, test } from 'bun:test';
import type { Request } from 'playwright';
import { createInBrowser, fixture, gate, submitInBrowser, until, visible } from './fixture';
import {
createInBrowser,
fixture,
gate,
retainDataset,
submitInBrowser,
until,
visible,
} from './fixture';
// Serial by design: the production WebSocket projection has a process-local handler.
test('browser_saved_run_lifecycle', async () => {
@ -24,6 +32,7 @@ test('browser_saved_run_lifecycle', async () => {
expect(response.headers().location).toBe(`/api/v2/runs/${run.id}`);
expect(response.request().headers()['idempotency-key']).toBeTruthy();
await visible(f.page, 'Pending');
await visible(f.page, /Source request \(auto\) — unpinned/);
expect((await f.pool.query('SELECT * FROM runs')).rowCount).toBe(1);
expect((await f.pool.query('SELECT * FROM backtest_dispatch')).rowCount).toBe(1);
expect(await f.page.getByRole('button', { name: /pause|resume/i }).count()).toBe(0);
@ -31,6 +40,9 @@ test('browser_saved_run_lifecycle', async () => {
f.dispatcher.start();
await visible(f.page, 'Running');
await visible(f.page, '25%');
expect(
await f.page.getByRole('region', { name: 'Run dataset', exact: true }).textContent()
).toContain('Pinned dataset hash');
await until(
() =>
f.frames.some(
@ -278,3 +290,117 @@ test('native_smoke', async () => {
await f.close();
}
});
test('browser_frozen_dataset_native_run_and_reopen', async () => {
const f = await fixture(true);
try {
const dataset = await retainDataset(f.pool);
const hash = dataset.manifest.hash;
await f.login();
await f.page.getByRole('button', { name: 'New Backtest', exact: true }).click();
await f.page.getByPlaceholder('My Strategy Backtest').fill('Frozen dataset browser run');
await f.page.getByLabel('Historical data input', { exact: true }).selectOption('frozen');
await f.page.getByLabel('Frozen dataset', { exact: true }).selectOption(hash);
expect(await f.page.getByLabel('Start date (UTC)', { exact: true }).inputValue()).toBe(
'2024-01-01'
);
expect(await f.page.getByLabel('Start date (UTC)', { exact: true }).isDisabled()).toBe(true);
await f.page.getByRole('button', { name: 'Save and run', exact: true }).click();
await f.page.waitForURL(/\/backtests\/[^/]+\/run\/[^/]+$/);
const runId = f.page.url().split('/').at(-1)!;
await visible(f.page, 'Pending');
const pending = await f.request(`/api/v2/runs/${runId}`);
expect(pending.dataset).toMatchObject({
requestedHash: hash,
pinnedHash: null,
state: 'requested',
});
expect(
await f.page.getByRole('region', { name: 'Run dataset', exact: true }).textContent()
).toContain(hash);
f.dispatcher.start();
await visible(f.page, 'Completed');
const result = await f.request(`/api/v2/runs/${runId}/results`);
expect(result.config.dataset.hash).toBe(hash);
expect(result.engine).toBe('native');
expect(result.trades.length).toBeGreaterThan(0);
expect((await f.pool.query('SELECT count(*) FROM backtest_datasets')).rows[0].count).toBe('1');
await visible(f.page, 'Research only · Unverified economics');
const runDataset = f.page.getByRole('region', { name: 'Run dataset', exact: true });
expect(await runDataset.textContent()).toContain('Pinned dataset hash');
await f.page.reload();
await visible(f.page, 'Completed');
expect(await runDataset.textContent()).toContain(hash);
await f.page.getByRole('button', { name: 'Trades', exact: true }).click();
await visible(f.page, result.trades[0].side.toUpperCase());
expect(f.unexpectedNetwork).toEqual([]);
} finally {
await f.close();
}
});
test('browser_intraday_dataset_exact_settings_and_incompatible_submission', async () => {
const f = await fixture();
try {
const startDate = '2024-01-02T13:30:00.123Z';
const endDate = '2024-01-02T15:30:00.456Z';
const dataset = await retainDataset(f.pool, { startDate, endDate, dataFrequency: '1m' });
const hash = dataset.manifest.hash;
await f.login();
await f.page.getByRole('button', { name: 'New Backtest', exact: true }).click();
await f.page.getByPlaceholder('My Strategy Backtest').fill('Exact intraday frozen settings');
await f.page.getByLabel('Historical data input', { exact: true }).selectOption('frozen');
await f.page.getByLabel('Frozen dataset', { exact: true }).selectOption(hash);
await f.page.getByRole('button', { name: 'Save', exact: true }).click();
await f.page.waitForURL(/\/backtests\/[^/]+$/);
const backtestId = f.page.url().split('/').at(-1)!;
await f.page.getByRole('button', { name: 'Settings', exact: true }).click();
expect(await f.page.getByLabel('Start instant (UTC)', { exact: true }).inputValue()).toBe(
startDate
);
expect(await f.page.getByLabel('End instant (UTC)', { exact: true }).inputValue()).toBe(
endDate
);
const saved = f.page.waitForResponse(
response =>
response.request().method() === 'PUT' &&
response.url().endsWith(`/api/v2/backtests/${backtestId}`)
);
await f.page.getByRole('button', { name: 'Save', exact: true }).click();
expect((await saved).status()).toBe(200);
const stored = (
await f.pool.query('SELECT start_date,end_date,config FROM backtests WHERE id=$1', [
backtestId,
])
).rows[0];
expect(stored.start_date.toISOString()).toBe(startDate);
expect(stored.end_date.toISOString()).toBe(endDate);
expect(stored.config.datasetHash).toBe(hash);
expect(stored.config.dataFrequency).toBe('1m');
await f.page.reload();
await f.page.getByRole('button', { name: 'Settings', exact: true }).click();
expect(await f.page.getByLabel('Start instant (UTC)', { exact: true }).inputValue()).toBe(
startDate
);
// Simulate a template changed outside this browser; API must reject before durable dispatch.
await f.pool.query(
"UPDATE backtests SET config=jsonb_set(config,'{dataFrequency}','\"5m\"') WHERE id=$1",
[backtestId]
);
await f.page.getByRole('button', { name: 'Runs', exact: true }).click();
await f.page.getByRole('button', { name: 'New Run', exact: true }).click();
const rejected = f.page.waitForResponse(
response =>
response.request().method() === 'POST' &&
response.url().endsWith(`/api/v2/backtests/${backtestId}/runs`)
);
await f.page.getByRole('button', { name: 'Run current settings', exact: true }).click();
expect((await rejected).status()).toBe(400);
await visible(f.page, /Frozen dataset does not match/);
expect((await f.pool.query('SELECT count(*) FROM runs')).rows[0].count).toBe('0');
expect((await f.pool.query('SELECT count(*) FROM backtest_dispatch')).rows[0].count).toBe('0');
expect(f.unexpectedNetwork).toEqual([]);
} finally {
await f.close();
}
});