|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { buildSatisfactionFloorLooseningRecs, LOOSENING_REC_PROJECT } from "../../src/review/loosening-recs"; |
| 3 | +import type { SatisfactionFloorLooseningProposal } from "../../src/services/satisfaction-floor-loosening"; |
| 4 | + |
| 5 | +function proposal(overrides: Partial<SatisfactionFloorLooseningProposal> = {}): SatisfactionFloorLooseningProposal { |
| 6 | + return { |
| 7 | + ruleId: "linked_issue_scope_mismatch", |
| 8 | + currentFloor: 0.5, |
| 9 | + proposedFloor: 0.45, |
| 10 | + visibleCases: 24, |
| 11 | + heldOutCases: 7, |
| 12 | + visible: { |
| 13 | + ruleId: "linked_issue_scope_mismatch", |
| 14 | + baseline: { ruleId: "linked_issue_scope_mismatch", caseCount: 24, truePositive: 1, falsePositive: 4, trueNegative: 19, falseNegative: 0, precision: 0.2, recall: 1 }, |
| 15 | + candidate: { ruleId: "linked_issue_scope_mismatch", caseCount: 24, truePositive: 1, falsePositive: 0, trueNegative: 23, falseNegative: 0, precision: 1, recall: 1 }, |
| 16 | + regressedAxes: [], |
| 17 | + improvedAxes: ["precision"], |
| 18 | + verdict: "improved", |
| 19 | + }, |
| 20 | + heldOut: { |
| 21 | + ruleId: "linked_issue_scope_mismatch", |
| 22 | + baseline: { ruleId: "linked_issue_scope_mismatch", caseCount: 7, truePositive: 0, falsePositive: 0, trueNegative: 7, falseNegative: 0, precision: null, recall: null }, |
| 23 | + candidate: { ruleId: "linked_issue_scope_mismatch", caseCount: 7, truePositive: 0, falsePositive: 0, trueNegative: 7, falseNegative: 0, precision: null, recall: null }, |
| 24 | + regressedAxes: [], |
| 25 | + improvedAxes: [], |
| 26 | + verdict: "unchanged", |
| 27 | + }, |
| 28 | + ...overrides, |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +describe("buildSatisfactionFloorLooseningRecs (#8160)", () => { |
| 33 | + it("returns [] with no proposal and no applied history", () => { |
| 34 | + expect(buildSatisfactionFloorLooseningRecs({ flagEnabled: false, proposal: null, lastAppliedAt: null })).toEqual([]); |
| 35 | + }); |
| 36 | + |
| 37 | + it("surfaces a backtest-cleared proposal as a good-severity rec with both split verdicts, sample sizes, and precision movement", () => { |
| 38 | + const recs = buildSatisfactionFloorLooseningRecs({ flagEnabled: false, proposal: proposal(), lastAppliedAt: null }); |
| 39 | + expect(recs).toHaveLength(1); |
| 40 | + const [rec] = recs; |
| 41 | + expect(rec!.project).toBe(LOOSENING_REC_PROJECT); |
| 42 | + expect(rec!.severity).toBe("good"); |
| 43 | + expect(rec!.message).toContain("0.5 → 0.45"); |
| 44 | + expect(rec!.message).toContain("Visible split improved (24 case(s), precision 20% → 100%)"); |
| 45 | + expect(rec!.message).toContain("held-out split unchanged (7 case(s))"); |
| 46 | + // Null precision renders as the em-dash convention, never a fake number. |
| 47 | + const nullPrecision = buildSatisfactionFloorLooseningRecs({ |
| 48 | + flagEnabled: false, |
| 49 | + proposal: proposal({ visible: { ...proposal().visible, baseline: { ...proposal().visible.baseline, precision: null } } }), |
| 50 | + lastAppliedAt: null, |
| 51 | + }); |
| 52 | + expect(nullPrecision[0]!.message).toContain("precision — →"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("the action line matches the flag state, and the rec NEVER carries an overridePayload (the tightening-only channel)", () => { |
| 56 | + const off = buildSatisfactionFloorLooseningRecs({ flagEnabled: false, proposal: proposal(), lastAppliedAt: null }); |
| 57 | + expect(off[0]!.message).toContain("SATISFACTION_FLOOR_AUTOTUNE_ENABLED"); |
| 58 | + const on = buildSatisfactionFloorLooseningRecs({ flagEnabled: true, proposal: proposal(), lastAppliedAt: null }); |
| 59 | + expect(on[0]!.message).toContain("hourly tick will apply"); |
| 60 | + for (const rec of [...off, ...on]) expect(rec.overridePayload).toBeUndefined(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("reports a recently applied loosening as an info rec pointing at the operator status surface, alongside a proposal when both exist", () => { |
| 64 | + const both = buildSatisfactionFloorLooseningRecs({ flagEnabled: true, proposal: proposal(), lastAppliedAt: "2026-07-23T05:00:00.000Z" }); |
| 65 | + expect(both.map((rec) => rec.severity)).toEqual(["good", "info"]); |
| 66 | + expect(both[1]!.message).toContain("2026-07-23T05:00:00.000Z"); |
| 67 | + expect(both[1]!.message).toContain("/v1/internal/calibration/satisfaction-floor"); |
| 68 | + |
| 69 | + const appliedOnly = buildSatisfactionFloorLooseningRecs({ flagEnabled: true, proposal: null, lastAppliedAt: "2026-07-23T05:00:00.000Z" }); |
| 70 | + expect(appliedOnly).toHaveLength(1); |
| 71 | + expect(appliedOnly[0]!.severity).toBe("info"); |
| 72 | + }); |
| 73 | +}); |
0 commit comments