feat(research): archive backtests and protect retained evidence

This commit is contained in:
Boki 2026-10-23 20:18:58 -04:00
parent 4a2e5ad362
commit 8cca0ee588
25 changed files with 1550 additions and 79 deletions

View file

@ -98,6 +98,9 @@ test('browser_cancel_failure', async () => {
const runId = f.page.url().split('/').at(-1)!;
f.dispatcher.start();
await visible(f.page, 'Running');
await f.page.getByRole('button', { name: 'Archive backtest', exact: true }).click();
await f.page.getByRole('button', { name: 'Confirm archive', exact: true }).click();
await visible(f.page, 'Restore backtest');
const acknowledged = f.page.waitForResponse(response =>
response.url().endsWith(`/runs/${runId}/cancel`)
);
@ -136,8 +139,13 @@ test('browser_cancel_failure', async () => {
const emptyId = f.page.url().split('/').at(-1)!;
expect((await f.request(`/api/v2/runs/${emptyId}/results`)).trades).toEqual([]);
// A saved completed row whose result is unavailable must not appear as zero trades.
await f.pool.query('DELETE FROM backtest_results WHERE run_id=$1', [emptyId]);
await f.page.reload();
const missingResultId = randomUUID();
const definitionId = f.page.url().split('/')[4]!;
await f.pool.query(
"INSERT INTO runs(id,backtest_id,run_number,status) VALUES ($1,$2,2,'completed')",
[missingResultId, definitionId]
);
await f.page.goto(f.page.url().replace(emptyId, missingResultId));
await visible(f.page, 'Run completed, but saved results are unavailable.');
await visible(f.page, 'Retry results');
expect(
@ -404,3 +412,70 @@ test('browser_intraday_dataset_exact_settings_and_incompatible_submission', asyn
await f.close();
}
});
test('browser_archive_retains_history_and_restore_admits_new_work', async () => {
const f = await fixture();
try {
await f.login();
await createInBrowser(f.page, 'Retained browser evidence', true);
const savedUrl = f.page.url();
const runId = savedUrl.split('/').at(-1)!;
const definitionId = savedUrl.split('/')[4]!;
await f.page.getByRole('button', { name: 'Archive backtest', exact: true }).click();
await visible(f.page, 'Confirm archive');
await f.page.getByRole('button', { name: 'Confirm archive', exact: true }).click();
await visible(f.page, 'Restore backtest');
await f.page.getByRole('button', { name: 'Settings', exact: true }).click();
expect(await f.page.getByPlaceholder('My Strategy Backtest').isDisabled()).toBe(true);
await f.page.getByRole('button', { name: 'Runs', exact: true }).click();
expect(await f.page.getByRole('button', { name: 'New Run', exact: true }).isDisabled()).toBe(
true
);
await expect(f.request(`/api/v2/backtests/${definitionId}`, 'DELETE')).rejects.toThrow('409');
// Already admitted work still starts, binds its dataset, and completes after archival.
f.dispatcher.start();
await visible(f.page, '25%');
expect(await f.page.getByRole('button', { name: 'Cancel run', exact: true }).isEnabled()).toBe(
true
);
f.controlled.completion.resolve();
await visible(f.page, 'Completed');
const result = await f.request(`/api/v2/runs/${runId}/results`);
expect(result.config.dataset.hash).toMatch(/^[a-f0-9]{64}$/);
await f.page.getByRole('button', { name: 'Trades', exact: true }).click();
await visible(f.page, 'BUY');
await f.page.reload();
await visible(f.page, 'Restore backtest');
await f.page.getByRole('button', { name: 'Trades', exact: true }).click();
await visible(f.page, 'BUY');
expect(f.page.url()).toBe(savedUrl);
await f.page.getByRole('button', { name: 'Back to backtests', exact: true }).click();
await f.page.getByRole('checkbox', { name: 'Show archived backtests', exact: true }).waitFor();
await until(
async () => (await f.page.locator('tbody tr').count()) === 0,
'default list hides archived template'
);
await f.page.getByRole('checkbox', { name: 'Show archived backtests', exact: true }).check();
await f.page.getByText('Retained browser evidence').click();
await visible(f.page, 'Restore backtest');
await f.page.getByText('#1', { exact: true }).click();
await visible(f.page, 'Completed');
expect(f.page.url()).toBe(savedUrl);
await f.page.getByRole('button', { name: 'Restore backtest', exact: true }).click();
await f.page.getByRole('button', { name: 'Confirm restore', exact: true }).click();
await visible(f.page, 'Archive backtest');
await f.page.getByRole('button', { name: 'Settings', exact: true }).click();
expect(await f.page.getByPlaceholder('My Strategy Backtest').isEnabled()).toBe(true);
await f.page.getByRole('button', { name: 'Runs', exact: true }).click();
const accepted = await submitInBrowser(f.page);
expect(accepted.status()).toBe(202);
expect((await accepted.json()).id).not.toBe(runId);
expect(
(await f.pool.query('SELECT * FROM runs WHERE backtest_id=$1', [definitionId])).rowCount
).toBe(2);
expect(await f.request(`/api/v2/runs/${runId}/results`)).toEqual(result);
expect(f.unexpectedNetwork).toEqual([]);
} finally {
await f.close();
}
});