112 lines
4.9 KiB
TypeScript
112 lines
4.9 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { expect, mock, test } from 'bun:test';
|
|
import { Hono } from 'hono';
|
|
import type { PoolClient } from 'pg';
|
|
import type { IServiceContainer } from '@stock-bot/services';
|
|
import { createExperimentRoutes } from '../../apps/stock/web-api/src/routes/experiments.routes';
|
|
import { createPaperDeploymentRoutes } from '../../apps/stock/web-api/src/routes/paper-deployments.routes';
|
|
import { createResearchRoutes } from '../../apps/stock/web-api/src/routes/research.routes';
|
|
import { createStrategyRoutes } from '../../apps/stock/web-api/src/routes/strategies.routes';
|
|
import { ResearchEvidenceResolver } from '../../apps/stock/web-api/src/services/research-evidence';
|
|
import { createResearchServices } from '../../apps/stock/web-api/src/services/research-services';
|
|
import { walkForwardDataset } from '../fixtures/walk-forward';
|
|
|
|
test('composing research services and reading lists needs no worker, paper host, or transaction', async () => {
|
|
const query = mock(async () => ({ rows: [] }));
|
|
const transaction = mock(async () => {
|
|
throw new Error('Read-only listing must not start a transaction');
|
|
});
|
|
const paperIdentity = mock(async () => {
|
|
throw new Error('Read-only listing must not load a paper host');
|
|
});
|
|
const container = { postgres: { query, transaction } } as unknown as IServiceContainer;
|
|
const services = createResearchServices(container, { paperIdentity });
|
|
expect(query).not.toHaveBeenCalled();
|
|
const app = new Hono()
|
|
.route('/', createResearchRoutes(container, services.research))
|
|
.route('/', createExperimentRoutes(container, services))
|
|
.route('/', createStrategyRoutes(container, services.catalog))
|
|
.route(
|
|
'/',
|
|
createPaperDeploymentRoutes(container, {
|
|
service: services.paperDeployments,
|
|
origins: ['http://fixture.test'],
|
|
})
|
|
);
|
|
for (const [path, key] of [
|
|
['research-projects', 'projects'],
|
|
['experiments', 'experiments'],
|
|
['strategies', 'strategies'],
|
|
['paper-deployments', 'deployments'],
|
|
] as const) {
|
|
const response = await app.request(`/api/v2/${path}`);
|
|
expect(response.status).toBe(200);
|
|
expect(await response.json()).toEqual({ [key]: [] });
|
|
}
|
|
expect(query).toHaveBeenCalledTimes(4);
|
|
expect(transaction).not.toHaveBeenCalled();
|
|
expect(paperIdentity).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('walk-forward and chronological validation share the injected experiment preview inside their caller transaction', async () => {
|
|
const client = { query: mock(async () => ({ rows: [] })) } as unknown as PoolClient;
|
|
const poolQuery = mock(async () => {
|
|
throw new Error('Collaborator escaped its transaction');
|
|
});
|
|
const transaction = mock(async <T>(work: (client: PoolClient) => Promise<T>) => work(client));
|
|
const services = createResearchServices({
|
|
postgres: { query: poolQuery, transaction },
|
|
} as unknown as IServiceContainer);
|
|
const failure = new Error('Injected preview failure');
|
|
const lockedPreview = mock(async () => {
|
|
throw failure;
|
|
});
|
|
services.experiments.lockedPreview = lockedPreview;
|
|
const base = { backtestId: randomUUID(), name: 'Shared preview', grid: { fastPeriod: [1, 2] } };
|
|
const windows = walkForwardDataset().windows;
|
|
await expect(services.walkForwards.preview({ ...base, windows })).rejects.toBe(failure);
|
|
expect(lockedPreview).toHaveBeenLastCalledWith(client, base, 'operator');
|
|
await expect(
|
|
services.validations.submit(
|
|
{ ...base, recipe: windows[0], previewHash: 'a'.repeat(64) },
|
|
{ principal: 'operator', key: 'validation-preview' }
|
|
)
|
|
).rejects.toBe(failure);
|
|
expect(lockedPreview).toHaveBeenLastCalledWith(client, base, 'operator');
|
|
expect(lockedPreview).toHaveBeenCalledTimes(2);
|
|
expect(transaction).toHaveBeenCalledTimes(2);
|
|
expect(poolQuery).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('evidence accepts only a result-reader collaborator and passes through the supplied transaction', async () => {
|
|
const runId = randomUUID();
|
|
const client = {
|
|
query: mock(async () => ({
|
|
rows: [{ id: runId, backtest_id: randomUUID(), status: 'completed' }],
|
|
})),
|
|
} as unknown as PoolClient;
|
|
const poolQuery = mock(async () => {
|
|
throw new Error('Evidence escaped the supplied transaction');
|
|
});
|
|
const transaction = mock(async () => {
|
|
throw new Error('Evidence must not open a nested transaction');
|
|
});
|
|
const getRunResults = mock(async () => null);
|
|
const evidence = new ResearchEvidenceResolver(
|
|
{ postgres: { query: poolQuery, transaction } } as unknown as IServiceContainer,
|
|
{ getRunResults }
|
|
);
|
|
await expect(
|
|
evidence.resolve(
|
|
{
|
|
runId,
|
|
metricsVersion: 'canonical-analytics/v1',
|
|
metricPath: 'analytics.metrics.totalReturn',
|
|
},
|
|
client
|
|
)
|
|
).rejects.toThrow('Retained evidence result is missing');
|
|
expect(getRunResults).toHaveBeenCalledWith(runId, client);
|
|
expect(poolQuery).not.toHaveBeenCalled();
|
|
expect(transaction).not.toHaveBeenCalled();
|
|
});
|