import assert from "node:assert/strict"; import { describe, it } from "node:test"; import { abCaveats, abHeadline, abMethod, abSummaryText, halfWidth } from "./ab-report"; import type { AbSummary, PairedResult } from "./db/schema"; const r = (p: Partial): PairedResult => ({ metric: "decode_tps", n_pairs: 12, mean_a: 115, mean_b: 120, mean_diff: 5, pct_diff: 4.37, ci_low_pct: 3.24, ci_high_pct: 5.5, conf: 0.95, significant: true, verdict: "B is 4.37% faster — SIGNIFICANT", higher_is_better: true, ...p, }); const ab: AbSummary = { pairs: 12, decode_tps: r({}), gap_median: { ...r({ metric: "update_gap_median_ms", pct_diff: -4.6, ci_low_pct: -5.8, ci_high_pct: -3.4, higher_is_better: false }), batched: false }, paired_cv: 0.0178, pairs_needed_for_mde: 7, target_mde_pct: 2, conf: 0.95, maxPairs: 24, raw: { a_tps: [], b_tps: [] }, betterbenchVersion: "0.6.1", corpusVersion: "1.0", timestamp: null, config: { greedy: true, temperature: 0.7, warmup: 3, unique_nonce: true, ab_min_pairs: 12 }, }; describe("A/B report text", () => { it("states the result as BetterBench does", () => { assert.equal(abHeadline(ab.decode_tps), "B is 4.37% faster"); assert.equal(abHeadline(ab.gap_median), "B is 4.60% faster"); assert.equal(abHeadline(r({ significant: false, pct_diff: 0.3 })), "No measurable difference"); assert.equal(abHeadline(r({ pct_diff: -2 })), "B is 2.00% slower"); assert.ok(Math.abs(halfWidth(ab.decode_tps) - 1.13) < 1e-9); assert.equal(abSummaryText(ab, "4 threads", "8 threads"), "8 threads (B) against 4 threads (A), 12 interleaved pairs: decode +4.37% (95% CI +3.24% to +5.50%), significant."); }); it("flags imprecise and inconclusive results", () => { assert.deepEqual(abCaveats(ab), []); const wide = { ...ab, pairs: 24, decode_tps: r({ ci_low_pct: -1, ci_high_pct: 5, significant: false }), pairs_needed_for_mde: 90 }; const c = abCaveats(wide); assert.match(c[0], /±3\.00%, wider than the 2% target: the run stopped at the 24-pair limit first\. About 90 pairs/); assert.match(c[1], /can't be told apart/); assert.match(abMethod(ab).join(" "), /narrower than ±2% \(after at least 12 pairs\), or after 24 pairs/); }); });