// Tests for the MEASURE ledger kind (measure-totals.mjs, ledger-shared.mjs, // and ledger-totals.mjs's dispatch to them), ported from the diet-series // branch (scripts/report-to-video/ledger-totals.test.mjs there) with the // off-scope fix (offCloses). The headcount tests stay in ledger-totals.test.mjs; // the rail, panel and chart drawing tests of that branch are not ported (their // drawings are not on main). Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { ADJUDICATION_FIELDS, EVENTS, FIELD_KINDS, LEDGER_BUILTIN_KEYS, REGIME_KINDS, MEASURE_PREDICATES, claimVocab, fieldApplies, isPledgeKind, ledgerFields, ledgerKind, regimeGaps, adjudicationGaps, ledgerTotals, freshestReading, measureSchema, measureDisplay, measureScoreboard, measureScoreboardSplit, measureTotals, offCloses, validateFields, } from "./ledger-totals.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const readFixture = (name) => JSON.parse(readFileSync(path.join(HERE, "fixtures", name), "utf8")); const SAMPLE = readFixture("measure-sample.manifest.json"); const SCHEMA = SAMPLE.ledgerSchema; /** An adjudicated measure claim, with the boring answer for the six. */ const mclaim = (o) => ({ scope: "weight", scopeBasis: "he says it about himself", scopeConfidence: "clear", population: "current", valueKind: "uttered", flags: [], ...o, }); const totalsOf = (ledger, schema = SCHEMA) => ledgerTotals(ledger, { schema }); const firedOn = (t, id) => (t.steps.find((s) => s.id === id)?.flags ?? []).map((f) => f.rule); /** A schema with a diet track and a drinking track, and both sets of regimes. */ const TWO_TRACK = { kind: "measure", unit: "lb", yLabel: "POUNDS", from: "2025-01-01", to: "2026-01-01", bandGapDays: 60, tracks: [ { key: "weight", role: "value", label: "His weight", color: "#22AB83" }, { key: "regime", role: "regime", label: "What he eats", shortLabel: "DIET", color: "#D2732F", regimes: ["keto", "carnivore", "off"] }, { key: "drinking", role: "regime", label: "What he drinks", shortLabel: "DRINKING", color: "#C55F9C", regimes: ["dry", "cutting", "drinking"] }, ], forms: ["current", "past", "delta", "goal", "overweight_by", "status"], regimes: [ { key: "keto", label: "Keto", color: "#D2732F" }, { key: "carnivore", label: "Carnivore", color: "#6A82DC" }, { key: "off", label: "No diet", color: "#98A29B" }, { key: "dry", label: "Dry", color: "#22AB83" }, { key: "cutting", label: "Cutting back", color: "#98A29B" }, { key: "drinking", label: "Drinking", color: "#C55F9C" }, ], }; /** An adjudicated regime claim, on whichever track it names. */ const reg = (o) => ({ scopeBasis: "he names it", scopeConfidence: "clear", population: "status", valueKind: "uttered", flags: [], ...o, }); const TWO_FIX = readFixture("measure-two-track.manifest.json"); const twoTotals = () => totalsOf(TWO_FIX.ledger, TWO_FIX.ledgerSchema); const flagOn = (t, id, rule) => (t.steps.find((s) => s.id === id)?.flags ?? []).find((f) => f.rule === rule) ?? null; /** A drinking-track row, off the fixture's own template. */ const drinkRow = (id, date, value, event) => ({ ...JSON.parse(JSON.stringify(TWO_FIX.ledger.find((r) => r.id === "p1"))), id, date, value, event, entryId: null, display: value, label: id, }); const seq = (...rows) => totalsOf(rows, TWO_FIX.ledgerSchema); /** A schema with one field of each kind, and nothing else new. */ const fieldSchema = (fields) => ({ kind: "measure", unit: "lb", tracks: [ { key: "weight", role: "value", label: "WEIGHT" }, { key: "drinking", role: "regime", label: "DRINKING", regimes: ["dry", "drinking"] }, ], regimes: [ { key: "dry", label: "Dry", kind: "pledge" }, { key: "drinking", label: "Drinking", kind: "lapse" }, ], fields, }); /** The two-track schema with `cutting` bounded rather than a plain stop. */ const BOUNDED_SCHEMA = { ...TWO_FIX.ledgerSchema, regimes: TWO_FIX.ledgerSchema.regimes.map((r) => r.key === "cutting" ? { ...r, kind: "bounded" } : r, ), fields: [ { key: "breaks", label: "Breaks pledge", kind: "rowRef", appliesTo: { scope: "drinking", value: "drinking", event: "start" } }, { key: "breaksWhy", label: "Why it breaks", kind: "text", maxLength: 200 }, ], }; const bounded = (...rows) => totalsOf(rows, BOUNDED_SCHEMA); test("the kind, and the vocabulary, come off the schema", () => { assert.equal(ledgerKind(SAMPLE), "measure"); const v = claimVocab(SAMPLE); assert.deepEqual(v.scopes, ["weight", "regime"]); // The schema's forms, then the promise forms every measure ledger carries. assert.deepEqual(v.populations, ["current", "past", "delta", "goal", "overweight_by", "status", "promise", "promise_check", "promise_claim", "promise_outcome"]); assert.deepEqual(v.events, EVENTS); assert.deepEqual(v.regimes, ["keto", "carnivore", "off"]); assert.equal(v.unit, "lb"); }); test("a measure claim is gated on the six, and a regime claim on two more", () => { // A regime claim with no event cannot open or close a segment, and one whose // value is not a declared regime cannot be drawn at all. Both would fail // SILENTLY -- the band would just be shorter than the evidence. assert.deepEqual(adjudicationGaps(mclaim({ id: "a", value: 300 }), SAMPLE), []); assert.deepEqual( adjudicationGaps(mclaim({ id: "b", scope: "regime", population: "status", value: "keto" }), SAMPLE), ["event"], ); assert.deepEqual( adjudicationGaps( mclaim({ id: "c", scope: "regime", population: "status", value: "paleo", event: "start" }), SAMPLE, ), ["value"], ); // The vocabulary is the SCHEMA's, not the headcount one. assert.deepEqual(adjudicationGaps(mclaim({ id: "d", scope: "media" }), SAMPLE), ["scope"]); assert.deepEqual(adjudicationGaps(mclaim({ id: "e", population: "employees" }), SAMPLE), ["population"]); }); test("an unadjudicated measure ledger throws rather than plotting half a series", () => { assert.throws( () => totalsOf([{ id: "x", date: "2025-01-01", scope: "weight", value: 300 }]), (err) => err.name === "UnadjudicatedLedger" && err.ids.includes("x"), ); }); test("only `current` and `past` reach the line; the other forms are marks", () => { const t = totalsOf(SAMPLE.ledger); assert.deepEqual(t.points.map((p) => p.id), ["w1", "w4", "w2", "w3", "w5"]); assert.deepEqual(t.marks.delta.map((m) => m.id), ["d1"]); assert.deepEqual(t.marks.goal.map((m) => m.id), ["g1"]); assert.deepEqual(t.marks.overweightBy.map((m) => m.id), ["o1"]); }); test("a retrospective plots at the date it is ABOUT, not the date he said it", () => { const t = totalsOf(SAMPLE.ledger); const w4 = t.points.find((p) => p.id === "w4"); // Uttered 2026-02-01, about 2025-02-15. Plotting it at the utterance would // draw a sixty-pound gain in 2026 that never happened. assert.equal(w4.at, "2025-02-15"); assert.equal(w4.form, "past"); // …and the ordering is by the date it is about, so it lands between w1 and w2. assert.deepEqual(t.points.map((p) => p.at), [ "2025-02-10", "2025-02-15", "2025-02-18", "2025-11-05", "2026-08-01", ]); }); test("the band opens on start, closes on a different start, and hands over on stop", () => { const t = totalsOf(SAMPLE.ledger); const named = t.band.filter((b) => !b.unknown).map((b) => [b.regime, b.from, b.to]); assert.deepEqual(named, [ ["keto", "2025-03-01", "2025-03-20"], // carnivore is asserted for 60 days past the last claim that supports it, // and no further. ["carnivore", "2025-03-20", "2025-05-19"], // a `stop` hands over to the schema's own `off` regime rather than to // silence -- "he quit" IS a claim about what he is on. ["off", "2026-01-15", "2026-03-16"], ["keto", "2026-06-01", "2026-07-31"], ]); }); test("silence longer than bandGapDays is an `unknown` segment, not a longer regime", () => { const t = totalsOf(SAMPLE.ledger); const drawn = t.band.filter((b) => b.unknown).map((b) => [b.from, b.to]); assert.deepEqual(drawn, [ ["2025-05-19", "2026-01-15"], ["2026-03-16", "2026-06-01"], ]); // The DECISION spans the whole silence, not just the undrawn remainder: the // operator is being pointed at a stretch to go and find claims in, and that // stretch runs from the last thing he said to the next one. assert.deepEqual(regimeGaps(t).map((g) => [g.from, g.to, g.days]), [ ["2025-03-20", "2026-01-15", 301], ["2026-01-15", "2026-06-01", 137], // The last one has no hatch on the chart — the ledger's right edge is only // a day past where the band expired — but the silence is still 61 days and // the inbox is where that gets said. ["2026-06-01", "2026-08-01", 61], ]); }); test("a gap is reported from the LAPSE, not from what survived the drawing rule", () => { // Regression. The decision used to be read back off the band, and the band // suppresses a hatched sliver under a week — so a 63-day silence produced no // segment, therefore no decision, and the inbox went quiet about exactly the // case the sixty-day rule exists to catch. const at = (d) => [ mclaim({ id: "r1", date: "2025-01-01", scope: "regime", population: "status", value: "keto", event: "start" }), mclaim({ id: "r2", date: d, scope: "regime", population: "status", value: "keto", event: "on" }), ]; // 61 days: over the threshold by one, and the undrawn remainder is a single // day, so nothing is hatched — and it is still a decision. const one = totalsOf(at("2025-03-03")); assert.equal(one.band.filter((b) => b.unknown).length, 0); assert.deepEqual(regimeGaps(one).map((g) => g.days), [61]); // 68 days: the remainder (eight) clears the drawing rule too, so it is both. const six = totalsOf(at("2025-03-10")); assert.equal(six.band.filter((b) => b.unknown).length, 1); assert.deepEqual(regimeGaps(six).map((g) => g.days), [68]); // 60 days exactly: not a lapse at all. One segment, no decision. const none = totalsOf(at("2025-03-02")); assert.deepEqual(regimeGaps(none), []); assert.equal(none.band.length, 1); }); test("a shorter silence is simply carried — the segment still speaks for it", () => { const t = totalsOf([ mclaim({ id: "r1", date: "2025-01-01", scope: "regime", population: "status", value: "keto", event: "start" }), mclaim({ id: "r2", date: "2025-02-10", scope: "regime", population: "status", value: "keto", event: "on" }), ]); // Forty days apart, under the sixty-day rule, so one segment and no hatch. assert.equal(t.band.length, 1); assert.equal(t.band[0].regime, "keto"); assert.deepEqual(t.band[0].ids, ["r1", "r2"]); }); test("contradicts_recent: two readings close in time, far apart in value", () => { const t = totalsOf(SAMPLE.ledger); // 380 on the 10th, 340 on the 18th: eight days, forty pounds. assert.ok(firedOn(t, "w2").includes("contradicts_recent")); const f = t.steps.find((s) => s.id === "w2").flags.find((x) => x.rule === "contradicts_recent"); assert.match(f.text, /340 lb; he said 380 lb 8 days earlier — 40 lb apart/); assert.equal(f.against, "w1"); // Fluctuation is the SUBJECT, not a defect: fifteen pounds or less does not fire. const mild = totalsOf([ mclaim({ id: "a", date: "2025-01-01", value: 340 }), mclaim({ id: "b", date: "2025-01-08", value: 352 }), ]); assert.deepEqual(firedOn(mild, "b"), []); // Nor does a big change with time to have happened in. const slow = totalsOf([ mclaim({ id: "a", date: "2025-01-01", value: 400 }), mclaim({ id: "b", date: "2025-06-01", value: 330 }), ]); assert.deepEqual(firedOn(slow, "b"), []); }); test("retro_mismatch: what he says now he WAS, against what he said at the time", () => { const t = totalsOf(SAMPLE.ledger); const f = t.steps.find((s) => s.id === "w4").flags.find((x) => x.rule === "retro_mismatch"); assert.ok(f); assert.match(f.text, /he now says 400 lb for 2025-02-15; at the time he said 340 lb/); // …and it is the ONLY thing that fires on it. A retrospective three days from // a contemporaneous reading is one disagreement, and reporting it as // `contradicts_recent` as well would be reporting it twice under two names. assert.deepEqual(firedOn(t, "w4"), ["retro_mismatch"]); }); test("regime_flip and quit_unannounced are different findings about the same claim", () => { const t = totalsOf(SAMPLE.ledger); const rules = firedOn(t, "r2"); assert.ok(rules.includes("regime_flip")); assert.ok(rules.includes("quit_unannounced")); const flip = t.steps.find((s) => s.id === "r2").flags.find((x) => x.rule === "regime_flip"); assert.match(flip.text, /Keto 19 days earlier, now Carnivore/); // A restart after an announced stop is neither: he said he stopped, and the // gap is long enough that it is not a flip. assert.deepEqual(firedOn(t, "r4"), []); }); test("same_day_conflict: two claims on one track, one day, two answers", () => { const t = totalsOf([ mclaim({ id: "a", date: "2025-01-01", value: 340 }), mclaim({ id: "b", date: "2025-01-01", value: 355 }), ]); const f = t.steps.find((s) => s.id === "b").flags.find((x) => x.rule === "same_day_conflict"); assert.ok(f); assert.match(f.text, /two different His weight claims the same day — 355 lb and 340 lb/); }); test("not_his_number: our midpoint is never drawn as his figure", () => { const t = totalsOf([mclaim({ id: "a", date: "2025-01-01", value: 337.5, valueKind: "synthetic" })]); const f = t.steps.find((s) => s.id === "a").flags.find((x) => x.rule === "not_his_number"); assert.equal(f.text, "our midpoint, not his figure"); }); test("every predicate the module publishes actually fires somewhere", () => { // The list and the code drifting apart is how a rule quietly stops existing. const two = readFixture("measure-two-track.manifest.json"); const fired = new Set([ ...totalsOf(SAMPLE.ledger).steps.flatMap((s) => s.flags.map((f) => f.rule)), // broke_pledge needs a track with a pledge AND a lapse on it, which the // one-regime sample does not have and never will. ...totalsOf(two.ledger, two.ledgerSchema).steps.flatMap((s) => s.flags.map((f) => f.rule)), "same_day_conflict", "not_his_number", ]); for (const rule of MEASURE_PREDICATES) assert.ok(fired.has(rule), `${rule} never fires`); }); test("the switch counter counts changes of mind, and the final state is a LIVE segment", () => { const t = totalsOf(SAMPLE.ledger); // keto opens (not a change), carnivore replaces it, the stop, then keto again. assert.equal(t.switches, 3); assert.equal(t.final.value, 337); // The sample's last keto segment EXPIRED — he said "keto, day three" on // 2026-06-01 and nothing since, and the band stopped asserting it sixty days // later. So there is no current regime, and saying "Keto, since June" in the // closing card would be the card claiming what the chart refuses to draw. assert.equal(t.band[t.band.length - 1].reason, "silence"); assert.equal(t.final.regime, null); assert.equal(t.final.regimeSince, null); // A claim inside the window keeps it live, and then it is reported. const live = totalsOf([ ...SAMPLE.ledger, mclaim({ id: "r5", date: "2026-07-15", scope: "regime", population: "status", value: "keto", event: "on" }), ]); assert.equal(live.final.regime, "keto"); assert.equal(live.final.regimeSince, "2026-06-01"); }); test("same_day_conflict does not fire across FORMS", () => { // "I'm at 339, and I want to be 300 by Christmas" is one sentence: a reading // and a wish, on one day, holding two different numbers. Keyed on the track // alone this flagged both rows as a contradiction. const t = totalsOf([ mclaim({ id: "a", date: "2025-01-01", value: 339 }), mclaim({ id: "b", date: "2025-01-01", population: "goal", value: 300 }), ]); assert.deepEqual(firedOn(t, "a"), []); assert.deepEqual(firedOn(t, "b"), []); // Two goals the same day still are a conflict — same form, same track, two // answers. const two = totalsOf([ mclaim({ id: "a", date: "2025-01-01", population: "goal", value: 300 }), mclaim({ id: "b", date: "2025-01-01", population: "goal", value: 280 }), ]); assert.ok(firedOn(two, "b").includes("same_day_conflict")); }); test("an `unresolved` scope feeds neither the line nor the band", () => { const t = totalsOf([ mclaim({ id: "a", date: "2025-01-01", value: 340 }), mclaim({ id: "b", date: "2025-02-01", value: 999, scopeConfidence: "unresolved" }), ]); assert.deepEqual(t.points.map((p) => p.id), ["a"]); assert.deepEqual(t.unresolved, ["b"]); }); test("a regime a track claims and the schema never declares is a loud error", () => { const bad = { ...TWO_TRACK, tracks: [ TWO_TRACK.tracks[0], { key: "drinking", role: "regime", regimes: ["dry", "sober", "tipsy"] }, ], }; assert.throws(() => measureSchema(bad), /drinking/); assert.throws(() => measureSchema(bad), /sober, tipsy/); // …and a track that names none is not asked to, so no existing schema moves. assert.doesNotThrow(() => measureSchema(SCHEMA)); }); test("each regime track gets its own band, and neither closes the other's", () => { const led = [ reg({ id: "r1", date: "2025-02-01", scope: "regime", value: "keto", event: "start" }), reg({ id: "d1", date: "2025-02-10", scope: "drinking", value: "dry", event: "start" }), reg({ id: "d2", date: "2025-02-20", scope: "drinking", value: "drinking", event: "start" }), reg({ id: "r2", date: "2025-03-01", scope: "regime", value: "keto", event: "on" }), ]; const t = measureTotals(led, TWO_TRACK); // The diet is ONE keto segment across the whole run. Flattened, the two // drinking claims replaced it twice and it drew as three segments. const diet = t.bands.regime.band; assert.deepEqual(diet.map((b) => b.regime), ["keto"]); assert.deepEqual(diet[0].ids, ["r1", "r2"]); const drink = t.bands.drinking.band; assert.deepEqual(drink.map((b) => b.regime), ["dry", "drinking"]); // `totals.band` is still the FIRST regime track's, so nothing downstream had // to learn a new field to keep drawing what it drew. assert.deepEqual(t.band, diet); }); test("the counter counts switches OF A TRACK, and the predicates read one track back", () => { const led = [ reg({ id: "r1", date: "2025-02-01", scope: "regime", value: "keto", event: "start" }), reg({ id: "d1", date: "2025-02-05", scope: "drinking", value: "dry", event: "start" }), reg({ id: "r2", date: "2025-06-01", scope: "regime", value: "carnivore", event: "start" }), ]; const t = measureTotals(led, TWO_TRACK); assert.deepEqual(t.switchesByTrack, { regime: 1, drinking: 0 }); // `switches` is the first regime track's, which is what it has always been. assert.equal(t.switches, 1); // d1 starts a regime four days after keto started. Shared state made that a // `regime_flip` (keto, four days ago, now Dry) and a `quit_unannounced` // against keto — two findings about a claim that says nothing about food. const d1 = t.steps.find((st) => st.id === "d1"); assert.deepEqual(d1.flags, []); // …and carnivore in June still sees keto, four months back, as its own // track's previous segment: not a flip, but nothing was ever said about // stopping keto. const r2 = t.steps.find((st) => st.id === "r2"); assert.deepEqual(r2.flags.map((f) => f.rule), ["quit_unannounced"]); assert.equal(r2.flags[0].against, "r1"); }); test("broke_pledge: a drink two days after he says he has stopped", () => { const f = flagOn(twoTotals(), "a1", "broke_pledge"); assert.ok(f, "a1 should break p1"); assert.equal(f.against, "p1"); assert.equal(f.days, 2); assert.equal(f.text, "Dry on 2026-01-16, Drinking 2 days later"); }); test("broke_pledge fires once per pledge, not once per drink", () => { // a2 is a week after a1 and the same pledge is behind both. Flagging it again // would double every count the finale reads off the ledger. assert.equal(flagOn(twoTotals(), "a2", "broke_pledge"), null); }); test("broke_pledge stops at pledgeDays, and the schema says how many", () => { // a3 is thirty-one days after p2 — one day outside the window. assert.equal(flagOn(twoTotals(), "a3", "broke_pledge"), null); // …and the window is read off the schema, not hardcoded. At 40 days it fires. const wide = { ...TWO_FIX.ledgerSchema, pledgeDays: 40 }; const f = flagOn(totalsOf(TWO_FIX.ledger, wide), "a3", "broke_pledge"); assert.ok(f); assert.equal(f.days, 31); }); test("a pledge he called off out loud is not a pledge he broke", () => { // p3 (cutting back) → s1 (he says the rule is over) → a4 (drinking again). // Two weeks, well inside the window, and it is not a broken pledge: he said // so first. This is the case that keeps the red mark worth anything. const t = twoTotals(); assert.equal(flagOn(t, "a4", "broke_pledge"), null); assert.equal(flagOn(t, "s1", "broke_pledge"), null); }); test("broke_pledge suppresses the two generic names for the same event", () => { // A drink after a pledge is mechanically also an unannounced quit and a flip. // One event, one name — otherwise the inbox shows three findings about one // drink and the operator adjudicates the same fact three times. const t = twoTotals(); assert.deepEqual(firedOn(t, "a1"), ["broke_pledge"]); // …and the suppression is scoped to the row that fires it. a3 is outside the // window, so it keeps the generic finding it has always had. assert.ok(firedOn(t, "a3").includes("quit_unannounced")); assert.ok(!firedOn(t, "a3").includes("broke_pledge")); }); test("no pledge row is flagged: the promise was not the lie", () => { const t = twoTotals(); for (const id of ["p1", "p2", "p3"]) { assert.equal(flagOn(t, id, "broke_pledge"), null, `${id} must not carry the flag`); } }); // The event semantics, which are the adjudication's own and were not the // walk's. `start` is an ACT — he makes the promise, he takes the drink. `on` is // a REPORT: "I'm nine days dry" is not a new pledge and "I was a four-days-a- // week drinker" is not a fresh drink taken against one. test("a recounted drink breaks nothing, and leaves the pledge standing", () => { // The d030 case from the drinking adjudication: the pledge, a habitual claim // the same day that the report explicitly rules "not a lapse", and the fresh // drink ten days later that does break it. The middle row moves the board to // Drinking, so the break has to fire on a row where the regime did NOT // change — which is why the pledge ledger is kept outside that test. const t = seq( drinkRow("q1", "2026-01-01", "cutting", "start"), drinkRow("q2", "2026-01-02", "drinking", "on"), drinkRow("q3", "2026-01-11", "drinking", "start"), ); assert.equal(flagOn(t, "q2", "broke_pledge"), null, "a recounted drink is not a lapse"); const f = flagOn(t, "q3", "broke_pledge"); assert.ok(f, "the fresh drink ten days later breaks it"); assert.equal(f.against, "q1"); assert.equal(f.days, 10); // …and the generic names come back on the row that no longer takes the // specific one. One event still has one name. assert.ok(firedOn(t, "q2").includes("quit_unannounced")); assert.deepEqual(firedOn(t, "q3"), ["broke_pledge"]); }); test("a report that a pledge is holding is not a second pledge", () => { // "I'm nine days dry" is `dry`/`on`. Counting it opened 122 pledges out of // 27, and it would give a later drink something to break that he never // promised twice. const t = seq( drinkRow("q1", "2026-01-01", "dry", "start"), drinkRow("q2", "2026-01-10", "dry", "on"), drinkRow("q3", "2026-02-20", "drinking", "start"), ); assert.equal(measureScoreboard(t).final.pledges.count, 1); // q3 is 50 days after the promise and 41 after the report: outside the window // either way, and it must be measured from the PROMISE. assert.equal(flagOn(t, "q3", "broke_pledge"), null); const wide = { ...TWO_FIX.ledgerSchema, pledgeDays: 45 }; assert.equal(flagOn(totalsOf( [drinkRow("q1", "2026-01-01", "dry", "start"), drinkRow("q2", "2026-01-10", "dry", "on"), drinkRow("q3", "2026-02-20", "drinking", "start")], wide), "q3", "broke_pledge"), null); }); test("a report of drinking with no promise open opens nothing to break", () => { const t = seq( drinkRow("q1", "2026-01-01", "dry", "on"), drinkRow("q2", "2026-01-05", "drinking", "start"), ); assert.equal(measureScoreboard(t).final.pledges.count, 0); assert.equal(flagOn(t, "q2", "broke_pledge"), null); }); test("a lapse regime with no pledge anywhere is a schema error, by name", () => { const bad = { ...TWO_FIX.ledgerSchema, regimes: TWO_FIX.ledgerSchema.regimes.map((r) => r.kind === "pledge" ? { key: r.key, label: r.label, color: r.color } : r, ), }; assert.throws(() => measureSchema(bad), /`drinking`.*`lapse`|lapse.*pledge/s); // The message has to name the track, or nobody can find it in a schema with // three of them. try { measureSchema(bad); assert.fail("should have thrown"); } catch (err) { assert.match(err.message, /drinking/); } }); test("a regime that declares no kind is neither, and nothing gains a flag", () => { // Every shipped manifest is this one. The diet track's seven regimes carry no // `kind`, so broke_pledge cannot fire on them however the dates fall. const s = measureSchema(SAMPLE.ledgerSchema); for (const r of s.regimes) assert.equal(r.kind, null); assert.equal(s.pledgeDays, 30); const t = totalsOf(SAMPLE.ledger); assert.ok(!t.steps.some((st) => st.flags.some((f) => f.rule === "broke_pledge"))); }); test("the scoreboard's closing figures on the two-track fixture", () => { const sb = measureScoreboard(twoTotals()); assert.equal(sb.final.pledges.count, 3); assert.equal(sb.final.pledges.broken, 1); assert.equal(sb.final.pledges.shortest.days, 2); assert.equal(sb.final.pledges.shortest.pledge, "p1"); assert.equal(sb.final.pledges.shortest.lapse, "a1"); assert.equal(sb.final.goals.count, 2); // g1 (goal_loss) and g2 (goal) assert.equal(sb.final.goals.last.id, "g2"); assert.equal(sb.final.diets.count, 1); // keto opens, carnivore replaces it assert.equal(sb.final.net.delta, -28); assert.equal(sb.final.net.unit, "lb"); }); test("the net spans the record, and both ends are the cell's own rule", () => { // w3 is 305 on 2026-06-01. w4 says he WAS 312 on 2026-06-20 — a `past`, and // the freshest thing the ledger holds. // // It used to be excluded, on the argument that mixing forms would put one // number in the finale and a different one in the cell that had been on // screen for fifteen minutes. That had it exactly backwards: the CURRENT cell // is drawn by `freshestReading()`, which lets a `past` with a later `asOf` // win, so the cell reads 312 and only the scoreboard said 305. One rule for // both, and the rule is the one the viewer can see. const sb = measureScoreboard(twoTotals()); assert.equal(sb.final.net.from, 340); assert.equal(sb.final.net.fromAt, "2025-06-01"); assert.equal(sb.final.net.to, 312); assert.equal(sb.final.net.toAt, "2026-06-20"); assert.equal(sb.running.at(-1).net, -28); }); test("the net opens at the earliest reading, whatever form it took", () => { // The old `from` was the earliest `current`, which on a man who spent years // giving retrospectives and almost no contemporaneous figures is not the // start of the record — on the diet manifest it read the last twelve months // of an eight-year ledger. A `past` older than any `current` opens it now. const rows = [ { id: "p0", date: "2025-07-01", asOf: "2024-01-01", scope: "weight", population: "past", value: 400, display: "400 lb", plotted: true, valueKind: "uttered", scopeConfidence: "read", scopeBasis: "said", flags: [], hedged: false, unsourced: true, label: "400, remembered" }, ...JSON.parse(JSON.stringify(TWO_FIX.ledger)), ]; const sb = measureScoreboard(totalsOf(rows, TWO_FIX.ledgerSchema)); assert.equal(sb.final.net.from, 400); assert.equal(sb.final.net.fromAt, "2024-01-01"); assert.equal(sb.final.net.delta, -88); }); test("the running figures are the closing ones, seen from partway", () => { const sb = measureScoreboard(twoTotals()); const at = (id) => sb.running.find((r) => r.id === id); // Before any drink: three pledges are not yet made, none is broken. assert.equal(at("w1").pledges, 0); assert.equal(at("w1").broken, 0); assert.equal(at("w1").net, null); // one reading is not a net // p1 is made, then broken two days later. assert.equal(at("p1").pledges, 1); assert.equal(at("p1").broken, 0); assert.equal(at("a1").broken, 1); // …and the last row of `running` is `final`, or the totals line and the // scoreboard would disagree in the last second of the cut. const last = sb.running.at(-1); assert.equal(last.pledges, sb.final.pledges.count); assert.equal(last.broken, sb.final.pledges.broken); assert.equal(last.diets, sb.final.diets.count); assert.equal(last.net, sb.final.net.delta); }); test("the diet count is the ledger's switch counter, not a second walk of it", () => { const t = twoTotals(); const sb = measureScoreboard(t); assert.equal(sb.final.diets.count, t.switchesByTrack.regime); assert.equal(sb.final.diets.track, "regime"); }); test("a ledger with no pledges scores zero rather than nothing", () => { // The one-regime sample: no drinking track, no `kind` on anything. const sb = measureScoreboard(totalsOf(SAMPLE.ledger)); assert.equal(sb.final.pledges.count, 0); assert.equal(sb.final.pledges.broken, 0); assert.equal(sb.final.pledges.shortest, null); assert.equal(sb.final.diets.count, totalsOf(SAMPLE.ledger).switches); assert.ok(sb.final.span.claims > 0); }); test("an off label belongs to a track, and a shared one to the first", () => { // The bug: one flat `offKey` put "No diet" — the diet track's word — in the // DRINKING cell the moment he said he had stopped being dry. const base = { kind: "measure", unit: "lb", tracks: [ { key: "weight", role: "value" }, { key: "regime", role: "regime", regimes: ["keto", "off"] }, { key: "drinking", role: "regime", regimes: ["dry", "drinking", "off"] }, ], regimes: [ { key: "keto", label: "Keto" }, { key: "dry", label: "Dry", kind: "pledge" }, { key: "drinking", label: "Drinking", kind: "lapse" }, { key: "off", label: "No diet" }, ], }; const offOf = (schema) => Object.fromEntries(measureSchema(schema).tracks.map((t) => [t.key, t.offKey])); // Shared: the first regime track keeps it, the second gets nothing — the same // convention `switches`, `band` and `final.regime` already keep. assert.deepEqual(offOf(base), { weight: null, regime: "off", drinking: null }); // …and `offKey` stays schema-wide for the walk, which is about segments. assert.equal(measureSchema(base).offKey, "off"); // Sole ownership needs nothing said outright: an off-shaped regime only one // regime track declares is that track's. const owned = JSON.parse(JSON.stringify(base)); owned.tracks[2].regimes = ["dry", "drinking", "off_drinking"]; owned.regimes.push({ key: "off_drinking", label: "Not saying" }); assert.deepEqual(offOf(owned), { weight: null, regime: "off", drinking: "off_drinking" }); // And a track may simply say so, which is how a manifest fixes this for good. const named = JSON.parse(JSON.stringify(base)); named.tracks[2].offRegime = "off_drinking"; named.regimes.push({ key: "off_drinking", label: "Not saying" }); assert.deepEqual(offOf(named).drinking, "off_drinking"); // One regime track is the case every shipped manifest was: unchanged. const one = JSON.parse(JSON.stringify(base)); one.tracks = one.tracks.slice(0, 2); one.regimes = one.regimes.filter((r) => r.key !== "dry" && r.key !== "drinking"); assert.deepEqual(offOf(one), { weight: null, regime: "off" }); }); test("the bridge's peak is capped at the verdict's own gap", () => { // The two walks pair the same lapse against different pledges: the hidden d1 // breaks p1 in the combined walk and consumes it, and the on-camera d2 then // breaks p1 FOR the on-camera walk. Final gap zero, mid-walk difference +1 — // and the running maximum used to carry that +1 to the last frame (the diet // cut read 12 BROKEN against a ledger of 11; rev 6 shipped 17 against 16). const S = { ...TWO_TRACK, pledgeDays: 30, regimes: TWO_TRACK.regimes.map((r) => r.key === "dry" ? { ...r, kind: "pledge" } : r.key === "drinking" ? { ...r, kind: "lapse" } : r), }; const led = [ reg({ id: "p1", date: "2025-02-01", scope: "drinking", value: "dry", event: "start" }), reg({ id: "d1", date: "2025-02-05", scope: "drinking", value: "drinking", event: "start" }), reg({ id: "d2", date: "2025-02-10", scope: "drinking", value: "drinking", event: "start" }), ]; const cam = new Set(["p1", "d2"]); // d1 has no clip const split = measureScoreboardSplit( totalsOf(led, S), totalsOf(led.filter((c) => cam.has(c.id)), S), ); assert.equal(split.combined.final.pledges.broken, 1, "the record breaks p1 once"); assert.equal(split.onCamera.final.pledges.broken, 1, "the camera also sees p1 break once"); const last = split.bridge[split.bridge.length - 1]; assert.equal(last.broken, 1, "the mid-walk peak must not ride to the verdict"); // …and capping it costs none of the monotonicity the maximum bought. for (let i = 1; i < split.bridge.length; i += 1) { assert.ok(split.bridge[i].broken >= split.bridge[i - 1].broken); } }); test("a manifest that declares no fields declares no fields", () => { assert.deepEqual(ledgerFields({}), []); assert.deepEqual(ledgerFields({ ledgerSchema: fieldSchema(undefined) }), []); assert.deepEqual(claimVocab({}).fields, [], "and headcount has none either"); assert.deepEqual(measureSchema(fieldSchema([])).fields, []); }); test("a declared field is normalised once, with its defaults filled in", () => { const s = measureSchema( fieldSchema([ { key: "gloss", label: "Gloss", kind: "text", render: "gloss" }, { key: "qualifier", kind: "enum", options: [">", "<", "≈"], appliesTo: { scope: "weight" }, render: "cell" }, ]), ); assert.deepEqual(s.fields.map((f) => f.key), ["gloss", "qualifier"]); const [gloss, qual] = s.fields; assert.equal(gloss.maxLength, 200, "a text field gets the default cap"); assert.equal(gloss.render, "gloss"); assert.equal(qual.label, "qualifier", "the key is the label when none is given"); assert.equal(qual.maxLength, null, "…and only text has one"); assert.deepEqual(qual.appliesTo, { scope: "weight" }); // One vocabulary, read by the bench and by the gate alike. assert.deepEqual( claimVocab({ ledgerSchema: fieldSchema(s.fields) }).fields.map((f) => f.key), ["gloss", "qualifier"], ); }); test("the four ways a fields[] declaration is wrong are all loud", () => { assert.throws(() => measureSchema(fieldSchema([{ key: "x", kind: "colour" }])), /field `x` has kind `colour`/); assert.throws(() => measureSchema(fieldSchema([{ key: "x", kind: "enum" }])), /enum field `x` declares no `options`/); assert.throws( () => measureSchema(fieldSchema([{ key: "x", kind: "text" }, { key: "x", kind: "text" }])), /two `fields` entries declare `x`/, ); // A field named after a built-in would be a second, differently-validated // door onto a key the code already means. assert.throws(() => measureSchema(fieldSchema([{ key: "display", kind: "text" }])), /`display`, which is a built-in row key/); assert.throws(() => measureSchema(fieldSchema([{ key: "x", kind: "text", render: "banner" }])), /field `x` renders as `banner`/); assert.throws(() => measureSchema(fieldSchema([{ kind: "text" }])), /has no `key`/); }); test("every built-in a row carries is in LEDGER_BUILTIN_KEYS", () => { // The gate `umtool check` reads: a key in neither this set nor `fields[]` // is adjudication work nothing will ever read. for (const k of Object.keys(TWO_FIX.ledger[0])) { assert.ok(LEDGER_BUILTIN_KEYS.has(k), `${k} is a fixture row key and must be declared`); } for (const k of ["scopeBasis", "flags", "entryId", "hedged", "citeFrom", "unsourced"]) { assert.ok(LEDGER_BUILTIN_KEYS.has(k)); } assert.equal(LEDGER_BUILTIN_KEYS.has("gloss"), false, "…and a declared field is not one"); }); test("validateFields rules on one kind at a time, and clears on empty", () => { const s = fieldSchema([ { key: "gloss", kind: "text", maxLength: 12 }, { key: "qualifier", kind: "enum", options: [">", "<"] }, { key: "weighed", kind: "number", min: 0, max: 500 }, { key: "guessed", kind: "bool" }, { key: "at", kind: "seconds" }, { key: "breaks", kind: "rowRef" }, ]); const ledger = [{ id: "a1" }, { id: "p1" }]; const row = { id: "a1", scope: "weight" }; const ok = (patch) => validateFields(row, patch, s, ledger); assert.deepEqual(ok({ gloss: " his own " }), { set: { gloss: "his own" }, clear: [] }); assert.deepEqual(ok({ qualifier: ">" }).set, { qualifier: ">" }); assert.deepEqual(ok({ weighed: "351" }).set, { weighed: 351 }, "a number arrives as text"); assert.deepEqual(ok({ guessed: true }).set, { guessed: true }); assert.deepEqual(ok({ at: 0 }).set, { at: 0 }); assert.deepEqual(ok({ breaks: "p1" }).set, { breaks: "p1" }); // Empty CLEARS, for every kind: a field an operator has emptied is one they // are saying nothing about. assert.deepEqual(ok({ gloss: "" }), { set: {}, clear: ["gloss"] }); assert.deepEqual(ok({ gloss: " " }), { set: {}, clear: ["gloss"] }); assert.deepEqual(ok({ qualifier: null }), { set: {}, clear: ["qualifier"] }); // …and a key the schema does not declare is not this function's business. assert.deepEqual(ok({ display: "over 300 lb" }), { set: {}, clear: [] }); // Every violation, by name. assert.throws(() => ok({ gloss: "far too long to fit" }), /19 characters, and 12 is the most/); assert.throws(() => ok({ qualifier: "~" }), /`~` is not one of > ok({ weighed: "heavy" }), /must be a number/); assert.throws(() => ok({ weighed: 900 }), /900 is above 500/); assert.throws(() => ok({ at: -1 }), /-1 is before the start of the clip/); // "false" is a truthy string and a bug, never a value. assert.throws(() => ok({ guessed: "false" }), /must be true or false, not a string/); assert.throws(() => ok({ breaks: "a1" }), /a row cannot refer to itself/); assert.throws(() => ok({ breaks: "nope" }), /no ledger row has the id `nope`/); }); test("appliesTo refuses a field on a row that cannot carry it", () => { const s = fieldSchema([ { key: "breaks", kind: "rowRef", appliesTo: { scope: "drinking", value: "drinking", event: "start" } }, { key: "qualifier", kind: "enum", options: [">"], appliesTo: { role: "value" } }, ]); const ledger = [{ id: "p1" }, { id: "a1" }]; const lapse = { id: "a1", scope: "drinking", value: "drinking", event: "start" }; const reading = { id: "w1", scope: "weight", population: "current" }; assert.deepEqual(validateFields(lapse, { breaks: "p1" }, s, ledger).set, { breaks: "p1" }); assert.deepEqual(validateFields(reading, { qualifier: ">" }, s, ledger).set, { qualifier: ">" }); assert.throws( () => validateFields(reading, { breaks: "p1" }, s, ledger), /this row does not carry it — scope must be drinking, value must be drinking, event must be start/, ); assert.throws(() => validateFields(lapse, { qualifier: ">" }, s, ledger), /role must be value/); // The PATCH is what is judged, not the row as it stands: an edit that makes // the row eligible in the same save is a legal edit. assert.deepEqual( validateFields({ id: "a1", scope: "drinking", value: "dry", event: "start" }, { value: "drinking", breaks: "p1" }, s, ledger).set, { breaks: "p1" }, ); // …and clearing is always allowed, whatever the row says now. assert.deepEqual(validateFields(reading, { breaks: "" }, s, ledger), { set: {}, clear: ["breaks"] }); const [breaks] = measureSchema(s).fields; assert.equal(fieldApplies(breaks, lapse, s), true); assert.equal(fieldApplies(breaks, reading, s), false); }); test("both shapes of a promise are a promise", () => { assert.deepEqual(REGIME_KINDS, ["pledge", "bounded", "lapse"]); assert.equal(isPledgeKind("pledge"), true); assert.equal(isPledgeKind("bounded"), true); assert.equal(isPledgeKind("lapse"), false); assert.equal(isPledgeKind(null), false); const s = measureSchema(BOUNDED_SCHEMA); assert.equal(s.regimes.find((r) => r.key === "cutting").kind, "bounded"); assert.equal(s.regimes.find((r) => r.key === "dry").kind, "pledge"); // A track carrying a lapse and only a BOUNDED promise is a legal track: the // cross-check is about whether a lapse has anything to lapse from. assert.doesNotThrow(() => measureSchema({ kind: "measure", tracks: [{ key: "d", role: "regime", regimes: ["cut", "drink"] }], regimes: [{ key: "cut", kind: "bounded" }, { key: "drink", kind: "lapse" }], }), ); }); test("an unmarked drink does not break a bounded pledge, and does not close it", () => { // THE RISK THIS RULE EXISTS FOR. He bounds his drinking; he drinks, inside // the bound, which is what a bound is FOR; and a fortnight later he drinks in // a way the adjudicator says violates it. Closing the pledge on the first // drink leaves the second one with nothing to break. const rows = [ drinkRow("q1", "2026-01-01", "cutting", "start"), drinkRow("q2", "2026-01-05", "drinking", "start"), drinkRow("q3", "2026-01-19", "drinking", "start"), ]; rows[2].breaks = "q1"; rows[2].breaksWhy = "four nights running, against two a week"; const t = bounded(...rows); assert.equal(flagOn(t, "q2", "broke_pledge"), null, "the unmarked drink breaks nothing"); const f = flagOn(t, "q3", "broke_pledge"); assert.ok(f, "the marked one breaks the pledge that was still open"); assert.equal(f.against, "q1"); assert.equal(f.days, 18); assert.equal(f.explicit, true); // …and the same three rows against a plain pledge are the old rule exactly: // the FIRST drink breaks it and the marked one is a week too late to matter. const plain = totalsOf(rows, TWO_FIX.ledgerSchema); assert.equal(flagOn(plain, "q2", "broke_pledge").against, "q1"); assert.equal(flagOn(plain, "q3", "broke_pledge"), null); }); test("a bounded pledge nobody marks is never broken", () => { const t = bounded( drinkRow("q1", "2026-01-01", "cutting", "start"), drinkRow("q2", "2026-01-05", "drinking", "start"), drinkRow("q3", "2026-01-09", "drinking", "start"), ); for (const id of ["q2", "q3"]) assert.equal(flagOn(t, id, "broke_pledge"), null); assert.equal(measureScoreboard(t).final.pledges.broken, 0); // It still counts as a promise — the board says he made one and did not // break it, which is a statement the ledger has actually made. assert.equal(measureScoreboard(t).final.pledges.count, 1); }); test("a marked break is not subject to the window", () => { // The window stands in for "these two claims are about each other"; the mark // is the adjudicator saying so outright, and a proxy does not overrule the // thing it stands in for. const rows = [ drinkRow("q1", "2026-01-01", "cutting", "start"), drinkRow("q2", "2026-04-01", "drinking", "start"), ]; rows[1].breaks = "q1"; const f = flagOn(bounded(...rows), "q2", "broke_pledge"); assert.ok(f, "ninety days later, and named"); assert.equal(f.days, 90); assert.equal(f.explicit, true); }); test("a bounded pledge nobody names goes quiet after pledgeDays", () => { // Finite, or it collects lapses years after he stopped talking about it. const t = bounded( drinkRow("q1", "2026-01-01", "cutting", "start"), drinkRow("q2", "2026-04-01", "drinking", "start"), drinkRow("q3", "2026-04-02", "dry", "start"), drinkRow("q4", "2026-04-05", "drinking", "start"), ); assert.equal(flagOn(t, "q2", "broke_pledge"), null, "the bound has run out"); // …and the plain pledge that follows it breaks on the window, as it always // has: the two rules live side by side on one track. const f = flagOn(t, "q4", "broke_pledge"); assert.ok(f); assert.equal(f.against, "q3"); assert.equal(f.explicit, false); }); test("a breaks reference that names no open pledge marks nothing", () => { const rows = [ drinkRow("q1", "2026-01-01", "cutting", "start"), drinkRow("q2", "2026-01-10", "cutting", "start"), drinkRow("q3", "2026-01-15", "drinking", "start"), ]; // q1 was replaced by q2 nine days later; naming it is an adjudication error // and must not reach back past the promise he is actually under. rows[2].breaks = "q1"; const t = bounded(...rows); assert.equal(flagOn(t, "q3", "broke_pledge"), null); assert.equal(measureScoreboard(t).final.pledges.count, 2, "both promises still count"); }); test("PLEDGES counts the bounded ones too", () => { const rows = [ drinkRow("q1", "2026-01-01", "dry", "start"), drinkRow("q2", "2026-03-01", "cutting", "start"), drinkRow("q3", "2026-05-01", "cutting", "on"), ]; // Under the plain schema `cutting` is already a pledge, so the count is the // same two either way — which is the point: bounding a promise changes how it // BREAKS and nothing else about it. assert.equal(measureScoreboard(bounded(...rows)).final.pledges.count, 2); assert.equal(measureScoreboard(totalsOf(rows, TWO_FIX.ledgerSchema)).final.pledges.count, 2); // A report is still not a promise, under either kind. assert.equal(flagOn(bounded(...rows), "q3", "broke_pledge"), null); }); test("a pledge he called off out loud is not a bounded pledge he broke either", () => { const rows = [ drinkRow("q1", "2026-01-01", "cutting", "start"), drinkRow("q2", "2026-01-05", "cutting", "stop"), drinkRow("q3", "2026-01-08", "drinking", "start"), ]; rows[2].breaks = "q1"; assert.equal(flagOn(bounded(...rows), "q3", "broke_pledge"), null); }); test("the walk carries the breaks reference onto the step", () => { const rows = [ drinkRow("q1", "2026-01-01", "cutting", "start"), drinkRow("q2", "2026-01-10", "drinking", "start"), ]; rows[1].breaks = "q1"; const t = bounded(...rows); assert.equal(t.steps.find((s) => s.id === "q2").breaks, "q1"); assert.equal(t.steps.find((s) => s.id === "q1").breaks, null); }); test("a bound prefixes the figure and nothing else changes", () => { const m = JSON.parse(JSON.stringify(TWO_FIX)); m.ledgerSchema.fields = [ { key: "qualifier", label: "Bound", kind: "enum", options: [">", "<", "\u2248"], appliesTo: { scope: "weight" }, render: "cell" }, ]; for (const c of m.ledger) if (c.id === "w2") c.qualifier = ">"; const w2 = m.ledger.find((c) => c.id === "w2"); assert.equal(w2.qualifier, ">"); assert.equal(measureDisplay({ ...w2, display: null }, m.ledgerSchema), ">317 lb"); // HIS OWN WORDS still win the wording; the bound is added in front of them. assert.equal(measureDisplay(w2, m.ledgerSchema), ">317 lb"); // Idempotent, so a display an operator has already written survives a // value correction without the glyph coming back twice. assert.equal(measureDisplay({ ...w2, display: ">317 lb" }, m.ledgerSchema), ">317 lb"); // …and the NUMBER is untouched, everywhere it is used. const totals = totalsOf(m.ledger, m.ledgerSchema); const pt = totals.points.find((p) => p.id === "w2"); assert.equal(pt.value, 317); assert.equal(pt.display, ">317 lb"); // A schema that declares no such field leaves the display alone. const bare = { ...m.ledgerSchema, fields: [] }; assert.equal(measureDisplay({ ...w2, display: null }, bare), "317 lb"); }); test("offCloses: an off closes the regime it names, nothing else", () => { assert.equal(offCloses(null, "glp1", "off"), true); // nothing open: off is the reading assert.equal(offCloses("off", "glp1", "off"), true); // already off: stays off assert.equal(offCloses("keto", "keto", "off"), true); // off what is open: closes it assert.equal(offCloses("keto", "glp1", "off"), false); // off something else: keto stands assert.equal(offCloses("keto", "carnivore", null), false); }); test("an off for a regime that is not open neither switches the diet nor ends the band", () => { const m = JSON.parse(readFileSync(path.join(HERE, "fixtures", "measure-two-track.manifest.json"), "utf8")); const keto = m.ledger.find((r) => r.id === "r1"); const base = m.ledger.filter((r) => r.scope === "regime" && r.id === "r1"); const offOther = { ...keto, id: "rx", date: "2025-07-01", value: "carnivore", event: "off", display: "off Carnivore", label: "not carnivore", quote: "not doing carnivore", entryId: undefined }; const offSame = { ...offOther, id: "ry", value: "keto", display: "off Keto" }; const diets = (ledger) => measureScoreboard(measureTotals(ledger, m.ledgerSchema)).final.diets.count; const bandOf = (ledger) => measureTotals(ledger, m.ledgerSchema).band.filter((b) => !b.unknown).map((b) => b.regime); assert.equal(diets([...base, offOther]), diets(base), "off carnivore on keto is not a change of diet"); assert.deepEqual(bandOf([...base, offOther]), bandOf(base).length ? bandOf(base) : ["keto"]); assert.ok(!bandOf([...base, offOther]).includes("off"), "keto's band is not cut to off"); assert.equal(diets([...base, offSame]), diets(base) + 1, "off keto on keto still is one"); const ketoEnd = (ledger) => measureTotals(ledger, m.ledgerSchema).band.find((b) => b.regime === "keto")?.reason; assert.equal(ketoEnd([...base, offSame]), "stopped", "off keto ends keto's band"); assert.notEqual(ketoEnd([...base, offOther]), "stopped", "off carnivore does not"); });