// The analysis item's plan (analysis-plan.mjs): every figure from a ledger row, deltas against the run's start, bounds // never exact (in words, in the right direction on both sides of a pace row), typed numbers said, and the refusals // the lint names. import { test } from "node:test"; import assert from "node:assert/strict"; import { analysisFigures, analysisPlan, analysisProblems, analysisRowRefs, analysisSeconds, dayMonth, paceBound, paceWords, spanWords, typedNumbers, } from "./analysis-plan.mjs"; import { validateClipAudioEntries } from "./clip-audio.mjs"; // The first use's shape (quartering-diet, 12 Nov): ids, dates and values only -- no quotes. const SCHEMA = { kind: "measure", unit: "lb", bandGapDays: 60, forms: ["current", "past", "delta", "goal", "goal_loss"], tracks: [{ key: "weight", role: "value", label: "Weight" }], fields: [{ key: "qualifier", label: "Bound", kind: "enum", options: [">", "<", "≈"], render: "cell" }], }; const W = (id, population, date, value, extra = {}) => ({ id, scope: "weight", population, date, value, ...extra }); const LEDGER = [ W("w81", "delta", "2026-07-21", -40, { retro: true }), W("w83", "delta", "2026-07-23", -5), W("w87", "current", "2026-07-31", 338), W("w91", "past", "2026-08-03", 349, { asOf: "2026-07-19" }), W("w93", "current", "2026-08-03", 335.5), W("w104", "delta", "2026-09-04", -31.5), W("w110", "current", "2026-09-24", 310.5), W("w115", "current", "2026-10-15", 300, { qualifier: "<" }), W("w117", "current", "2026-10-23", 291), W("w118", "current", "2026-10-26", 291.5, { asOf: "2026-10-23" }), W("w122", "delta", "2026-11-03", -58.2), W("w137", "current", "2026-11-07", 288.5), W("x1", "current", "2026-10-01", 250, { speaker: "madamsavvy" }), { id: "p01", scope: "promise", population: "promise", date: "2026-10-23" }, { id: "s01", scope: "promise", population: "promise_claim", date: "2026-10-23", speaker: "madamsavvy" }, ]; const TIMELINE = [ { type: "clip", id: "c46", video: "v1", start: 4111.4, end: 4158.8, cutStart: 4112.6, cutEnd: 4158.5, gaps: [{ from: 4127.15, to: 4132.45 }] }, { type: "card", id: "k1", seconds: 4 }, ]; const CTX = { ledger: LEDGER, schema: SCHEMA, timeline: TIMELINE, render: { width: 1920, height: 1080, fps: 30 } }; const ITEM = { type: "analysis", id: "an1", title: "What his own numbers say", kicker: "THE RUN", series: { rows: ["w91", "w87", "w93", "w104", "w110", "w115", "w118", "w122", "w137"] }, band: { from: "2026-10-23", to: "2026-11-03", label: "WORLD SERIES" }, marks: [{ row: "w118", label: "offers the scale video" }, { row: "w115", label: "" }], pace: [{ from: "2026-09-24", to: "2026-10-15" }, { from: "2026-10-15", to: "2026-10-23" }, { from: "2026-10-23", to: "2026-11-03", note: "the World Series" }, { from: "w122", to: "w137" }], status: [{ label: "SCALE VIDEO", value: "NOT POSTED", note: "offered 23 Oct", color: "red", row: "p01" }], reading: "By his own numbers the loss is steady.", footnote: "All figures are his own statements.", mark: { row: "w115" }, }; const errorsOf = (e, ctx = CTX) => analysisProblems(e, ctx).errors; const warningsOf = (e, ctx = CTX) => analysisProblems(e, ctx).warnings; test("every point is its row's weight at its plot date; a delta is the run's start plus it, naming the start", () => { const plan = analysisPlan(ITEM, CTX); const by = Object.fromEntries(plan.points.map((p) => [p.row, p])); assert.equal(by.w91.value, 349); assert.equal(by.w91.date, "2026-07-19"); assert.equal(by.w118.date, "2026-10-23"); assert.equal(by.w104.value, 317.5); assert.deepEqual(by.w104.base, { row: "w91", value: 349 }); assert.equal(by.w122.value, 290.8); assert.equal(by.w115.bound, true); assert.equal(plan.points[plan.line[0]].row, "w91"); assert.equal(plan.points[plan.line.at(-1)].row, "w137"); // The dashed line is a row's: w115's value, labelled with it. assert.equal(plan.layout.mark, 300); assert.equal(plan.layout.markLabel, "300"); assert.equal(plan.layout.markRow, "w115"); }); test("pace rows: pounds and days computed, never typed; a date names the one point that day", () => { const { pace } = analysisPlan(ITEM, CTX); assert.deepEqual(pace.map((x) => [x.from.row, x.to.row, x.lb, x.days]), [ ["w110", "w115", -10.5, 21], ["w115", "w118", -8.5, 8], ["w118", "w122", -0.7, 11], ["w122", "w137", -2.3, 4], ]); assert.equal(pace[0].span, "24 Sep → 15 Oct"); assert.equal(pace[1].span, "15 → 23 Oct"); assert.equal(pace[2].text, "−0.7 lb"); assert.equal(pace[2].bound, null); }); test("a bound end is said in words, unsigned, the right way: ending on < 300 MORE THAN n lost, starting on it AT MOST n lost", () => { const { pace } = analysisPlan(ITEM, CTX); assert.equal(pace[0].text, "MORE THAN 10.5 lb lost"); assert.deepEqual(pace[0].boundRows, ["w115"]); assert.equal(pace[1].text, "AT MOST 8.5 lb lost"); // Every case: the computed change and the direction the truth lies in. assert.equal(paceWords(-10.5, "lt").text, "MORE THAN 10.5 lb lost"); assert.equal(paceWords(-8.5, "gt").text, "AT MOST 8.5 lb lost"); assert.equal(paceWords(4, "gt").text, "MORE THAN 4 lb gained"); assert.equal(paceWords(4, "lt").text, "AT MOST 4 lb gained"); assert.equal(paceWords(-3, "unknown").text, "—"); assert.equal(paceWords(-9, null, true).text, "about −9 lb"); assert.equal(paceWords(-0.7, null).text, "−0.7 lb"); // The ends' glyphs to a direction: "<" at the end, ">" at the start -> less than computed; opposite bounds -> none. assert.equal(paceBound(null, "<"), "lt"); assert.equal(paceBound(">", null), "lt"); assert.equal(paceBound("<", null), "gt"); assert.equal(paceBound(null, ">"), "gt"); assert.equal(paceBound("<", ">"), "gt"); assert.equal(paceBound(">", "<"), "lt"); assert.equal(paceBound("<", "<"), "unknown"); assert.equal(paceBound(">", ">"), "unknown"); const w = warningsOf(ITEM); assert.ok(w.some((x) => /pace\[0\] ends on a bound \(w115 < 300\) -- drawn as MORE THAN 10\.5 lb lost/.test(x)), w.join("\n")); assert.ok(w.some((x) => /pace\[1\] ends on a bound \(w115 < 300\) -- drawn as AT MOST 8\.5 lb lost/.test(x)), w.join("\n")); assert.ok(!w.some((x) => /\bover\b|\bunder\b/i.test(x)), w.join("\n")); }); test("an approximate end: 'about'", () => { const ledger = [...LEDGER, W("ap", "current", "2026-10-01", 301.5, { qualifier: "≈" })]; const e = { ...ITEM, series: { rows: ["w110", "ap"] }, pace: [{ from: "w110", to: "ap" }], marks: [], band: null, mark: null }; const plan = analysisPlan(e, { ...CTX, ledger }); assert.equal(plan.pace[0].text, "about −9 lb"); assert.ok(analysisFigures(plan).some((l) => l === "pace[0] 24 Sep → 1 Oct: about −9 lb (w110 310.5 → ap ≈301.5), 7 days"), analysisFigures(plan).join("\n")); }); test("--dry-run's figures name every row, a bound with its glyph", () => { const lines = analysisFigures(analysisPlan(ITEM, CTX)); assert.ok(lines.includes("point w104: 317.5 on 2026-09-04 (-31.5 from w91 349)"), lines.join("\n")); assert.ok(lines.includes("point w115: <300 on 2026-10-15 -- a bound, drawn hollow")); assert.ok(lines.includes("pace[0] 24 Sep → 15 Oct: MORE THAN 10.5 lb lost (w110 310.5 → w115 < 300), 21 days -- a bound: w115"), lines.join("\n")); assert.ok(lines.includes("pace[1] 15 → 23 Oct: AT MOST 8.5 lb lost (w115 < 300 → w118 291.5), 8 days -- a bound: w115")); assert.ok(lines.includes("line 300 (w115), dashed")); assert.ok(lines.includes("marks[0] w118: 291.5 · 23 Oct · offers the scale video")); assert.ok(lines.includes("marks[1] w115: < 300 · 15 Oct")); assert.ok(lines.includes("silent, 7 s")); }); test("typed numbers: dates taken out, every other digit said, naming the field; a row backs only its own figure", () => { assert.deepEqual(typedNumbers("offered 23 Oct, Oct 2026 and 2026-10-23").numbers, []); assert.deepEqual(typedNumbers("In 2026 he lost 75 lb").numbers, ["2026", "75"]); assert.deepEqual(typedNumbers("Oct 2026 · 75 LB").numbers, ["75"]); assert.deepEqual(typedNumbers("on Oct 23, 2026 he said 61").numbers, ["61"]); const w = warningsOf({ ...ITEM, title: "He lost 61 lb", kicker: "61 LB DOWN", footnote: "Down 61 lb since July", reading: "In 2026 he lost 75 lb.", band: { ...ITEM.band, label: "2026 · 75 LB" }, marks: [{ row: "w115", label: "under 300" }, { row: "w118", label: "291.5 on the scale" }, { row: "w137", label: "lost 70" }], pace: [{ from: "w110", to: "w137", note: "3 weeks" }], status: [{ label: "S", value: "5 days", note: "x" }, { label: "P", value: "SAID", note: "offered 24 Oct", row: "p01" }], }); const has = (re) => assert.ok(w.some((x) => re.test(x)), `${re}\n${w.join("\n")}`); has(/title "He lost 61 lb" has "61" typed/); has(/kicker "61 LB DOWN" has "61" typed/); has(/footnote "Down 61 lb since July" has "61" typed/); has(/reading "In 2026 he lost 75 lb\." has "2026", "75" typed/); has(/band\.label "2026 · 75 LB" has "2026", "75" typed/); has(/marks\[2\]\.label "lost 70" has "70" typed -- not w137's figure/); has(/pace\[0\]\.note "3 weeks" has "3" typed/); has(/status\[0\]\.value "5 days" has "5" typed/); has(/status\[1\]\.note "offered 24 Oct" says "24 Oct" -- not p01's date \(23 Oct\)/); // A mark's label that says its own row's figure is backed: "under 300" is w115's, "291.5" w118's. assert.ok(!w.some((x) => /marks\[0\]|marks\[1\]/.test(x)), w.join("\n")); // The first use's words: nothing. assert.ok(!warningsOf(ITEM).some((x) => /typed|says/.test(x)), warningsOf(ITEM).join("\n")); }); test("refusals, each naming the row", () => { const has = (e, re, ctx) => assert.ok(errorsOf(e, ctx).some((x) => re.test(x)), `${re}\n${errorsOf(e, ctx).join("\n")}`); has({ ...ITEM, series: { rows: ["w91", "w999"] } }, /series row w999 is not in the ledger/); has({ ...ITEM, series: { rows: ["w91", "p01"] } }, /series row p01 is not a weight/); has({ ...ITEM, series: { rows: ["w91", "x1"] } }, /series row x1 is madamsavvy's/); has({ ...ITEM, band: { from: "2026-07-01", to: "2026-08-01", label: "" } }, /band 2026-07-01 → 2026-08-01 is outside the series \(2026-07-19 → 2026-11-07\)/); has({ ...ITEM, pace: [{ from: "2026-09-24" }] }, /pace\[0\]\.to is missing/); has({ ...ITEM, pace: [{ from: "2026-09-25", to: "w137" }] }, /pace\[0\]\.from 2026-09-25: no point of the series on that day/); has({ ...ITEM, pace: [{ from: "w137", to: "w110" }] }, /pace\[0\] ends \(2026-09-24\) on or before it starts/); has({ ...ITEM, marks: [{ row: "w117" }] }, /marks\[0\]\.row w117 is not a point of the series/); has({ ...ITEM, reading: " " }, /reading is empty/); has({ ...ITEM, status: [{ label: "X", value: "Y", row: "zz" }] }, /status\[0\]\.row zz is not in the ledger/); has({ ...ITEM, status: [{ label: "X", value: "Y", row: "s01" }] }, /status\[0\]\.row s01 is madamsavvy's, not his/); has({ ...ITEM, status: [{ label: "X", value: "Y", color: "pink" }] }, /status\[0\]\.color "pink"/); has({ ...ITEM, bogus: 1 }, /unknown key "bogus"/); has({ ...ITEM, mark: 300 }, /mark is \{ "row": "" \} -- the dashed line at that row's value \(a bare number is not backed by a row\)/); has({ ...ITEM, mark: { row: "p01" } }, /mark\.row p01 has no weight to draw a line at/); has({ ...ITEM, mark: { row: "x1" } }, /mark\.row x1 is madamsavvy's/); // Two points for one typed day: name one by id. has({ ...ITEM, series: { rows: [...ITEM.series.rows, "w117"] } }, /pace\[1\]\.to 2026-10-23: w117, w118 are all on that day -- name one by id/); // A delta with no run start in the record to resolve it against. const lone = { ...CTX, ledger: [W("d1", "delta", "2020-01-05", -3), W("s1", "current", "2020-01-01", 200)] }; const bare = { ...ITEM, band: null, pace: [], marks: [], status: [], mark: null }; has({ ...bare, series: { rows: ["s1", "d1"] } }, /series row d1 is a delta \(-3\) with no run start/, lone); // ... in a span: one sentence naming them all and the rows to name instead. has({ ...bare, series: { from: "2020-01-01", to: "2020-01-31" } }, /series \{from, to\} takes d1 \(-3\) -- deltas with no run start .*"series": \{ "rows": \["s1"\] \}/, lone); assert.throws(() => analysisPlan({ ...ITEM, series: { rows: ["w999"] } }, CTX), /w999 is not in the ledger/); }); test("audio: a clip of the cut, inside what its cut keeps -- never a gap, never past its cut, at most 30 s", () => { const has = (e, re) => assert.ok(errorsOf(e).some((x) => re.test(x)), `${re}\n${errorsOf(e).join("\n")}`); has({ ...ITEM, audio: { clip: "c99", start: 1, end: 9 } }, /audio\.clip c99 is not an entry of the cut/); has({ ...ITEM, audio: { clip: "k1", start: 1, end: 9 } }, /audio\.clip k1 is a card entry, not a clip/); has({ ...ITEM, audio: { clip: "c46", start: 4111.4, end: 4116 } }, /audio 4111\.4–4116 is outside c46's cut 4112\.6–4158\.5 -- it plays only words the cut keeps/); has({ ...ITEM, audio: { clip: "c46", start: 4120, end: 4130 } }, /audio 4120–4130 runs into c46's gap 4127\.15–4132\.45/); has({ ...ITEM, audio: { clip: "c46", start: 4127.2, end: 4132.4 } }, /runs into c46's gap/); has({ ...ITEM, audio: { clip: "c46", start: 4113, end: 4158 } }, /audio is 45 s -- at most 30/); const ok = { ...ITEM, audio: { clip: "c46", start: 4112.6, end: 4127.1 } }; assert.deepEqual(errorsOf(ok), []); assert.equal(analysisSeconds(ok), 14.5); assert.ok(warningsOf(ok).some((w) => /the audio's span plays in the cut too \(c46 plays 4112\.6–4127\.1\)/.test(w))); // The build's own clip-audio check leaves an analysis item's `audio` to this one. assert.deepEqual(validateClipAudioEntries({ timeline: [...TIMELINE, ok] }), []); }); test("length: silent 7 s by default, at most 8 without audio; no vertical form", () => { assert.equal(analysisSeconds(ITEM), 7); assert.deepEqual(errorsOf({ ...ITEM, seconds: 8 }), []); assert.ok(errorsOf({ ...ITEM, seconds: 9 }).some((e) => /held 9 s with no audio \(at most 8/.test(e))); assert.ok(errorsOf({ ...ITEM, seconds: 2 }).some((e) => /too short to read/.test(e))); assert.ok(errorsOf(ITEM, { ...CTX, render: { width: 1080, height: 1920 } }).some((e) => /no vertical form/.test(e))); assert.ok(errorsOf(ITEM, { ...CTX, render: { width: 1920, height: 1080, format: "vertical" } }).some((e) => /no vertical form/.test(e))); }); test("the frame's room: words past their length and rows past the panel are refused", () => { const has = (e, re) => assert.ok(errorsOf(e).some((x) => re.test(x)), `${re}\n${errorsOf(e).join("\n")}`); has({ ...ITEM, title: "W".repeat(80) }, /title is 80 characters -- at most 44 fit the frame/); has({ ...ITEM, reading: "word ".repeat(60) }, /reading \(two lines\) is 299 characters -- at most 185/); has({ ...ITEM, reading: "word ".repeat(34), audio: { clip: "c46", start: 4112.6, end: 4127.1 } }, /reading \(two lines, beside the QR\) is 169 characters -- at most 160/); has({ ...ITEM, status: [...ITEM.status, { label: "WEIGH-IN", value: "NONE", color: "amber" }] }, /6 pace and status rows -- the side panel holds 5/); }); test("the rows an item names, for `umtool ledger rm`", () => { assert.deepEqual(analysisRowRefs(ITEM).sort(), ["p01", "w104", "w110", "w115", "w118", "w122", "w137", "w87", "w91", "w93"].sort()); }); test("words: a day and a span", () => { assert.equal(dayMonth("2026-10-23"), "23 Oct"); assert.equal(spanWords("2026-10-23", "2026-11-03"), "23 Oct → 3 Nov"); assert.equal(spanWords("2026-11-03", "2026-11-07"), "3 → 7 Nov"); }); test("axes: one cluster fitted to the points (trailLayout), a tick per month, the mark", () => { const plan = analysisPlan(ITEM, CTX, { x0: 0, x1: 1000, y0: 0, y1: 400 }); assert.equal(plan.layout.lo, 280); assert.equal(plan.layout.hi, 360); assert.deepEqual(plan.layout.months.map((m) => m.text), ["JUL", "AUG", "SEP", "OCT", "NOV"]); assert.ok(plan.band.x1 > plan.band.x0); // The date axis is by days alone (trailLayout dayShare 1), whatever share the by-date trail gives to its weights: // equal days, equal widths. const per = (a, b) => (plan.layout.X(b) - plan.layout.X(a)) / (b - a); const d = (iso) => Date.parse(`${iso}T00:00:00Z`) / 864e5; assert.ok(Math.abs(per(d("2026-07-19"), d("2026-08-03")) - per(d("2026-10-23"), d("2026-11-07"))) < 0.02); });