|
| 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 { |
| 8 | + renderBacktestComparison, |
| 9 | + renderBacktestScoreReport, |
| 10 | +} from "../../packages/loopover-engine/src/calibration/backtest-report"; |
| 11 | +import type { BacktestComparison } from "../../packages/loopover-engine/src/calibration/backtest-compare"; |
| 12 | +import type { BacktestScoreReport } from "../../packages/loopover-engine/src/calibration/backtest-score"; |
| 13 | + |
| 14 | +function report(overrides: Partial<BacktestScoreReport> = {}): BacktestScoreReport { |
| 15 | + return { |
| 16 | + ruleId: "missing_linked_issue", |
| 17 | + caseCount: 4, |
| 18 | + truePositive: 1, |
| 19 | + falsePositive: 1, |
| 20 | + trueNegative: 1, |
| 21 | + falseNegative: 1, |
| 22 | + precision: 0.5, |
| 23 | + recall: 0.5, |
| 24 | + ...overrides, |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +function comparison(overrides: Partial<BacktestComparison> = {}): BacktestComparison { |
| 29 | + return { |
| 30 | + ruleId: "missing_linked_issue", |
| 31 | + baseline: report(), |
| 32 | + candidate: report({ precision: 0.75 }), |
| 33 | + regressedAxes: [], |
| 34 | + improvedAxes: ["precision"], |
| 35 | + verdict: "improved", |
| 36 | + ...overrides, |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +describe("renderBacktestScoreReport (#8088)", () => { |
| 41 | + it("renders the exact snapshot for a non-null report", () => { |
| 42 | + expect(renderBacktestScoreReport(report())).toBe( |
| 43 | + [ |
| 44 | + "### Backtest score — `missing_linked_issue`", |
| 45 | + "", |
| 46 | + "| Metric | Value |", |
| 47 | + "| --- | --- |", |
| 48 | + "| Cases scored | 4 |", |
| 49 | + "| True positives | 1 |", |
| 50 | + "| False positives | 1 |", |
| 51 | + "| True negatives | 1 |", |
| 52 | + "| False negatives | 1 |", |
| 53 | + "| Precision | 0.5 |", |
| 54 | + "| Recall | 0.5 |", |
| 55 | + ].join("\n"), |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it("renders null precision/recall as N/A -- never 0, null, or an empty cell", () => { |
| 60 | + const rendered = renderBacktestScoreReport(report({ precision: null, recall: null })); |
| 61 | + expect(rendered).toContain("| Precision | N/A |"); |
| 62 | + expect(rendered).toContain("| Recall | N/A |"); |
| 63 | + expect(rendered).not.toContain("| Precision | 0 |"); |
| 64 | + expect(rendered).not.toContain("null"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("is deterministic for identical input", () => { |
| 68 | + expect(renderBacktestScoreReport(report())).toBe(renderBacktestScoreReport(report())); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe("renderBacktestComparison (#8088)", () => { |
| 73 | + it("puts each axis under its own section and pins the literal REGRESSED do-not-merge wording", () => { |
| 74 | + const rendered = renderBacktestComparison( |
| 75 | + comparison({ regressedAxes: ["recall"], improvedAxes: ["precision"], verdict: "regressed" }), |
| 76 | + ); |
| 77 | + expect(rendered).toContain("**Regressed**\n\n- recall"); |
| 78 | + expect(rendered).toContain("**Improved**\n\n- precision"); |
| 79 | + expect(rendered).toContain("Verdict: REGRESSED — do not merge"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("claims no regressed axis for an improved comparison", () => { |
| 83 | + const rendered = renderBacktestComparison(comparison()); |
| 84 | + expect(rendered).toContain("**Regressed**\n\n- (none)"); |
| 85 | + expect(rendered).toContain("**Improved**\n\n- precision"); |
| 86 | + expect(rendered).toContain("Verdict: improved"); |
| 87 | + expect(rendered).not.toContain("REGRESSED"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("lists no axis on either side for an unchanged comparison", () => { |
| 91 | + const rendered = renderBacktestComparison( |
| 92 | + comparison({ improvedAxes: [], verdict: "unchanged", candidate: report() }), |
| 93 | + ); |
| 94 | + expect(rendered).toContain("**Regressed**\n\n- (none)"); |
| 95 | + expect(rendered).toContain("**Improved**\n\n- (none)"); |
| 96 | + expect(rendered).toContain("Verdict: unchanged"); |
| 97 | + }); |
| 98 | + |
| 99 | + it("is deterministic for identical input", () => { |
| 100 | + const regressed = comparison({ regressedAxes: ["recall"], verdict: "regressed" }); |
| 101 | + expect(renderBacktestComparison(regressed)).toBe(renderBacktestComparison(regressed)); |
| 102 | + }); |
| 103 | +}); |
0 commit comments