// The goal board (measure-goals.mjs) and how the dial draws it // (chrome-instruments.mjs): every goal in force, the closest as the headline, // a newer goal of the same horizon replacing the older, a goal met or past its // day leaving -- missed, struck in red a moment, when his weight is over it. import assert from "node:assert/strict"; import test from "node:test"; import { boardGoalWords, dialHtml, goalLines, goalMarks } from "./chrome-instruments.mjs"; import { estimateSchedule } from "./deck.mjs"; import { frameKeys } from "./frame-keys.mjs"; import { MISS_HOLD, goalBoard, goalNeed, goalsAt, headlineOf } from "./measure-goals.mjs"; const FPS = 30; const SCALE = { min: 220, max: 400 }; const fonts = { DeckSans: "assets/DeckSans.ttf", DeckSansBold: "assets/DeckSansBold.ttf" }; /** A measure from events: each state after it, its `goal` the resolution of a goalLoss when `resolved` names one. */ function measureOf(events) { const state = [{ t: 0 }]; for (const e of events) state.push({ t: e.t, ...(e.resolved ? { goal: { row: e.row, value: e.resolved, loss: Math.abs(e.value) } } : {}) }); return { scale: SCALE, events: events.map(({ resolved, ...e }) => e), state }; } const stated = (t, row, date, value, qualifier = null) => ({ t, row, date, kind: "stated", value, qualifier }); const goal = (t, row, date, value, extra = {}) => ({ t, row, date, kind: "goal", value, ...extra }); const loss = (t, row, date, value, resolved, extra = {}) => ({ t, row, date, kind: "goalLoss", value, resolved, ...extra }); const rows = (b) => b.active.map((g) => g.row); test("a ladder of three: each in force, the CLOSEST the headline -- not the most ambitious, not the latest", () => { const m = measureOf([ stated(1, "s1", "2026-01-09", 337), goal(2, "g1", "2026-01-16", 320, { asOf: "2026-04-01" }), // by his birthday: 17 to go loss(3, "g2", "2026-01-16", -75, 262, { deadline: "by the end of 2026" }), // 75 to go goal(4, "g3", "2026-01-16", 240), // eventual: 97 to go ]); const board = goalBoard(m); const end = goalsAt(board, 10); assert.deepEqual(rows(end), ["g1", "g2", "g3"]); assert.equal(end.headline.row, "g1"); assert.equal(end.headline.deadlineDate, "2026-04-01", "a goal's asOf is the day it is for"); assert.equal(end.active[1].deadlineDate, "2026-12-31", "his deadline words, read"); assert.equal(goalNeed(end.active[1], 337), 75); // Each step on its own second. assert.equal(goalsAt(board, 2.5).headline.row, "g1"); assert.deepEqual(rows(goalsAt(board, 3.5)), ["g1", "g2"]); // The words: the headline's, with its day. assert.equal(boardGoalWords(end.headline), "GOAL 320 · BY APR 2026"); assert.equal(boardGoalWords(end.active[1]), "GOAL 262 · BY THE END OF 2026"); assert.equal(boardGoalWords(end.active[2]), "GOAL 240"); }); test("ties go to the soonest deadline, then the latest set; a goal with no weight ranks by its loss", () => { const a = { row: "a", t: 1, target: 300, deadlineDate: "2026-12-25" }; const b = { row: "b", t: 2, target: 300, deadlineDate: "2026-06-01" }; const c = { row: "c", t: 3, target: 300, deadlineDate: null }; assert.equal(headlineOf([a, b, c], 320).row, "b"); assert.equal(headlineOf([a, c], 320).row, "a", "undated last"); assert.equal(headlineOf([{ ...c, row: "c1", t: 1 }, { ...c, row: "c2", t: 2 }], 320).row, "c2", "then the latest set"); const unresolved = { row: "u", t: 1, target: null, loss: 15, deadlineDate: null }; assert.equal(headlineOf([a, unresolved], 320).row, "u", "15 to lose beats 20"); // Before any stated weight a goal's own loss is all there is; a weight already under reach ranks after any still ahead. assert.equal(goalNeed({ target: 250, loss: 50 }, null), 50); assert.equal(headlineOf([{ row: "met", t: 1, target: 330 }, { row: "ahead", t: 2, target: 300 }], 320).row, "ahead"); }); test("a newer goal of the same horizon replaces the older: the same day, the same words, or both undated", () => { const m = measureOf([ goal(1, "a", "2025-08-15", 275, { asOf: "2026-04-01" }), goal(2, "b", "2026-01-16", 320, { asOf: "2026-04-01" }), goal(3, "c", "2026-01-16", 240), goal(4, "d", "2026-01-17", 250), loss(5, "e", "2026-01-18", -80, 211, { deadline: "by my birthday" }), loss(6, "f", "2026-01-18", -90, 201, { deadline: "By my birthday" }), ]); assert.deepEqual(rows(goalsAt(goalBoard(m), 10)), ["b", "d", "f"]); }); test("a goal met in time leaves quietly; one past its day with his weight over it is MISSED, held, then gone", () => { const m = measureOf([ stated(1, "s1", "2026-01-09", 337), goal(2, "g1", "2026-01-16", 320, { asOf: "2026-04-01" }), goal(3, "g2", "2026-01-16", 330, { due: "2026-03-01" }), goal(4, "g3", "2026-01-16", 240), stated(5, "s2", "2026-02-10", 329), // meets g2 (330) by its day stated(10, "s3", "2026-05-02", 326), // past g1's day (Apr 2026), over 320: MISSED stated(30, "s4", "2026-06-01", 238.5, "<"), // meets the eventual 240 ]); const board = goalBoard(m); assert.deepEqual(rows(goalsAt(board, 6)), ["g1", "g3"], "g2 met"); assert.equal(goalsAt(board, 6).headline.row, "g1"); const miss = goalsAt(board, 10); assert.deepEqual(rows(miss), ["g3"]); assert.deepEqual(miss.missed.map((g) => [g.row, g.at, g.wasHeadline]), [["g1", 10, true]]); assert.equal(miss.headline.row, "g3"); assert.equal(goalsAt(board, 10 + MISS_HOLD - 0.01).missed.length, 1); assert.equal(goalsAt(board, 10 + MISS_HOLD).missed.length, 0, "held MISS_HOLD s, then gone"); assert.deepEqual(rows(goalsAt(board, 31)), [], "met"); assert.equal(goalsAt(board, 31).missed.length, 0, "a goal met is no miss"); // The line under the digits says the miss while it is held, then the next headline. const lines = goalLines(board); const lineAt = (t) => [...lines].reverse().find((l) => l.t <= t); assert.deepEqual(lineAt(11), { t: 10, words: "MISSED 320 · BY APR 2026", missed: true }); assert.equal(lineAt(17).words, "GOAL 240"); // A day passed with his latest weight UNDER the goal (a reading after the day): no miss, it leaves quietly. const late = goalBoard(measureOf([stated(1, "s1", "2026-01-09", 337), goal(2, "g1", "2026-01-16", 320, { asOf: "2026-04-01" }), stated(3, "s2", "2026-07-05", 300)])); assert.deepEqual(goalsAt(late, 4), { ...goalsAt(late, 4), active: [], missed: [] }); }); test("seek-safe: the board at t is a pure function of the measure, and the dial's marks read it from either side", () => { const m = measureOf([ stated(1, "s1", "2026-01-09", 337), goal(2, "g1", "2026-01-16", 320, { asOf: "2026-04-01" }), goal(3, "g2", "2026-01-16", 240), goal(4, "g3", "2026-01-16", 225, { range: [220, 230], due: "2027-01-01" }), stated(6, "s2", "2026-05-02", 326), ]); assert.deepEqual(goalBoard(m), goalBoard(structuredClone(m))); const board = goalBoard(m); const marks = goalMarks(board, SCALE); assert.deepEqual(marks.map((x) => x.goal.row), ["g1", "g2", "g3"]); assert.equal(marks[2].band, true); const statusAt = (x, t) => [...x.states].reverse().find((s) => s.t <= t)?.status; assert.deepEqual(marks.map((x) => statusAt(x, 3.5)), ["head", "other", "off"]); assert.deepEqual(marks.map((x) => statusAt(x, 6.5)), ["missed", "head", "other"]); assert.deepEqual(marks.map((x) => statusAt(x, 6 + MISS_HOLD + 1)), ["off", "head", "other"]); // On the dial's page: the marks' cues, which the frame keys model. const timeline = Array.from({ length: 2 }, (_, i) => ({ type: "clip", id: `c${i + 1}`, video: "v", start: 10 * i, end: 10 * i + 8, cite: 1, section: 0, quote: "q", onscreen: { title: "T" } })); const render = { width: 1920, height: 1080, fps: FPS, palette: {}, chrome: { engine: "hyperframes", layout: "deck", deck: { dial: true, qr: { show: false } } } }; const schedule = { ...estimateSchedule({ slug: "m", title: "M", provenance: {}, render, timeline }), measure: m }; const html = dialHtml(schedule, render, { fonts }); assert.match(html, /data-k="gm0h"/); assert.match(html, /class="goalw miss" data-k="gw\d+"[^>]*>MISSED 320 · BY APR 2026(.*?)<\/script>/s.exec(html)[1]); const at = (k, t) => { let v = data.init[k]?.autoAlpha; for (const c of data.cues) if (c.k === k && "autoAlpha" in c.to && c.at + c.dur <= t + 1e-9) v = c.to.autoAlpha; return v; }; assert.deepEqual([at("gm0h", 3.5), at("gm1o", 3.5), at("gm0x", 3.5)], [1, 0.85, 0]); assert.deepEqual([at("gm0x", 7), at("gm1h", 7), at("gm0p", 9)], [1, 1, 0]); assert.ok(data.cues.some((c) => c.k === "gm0p" && c.to.autoAlpha === 1), "the miss flashes"); const frames = Math.round(schedule.total * FPS); assert.ok(frameKeys({ html, region: "dial", fps: FPS, frames, version: "t" })); }); test("the shelf's goal fan reads the same board: the headline in front, +N more, a missed goal struck; a joke or another's goal is no note", async () => { const { goalStates } = await import("./measure-extras.mjs"); const { goalsHtml, shelfGoalStates } = await import("./chrome-measure-extras.mjs"); const m = { ...measureOf([ stated(1, "s1", "2026-01-09", 337), goal(2, "g1", "2026-01-16", 320, { asOf: "2026-04-01" }), goal(3, "g2", "2026-01-16", 240), goal(4, "j1", "2026-01-16", 150, { unresolved: true }), goal(5, "o1", "2026-01-16", 200, { speaker: "madamsavvy" }), stated(10, "s2", "2026-05-02", 326), ]), unit: "lb" }; const render = { width: 1920, height: 1080, fps: FPS, palette: { bg: "#0F1312", fg: "#EDF0EC", muted: "#98A29B", accent: "#22AB83", amber: "#E8A33F" }, chrome: { engine: "hyperframes", layout: "deck", deck: { measure: {}, dial: true, shelf: { items: ["goals"] }, goals: { position: "shelf" } } } }; const schedule = { kind: "deck", fps: FPS, total: 40, transition: 0, segments: [{ id: "a", start: 0, duration: 40, end: 40 }], measure: m }; const states = goalStates(schedule, render); assert.deepEqual(states.map((s) => s.at), [2, 3], "the joke and her goal are no notes"); const shelf = shelfGoalStates(schedule, states); // A state per goal set, and one where the board alone changes: g1 missed at 10, its hold's end. assert.deepEqual(shelf.map((s) => s.at), [2, 3, 10, 10 + MISS_HOLD]); const front = (s) => s.notes.find((n) => n.depth === 0).key; // At 3 the newest is 240, but the CLOSEST leads: 320, with one more in force. assert.deepEqual([front(shelf[1]), shelf[1].headline, shelf[1].active], ["320 lb", "320 lb", 2]); assert.deepEqual([shelf[2].top, shelf[2].missed, front(shelf[2])], [null, ["320 lb"], "240 lb"]); assert.deepEqual(shelf[3].missed, []); const { html } = goalsHtml(schedule, render, {}); assert.match(html, /\+1 more/); assert.match(html, /class="note missed"/); });