feat(research): download validated retained datasets (RP07)

This commit is contained in:
Boki 2026-10-27 10:59:19 -04:00
parent 43ea2cc9b6
commit b959779ada
14 changed files with 1076 additions and 11 deletions

View file

@ -1,4 +1,5 @@
import { createHash, randomUUID } from 'node:crypto';
import { readFileSync } from 'node:fs';
import { afterAll, beforeAll, expect, test } from 'bun:test';
import { chromium, type Browser, type Request } from 'playwright';
import {
@ -33,10 +34,14 @@ import type {
} from '@stock-bot/stock-config/walk-forward';
import type {
HistoricalDatasetInputV2,
HistoricalDatasetV2,
ResearchArtifact,
ResearchFindingSource,
} from '@stock-bot/types';
import { createDatasetV2 } from '../../apps/stock/orchestrator/src/data/historical-dataset-v2';
import {
createDatasetV2,
validateDatasetV2,
} from '../../apps/stock/orchestrator/src/data/historical-dataset-v2';
import { retainResearchDataset } from '../../scripts/import-research-dataset';
import { canonicalSettings } from '../fixtures/canonical-research';
import { canonicalSmaFixture } from '../fixtures/canonical-sma';
@ -63,6 +68,32 @@ afterAll(async () => {
});
const fixture = (native = false) => createFixture(browser, native);
async function downloadDatasetInBrowser(
f: Awaited<ReturnType<typeof fixture>>,
expected: HistoricalDatasetV2
) {
const hash = expected.manifest.hash;
const responding = f.page.waitForResponse(response =>
response.url().endsWith(`/api/v2/datasets/canonical/${hash}/export`)
);
const downloading = f.page.waitForEvent('download');
await f.page.getByRole('button', { name: 'Download retained dataset', exact: true }).click();
const response = await responding;
expect(response.status()).toBe(200);
expect(response.headers()['cache-control']).toBe('no-store');
expect(response.headers()['content-disposition']).toBe(
`attachment; filename="dataset-${hash}.json"`
);
const download = await downloading;
expect(download.suggestedFilename()).toBe(`dataset-${hash}.json`);
expect(await download.failure()).toBeNull();
const path = await download.path();
if (!path) {
throw new Error('Expected a downloaded fixture file');
}
expect(validateDatasetV2(JSON.parse(readFileSync(path, 'utf8')))).toEqual(expected);
}
test('operator_provisions_bounded_agent_views_usage_and_revokes_without_corrupting_accepted_work_browser', async () => {
const f = await fixture(true);
try {
@ -1857,7 +1888,7 @@ test('canonical_native_smoke', async () => {
const f = await fixture(true);
try {
await f.login();
const { id } = await createCanonicalInBrowser(f, 'Actual Rust browser smoke');
const { id, dataset } = await createCanonicalInBrowser(f, 'Actual Rust browser smoke');
await f.page.getByRole('button', { name: 'Settings', exact: true }).click();
await f.page.getByLabel('Backtest name', { exact: true }).fill('Unsaved native research draft');
await f.page.getByRole('button', { name: 'Runs', exact: true }).click();
@ -1896,10 +1927,29 @@ test('canonical_native_smoke', async () => {
await f.page.getByRole('button', { name: 'Trades', exact: true }).click();
await visible(f.page, result.financial.projection.fills[0].side);
await visible(f.page, result.financial.projection.fills[0].price);
expect(
await f.page.getByRole('button', { name: 'Download retained dataset', exact: true }).count()
).toBe(0);
// The editable template moves to a different retained source; this run's
// export must still resolve its immutable original request hash.
const replacement = canonicalSmaFixture(['A'], { volume: 2000 }).dataset;
await retainResearchDataset(f.pool, replacement);
const template = await f.request(`/api/v2/backtests/${id}`);
await f.request(`/api/v2/backtests/${id}`, 'PUT', {
config: { ...template.config, datasetHash: replacement.manifest.hash },
});
await f.page.reload();
await visible(f.page, 'Completed');
await f.page.getByText('Immutable configuration and reporting policy', { exact: true }).click();
await visible(f.page, result.request.datasetHash);
await downloadDatasetInBrowser(f, dataset);
expect((await f.request(`/api/v2/backtests/${id}`)).config.datasetHash).toBe(
replacement.manifest.hash
);
expect((await f.pool.query('SELECT count(*)::int AS n FROM runs')).rows[0].n).toBe(1);
expect((await f.pool.query('SELECT count(*)::int AS n FROM backtest_dispatch')).rows[0].n).toBe(
1
);
expect(f.unexpectedNetwork).toEqual([]);
} finally {
await f.close();
@ -2433,6 +2483,7 @@ test('canonical_dataset_browser_reuses_older_pages_and_known_hash_without_new_ru
() => choice.inputValue().then(value => value === known.manifest.hash),
'known hash selection'
);
await downloadDatasetInBrowser(f, known);
expect(await f.request(`/api/v2/backtests/${id}`)).toEqual(before);
await f.page.getByRole('button', { name: 'Load more canonical datasets', exact: true }).click();
await choice.locator(`option[value="${older.manifest.hash}"]`).waitFor({ state: 'attached' });