// The measure extras (measure-extras.mjs, chrome-measure-extras.mjs, modules/goals, // peak, promise, diet): the goal stack, the moving start, the promise clock and // the diet badge, read off the schedule's `measure` slice. Fixture: // fixtures/measure-extras/measure.json (make-fixture.mjs: the lanes' synthetic // slice, real ledger rows and fake cut times, trimmed to what these read). import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import path from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; import { goalsHtml, dietHtml, moverCues, moverSteps, peakHtml, promiseHtml, spreadLabels, stampSize, streakHtml } from "./chrome-measure-extras.mjs"; import { cuePageHtml } from "./chrome-cues.mjs"; import { entryHides, estimateSchedule, HIDEABLE_MODULES, parseHideModules, validateChrome } from "./deck.mjs"; import { frameKeys } from "./frame-keys.mjs"; import { daysBetween, deadlineDate, dietRuns, dietStates, goalStates, goalText, lostRunsRuns, measureExtraGeometry, measurePlacements, peakStates, pledgeRuns, promiseStates, streakStates, upSpans, } from "./measure-extras.mjs"; import { deckOverlays, moduleById, moduleProblems, playModules, scheduleSlices } from "./modules/index.mjs"; import { resolveMeasureExtra, validateMeasureExtra } from "./modules/measure-extras-settings.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const MEASURE = JSON.parse(readFileSync(path.join(HERE, "fixtures/measure-extras/measure.json"), "utf8")); const IDS = ["goals", "peak", "promise", "diet", "streak"]; const PAGES = { goals: goalsHtml, peak: peakHtml, promise: promiseHtml, diet: dietHtml, streak: streakHtml }; const RENDER = { width: 1920, height: 1080, fps: 30, palette: { bg: "#0F1312", fg: "#EDF0EC", muted: "#98A29B", accent: "#22AB83", amber: "#E8A33F" }, chrome: { engine: "hyperframes", layout: "deck", deck: {} }, }; const withDeck = (deck) => ({ ...RENDER, chrome: { ...RENDER.chrome, deck } }); const schedule = (measure = MEASURE, extra = {}) => ({ kind: "deck", fps: 30, total: 740, transition: 0.4, segments: [{ id: "a", start: 0, duration: 740, end: 740 }], measure, ...extra, }); const clone = (v) => JSON.parse(JSON.stringify(v)); test("deadlineDate: the next named day from when he said it; words with no date in them name none", () => { assert.equal(deadlineDate("by Christmas", "2026-10-26"), "2026-12-25"); assert.equal(deadlineDate("by Christmas", "2026-12-28"), "2027-12-25", "said after it: the next one"); assert.equal(deadlineDate("by Halloween next year", "2026-03-01"), "2027-10-31"); assert.equal(deadlineDate("by Christmas 2027", "2026-03-01"), "2027-12-25"); assert.equal(deadlineDate("by the end of 2027", "2026-03-01"), "2027-12-31"); assert.equal(deadlineDate("by the end of the year", "2026-03-01"), "2026-12-31"); assert.equal(deadlineDate("by New Year's", "2026-05-01"), "2027-01-01"); assert.equal(deadlineDate("by Thanksgiving", "2026-05-01"), "2026-11-26"); assert.equal(deadlineDate("by March 3rd", "2026-05-01"), "2027-03-03"); assert.equal(deadlineDate("by 2026-12-01", "2026-05-01"), "2026-12-01"); for (const w of ["by my birthday", "by his birthday", "by summer", "", null]) assert.equal(deadlineDate(w, "2026-05-01"), null, String(w)); assert.equal(daysBetween("2026-11-05", "2026-12-25"), 50); }); test("goals: a note per goal, a repeat merged with its count and brought to the front, the deadline counted to the render date", () => { const states = goalStates(schedule(), RENDER); assert.equal(states.length, 29, "every goal and goalLoss row on camera"); assert.equal(states.at(-1).goalsSet, 29); // "lose 100", one note with its count: the seven times the cut shows it (the // totals' goalRepeats, 8, count the record's off-camera row too). const lose100 = Math.max(...states.flatMap((s) => s.notes.filter((n) => n.key === "lose 100").map((n) => n.count))); assert.equal(lose100, MEASURE.events.filter((e) => e.display === "lose 100").length); assert.equal(lose100, 7); const rep = states.find((s) => s.top === "lose 100" && s.repeat); assert.equal(rep.notes.at(-1).key, "lose 100", "the repeat comes to the front"); assert.equal(rep.notes.at(-1).depth, 0); assert.ok(rep.notes.length <= 4, "max notes shown (4)"); // The deadline: "65 by Christmas", 50 days from the render date, the target the state resolved. const last = states.at(-1).notes.at(-1); assert.deepEqual([last.text, last.deadline, last.deadlineDate, last.days, last.target], ["LOSE 65", "by Christmas", "2026-12-25", 50, 226.5]); assert.equal(goalText({ kind: "goal", display: "220-240", qualifier: null }), "220–240 LB"); assert.equal(goalText({ kind: "goal", display: "250", qualifier: "≈" }), "≈250 LB"); // A deadline that names no date: its label, no countdown. const m = clone(MEASURE); const w61 = m.events.findIndex((e) => e.row === "w61"); m.events[w61].deadline = "by my birthday"; const n61 = goalStates(schedule(m), RENDER).find((s) => s.notes.some((n) => n.deadline === "by my birthday")).notes.at(-1); assert.deepEqual([n61.deadline, n61.deadlineDate, n61.days], ["by my birthday", null, null]); assert.equal(goalStates(schedule(), withDeck({ goals: { max: 2 } })).at(-1).notes.length, 2); }); test("peak: a tick per telling of a start in the drift, a count per retelling, the drift from lowest to highest", () => { const states = peakStates(schedule(), RENDER); const last = states.at(-1); assert.deepEqual(last.ticks.map((t) => t.value), MEASURE.totals.peakDrift); const by = Object.fromEntries(last.ticks.map((t) => [t.value, t])); assert.deepEqual([by[390].count, by[390].approx, by[365.7].count], [5, true, 3], "≈390 ×5, 365.7 ×3"); assert.equal(last.drift, 24.3); assert.equal(last.told, 11); assert.deepEqual(last.range, states[0].range, "the scale's range is fixed over the cut"); assert.ok(last.range[0] < 365.7 && last.range[1] > 390); // `values: "all"` draws every remembered weight (the wedding's 368, the keto 349 and 342). assert.equal(peakStates(schedule(), withDeck({ peak: { values: "all" } })).length, 15); assert.deepEqual(spreadLabels([100, 104, 300], 30, 0, 400), [100, 130, 300]); }); test("promise: the days to the clip's in-record date, her claim and the outcome, the words flashed, then counted on to the render date", () => { const states = promiseStates(schedule(), RENDER); // AS OF is the in-record date of the playhead: the latest date of ANY event so far -- the goal said on 31 Oct // moves it too (rev 9, 6 Nov). assert.deepEqual(states.map((s) => s.rows[0].days), [0, 1, 8, 8, 11, 12, 13]); assert.deepEqual(states.map((s) => s.rows[0].to), ["2026-10-23", "2026-10-24", "2026-10-31", "2026-10-31", "2026-11-03", "2026-11-05", "2026-11-05"]); assert.equal(states[0].at, 712.18, "from the promise on"); assert.equal(states[0].rows[0].what, "scale video"); assert.equal(states[0].rows[0].claim, null); // Her claim (SYNTHETIC in the fixture): the counterparty, her words fresh, in her voice. const c = states[1].rows[0]; assert.deepEqual(c.claim, { speaker: "madamsavvy", date: "2026-10-24", paidOn: null }); assert.deepEqual([c.word.speaker, c.word.his, c.fresh], ["madamsavvy", false, true]); assert.match(c.word.text, /I bought the 5 bags/); // The outcome (SYNTHETIC): refunded, and the count keeps running. assert.deepEqual(states[3].rows[0].outcome, { kind: "refunded", date: "2026-10-31" }); assert.equal(states[3].rows[0].word.outcome, true); assert.equal(states[4].rows[0].fresh, true, "his check flashes"); assert.equal(states[4].rows[0].word.his, true); assert.match(states[4].rows[0].word.text, /pull up a video from three months ago/); assert.equal(states.at(-1).asOf, true); assert.equal(states.at(-1).rows[0].to, MEASURE.asOfRender); assert.equal(states.at(-1).rows[0].kept, false); // From the finale's start when there is one. const fin = promiseStates(schedule(MEASURE, { finale: { from: 730, to: 740 } }), RENDER); assert.equal(fin.at(-2).at, 730); // Kept: green, and the count stops on the day it was kept. const m = clone(MEASURE); for (const s of m.state) for (const p of s.promises ?? []) if (p.outcome) p.outcome = { kind: "kept", date: "2026-10-31", row: "x" }; const kept = promiseStates(schedule(m), RENDER); assert.equal(kept.at(-1).rows[0].kept, true); assert.equal(kept.at(-1).rows[0].days, 8, "stopped on 31 Oct"); // PAID: the first claim that carries a paidOn (s01 23 Oct), not the latest claim's date. const paid = clone(MEASURE); for (const s of paid.state) for (const p of s.promises ?? []) { if (p.claims?.length) p.claims = [{ row: "s01", speaker: "madamsavvy", date: "2026-10-23", paidOn: "2026-10-23" }, { row: "s02", speaker: "madamsavvy", date: "2026-10-24" }, { row: "s03", speaker: "madamsavvy", date: "2026-10-25" }]; } const ps = promiseStates(schedule(paid), RENDER); assert.deepEqual(ps[1].rows[0].claim, { speaker: "madamsavvy", date: "2026-10-23", paidOn: "2026-10-23" }); assert.equal(ps[1].rows[0].to, "2026-10-24", "AS OF: the events seen so far, not a claim's date the cut has not reached"); // No paidOn anywhere: the first claim, by its date. const unpaid = clone(paid); for (const s of unpaid.state) for (const p of s.promises ?? []) for (const c of p.claims ?? []) delete c.paidOn; assert.equal(promiseStates(schedule(unpaid), RENDER)[1].rows[0].claim.date, "2026-10-23"); // An older slice's `kept: true` reads as kept. const old = clone(MEASURE); for (const s of old.state) for (const p of s.promises ?? []) { delete p.outcome; p.kept = true; } assert.equal(promiseStates(schedule(old), RENDER)[0].rows[0].kept, true); // The quote instead of the label. assert.match(promiseStates(schedule(), withDeck({ promise: { checks: "quote" } }))[4].rows[0].word.text, /brother just pull up/); // The page: her handle (speakers), her picture when given, else a monogram; the refunded stamp. const page = (o) => promiseHtml(schedule(), withDeck({ promise: { speakers: { madamsavvy: { handle: "MadamSavvy" } } } }), { fonts: {}, ...o }).html; assert.match(page(), /by @MadamSavvy<\/b>/); assert.match(page(), /class="av mono"[^>]*>MRefunded<\/div>
by 31 Oct · no video<\/div>/); assert.match(page(), /@MadamSavvy<\/b> reads/); }); test("promise: AS OF follows the playhead's in-record date in the rev9 order -- any event's, a running max", () => { // The rev9 cut's tail, in its order: the promise (23 Oct), her claims, c46 (a weight said 26 Oct), a late // memory dated earlier (it must not pull AS OF back), n01 (3 Nov), then his check p05. const said = "2026-10-23"; const ev = [ { t: 535, kind: "promise", date: said, row: "p01", label: "scale video" }, { t: 630.6, kind: "promiseClaim", date: "2026-10-25", row: "s03", promise: "p01", speaker: "madamsavvy" }, { t: 686.2, kind: "stated", date: "2026-10-26", row: "w118", value: 291.5 }, { t: 700, kind: "remembered", date: "2026-10-15", row: "w200", value: 390 }, { t: 736.9, kind: "promiseClaim", date: "2026-11-02", row: "s04", promise: "p01", speaker: "madamsavvy" }, { t: 758.8, kind: "lost", date: "2026-11-03", row: "w122", value: -10 }, { t: 778, kind: "promiseCheck", date: "2026-11-03", row: "p05", promise: "p01", label: "still no video" }, ]; const promises = [{ row: "p01", said, claims: [] }]; const m = { version: 1, unit: "lb", asOfRender: "2026-11-06", events: ev, hidden: [], state: [{ t: 0, promises: [] }, ...ev.map((e) => ({ t: e.t, promises }))] }; const states = promiseStates({ kind: "deck", fps: 30, total: 800, transition: 0.4, segments: [{ id: "a", start: 0, duration: 800, end: 800 }], measure: m }, withDeck({ promise: { label: "scale video" } })); const at = (t) => states.filter((s) => s.at <= t).at(-1).rows[0]; assert.deepEqual([at(690).to, at(690).days], ["2026-10-26", 3], "during c46: 3 DAYS AS OF 26 OCT, not 25 Oct"); assert.equal(at(710).to, "2026-10-26", "an earlier date later in the cut never takes it back"); assert.deepEqual([at(760).to, at(760).days], ["2026-11-03", 11], "during n01: AS OF 3 NOV before p05 lands"); assert.deepEqual([states.at(-1).rows[0].to, states.at(-1).asOf], ["2026-11-06", true], "the render date from the finale on"); }); test("hideModules: an entry hides the named extras over its span, with their own fade, whatever else they did there", () => { // The entry's knob, read into the schedule (deck.mjs entryHides): its segment's span, the ids it names. const segs = [{ id: "v01", start: 690, duration: 10 }, { id: "v02", start: 699.6, duration: 12.4 }]; assert.deepEqual(entryHides([{ id: "v01" }, { id: "v02", hideModules: ["streak", "diet", "nope"] }], segs), [{ segment: "v02", from: 699.6, to: 712, modules: ["diet", "streak"] }]); assert.deepEqual(parseHideModules("streak, diet"), { value: ["diet", "streak"], error: null }); assert.match(parseHideModules("dial").error, /dial cannot be hidden per entry \(goals, peak, promise, diet, streak\)/); assert.deepEqual(parseHideModules(""), { value: null, error: null }); assert.deepEqual([...HIDEABLE_MODULES], ["goals", "peak", "promise", "diet", "streak"]); // Over the span: hidden (rect null), each module's moves; a dodge across it is cut around it. const r = withDeck({ diet: {}, streak: {} }); // A card on the diet badge's home: it dodges to a slot over the card's span. const card = { segment: "p1", from: 715, to: 725, rects: [measureExtraGeometry("diet", r)] }; const plain = measurePlacements(schedule(MEASURE, { covers: [card] }), r); const hid = measurePlacements(schedule(MEASURE, { covers: [card], hides: [{ segment: "v02", from: 718, to: 721, modules: ["diet"] }] }), r); assert.ok(plain.diet.moves.some((m) => m.from === 715 && m.to === 725 && m.rect), "the card's own move: a dodge"); const at = (moves, t) => moves.filter((m) => m.from <= t && t < m.to); assert.deepEqual(at(hid.diet.moves, 719).map((m) => m.rect), [null], "hidden over v02, one move"); assert.ok(hid.diet.moves.every((m) => !(m.from < 721 && m.to > 718) || (m.from === 718 && m.to === 721 && m.rect === null)), "nothing else in the span"); assert.ok(at(hid.diet.moves, 716)[0].rect && at(hid.diet.moves, 723)[0].rect, "the dodge on either side of it"); assert.deepEqual(hid.streak.moves, plain.streak.moves, "a module it does not name: as it was"); // A clean span: just the hide. const clean = measurePlacements(schedule(MEASURE, { hides: [{ segment: "v02", from: 600, to: 610, modules: ["diet", "streak"] }] }), r); assert.deepEqual(clean.diet.moves, [{ from: 600, to: 610, rect: null, size: 1 }]); assert.deepEqual(clean.streak.moves, [{ from: 600, to: 610, rect: null, size: 1 }]); }); test("diet: a stamp per change of regime or number, words alone not slammed, a null after one is No diet", () => { const states = dietStates(schedule(), RENDER); assert.equal(states[0].diet.label, "Some diet"); assert.equal(states[0].diet.n, 1); assert.equal(states[0].slam, true); assert.equal(states.at(-1).diet.detail, "Keto + IF + walking"); assert.equal(states.at(-1).diet.color, "#4E9BD8"); const m = clone(MEASURE); // The state after the last event: no diet (the producer's off-scope rule said so). m.state[m.state.length - 1].diet = null; // And one change of words only. const k = m.state.findIndex((s) => s.diet?.n === 63); m.state[k + 1] = { ...m.state[k + 1], diet: { ...m.state[k].diet, display: "Keto · strict" } }; const s2 = dietStates(schedule(m), RENDER); assert.equal(s2.at(-1).diet, null); const words = s2.find((s) => s.diet?.detail === "Keto · strict"); assert.equal(words.slam, false); assert.equal(dietStates(schedule(), withDeck({ diet: { labels: { glp1: "Ozempic" } } })).find((s) => s.diet.regime === "glp1").diet.label, "Ozempic"); assert.ok(stampSize("Counting calories", 330) < stampSize("Keto", 330)); }); test("streak: runs through a per-source adapter -- diet runs, lost runs, pledges -- and the chain's states", () => { const cfg = resolveMeasureExtra("streak", RENDER); const runs = dietRuns(MEASURE, cfg); assert.equal(runs.length, 66, "a run per diet (regime and number)"); assert.deepEqual([runs[0].name, runs[0].start, runs[0].ended, runs[0].detail], ["Some diet", "2018-02-06", "new", "diet #1"]); assert.equal(runs.at(-1).end, null, "the last is open"); for (const r of runs.slice(0, -1)) assert.ok(r.endT >= r.startT && r.end >= r.start); // A diet turned off ends its run "off". const m = clone(MEASURE); m.state[m.state.length - 1].diet = null; assert.equal(dietRuns(m, cfg).at(-1).ended, "off"); // Lost runs: the state's runs, each ended at its last claim before the next. const lost = clone(MEASURE); lost.state = lost.state.map((s, i) => ({ ...s, lost: { total: 0, runs: i < 40 ? [{ run: 1, start: "2021-04-30", peak: 13.5 }] : [{ run: 1, start: "2021-04-30", peak: 13.5 }, { run: 2, start: "2023-05-09", peak: 29 }] } })); const lr = lostRunsRuns(lost, cfg, RENDER.palette); assert.deepEqual(lr.map((r) => [r.name, r.start, r.ended, r.detail]), [["Run 1", "2021-04-30", "new", "down 13.5"], ["Run 2", "2023-05-09", null, "down 29"]]); // Pledges (the sobriety video's adapter): a pledge starts a run, a lapse snaps it. const p = { events: [ { t: 1, row: "d1", kind: "pledge", date: "2026-01-01", display: "Dry" }, { t: 2, row: "d2", kind: "lapse", date: "2026-03-15" }, { t: 3, row: "d3", kind: "pledge", date: "2026-04-01", display: "Dry" }, ], state: [{}, {}, {}, {}] }; const pr = pledgeRuns(p, cfg, RENDER.palette); assert.deepEqual(pr.map((r) => [r.start, r.end, r.ended]), [["2026-01-01", "2026-03-15", "lapse"], ["2026-04-01", null, null]]); // The chain: a state per change, the live run's days with the in-record date; the axis' span to the render date. const { states, span, runs: rs } = streakStates(schedule(), RENDER); assert.equal(span[1], MEASURE.asOfRender); assert.ok(states.some((s) => s.cracked !== null)); assert.equal(states.at(-1).live.i, rs.length - 1); assert.equal(streakStates(schedule(), withDeck({ streak: { source: "lostRuns" } })).runs.length, 0, "the trimmed fixture keeps no lost runs"); }); test("up: from the first event to the end (or the finale), or `hold` seconds after each", () => { assert.deepEqual(upSpans([10, 20], { hold: null, finale: "hide" }, { total: 100 }), [{ in: 10, out: 100 }]); assert.deepEqual(upSpans([10, 20], { hold: 0, finale: "hide" }, { total: 100 }), [{ in: 10, out: 100 }], "0 is stays up (look's null removes the key)"); assert.deepEqual(upSpans([10, 20], { hold: null, finale: "hide" }, { total: 100, finale: { from: 90, to: 100 } }), [{ in: 10, out: 90 }]); assert.deepEqual(upSpans([10, 20], { hold: null, finale: "show" }, { total: 100, finale: { from: 90, to: 100 } }), [{ in: 10, out: 100 }]); assert.deepEqual(upSpans([10, 15, 40], { hold: 8, finale: "hide" }, { total: 100 }), [{ in: 10, out: 23 }, { in: 40, out: 48 }]); }); test("the pages: a block per state, each an item gated by its own autoAlpha, keyed so unchanged frames are reused", () => { for (const id of IDS) { const { html, items } = PAGES[id](schedule(), RENDER, { fonts: { regular: "assets/DeckSans.ttf", bold: "assets/DeckSansBold.ttf" } }); assert.match(html, new RegExp(`