refactor(api): compose research services with narrow collaborators (CL28)
This commit is contained in:
parent
3a63b2039e
commit
24487d35da
28 changed files with 281 additions and 94 deletions
|
|
@ -16,13 +16,13 @@ import { parseValidationDetail } from '../../apps/stock/config/src/contracts/res
|
|||
import { parseChronologicalValidationPreview } from '../../apps/stock/config/src/contracts/research-responses/validation-preview';
|
||||
import { parseWalkForwardDetail } from '../../apps/stock/config/src/contracts/research-responses/walk-forward';
|
||||
import { canonicalRiskSessionClock } from '../../apps/stock/web-api/src/services/canonical-risk-clock';
|
||||
import { ChronologicalValidationService } from '../../apps/stock/web-api/src/services/chronological-validation.service';
|
||||
import {
|
||||
buildExperimentPreview,
|
||||
experimentHash,
|
||||
experimentTrialRequest,
|
||||
} from '../../apps/stock/web-api/src/services/experiment-input';
|
||||
import { ExperimentsService } from '../../apps/stock/web-api/src/services/experiments.service';
|
||||
import { createResearchServices } from '../../apps/stock/web-api/src/services/research-services';
|
||||
import {
|
||||
selectValidationWinner,
|
||||
validationPhasePreview,
|
||||
|
|
@ -181,7 +181,7 @@ async function pendingValidation(preview: ChronologicalValidationPreview, id = v
|
|||
validation_selection: null,
|
||||
validation_report: null,
|
||||
});
|
||||
return new ChronologicalValidationService(stored.container).readDetail(stored.client, id);
|
||||
return createResearchServices(stored.container).validations.readDetail(stored.client, id);
|
||||
}
|
||||
|
||||
for (const [label, parameters] of [
|
||||
|
|
|
|||
112
test/integration/research-service-composition.test.ts
Normal file
112
test/integration/research-service-composition.test.ts
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
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();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue