// The finale's detailed diet runs (`deck.finale.dietRuns`; diet-record.mjs, // chrome-runs-finale.mjs, modules/runs-finale.mjs): the whole diet record from // its earliest row, an empty stretch a break, the stops and the medication // marked, in the finale's panel under the rail -- and nothing of it unless asked. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { dietRecord, runName } from "./diet-record.mjs"; import { RUNS_BREAK_DAYS, runsAxis, runsFinaleGeometry, runsFinaleHtml } from "./chrome-runs-finale.mjs"; import { validateFinaleSettings } from "./finale.mjs"; import { measureSchema } from "./measure-totals.mjs"; import { measureSlice } from "./measure-slice.mjs"; import { moduleById } from "./modules/index.mjs"; const SCHEMA = { kind: "measure", unit: "lb", yMin: 210, yMax: 400, bandGapDays: 60, tracks: [ { key: "weight", role: "value", label: "Weight" }, { key: "regime", role: "regime", label: "Diet", regimes: ["keto", "carnivore", "calories", "glp1", "off"], offKey: "off" }, ], forms: ["current", "past", "delta", "goal", "goal_loss", "overweight_by", "status"], regimes: [ { key: "keto", label: "Keto" }, { key: "carnivore", label: "Carnivore" }, { key: "calories", label: "Counting calories" }, { key: "glp1", label: "GLP-1" }, { key: "off", label: "No diet" }, ], }; const row = (o) => ({ scope: "weight", scopeBasis: "his words", scopeConfidence: "read", valueKind: "uttered", flags: [], asOf: null, ...o }); const LEDGER = [ // 2013 (MeVsMe): Weight Watchers, three weights -- then nothing until 2018. row({ id: "r105", date: "2013-02-22", scope: "regime", population: "status", value: "calories", event: "start", display: "Weight Watchers" }), row({ id: "w127", date: "2013-03-09", asOf: "2013-03-02", population: "current", value: 345, display: "345 lb" }), row({ id: "w130", date: "2013-03-21", population: "current", value: 342.9, display: "342.9 lb" }), row({ id: "r07", date: "2021-09-14", scope: "regime", population: "status", value: "keto", event: "start", display: "Keto", entryId: "c1" }), row({ id: "r11", date: "2022-07-27", scope: "regime", population: "status", value: "keto", event: "off", display: "off Keto" }), row({ id: "r14", date: "2022-12-30", scope: "regime", population: "status", value: "carnivore", event: "on", display: "Carnivore", entryId: "c2" }), row({ id: "r40", date: "2024-04-25", scope: "regime", population: "status", value: "glp1", event: "off", display: "off GLP-1" }), row({ id: "w50", date: "2024-05-01", population: "current", value: 330, display: "330 lb", entryId: "c2" }), ]; const TIMELINE = [ { type: "clip", id: "c1", video: "v1", start: 0, end: 10, quoteStart: 1 }, { type: "clip", id: "c2", video: "v2", start: 0, end: 10, quoteStart: 1, layout: "finale" }, ]; const SEGMENTS = [{ id: "c1", start: 0, duration: 10 }, { id: "c2", start: 10, duration: 10 }]; const PALETTE = { bg: "#0F1312", fg: "#EDF0EC", muted: "#98A29B", accent: "#22AB83", amber: "#E8A33F" }; const renderOf = (finale = { railLabels: "sections", dietRuns: true }) => ({ width: 1920, height: 1080, fps: 30, transition: 0, palette: PALETTE, chrome: { engine: "hyperframes", layout: "deck", deck: { measure: { scopes: ["weight", "regime"], scale: { min: 220, max: 400 } }, finale } }, }); const manifest = (render = renderOf()) => ({ ledgerSchema: SCHEMA, ledger: LEDGER, timeline: TIMELINE, render }); test("the record: every regime row from the earliest, runs by the state's rule, named by their own words; stops and medication marked", () => { const own = LEDGER; const readings = LEDGER.filter((r) => r.scope === "weight"); const rec = dietRecord(own, measureSchema(SCHEMA), "regime", readings); assert.equal(rec.from, "2013-02-22"); assert.deepEqual(rec.runs.map((r) => [r.n, r.key, r.since, r.end, r.ended, r.name]), [ [1, "calories", "2013-02-22", "2021-09-14", "new", "Weight Watchers"], [2, "keto", "2021-09-14", "2022-07-27", "off", null], [3, "carnivore", "2022-12-30", null, null, null], ]); assert.deepEqual(rec.marks.map((m) => [m.row, m.kind]), [["r11", "off"], ["r40", "off"]]); assert.deepEqual(rec.weights.map((w) => [w.date, w.value]), [["2013-03-02", 345], ["2013-03-21", 342.9], ["2024-05-01", 330]], "by the day it was his weight"); assert.equal(runName("Keto · 5 carbs a day", { key: "keto", label: "Keto" }), null); assert.equal(runName("off GLP-1", { key: "glp1", label: "GLP-1" }), null); }); test("the slice carries the record only when the finale asks for it", () => { const off = measureSlice(manifest(renderOf({ railLabels: "sections" })), { segments: SEGMENTS }, { asOf: "2026-11-03" }); assert.equal("record" in off, false, "a cut without it writes the slice it always did"); const on = measureSlice(manifest(), { segments: SEGMENTS }, { asOf: "2026-11-03" }); assert.equal(on.record.runs.length, 3); }); test("the axis: an empty stretch past two years squeezed to a marked break; the years on the rest", () => { const A = runsAxis(["2013-02-22", "2013-03-21", "2021-09-14", "2022-07-27", "2024-05-01"], 1704, { brk: 44, pad: 8 }); assert.equal(A.breaks.length, 1); assert.deepEqual([A.breaks[0].from, A.breaks[0].to], ["2013-03-21", "2021-09-14"]); assert.equal(Math.round(A.breaks[0].x1 - A.breaks[0].x0), 44); assert.ok(A.x("2024-05-01") <= 1704 - 8 + 1e-6); assert.deepEqual(A.years.map((y) => y.year), [2022, 2023, 2024], "no year ticks inside the break"); assert.ok(RUNS_BREAK_DAYS === 730); }); test("validation and the page: railLabels must leave the space; the page labels the open run 'and counting'", () => { assert.match(validateFinaleSettings({ dietRuns: true }).join(), /dietRuns draws under the rail, where railLabels "all" puts the pips' words: set render\.chrome\.deck\.finale\.railLabels "sections" or "none"/); assert.deepEqual(validateFinaleSettings({ dietRuns: true, railLabels: "none" }), []); assert.match(validateFinaleSettings({ dietRuns: "yes" }).join(), /dietRuns must be true, false or \{ marks, low, weights \}/); const r = renderOf(); const m = measureSlice(manifest(r), { segments: SEGMENTS }, { asOf: "2026-11-03" }); const schedule = { kind: "deck", fps: 30, total: 20, transition: 0, segments: SEGMENTS.map((s) => ({ ...s, end: s.start + s.duration, type: "clip", title: "", subtitle: "" })), finale: { segments: ["c2"], from: 10.5, to: 20, video: "side", motion: "grow", grow: 0.8 }, measure: m }; const g = runsFinaleGeometry(schedule, r); assert.ok(g && g.x === 120 && g.y > 200 && g.height >= 100, JSON.stringify(g)); const { html, items } = runsFinaleHtml(schedule, r, { from: 10.5, to: 20 }, { fonts: {} }); assert.match(html, /CARNIVORE/); assert.match(html, /days and counting/); assert.match(html, /WEIGHT WATCHERS/); assert.match(html, /2013–2021/, "the break says what it spans"); assert.ok(items.length >= 2); const mod = moduleById("runs-finale"); assert.equal(mod.on(schedule, r), true); assert.equal(mod.on(schedule, renderOf({ railLabels: "sections" })), false, "off by default"); }); test("marks on the weight line: the author's rows with his words, the heaviest THE peak; the low by default, never a bound", async () => { const { recordPoints } = await import("./diet-record.mjs"); const rows = [ ...LEDGER, row({ id: "w21", date: "2023-09-29", asOf: "2023-02-24", population: "past", value: 390, qualifier: "≈", display: "390 lb" }), row({ id: "w07", date: "2022-06-15", population: "current", value: 240, qualifier: ">", display: "over 240" }), row({ id: "w117", date: "2026-10-23", population: "current", value: 291, display: "291." }), row({ id: "w118", date: "2026-10-26", asOf: "2026-10-23", population: "current", value: 291.5, display: "291.5 lb" }), ]; const readings = rows.filter((r) => r.scope === "weight"); const pts = recordPoints(rows, readings, { marks: [{ row: "w21", label: "his heaviest · \"almost 400\"" }, { row: "w127", label: "2013 · 345" }, { row: "nope", label: "x" }] }); assert.deepEqual(pts.map((p) => [p.row, p.date, p.value, p.kind]), [ ["w127", "2013-03-02", 345, "mark"], ["w21", "2023-02-24", 390, "peak"], ["w118", "2026-10-23", 291.5, "low"], ], "by the day it was his weight; the heaviest the peak; the low not 'over 240', and 291.5 over the same day's '291.'"); assert.equal(pts[2].label, "his low · 291.5"); assert.deepEqual(recordPoints(rows, readings, { low: false }), []); assert.equal(recordPoints(rows, readings, { marks: [{ row: "w127", label: "a", peak: true }, { row: "w21", label: "b" }] })[0].kind, "peak", "the one marked peak"); assert.deepEqual(validateFinaleSettings({ railLabels: "sections", dietRuns: { marks: [{ row: "w21", label: "his heaviest" }], low: "his low" } }), []); assert.match(validateFinaleSettings({ railLabels: "sections", dietRuns: { marks: [{ row: "w21" }], lowest: true } }).join(), /dietRuns\.lowest is not a diet-runs setting.*marks\[0\]\.label/s); const on = measureSlice({ ...manifest(renderOf({ railLabels: "none", dietRuns: { marks: [{ row: "w127", label: "2013" }] } })), ledger: rows }, { segments: SEGMENTS }, { asOf: "2026-11-03" }); assert.deepEqual(on.record.points.map((p) => p.kind), ["peak", "low"]); }); test("the weight layer: every weight he gave at the day it applied, the retold ones apart; losses and 'N over' left out and counted", async () => { const { weightClaims } = await import("./diet-record.mjs"); const rows = [ ...LEDGER, row({ id: "w21", date: "2023-09-29", asOf: "2023-02-24", population: "past", value: 390, qualifier: "≈", display: "390 lb" }), row({ id: "w07", date: "2022-06-15", population: "current", value: 240, qualifier: ">", display: "over 240" }), row({ id: "d1", date: "2024-02-01", population: "delta", value: -19.4, display: "-19.4" }), row({ id: "o1", date: "2023-08-18", population: "overweight_by", value: 100, display: "100 over" }), row({ id: "g1", date: "2023-08-18", population: "goal_loss", value: -100, display: "lose 100" }), ]; const { claims, leftOut } = weightClaims(rows); assert.deepEqual(claims.map((c) => [c.row, c.applied, c.said, c.retold, c.qualifier]), [ ["w127", "2013-03-02", "2013-03-09", false, null], ["w130", "2013-03-21", "2013-03-21", false, null], ["w07", "2022-06-15", "2022-06-15", false, ">"], ["w21", "2023-02-24", "2023-09-29", true, "≈"], ["w50", "2024-05-01", "2024-05-01", false, null], ], "a week's asOf is still his weight then; seven months on is a retelling"); assert.deepEqual(leftOut, { delta: 1, overweight_by: 1 }); assert.match(validateFinaleSettings({ railLabels: "none", dietRuns: { weights: "both" } }).join(), /weights must be "applied", "said" or false/); const r = renderOf({ railLabels: "sections", dietRuns: { weights: "applied" } }); const m = measureSlice({ ...manifest(r), ledger: rows }, { segments: SEGMENTS }, { asOf: "2026-11-03" }); const schedule = { kind: "deck", fps: 30, total: 20, transition: 0, segments: SEGMENTS.map((s) => ({ ...s, end: s.start + s.duration, type: "clip", title: "", subtitle: "" })), finale: { segments: ["c2"], from: 10.5, to: 20, video: "side", motion: "grow", grow: 0.8 }, measure: m }; const g = runsFinaleGeometry(schedule, r); assert.ok(g.height >= 200, `the layer has its room under the track (${g.height})`); const { html } = runsFinaleHtml(schedule, r, { from: 10.5, to: 20 }, { fonts: {} }); assert.match(html, /class="hol"/, "a retold weight hollow"); assert.match(html, /class="ldr"/, "its leader to the day he said it"); assert.match(html, /class="car"/, "a bound's caret"); assert.match(html, /class="apx"/, "an about's ring"); const off = runsFinaleHtml(schedule, renderOf({ railLabels: "sections", dietRuns: { weights: false } }), { from: 10.5, to: 20 }, { fonts: {} }).html; assert.doesNotMatch(off, /class="hol"/); });