// Tests for panel-model.mjs: the measure cells' three rules. 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 { goalCell, nextReading, nextRegime, offOf, regimeOf } from "./panel-model.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const TWO = JSON.parse(readFileSync(path.join(HERE, "fixtures", "measure-two-track.manifest.json"), "utf8")); const S = TWO.ledgerSchema; const row = (o) => ({ scope: "regime", population: "status", ...o }); test("an off closes the regime it names, and nothing else (the off-scope fix)", () => { const keto = nextRegime(null, row({ id: "r1", date: "2025-07-20", value: "keto", event: "start" }), S); assert.equal(keto.key, "keto"); // "off carnivore" three weeks into keto: keto stands. const still = nextRegime(keto, row({ id: "r2", date: "2025-08-10", value: "carnivore", event: "off" }), S); assert.equal(still, keto); // "off keto": the track's own off word takes the cell. const off = nextRegime(keto, row({ id: "r3", date: "2025-09-01", value: "keto", event: "off" }), S); assert.equal(off.key, offOf(S, "regime").key); assert.equal(off.since, "2025-09-01"); }); test("the regime cell is in his words, and an `on` keeps when it opened", () => { const a = nextRegime(null, row({ id: "r1", date: "2025-07-20", value: "keto", event: "start", display: "keto · no carbs" }), S); const b = nextRegime(a, row({ id: "r2", date: "2025-08-06", value: "keto", event: "on", display: "Keto · 5 carbs a day" }), S); assert.equal(b.display, "Keto · 5 carbs a day"); assert.equal(b.since, "2025-07-20"); assert.equal(b.row, "r2"); // A row that is no regime leaves the cell alone. assert.equal(nextRegime(b, { id: "w1", scope: "weight", population: "current", value: 300 }, S), b); assert.equal(regimeOf(S, "nope"), null); }); test("a retrospective is the reading only when it is later than the board's", () => { const cur = nextReading(null, { id: "w1", scope: "weight", population: "current", date: "2025-03-01", value: 340 }, S); assert.equal(cur.value, 340); const older = nextReading(cur, { id: "w2", scope: "weight", population: "past", date: "2025-04-01", asOf: "2024-01-01", value: 390 }, S); assert.equal(older, cur); const newer = nextReading(cur, { id: "w3", scope: "weight", population: "past", date: "2025-04-01", asOf: "2025-03-20", value: 335 }, S); assert.equal(newer.value, 335); }); test("a goal_loss is a weight only against a reading", () => { const g = { id: "g1", scope: "weight", population: "goal_loss", value: -65, deadline: "by Christmas" }; assert.deepEqual(goalCell(g, { value: 349 }, S), { value: 284, loss: 65, display: "284 lb", deadline: "by Christmas", row: "g1" }); const words = goalCell(g, null, S); assert.equal(words.value, null); assert.equal(words.loss, 65); assert.equal(goalCell({ id: "g2", scope: "weight", population: "goal", value: 250 }, null, S).value, 250); });