|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +// Import the engine SOURCE directly (not the built dist) -- coverage.include lists |
| 4 | +// packages/loopover-engine/src/**, so only a source-path import exercises the .ts these branches live in |
| 5 | +// (the dist-importing twin in packages/loopover-engine/test/ covers the built barrel for the workspace |
| 6 | +// suite). Same pattern as backtest-corpus-engine.test.ts / miner-deny-hook-synthesis.test.ts. |
| 7 | +import { compareBacktestScores } from "../../packages/loopover-engine/src/calibration/backtest-compare"; |
| 8 | +import type { BacktestScoreReport } from "../../packages/loopover-engine/src/calibration/backtest-score"; |
| 9 | + |
| 10 | +function report(overrides: Partial<BacktestScoreReport> = {}): BacktestScoreReport { |
| 11 | + return { |
| 12 | + ruleId: "missing_linked_issue", |
| 13 | + caseCount: 10, |
| 14 | + truePositive: 4, |
| 15 | + falsePositive: 2, |
| 16 | + trueNegative: 3, |
| 17 | + falseNegative: 1, |
| 18 | + precision: 0.5, |
| 19 | + recall: 0.5, |
| 20 | + ...overrides, |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +describe("compareBacktestScores (#8086)", () => { |
| 25 | + it("marks both-axes improvement as improved with empty regressedAxes", () => { |
| 26 | + const comparison = compareBacktestScores(report(), report({ precision: 0.7, recall: 0.6 })); |
| 27 | + expect(comparison.improvedAxes).toEqual(["precision", "recall"]); |
| 28 | + expect(comparison.regressedAxes).toEqual([]); |
| 29 | + expect(comparison.verdict).toBe("improved"); |
| 30 | + expect(comparison.baseline.precision).toBe(0.5); |
| 31 | + expect(comparison.candidate.precision).toBe(0.7); |
| 32 | + }); |
| 33 | + |
| 34 | + it("PARETO FLOOR: one axis improving while the other regresses is a regressed verdict", () => { |
| 35 | + const comparison = compareBacktestScores(report(), report({ precision: 0.9, recall: 0.3 })); |
| 36 | + expect(comparison.improvedAxes).toEqual(["precision"]); |
| 37 | + expect(comparison.regressedAxes).toEqual(["recall"]); |
| 38 | + expect(comparison.verdict).toBe("regressed"); |
| 39 | + }); |
| 40 | + |
| 41 | + it("marks a regression on both axes as regressed with empty improvedAxes", () => { |
| 42 | + const comparison = compareBacktestScores(report(), report({ precision: 0.1, recall: 0.2 })); |
| 43 | + expect(comparison.regressedAxes).toEqual(["precision", "recall"]); |
| 44 | + expect(comparison.improvedAxes).toEqual([]); |
| 45 | + expect(comparison.verdict).toBe("regressed"); |
| 46 | + }); |
| 47 | + |
| 48 | + it("excludes an axis from both lists when either side is null -- null is never 0 and never 'no change'", () => { |
| 49 | + const nullBaseline = compareBacktestScores(report({ precision: null }), report({ precision: 0.9, recall: 0.6 })); |
| 50 | + expect(nullBaseline.improvedAxes).toEqual(["recall"]); |
| 51 | + expect(nullBaseline.regressedAxes).toEqual([]); |
| 52 | + expect(nullBaseline.verdict).toBe("improved"); |
| 53 | + |
| 54 | + const nullCandidate = compareBacktestScores(report(), report({ recall: null })); |
| 55 | + expect(nullCandidate.improvedAxes).toEqual([]); |
| 56 | + expect(nullCandidate.regressedAxes).toEqual([]); |
| 57 | + expect(nullCandidate.verdict).toBe("unchanged"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("yields unchanged when every comparable axis is equal", () => { |
| 61 | + const comparison = compareBacktestScores(report(), report()); |
| 62 | + expect(comparison.improvedAxes).toEqual([]); |
| 63 | + expect(comparison.regressedAxes).toEqual([]); |
| 64 | + expect(comparison.verdict).toBe("unchanged"); |
| 65 | + expect(comparison.ruleId).toBe("missing_linked_issue"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("throws on mismatched ruleIds, naming both rules in the message", () => { |
| 69 | + expect(() => compareBacktestScores(report(), report({ ruleId: "other_rule" }))).toThrow( |
| 70 | + "cannot compare backtest scores for different rules: missing_linked_issue vs other_rule", |
| 71 | + ); |
| 72 | + }); |
| 73 | +}); |
0 commit comments