173 lines
5.9 KiB
TypeScript
173 lines
5.9 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { describe, expect, test } from 'bun:test';
|
|
import type { CanonicalRunResult } from '@stock-bot/stock-config/canonical-research';
|
|
import {
|
|
parseResearchInput,
|
|
researchEvidenceReferenceSchema,
|
|
} from '@stock-bot/stock-config/research';
|
|
import type { BacktestRunResult, ResearchEvidenceReference } from '@stock-bot/types';
|
|
import { projectResearchEvidence } from '../../apps/stock/web-api/src/services/research-evidence';
|
|
|
|
const runId = randomUUID();
|
|
const backtestId = randomUUID();
|
|
const versionId = randomUUID();
|
|
const reference = (metricPath: string): ResearchEvidenceReference => ({
|
|
runId,
|
|
metricPath,
|
|
metricsVersion: 'canonical-analytics/v1',
|
|
strategyVersionId: versionId,
|
|
});
|
|
// Projection-only transport fixtures: the real PostgreSQL suite runs the retained
|
|
// worker and validates full Rust reports. These deliberately supply only reader fields.
|
|
const canonical = {
|
|
kind: 'canonical-result/v1',
|
|
runId,
|
|
backtestId,
|
|
status: 'completed',
|
|
request: { datasetHash: 'a'.repeat(64), strategy: { versionId } },
|
|
analytics: {
|
|
version: 1,
|
|
fillCount: 2,
|
|
metrics: {
|
|
totalReturn: -0.125,
|
|
exposureTime: 0,
|
|
sharpeRatio: 0,
|
|
beta: null,
|
|
avgWin: null,
|
|
roundTripCount: null,
|
|
},
|
|
unavailable: {
|
|
beta: 'missing-benchmark',
|
|
avgWin: 'unsupported-round-trips',
|
|
roundTripCount: 'unsupported-round-trips',
|
|
},
|
|
},
|
|
financial: {
|
|
limitations: ['Fixture data limitation'],
|
|
modelLimitations: ['Fixture model limitation'],
|
|
},
|
|
} as unknown as CanonicalRunResult;
|
|
|
|
describe('research evidence projection', () => {
|
|
test('retains measurements without scaling or treating fills as completed round trips', () => {
|
|
const loss = projectResearchEvidence(reference('analytics.metrics.totalReturn'), canonical);
|
|
expect(loss.value).toBe(-0.125);
|
|
expect(loss.unit).toBe('fraction');
|
|
expect(loss.resultKind).toBe('canonical');
|
|
expect(loss.financialEngine).toBe('rust');
|
|
expect(loss.limitations.map(item => item.message)).toEqual([
|
|
'Fixture data limitation',
|
|
'Fixture model limitation',
|
|
]);
|
|
const zero = projectResearchEvidence(reference('analytics.metrics.exposureTime'), canonical);
|
|
expect(zero).toMatchObject({ value: 0, availability: 'available', unavailableReason: null });
|
|
const fills = projectResearchEvidence(reference('analytics.fillCount'), canonical);
|
|
expect(fills).toMatchObject({ value: 2, unit: 'count' });
|
|
expect(
|
|
projectResearchEvidence(reference('analytics.metrics.roundTripCount'), canonical)
|
|
).toMatchObject({
|
|
value: null,
|
|
unit: 'count',
|
|
availability: 'unavailable',
|
|
unavailableReason: 'unsupported-round-trips',
|
|
});
|
|
expect(projectResearchEvidence(reference('analytics.metrics.beta'), canonical)).toMatchObject({
|
|
value: null,
|
|
unit: 'ratio',
|
|
unavailableReason: 'missing-benchmark',
|
|
});
|
|
expect(projectResearchEvidence(reference('analytics.metrics.avgWin'), canonical).unit).toBe(
|
|
'currency'
|
|
);
|
|
expect(
|
|
projectResearchEvidence(reference('analytics.metrics.sharpeRatio'), canonical)
|
|
).toMatchObject({ value: 0, unit: 'ratio' });
|
|
});
|
|
|
|
test('legacy display adapter version never claims a recorded engine version or recalculates values', () => {
|
|
const legacy = {
|
|
runId,
|
|
backtestId,
|
|
status: 'completed',
|
|
engine: 'native',
|
|
metrics: { totalReturn: 0.12 },
|
|
analytics: { exposureTime: null },
|
|
provenance: {
|
|
datasetHash: null,
|
|
limitations: [{ code: 'same-bar', message: 'Saved same-bar policy' }],
|
|
},
|
|
} as unknown as BacktestRunResult;
|
|
const result = projectResearchEvidence(
|
|
{ runId, metricsVersion: 'legacy-display/v1', metricPath: 'metrics.totalReturn' },
|
|
legacy
|
|
);
|
|
expect(result).toMatchObject({
|
|
value: 0.12,
|
|
unit: 'fraction',
|
|
resultKind: 'legacy',
|
|
financialEngine: 'native',
|
|
datasetHash: null,
|
|
});
|
|
expect(result.limitations.map(item => item.code)).toEqual([
|
|
'legacy-unverified-economics',
|
|
'same-bar',
|
|
]);
|
|
expect(
|
|
projectResearchEvidence(
|
|
{ runId, metricsVersion: 'legacy-display/v1', metricPath: 'analytics.exposureTime' },
|
|
legacy
|
|
)
|
|
).toMatchObject({
|
|
value: null,
|
|
availability: 'unavailable',
|
|
unavailableReason: 'legacy-unavailable',
|
|
});
|
|
expect(() =>
|
|
projectResearchEvidence(reference('analytics.metrics.totalReturn'), legacy)
|
|
).toThrow('Legacy');
|
|
});
|
|
|
|
test('rejects incompatible retained identities, incomplete runs and nonfinite measurements', () => {
|
|
expect(() =>
|
|
projectResearchEvidence(
|
|
{ ...reference('analytics.fillCount'), strategyVersionId: randomUUID() },
|
|
canonical
|
|
)
|
|
).toThrow('not bound');
|
|
expect(() =>
|
|
projectResearchEvidence(
|
|
{ ...reference('analytics.fillCount'), runId: randomUUID() },
|
|
canonical
|
|
)
|
|
).toThrow('completed retained run');
|
|
expect(() =>
|
|
projectResearchEvidence(
|
|
{ runId, metricsVersion: 'legacy-display/v1', metricPath: 'metrics.totalReturn' },
|
|
canonical
|
|
)
|
|
).toThrow('Metrics version');
|
|
expect(() =>
|
|
projectResearchEvidence(reference('analytics.fillCount'), {
|
|
...canonical,
|
|
analytics: { ...canonical.analytics, fillCount: NaN },
|
|
})
|
|
).toThrow('finite scalar');
|
|
});
|
|
|
|
test('accepts only versioned scalar paths and references, never client-supplied measurements', () => {
|
|
for (const change of [
|
|
{ metricPath: '__proto__.constructor' },
|
|
{ metricPath: 'request.strategy.package.artifact.content' },
|
|
{ metricsVersion: 'canonical-analytics/v2' },
|
|
{ value: 999 },
|
|
{ validationState: 'validated' },
|
|
]) {
|
|
expect(() =>
|
|
parseResearchInput(researchEvidenceReferenceSchema, {
|
|
...reference('analytics.fillCount'),
|
|
...change,
|
|
})
|
|
).toThrow();
|
|
}
|
|
});
|
|
});
|