feat(research): add durable walk-forward validation workflows
This commit is contained in:
parent
abd1c5b83d
commit
3ed7b98f7c
45 changed files with 5188 additions and 199 deletions
125
test/integration/walk-forward-api.test.ts
Normal file
125
test/integration/walk-forward-api.test.ts
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
import { randomUUID } from 'node:crypto';
|
||||
import { describe, expect, mock, test } from 'bun:test';
|
||||
import { Hono } from 'hono';
|
||||
import { z } from 'zod';
|
||||
import { createHttpSecurity } from '@stock-bot/di';
|
||||
import type { IServiceContainer } from '@stock-bot/services';
|
||||
import { createExperimentRoutes } from '../../apps/stock/web-api/src/routes/experiments.routes';
|
||||
import { walkForwardDataset } from '../fixtures/walk-forward';
|
||||
|
||||
const tokens = {
|
||||
operator: 'validation-admin-fixture-not-a-real-secret-1234',
|
||||
service: 'validation-service-fixture-not-a-real-secret-1234',
|
||||
read: 'validation-read-fixture-not-a-real-secret-1234',
|
||||
};
|
||||
const input = {
|
||||
backtestId: randomUUID(),
|
||||
name: 'Validation preview',
|
||||
grid: { fastPeriod: [1, 2] },
|
||||
windows: walkForwardDataset().windows,
|
||||
};
|
||||
type Query = (sql: string, values?: unknown[]) => Promise<{ rows: Record<string, unknown>[] }>;
|
||||
function fixture(
|
||||
mode: 'enabled' | 'disabled' | 'absent' = 'enabled',
|
||||
query: Query = async () => ({ rows: [] })
|
||||
) {
|
||||
const client = { query: mock(query) };
|
||||
const database = {
|
||||
...client,
|
||||
transaction: mock(async <T>(work: (client: { query: Query }) => Promise<T>) => work(client)),
|
||||
};
|
||||
const app = new Hono();
|
||||
if (mode !== 'absent') {
|
||||
createHttpSecurity({
|
||||
adminToken: tokens.operator,
|
||||
serviceToken: tokens.service,
|
||||
readToken: tokens.read,
|
||||
environment: 'test',
|
||||
hostname: '127.0.0.1',
|
||||
disabled: mode === 'disabled',
|
||||
}).mount(app);
|
||||
}
|
||||
app.route('/', createExperimentRoutes({ postgres: database } as unknown as IServiceContainer));
|
||||
return {
|
||||
database,
|
||||
request(path = '', method = 'GET', body?: string, role?: keyof typeof tokens, key?: string) {
|
||||
return app.request('/api/v2/experiments/walk-forward' + path, {
|
||||
method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(role ? { Authorization: `Bearer ${tokens[role]}` } : {}),
|
||||
...(key ? { 'Idempotency-Key': key } : {}),
|
||||
},
|
||||
...(body === undefined ? {} : { body }),
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const id = randomUUID(),
|
||||
previewHash = 'a'.repeat(64);
|
||||
|
||||
const actions = [
|
||||
['/preview', JSON.stringify(input)],
|
||||
['', JSON.stringify({ ...input, previewHash })],
|
||||
[`/${id}/finalize`, JSON.stringify({ previewHash })],
|
||||
[`/${id}/cancel`, '{}'],
|
||||
];
|
||||
describe('walk-forward HTTP gates', () => {
|
||||
test('writes need real operator/service proof, readers may inspect only', async () => {
|
||||
for (const mode of ['enabled', 'disabled', 'absent'] as const) {
|
||||
const f = fixture(mode);
|
||||
for (const [path, body] of actions) {
|
||||
expect((await f.request(path, 'POST', body, undefined, 'key')).status).toBe(401);
|
||||
}
|
||||
expect(f.database.transaction.mock.calls).toHaveLength(0);
|
||||
}
|
||||
const f = fixture();
|
||||
for (const [path, body] of actions) {
|
||||
expect((await f.request(path, 'POST', body, 'read', 'key')).status).toBe(403);
|
||||
}
|
||||
expect((await f.request('', 'GET', undefined, 'read')).status).toBe(200);
|
||||
expect((await f.request(`/${id}`, 'GET', undefined, 'read')).status).toBe(404);
|
||||
expect((await f.request()).status).toBe(401);
|
||||
});
|
||||
test('strict malformed, reserved, missing-key and mixed-operation inputs fail before storage', async () => {
|
||||
const cases: [string, string, string | undefined][] = [
|
||||
['/preview', '{', 'key'],
|
||||
['/preview', JSON.stringify({ ...input, winner: 0 }), 'key'],
|
||||
['/preview', JSON.stringify(input).replace('"fastPeriod":[1,2]', '"__proto__":[1]'), 'key'],
|
||||
['', JSON.stringify({ ...input, previewHash }), undefined],
|
||||
[`/${id}/finalize`, JSON.stringify({ previewHash }), undefined],
|
||||
[`/${id}/finalize`, JSON.stringify({ previewHash, selectionHash: 'a'.repeat(64) }), 'key'],
|
||||
[`/${id}/cancel`, '{"restart":true}', undefined],
|
||||
['/bad/finalize', JSON.stringify({ previewHash }), 'key'],
|
||||
];
|
||||
for (const [path, body, key] of cases) {
|
||||
const f = fixture();
|
||||
expect((await f.request(path, 'POST', body, 'operator', key)).status).toBe(400);
|
||||
expect(f.database.transaction.mock.calls).toHaveLength(0);
|
||||
expect(f.database.query.mock.calls).toHaveLength(0);
|
||||
}
|
||||
});
|
||||
test('valid routes return missing404 and unexpected storage/schema failure is sanitized500', async () => {
|
||||
for (const role of ['operator', 'service'] as const) {
|
||||
const f = fixture();
|
||||
for (const [path, body] of actions) {
|
||||
expect((await f.request(path, 'POST', body, role, 'key')).status).toBe(404);
|
||||
}
|
||||
}
|
||||
for (const error of [new Error('private storage failure'), new z.ZodError([])]) {
|
||||
const f = fixture('enabled', async () => {
|
||||
throw error;
|
||||
});
|
||||
const response = await f.request(
|
||||
`/${id}/finalize`,
|
||||
'POST',
|
||||
JSON.stringify({ previewHash }),
|
||||
'operator',
|
||||
'key'
|
||||
);
|
||||
expect(response.status).toBe(500);
|
||||
expect(await response.json()).toEqual({ error: 'Experiment operation unavailable' });
|
||||
}
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue