85 lines
3.4 KiB
TypeScript
85 lines
3.4 KiB
TypeScript
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');
|
|
});
|