feat(research): preview chronological validation inputs and windows
This commit is contained in:
parent
b373410320
commit
67ab30cb7a
25 changed files with 2917 additions and 54 deletions
|
|
@ -5,6 +5,7 @@ import {
|
|||
canonicalResearchSettingsSchema,
|
||||
type CanonicalRunResult,
|
||||
} from '@stock-bot/stock-config/canonical-research';
|
||||
import type { ChronologicalValidationPreview } from '@stock-bot/stock-config/chronological-validation';
|
||||
import type {
|
||||
ExperimentDetail,
|
||||
ExperimentPreview,
|
||||
|
|
@ -14,6 +15,7 @@ import type {
|
|||
import { retainResearchDataset } from '../../scripts/import-research-dataset';
|
||||
import { canonicalSettings } from '../fixtures/canonical-research';
|
||||
import { canonicalSmaFixture } from '../fixtures/canonical-sma';
|
||||
import { chronologicalDataset } from '../fixtures/chronological-validation';
|
||||
import {
|
||||
fixture as createFixture,
|
||||
createInBrowser,
|
||||
|
|
@ -36,6 +38,134 @@ afterAll(async () => {
|
|||
});
|
||||
const fixture = (native = false) => createFixture(browser, native);
|
||||
|
||||
test('chronological_preflight_checks_explicit_windows_without_admitting_or_saving_work_browser', async () => {
|
||||
const f = await fixture(true);
|
||||
f.page.on('dialog', dialog => void dialog.accept());
|
||||
try {
|
||||
const { dataset, recipe, start, minute } = chronologicalDataset();
|
||||
await retainResearchDataset(f.pool, dataset);
|
||||
const selected = (await f.seedCatalog()).find(
|
||||
item => item.package.program.strategyId === 'sma_crossover'
|
||||
)!;
|
||||
await f.request(`/api/v2/strategy-versions/${selected.id}/reviews`, 'POST', {
|
||||
decision: 'approve',
|
||||
reason: 'Reviewed canonical chronological preflight fixture',
|
||||
});
|
||||
const backtest = await f.request('/api/v2/backtests', 'POST', {
|
||||
name: 'Chronological preflight template',
|
||||
strategy: 'catalog',
|
||||
strategyVersionId: selected.id,
|
||||
strategyParameters: { fastPeriod: 2, slowPeriod: 3 },
|
||||
symbols: dataset.manifest.executionSeries.map(series => series.instrumentId),
|
||||
startDate: new Date(dataset.manifest.bounds.tradeStart).toISOString(),
|
||||
endDate: new Date(dataset.manifest.bounds.tradeEnd).toISOString(),
|
||||
initialCapital: 10000,
|
||||
config: {
|
||||
datasetHash: dataset.manifest.hash,
|
||||
dataFrequency: dataset.manifest.series[0]!.interval,
|
||||
canonical: canonicalSettings,
|
||||
},
|
||||
autoStart: false,
|
||||
});
|
||||
const evidence = async () =>
|
||||
(
|
||||
await f.pool.query(
|
||||
`SELECT (SELECT count(*) FROM backtest_datasets) AS datasets,
|
||||
(SELECT count(*) FROM research_experiments) AS experiments,
|
||||
(SELECT count(*) FROM runs) AS runs,
|
||||
(SELECT count(*) FROM backtest_dispatch) AS dispatch,
|
||||
(SELECT count(*) FROM backtest_results) AS results,
|
||||
(SELECT row_to_json(b) FROM backtests b WHERE id=$1) AS template`,
|
||||
[backtest.id]
|
||||
)
|
||||
).rows[0];
|
||||
const before = await evidence();
|
||||
await f.login();
|
||||
await f.page.goto(`${f.origin}/experiments?backtestId=${backtest.id}`);
|
||||
await f.page.getByLabel('Saved backtest').selectOption(backtest.id);
|
||||
await f.page.getByLabel('Experiment name', { exact: true }).fill('Chronological input check');
|
||||
await f.page.getByLabel('Parameter grid').fill('{"fastPeriod":[1,2]}');
|
||||
for (const phase of ['Train', 'Validation', 'Test'] as const) {
|
||||
const window = recipe[phase.toLowerCase() as 'train' | 'validation' | 'test'];
|
||||
await f.page
|
||||
.getByLabel(`${phase} warmup start (UTC)`, { exact: true })
|
||||
.fill(new Date(window.warmupStart).toISOString());
|
||||
await f.page
|
||||
.getByLabel(`${phase} trading start (UTC)`, { exact: true })
|
||||
.fill(new Date(window.tradeStart).toISOString());
|
||||
await f.page
|
||||
.getByLabel(`${phase} trading end (UTC)`, { exact: true })
|
||||
.fill(new Date(window.tradeEnd).toISOString());
|
||||
}
|
||||
const prepare = async () => {
|
||||
const reply = f.page.waitForResponse(response =>
|
||||
response.url().endsWith('/api/v2/experiments/validation/preview')
|
||||
);
|
||||
await f.page
|
||||
.getByRole('button', { name: 'Preview chronological validation', exact: true })
|
||||
.click();
|
||||
return reply;
|
||||
};
|
||||
const response = await prepare();
|
||||
expect(response.status()).toBe(200);
|
||||
const { preview } = (await response.json()) as { preview: ChronologicalValidationPreview };
|
||||
expect(response.request().postDataJSON().recipe).toEqual(recipe);
|
||||
expect(preview.execution).toBe('preview-only');
|
||||
expect(preview.source.dataset.hash).toBe(dataset.manifest.hash);
|
||||
expect(preview.partitions.map(partition => partition.phase)).toEqual([
|
||||
'train',
|
||||
'validation',
|
||||
'test',
|
||||
]);
|
||||
expect(preview.source.candidates).toHaveLength(2);
|
||||
const preparedInputs = f.page.getByRole('region', {
|
||||
name: 'Prepared chronological inputs',
|
||||
exact: true,
|
||||
});
|
||||
await visible(f.page, 'Exact chronological preview inputs and policies');
|
||||
await f.page
|
||||
.getByText('Exact chronological preview inputs and policies', { exact: true })
|
||||
.click();
|
||||
expect(await preparedInputs.locator('pre').textContent()).toContain(preview.previewHash);
|
||||
await visible(f.page, /No datasets, experiments or runs are saved/);
|
||||
for (const partition of preview.partitions) {
|
||||
await visible(f.page, partition.dataset.hash);
|
||||
expect(partition.requiredWarmup).toBe(4);
|
||||
}
|
||||
expect(await evidence()).toEqual(before);
|
||||
// Structurally valid bounds with too few available warmup observations reject explicitly.
|
||||
await f.page
|
||||
.getByLabel('Train warmup start (UTC)', { exact: true })
|
||||
.fill(new Date(start + 4 * minute).toISOString());
|
||||
expect(await preparedInputs.count()).toBe(0);
|
||||
const invalid = await prepare();
|
||||
expect(invalid.status()).toBe(400);
|
||||
const failure = (await invalid.json()) as { error: string };
|
||||
await visible(f.page, failure.error);
|
||||
await f.page
|
||||
.getByLabel('Train warmup start (UTC)', { exact: true })
|
||||
.fill(new Date(recipe.train.warmupStart).toISOString());
|
||||
const repeated = await prepare();
|
||||
expect(repeated.status()).toBe(200);
|
||||
expect((await repeated.json()).preview.previewHash).toBe(preview.previewHash);
|
||||
await visible(f.page, 'Exact chronological preview inputs and policies');
|
||||
await f.page
|
||||
.getByText('Exact chronological preview inputs and policies', { exact: true })
|
||||
.click();
|
||||
expect(await preparedInputs.locator('pre').textContent()).toContain(preview.previewHash);
|
||||
await f.page.getByLabel('Experiment name', { exact: true }).fill('Changed input');
|
||||
expect(await preparedInputs.count()).toBe(0);
|
||||
await f.page.reload();
|
||||
await visible(f.page, /No experiments yet/);
|
||||
expect(await preparedInputs.count()).toBe(0);
|
||||
expect(await evidence()).toEqual(before);
|
||||
expect(f.historicalDataReads).toEqual([]);
|
||||
expect(f.unexpectedNetwork).toEqual([]);
|
||||
} finally {
|
||||
await f.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('failed_experiment_trial_retry_preserves_history_and_reopens_native_result_browser', async () => {
|
||||
const f = await fixture(true);
|
||||
f.page.on('dialog', dialog => void dialog.accept());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue