|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { test } from "node:test"; |
| 3 | + |
| 4 | +import type { BacktestCase } from "../dist/index.js"; |
| 5 | +import { splitBacktestCorpus } from "../dist/calibration/backtest-split.js"; |
| 6 | + |
| 7 | +function corpusCase(targetKey: string, overrides: Partial<BacktestCase> = {}): BacktestCase { |
| 8 | + return { |
| 9 | + ruleId: "missing_linked_issue", |
| 10 | + targetKey, |
| 11 | + outcome: "block", |
| 12 | + label: "confirmed", |
| 13 | + firedAt: "2026-07-22T00:00:00.000Z", |
| 14 | + decidedAt: "2026-07-22T01:00:00.000Z", |
| 15 | + ...overrides, |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +/** A dozen distinct targets -- enough that a 0.5 split reliably lands cases in BOTH buckets and that two |
| 20 | + * different seeds reliably disagree on at least one case, without depending on any specific hash value. */ |
| 21 | +const corpus = Array.from({ length: 12 }, (_, index) => corpusCase(`acme/widgets#${index + 1}`)); |
| 22 | + |
| 23 | +test("splitBacktestCorpus: heldOutFraction 0 keeps every case visible", () => { |
| 24 | + const { visible, heldOut } = splitBacktestCorpus(corpus, 0, "seed-a"); |
| 25 | + assert.deepEqual(visible, corpus); |
| 26 | + assert.deepEqual(heldOut, []); |
| 27 | +}); |
| 28 | + |
| 29 | +test("splitBacktestCorpus: heldOutFraction 1 holds every case out", () => { |
| 30 | + const { visible, heldOut } = splitBacktestCorpus(corpus, 1, "seed-a"); |
| 31 | + assert.deepEqual(heldOut, corpus); |
| 32 | + assert.deepEqual(visible, []); |
| 33 | +}); |
| 34 | + |
| 35 | +test("splitBacktestCorpus: identical inputs produce byte-identical output, including per-bucket order", () => { |
| 36 | + const first = splitBacktestCorpus(corpus, 0.5, "seed-a"); |
| 37 | + const second = splitBacktestCorpus(corpus, 0.5, "seed-a"); |
| 38 | + assert.deepEqual(second, first); |
| 39 | +}); |
| 40 | + |
| 41 | +test("splitBacktestCorpus: preserves each case's original input order within its bucket, with both buckets populated", () => { |
| 42 | + const { visible, heldOut } = splitBacktestCorpus(corpus, 0.5, "seed-a"); |
| 43 | + assert.ok(visible.length > 0 && heldOut.length > 0, "0.5 over 12 distinct targets must populate both buckets"); |
| 44 | + const inputIndex = (backtestCase: BacktestCase) => corpus.indexOf(backtestCase); |
| 45 | + for (const bucket of [visible, heldOut]) { |
| 46 | + const order = bucket.map(inputIndex); |
| 47 | + assert.deepEqual(order, [...order].sort((a, b) => a - b)); |
| 48 | + } |
| 49 | +}); |
| 50 | + |
| 51 | +test("splitBacktestCorpus: a different seed produces a different split for at least one case", () => { |
| 52 | + const first = splitBacktestCorpus(corpus, 0.5, "seed-a"); |
| 53 | + const second = splitBacktestCorpus(corpus, 0.5, "seed-b"); |
| 54 | + assert.notDeepEqual( |
| 55 | + { visible: first.visible.map((c) => c.targetKey), heldOut: first.heldOut.map((c) => c.targetKey) }, |
| 56 | + { visible: second.visible.map((c) => c.targetKey), heldOut: second.heldOut.map((c) => c.targetKey) }, |
| 57 | + ); |
| 58 | +}); |
| 59 | + |
| 60 | +test("splitBacktestCorpus: throws on an out-of-range heldOutFraction in both directions, naming the value", () => { |
| 61 | + assert.throws(() => splitBacktestCorpus(corpus, -0.1, "seed-a"), /invalid_held_out_fraction: -0\.1/); |
| 62 | + assert.throws(() => splitBacktestCorpus(corpus, 1.5, "seed-a"), /invalid_held_out_fraction: 1\.5/); |
| 63 | +}); |
0 commit comments