feat(research): prepare and retain explicit source datasets in the web workbench
This commit is contained in:
parent
06039d17ba
commit
366bee810b
21 changed files with 2677 additions and 76 deletions
|
|
@ -3,6 +3,7 @@ import { afterAll, beforeAll, expect, test } from 'bun:test';
|
|||
import { chromium, type Browser, type Request } from 'playwright';
|
||||
import {
|
||||
canonicalResearchSettingsSchema,
|
||||
type CanonicalDatasetPreparation,
|
||||
type CanonicalRunResult,
|
||||
} from '@stock-bot/stock-config/canonical-research';
|
||||
import type {
|
||||
|
|
@ -28,7 +29,11 @@ import type {
|
|||
WalkForwardDetail,
|
||||
WalkForwardPreview,
|
||||
} from '@stock-bot/stock-config/walk-forward';
|
||||
import type { ResearchArtifact, ResearchFindingSource } from '@stock-bot/types';
|
||||
import type {
|
||||
HistoricalDatasetInputV2,
|
||||
ResearchArtifact,
|
||||
ResearchFindingSource,
|
||||
} from '@stock-bot/types';
|
||||
import { retainResearchDataset } from '../../scripts/import-research-dataset';
|
||||
import { canonicalSettings } from '../fixtures/canonical-research';
|
||||
import { canonicalSmaFixture } from '../fixtures/canonical-sma';
|
||||
|
|
@ -2109,7 +2114,25 @@ test('research_project_to_finding_browser_with_canonical_strategy_setup_run_and_
|
|||
f.page.on('dialog', dialog => void dialog.accept());
|
||||
try {
|
||||
const oracle = canonicalSmaFixture();
|
||||
const retained = await retainResearchDataset(f.pool, oracle.dataset);
|
||||
const retained = oracle.dataset.manifest;
|
||||
const { calendars, series, executionSeries, bounds, universe, corporateActions } = retained;
|
||||
// Explicit synthetic source export, with the same reviewed next-open oracle.
|
||||
// The browser submits raw source rows; only the server normalizes/hashes them.
|
||||
const source: HistoricalDatasetInputV2 = {
|
||||
metadata: { calendars, series, executionSeries, bounds, universe, corporateActions },
|
||||
bars: oracle.dataset.bars.map(bar => ({
|
||||
seriesId: bar.seriesId,
|
||||
timestamp: bar.intervalStart,
|
||||
availableAt: bar.availableAt,
|
||||
sourceSequence: bar.sourceSequence,
|
||||
open: bar.open,
|
||||
high: bar.high,
|
||||
low: bar.low,
|
||||
close: bar.close,
|
||||
volume: bar.volume,
|
||||
vwap: bar.vwap,
|
||||
})),
|
||||
};
|
||||
const seeded = await f.seedCatalog();
|
||||
const selected = seeded.find(item => item.package.program.strategyId === 'sma_crossover')!;
|
||||
expect(selected.reviewStatus).toBe('pending');
|
||||
|
|
@ -2216,7 +2239,93 @@ test('research_project_to_finding_browser_with_canonical_strategy_setup_run_and_
|
|||
);
|
||||
await f.page.getByRole('button', { name: 'Settings', exact: true }).click();
|
||||
await f.page.getByRole('button', { name: 'Configure canonical research', exact: true }).click();
|
||||
await f.page.getByLabel('Canonical dataset', { exact: true }).selectOption(retained.hash);
|
||||
await f.page.getByLabel('Initial capital (USD)', { exact: true }).fill('10000');
|
||||
const templateBeforePreparation = await f.request(`/api/v2/backtests/${backtestId}`);
|
||||
const preparationCounts = async () =>
|
||||
(
|
||||
await f.pool.query(`SELECT
|
||||
(SELECT count(*)::int FROM backtest_datasets) AS datasets,
|
||||
(SELECT count(*)::int FROM runs) AS runs,
|
||||
(SELECT count(*)::int FROM backtest_dispatch) AS dispatch`)
|
||||
).rows[0];
|
||||
expect(await preparationCounts()).toEqual({ datasets: 0, runs: 0, dispatch: 0 });
|
||||
await f.page.getByText('Prepare dataset from source export', { exact: true }).click();
|
||||
await f.page.getByLabel('Source dataset JSON', { exact: true }).setInputFiles({
|
||||
name: 'reviewed-synthetic-source.json',
|
||||
mimeType: 'application/json',
|
||||
buffer: Buffer.from(JSON.stringify(source)),
|
||||
});
|
||||
const previewingDataset = f.page.waitForResponse(response =>
|
||||
response.url().endsWith('/api/v2/datasets/canonical/preview')
|
||||
);
|
||||
await f.page.getByRole('button', { name: 'Preview dataset', exact: true }).click();
|
||||
const previewedDataset = await previewingDataset;
|
||||
expect(previewedDataset.status()).toBe(200);
|
||||
expect(previewedDataset.request().postDataJSON()).toEqual({ input: source });
|
||||
const preview = (await previewedDataset.json()) as {
|
||||
preparation: CanonicalDatasetPreparation;
|
||||
};
|
||||
expect(preview.preparation.manifest).toEqual(retained);
|
||||
expect(preview.preparation.hash).toBe(retained.hash);
|
||||
expect(preview.preparation.available).toBe(true);
|
||||
// Exercise real HTTP JSON consumption above the former web-server 1 MiB cap.
|
||||
// Whitespace changes transport size without changing the economic oracle.
|
||||
const largePreview = await f.page.evaluate(async input => {
|
||||
const body = JSON.stringify({ input }) + ' '.repeat(1_048_576);
|
||||
const response = await fetch('/api/v2/datasets/canonical/preview', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body,
|
||||
});
|
||||
return {
|
||||
bytes: new TextEncoder().encode(body).byteLength,
|
||||
status: response.status,
|
||||
cacheControl: response.headers.get('Cache-Control'),
|
||||
result: await response.json(),
|
||||
};
|
||||
}, source);
|
||||
expect(largePreview.bytes).toBeGreaterThan(1_048_576);
|
||||
expect(largePreview.status).toBe(200);
|
||||
expect(largePreview.cacheControl).toBe('no-store');
|
||||
expect(largePreview.result).toEqual(preview);
|
||||
await visible(f.page, `Dataset hash: ${retained.hash}`);
|
||||
const calendarDetails = f.page
|
||||
.getByRole('region', { name: 'Prepared dataset metadata', exact: true })
|
||||
.getByText(/^Calendar:/);
|
||||
await calendarDetails.waitFor();
|
||||
expect(await calendarDetails.textContent()).toContain(
|
||||
`Revision: ${retained.calendars[0]!.revision}`
|
||||
);
|
||||
expect(await preparationCounts()).toEqual({ datasets: 0, runs: 0, dispatch: 0 });
|
||||
expect(await f.request(`/api/v2/backtests/${backtestId}`)).toEqual(templateBeforePreparation);
|
||||
const retainingDataset = f.page.waitForResponse(response =>
|
||||
response.url().endsWith('/api/v2/datasets/canonical/retain')
|
||||
);
|
||||
await f.page.getByRole('button', { name: 'Retain dataset', exact: true }).click();
|
||||
const retainedDataset = await retainingDataset;
|
||||
expect(retainedDataset.status()).toBe(200);
|
||||
const retainedRequest = retainedDataset.request().postDataJSON();
|
||||
expect(retainedRequest).toEqual({ input: source, expectedHash: retained.hash });
|
||||
expect(await retainedDataset.json()).toEqual(preview);
|
||||
// Exact replay is safe after a lost response: one immutable row, no new jobs.
|
||||
expect(await f.request('/api/v2/datasets/canonical/retain', 'POST', retainedRequest)).toEqual(
|
||||
preview
|
||||
);
|
||||
expect(await preparationCounts()).toEqual({ datasets: 1, runs: 0, dispatch: 0 });
|
||||
expect(await f.request(`/api/v2/backtests/${backtestId}`)).toEqual(templateBeforePreparation);
|
||||
expect(
|
||||
(await f.pool.query('SELECT payload FROM backtest_datasets WHERE hash=$1', [retained.hash]))
|
||||
.rows[0].payload
|
||||
).toEqual(oracle.dataset);
|
||||
await until(
|
||||
async () =>
|
||||
(await f.page.getByLabel('Canonical dataset', { exact: true }).inputValue()) ===
|
||||
retained.hash,
|
||||
'retained source dataset selected in canonical setup'
|
||||
);
|
||||
expect(await f.page.getByLabel('Initial capital (USD)', { exact: true }).inputValue()).toBe(
|
||||
'10000'
|
||||
);
|
||||
await visible(f.page, new Date(retained.bounds.tradeStart).toISOString());
|
||||
await visible(f.page, new Date(retained.bounds.tradeEnd).toISOString());
|
||||
await f.page.getByLabel('Initial capital (USD)', { exact: true }).fill('10000');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue