101 lines
3.7 KiB
TypeScript
101 lines
3.7 KiB
TypeScript
import { describe, expect, test } from 'bun:test';
|
|
import type { ValidationResultEvidence } from '@stock-bot/stock-config/chronological-validation';
|
|
import type { ExperimentCandidate } from '@stock-bot/stock-config/experiments';
|
|
import {
|
|
parseValidationFinalization,
|
|
parseValidationSelection,
|
|
selectValidationWinner,
|
|
} from '../../apps/stock/web-api/src/services/validation-evidence';
|
|
|
|
function fixture(scores: (number | null)[]) {
|
|
const candidates: ExperimentCandidate[] = scores.map((_, index) => ({
|
|
index,
|
|
trialKey: String(index).padStart(64, '0'),
|
|
parameters: { fastPeriod: index + 1, slowPeriod: 5 },
|
|
}));
|
|
const evidence: ValidationResultEvidence[] = scores.map((totalReturn, index) => ({
|
|
phase: 'train',
|
|
experimentId: 'training',
|
|
runId: String(index),
|
|
sourceCandidateIndex: index,
|
|
trialKey: candidates[index]!.trialKey,
|
|
parameters: candidates[index]!.parameters,
|
|
datasetHash: 'a'.repeat(64),
|
|
requestHash: 'b'.repeat(64),
|
|
economicHash: 'c'.repeat(64),
|
|
metrics: {
|
|
totalReturn,
|
|
cagr: null,
|
|
maxDrawdown: null,
|
|
exposureTime: null,
|
|
volatility: null,
|
|
downsideDeviation: null,
|
|
sharpeRatio: null,
|
|
sortinoRatio: null,
|
|
var95: null,
|
|
cvar95: null,
|
|
var99: null,
|
|
alpha: null,
|
|
beta: null,
|
|
informationRatio: null,
|
|
roundTripCount: null,
|
|
winRate: null,
|
|
profitFactor: null,
|
|
avgWin: null,
|
|
avgLoss: null,
|
|
},
|
|
unavailable: {},
|
|
completePeriodCount: 0,
|
|
periodicSampleCount: 0,
|
|
fillCount: 0,
|
|
limitations: [],
|
|
}));
|
|
return { candidates, evidence };
|
|
}
|
|
describe('sealed chronological selection contract', () => {
|
|
test('maximizes finite zero/negative training scores, ties choose original index without reordering evidence', () => {
|
|
for (const [scores, expected] of [
|
|
[[-0.2, -0.1, -0.1], 1],
|
|
[[0, -0.1, 0], 0],
|
|
[[0.2, 0.4, 0.3], 1],
|
|
] as const) {
|
|
const { candidates, evidence } = fixture([...scores]),
|
|
before = structuredClone(evidence);
|
|
expect(selectValidationWinner(candidates, evidence)).toEqual(candidates[expected]!);
|
|
expect(evidence).toEqual(before);
|
|
}
|
|
});
|
|
test('missing/null/nonfinite/reordered or holdout evidence can never silently disappear from ranking', () => {
|
|
for (const scores of [[1, null], [1, Infinity], [1, NaN], []]) {
|
|
const f = fixture(scores);
|
|
expect(() => selectValidationWinner(f.candidates, f.evidence)).toThrow();
|
|
}
|
|
const f = fixture([0, 1]);
|
|
expect(() => selectValidationWinner(f.candidates, f.evidence.slice(1))).toThrow();
|
|
expect(() => selectValidationWinner(f.candidates, [...f.evidence].reverse())).toThrow();
|
|
f.evidence[0]!.phase = 'test';
|
|
expect(() => selectValidationWinner(f.candidates, f.evidence)).toThrow();
|
|
f.evidence[0]!.phase = 'train';
|
|
f.evidence[0]!.parameters = { fastPeriod: 99 };
|
|
expect(() => selectValidationWinner(f.candidates, f.evidence)).toThrow();
|
|
});
|
|
test('selection/finalization bodies cannot supply winners, scores, changed recipes or a different action', () => {
|
|
const previewHash = 'a'.repeat(64),
|
|
selectionHash = 'b'.repeat(64);
|
|
expect(parseValidationSelection({ previewHash })).toEqual({ previewHash });
|
|
expect(parseValidationFinalization({ previewHash, selectionHash })).toEqual({
|
|
previewHash,
|
|
selectionHash,
|
|
});
|
|
for (const extra of [
|
|
{ winner: 1 },
|
|
{ metrics: { totalReturn: 2 } },
|
|
{ recipe: {} },
|
|
{ operation: 'finalize-report' },
|
|
]) {
|
|
expect(() => parseValidationSelection({ previewHash, ...extra })).toThrow();
|
|
expect(() => parseValidationFinalization({ previewHash, selectionHash, ...extra })).toThrow();
|
|
}
|
|
expect(() => parseValidationFinalization({ previewHash })).toThrow();
|
|
});
|
|
});
|