stock-bot/test/integration/research-source-evidence.test.ts

104 lines
3.8 KiB
TypeScript

import { describe, expect, mock, test } from 'bun:test';
import {
createResearchArtifactSchema,
parseResearchInput,
researchFindingSourceSchema,
reviseResearchArtifactSchema,
} from '@stock-bot/stock-config/research';
import type { BacktestResultQuery } from '../../apps/stock/web-api/src/services/backtest-v2.service';
import { verifyResearchSource } from '../../apps/stock/web-api/src/services/research-source-evidence';
const id = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa';
const source = { kind: 'experiment/v1' as const, experimentId: id, previewHash: 'a'.repeat(64) };
const canonical = {
runId: id,
metricsVersion: 'canonical-analytics/v1' as const,
metricPath: 'analytics.metrics.totalReturn',
};
describe('finding source contract', () => {
test('strict source identities normalize UUIDs and reject client measurements, mixed kinds and missing hashes', () => {
expect(
researchFindingSourceSchema.parse({ ...source, experimentId: id.toUpperCase() })
).toEqual(source);
for (const raw of [
{ ...source, previewHash: 'A'.repeat(64) },
{ ...source, previewHash: null },
{ ...source, experimentId: null },
{ ...source, totalReturn: 0 },
{ ...source, reportId: id },
{ ...source, kind: 'unknown' },
{
kind: 'chronological-report/v1',
validationId: id,
previewHash: source.previewHash,
reportId: id,
},
{
kind: 'walk-forward-report/v1',
walkForwardId: id,
previewHash: source.previewHash,
reportId: id,
reportHash: 'bad',
},
]) {
expect(researchFindingSourceSchema.safeParse(raw).success).toBe(false);
}
for (const kind of ['chronological-report/v1', 'walk-forward-report/v1'] as const) {
const owner =
kind === 'chronological-report/v1'
? { validationId: id.toUpperCase() }
: { walkForwardId: id.toUpperCase() };
const parsed = researchFindingSourceSchema.parse({
kind,
...owner,
previewHash: source.previewHash,
reportId: id.toUpperCase(),
reportHash: 'b'.repeat(64),
});
expect('reportId' in parsed && parsed.reportId).toBe(id);
}
});
test('old create and revise requests preserve the distinction between omitted and explicitly detached sources', () => {
const body = { title: 'Observation', body: 'Unvalidated narrative', evidence: [canonical] };
expect(
parseResearchInput(createResearchArtifactSchema, { ...body, kind: 'finding' }).source
).toBeUndefined();
expect(
parseResearchInput(reviseResearchArtifactSchema, { ...body, expectedRevision: 1 }).source
).toBeUndefined();
expect(
parseResearchInput(reviseResearchArtifactSchema, {
...body,
expectedRevision: 1,
source: null,
}).source
).toBeNull();
expect(
parseResearchInput(createResearchArtifactSchema, { ...body, kind: 'note', source }).source
).toEqual(source);
});
test('source cannot substitute for canonical metric evidence; unavailable source fails before any result reader', async () => {
const query = mock(async () => ({ rows: [] })),
read = mock(async () => null);
const database = { query } as unknown as BacktestResultQuery;
for (const evidence of [
[],
[
{
runId: id,
metricsVersion: 'legacy-display/v1' as const,
metricPath: 'metrics.totalReturn',
},
],
]) {
await expect(verifyResearchSource(source, evidence, database, read)).rejects.toMatchObject({
status: 422,
});
}
expect(query.mock.calls).toHaveLength(0);
await expect(verifyResearchSource(source, [canonical], database, read)).rejects.toMatchObject({
status: 422,
});
expect(read.mock.calls).toHaveLength(0);
});
});