feat(research): select and reuse frozen datasets in workbench
This commit is contained in:
parent
6a67ae8683
commit
b106c1ca85
29 changed files with 2295 additions and 426 deletions
85
test/integration/dataset-catalog.test.ts
Normal file
85
test/integration/dataset-catalog.test.ts
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
import { expect, test } from 'bun:test';
|
||||
import { Hono } from 'hono';
|
||||
import { getLogger, setLoggerConfig } from '@stock-bot/logger';
|
||||
import type { PostgresClient } from '@stock-bot/services';
|
||||
import { parseBacktestRequest } from '@stock-bot/stock-config/backtest';
|
||||
import { createDatasetRoutes } from '../../apps/stock/web-api/src/routes/datasets.routes';
|
||||
import { datasetSelectionForRun } from '../../apps/stock/web-api/src/services/backtest-v2.service';
|
||||
|
||||
setLoggerConfig({ logLevel: 'fatal', logConsole: false });
|
||||
const hash = 'a'.repeat(64);
|
||||
const logger = getLogger('dataset-catalog-http-test');
|
||||
test('dataset endpoints return explicit empty/missing states and reject invalid query inputs before database reads', async () => {
|
||||
const calls: { text: string; values?: unknown[] }[] = [];
|
||||
const postgres = {
|
||||
async query(text: string, values?: unknown[]) {
|
||||
calls.push({ text, values });
|
||||
return { rows: [] };
|
||||
},
|
||||
} as unknown as PostgresClient;
|
||||
const app = new Hono().route('/', createDatasetRoutes({ postgres, logger }));
|
||||
const list = await app.request('/api/v2/datasets');
|
||||
expect(list.status).toBe(200);
|
||||
expect(await list.json()).toEqual({ datasets: [] });
|
||||
expect(calls[0]!.values).toEqual([50, 0]);
|
||||
expect((await app.request(`/api/v2/datasets/${hash}`)).status).toBe(404);
|
||||
const count = calls.length;
|
||||
for (const path of [
|
||||
'/api/v2/datasets/bad',
|
||||
'/api/v2/datasets?limit=1x',
|
||||
'/api/v2/datasets?limit=0',
|
||||
'/api/v2/datasets?offset=-1',
|
||||
'/api/v2/datasets?offset=1.5',
|
||||
]) {
|
||||
expect((await app.request(path)).status).toBe(400);
|
||||
}
|
||||
expect(calls).toHaveLength(count);
|
||||
});
|
||||
|
||||
test('catalog database errors are failures, never an empty success or leaked internal diagnostic', async () => {
|
||||
const postgres = {
|
||||
async query() {
|
||||
throw new Error('private database detail');
|
||||
},
|
||||
} as unknown as PostgresClient;
|
||||
const app = new Hono().route('/', createDatasetRoutes({ postgres, logger }));
|
||||
for (const path of ['/api/v2/datasets', `/api/v2/datasets/${hash}`]) {
|
||||
const response = await app.request(path);
|
||||
expect(response.status).toBe(500);
|
||||
expect(await response.text()).not.toContain('private database detail');
|
||||
}
|
||||
});
|
||||
|
||||
test('run dataset provenance distinguishes accepted request, active preparation, terminal failure and actual binding', () => {
|
||||
const snapshot = parseBacktestRequest({
|
||||
mode: 'backtest',
|
||||
strategy: 'sma-crossover',
|
||||
symbols: ['A.US'],
|
||||
startDate: '2024-01-01T00:00:00.000Z',
|
||||
endDate: '2024-01-02T00:00:00.000Z',
|
||||
initialCapital: 1000,
|
||||
dataFrequency: '1d',
|
||||
datasetHash: hash,
|
||||
});
|
||||
const row = {
|
||||
request_snapshot: snapshot,
|
||||
dataset_hash: null,
|
||||
status: 'pending',
|
||||
config: { dataSource: 'questdb', datasetHash: 'b'.repeat(64) },
|
||||
};
|
||||
expect(datasetSelectionForRun(row)).toEqual({
|
||||
requestedHash: hash,
|
||||
pinnedHash: null,
|
||||
source: 'auto',
|
||||
state: 'requested',
|
||||
});
|
||||
for (const status of ['running', 'paused']) {
|
||||
expect(datasetSelectionForRun({ ...row, status }).state).toBe('preparing');
|
||||
}
|
||||
for (const status of ['completed', 'failed', 'cancelled']) {
|
||||
expect(datasetSelectionForRun({ ...row, status }).state).toBe('unavailable');
|
||||
expect(datasetSelectionForRun({ ...row, status, dataset_hash: hash }).state).toBe('pinned');
|
||||
}
|
||||
expect(datasetSelectionForRun({ ...row, request_snapshot: null }).state).toBe('unknown');
|
||||
expect(datasetSelectionForRun({ ...row, request_snapshot: {} }).state).toBe('unknown');
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue