// `umtool ledger` and `umtool measure` (weight video rev 9): a measure // ledger's rows written only through its writers, checked against the // schema, and the slice printed off a project made here. Run with: // pnpm test:scripts import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-ledger-cli-")); process.env.REPORTS_DIR = path.join(ROOT, "reports"); const { ledgerCommand } = await import("./ledgerCli.mjs"); const { measureCommand } = await import("./measureCli.mjs"); const { ledgerRowOf, ledgerRowReferences, parseLedgerValue } = await import("./ledgerRows.mjs"); test.after(() => rm(ROOT, { recursive: true, force: true })); 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", "off"] }, { key: "drinking", role: "regime", label: "Drinking", regimes: ["dry", "drinking"] }, ], forms: ["current", "past", "delta", "goal", "goal_loss", "overweight_by", "status"], regimes: [{ key: "keto", label: "Keto" }, { key: "off", label: "No diet" }, { key: "dry", label: "Dry" }, { key: "drinking", label: "Drinking" }], fields: [ { key: "qualifier", label: "Bound", kind: "enum", options: [">", "<", "≈"], appliesTo: { scope: "weight" }, render: "cell" }, { key: "citeUrl", label: "Source link", kind: "text", maxLength: 200 }, ], }; const base = (o) => ({ scope: "weight", scopeBasis: "his words", scopeConfidence: "read", valueKind: "uttered", flags: [], asOf: null, ...o }); const MANIFEST = { schemaVersion: 1, slug: "weigh", provenance: { siteOrigin: "https://example.test", channelSlug: "chan", channelsDir: "nowhere" }, render: { fps: 30, transition: 0 }, ledgerSchema: SCHEMA, timeline: [ { type: "clip", id: "c1", video: "v1", start: 10, end: 30, quoteStart: 12, claim: "w01" }, { type: "clip", id: "c2", video: "v2", start: 0, end: 20, quoteStart: 5, claim: "w02" }, ], ledger: [ base({ id: "w01", date: "2026-09-15", population: "delta", value: -5, display: "-5", entryId: "c1" }), base({ id: "w02", date: "2026-10-26", population: "current", value: 291.5, display: "291.5 lb", entryId: "c2" }), base({ id: "w03", date: "2026-10-31", population: "delta", value: -60, display: "-60", qualifier: "≈" }), base({ id: "w04", date: "2023-03-02", population: "delta", value: -64, display: "-64" }), base({ id: "w05", date: "2008-06-30", population: "past", asOf: "2008-06-30", value: 368, display: "368 lb" }), base({ id: "d01", date: "2026-10-01", scope: "drinking", population: "status", value: "dry", event: "start", display: "dry" }), ], }; async function project() { const dir = path.join(ROOT, "reports", "weigh"); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(MANIFEST, null, 2)); return dir; } const lines = []; const run = (cmd, ...args) => cmd(args, { root: path.join(ROOT, "reports"), out: (s) => lines.push(s), err: (s) => lines.push(`ERR ${s}`) }); const manifestOf = async (dir) => JSON.parse(await readFile(path.join(dir, "video.manifest.json"), "utf8")); test("a row is checked against the schema, and every problem is named", () => { assert.throws(() => ledgerRowOf({ ...MANIFEST, ledgerSchema: undefined }, null, {}), /no measure ledger/); assert.throws( () => ledgerRowOf(MANIFEST, null, { id: "w01", date: "soon", scope: "gym", population: "guess", nonsense: 1 }), /"nonsense" is not a ledger key/, ); let e; try { ledgerRowOf(MANIFEST, null, { id: "w01", date: "soon", scope: "gym", population: "guess" }); } catch (err) { e = String(err.message); } for (const want of ["already has a row w01", "date must be", "scope must be one of the ledger's tracks: weight, regime, drinking", "population must be one of", "scopeBasis must quote"]) { assert.ok(e.includes(want), `${want} in: ${e}`); } assert.throws(() => ledgerRowOf(MANIFEST, null, base({ id: "r9", date: "2026-01-01", scope: "regime", population: "status", value: "dry" })), /value on the regime track must be one of its regimes: keto, off; event must be/); assert.throws(() => ledgerRowOf(MANIFEST, null, base({ id: "w9", date: "2026-01-01", population: "current", value: "heavy" })), /value must be a number/); assert.throws(() => ledgerRowOf(MANIFEST, null, base({ id: "w9", date: "2026-01-01", population: "current", value: 300, run: 2 })), /run is a loss claim's/); assert.throws(() => ledgerRowOf(MANIFEST, null, base({ id: "w9", date: "2026-01-01", population: "delta", value: -3, run: 1.5 })), /run must be a whole number/); assert.throws(() => ledgerRowOf(MANIFEST, null, base({ id: "p9", date: "2026-01-01", population: "promise_check", promise: "w01" })), /promise must name a promise row \(w01 is a delta row\)/); assert.throws(() => ledgerRowOf(MANIFEST, null, base({ id: "w9", date: "2026-01-01", population: "current", value: 300, qualifier: "?" })), /qualifier/); // A sound one: the defaults for three of the six, its display from its value. const ok = ledgerRowOf(MANIFEST, null, { id: "w9", date: "2026-11-03", scope: "weight", population: "delta", value: -58.2, scopeBasis: "his words" }); assert.deepEqual([ok.scopeConfidence, ok.valueKind, ok.flags, ok.display], ["read", "uttered", [], "-58.2 lb"]); assert.equal(parseLedgerValue("value", "-58.2"), -58.2); assert.equal(parseLedgerValue("retro", "true"), true); assert.deepEqual(parseLedgerValue("flags", "a|b"), ["a", "b"]); assert.deepEqual(ledgerRowReferences(MANIFEST, "w01"), ["timeline entry c1 pins it (claim)"]); }); test("umtool ledger: add, set, unset, rm -- through the writers, on the file", async () => { const dir = await project(); lines.length = 0; assert.equal(await run(ledgerCommand, "weigh", "ls", "--scope", "weight"), 0); assert.match(lines.at(-1), /5 of 6 row\(s\)/); // The promise, and a later word about it. assert.equal(await run(ledgerCommand, "weigh", "add", "p01", "--date", "2026-10-23", "--scope", "weight", "--population", "promise", "--display", "post a video of the scale", "--scopeBasis", "his own post", "--citeUrl", "https://x.com/a/status/1", "--json"), 0, lines.join("\n")); assert.equal(JSON.parse(lines.at(-1)).added.citeUrl, "https://x.com/a/status/1"); assert.equal(await run(ledgerCommand, "weigh", "add", "p02", "--date", "2026-11-03", "--scope", "weight", "--population", "promise_check", "--promise", "p01", "--display", "pull up a video from three months ago", "--scopeBasis", "the scale video"), 0, lines.join("\n")); // Another party's word about it: a promise_claim, with her key and the day she says she paid. assert.equal(await run(ledgerCommand, "weigh", "add", "s01", "--date", "2026-10-24", "--scope", "weight", "--population", "promise_claim", "--promise", "p01", "--speaker", "madamsavvy", "--paidOn", "2026-10-24", "--display", "bought the 5 bags", "--scopeBasis", "her post", "--citeUrl", "https://x.com/MadamSavvy/status/2"), 0, lines.join("\n")); assert.equal(await run(ledgerCommand, "weigh", "add", "s02", "--date", "2026-10-24", "--scope", "weight", "--population", "promise_check", "--promise", "p01", "--speaker", "madamsavvy", "--scopeBasis", "her post"), 1); assert.match(lines.at(-1), /a promise_check is the subject's own word: madamsavvy's is a promise_claim/); assert.equal(await run(ledgerCommand, "weigh", "add", "s02", "--date", "2026-10-24", "--scope", "weight", "--population", "current", "--value", "250", "--paidOn", "2026-10-24", "--speaker", "Madam Savvy", "--scopeBasis", "her post"), 1); assert.match(lines.at(-1), /speaker must be a speaker's key.*paidOn is a promise_claim's|paidOn is a promise_claim's/); // How it ended: a promise_outcome, refunded by her account. assert.equal(await run(ledgerCommand, "weigh", "add", "o01", "--date", "2026-10-31", "--scope", "weight", "--population", "promise_outcome", "--promise", "p01", "--outcome", "refunded", "--speaker", "madamsavvy", "--label", "refunded by 31 Oct", "--scopeBasis", "her video"), 0, lines.join("\n")); assert.equal(await run(ledgerCommand, "weigh", "add", "o02", "--date", "2026-10-31", "--scope", "weight", "--population", "promise_outcome", "--promise", "p01", "--outcome", "forgotten", "--scopeBasis", "x"), 1); assert.match(lines.at(-1), /outcome kept, refunded, withdrawn/); // The words under its stamp (`stampNote`): a promise_outcome's, one line; null takes them off again. assert.equal(await run(ledgerCommand, "weigh", "set", "o01", "--stampNote", "by her account · no receipt"), 0, lines.join("\n")); assert.equal((await manifestOf(dir)).ledger.find((r) => r.id === "o01").stampNote, "by her account · no receipt"); assert.equal(await run(ledgerCommand, "weigh", "set", "o01", "--stampNote", "x".repeat(81)), 1); assert.match(lines.at(-1), /stampNote is one line of 1–80 characters/); assert.equal(await run(ledgerCommand, "weigh", "set", "p01", "--stampNote", "nope"), 1); assert.match(lines.at(-1), /stampNote is a promise_outcome's/); assert.equal(await run(ledgerCommand, "weigh", "set", "o01", "--stampNote", "null"), 0, lines.join("\n")); assert.equal((await manifestOf(dir)).ledger.find((r) => r.id === "o01").stampNote ?? null, null); assert.equal(await run(ledgerCommand, "weigh", "rm", "o01"), 0); assert.equal(await run(ledgerCommand, "weigh", "rm", "s01"), 0); // A loss told in the past: off the odometer; then put back on it. assert.equal(await run(ledgerCommand, "weigh", "set", "w04", "--retro", "true"), 0, lines.join("\n")); let m = await manifestOf(dir); assert.equal(m.ledger.find((r) => r.id === "w04").retro, true); assert.equal(await run(ledgerCommand, "weigh", "set", "w04", "--unset", "retro"), 0); m = await manifestOf(dir); assert.equal("retro" in m.ledger.find((r) => r.id === "w04"), false); // Refused, by name, and nothing written. const before = await readFile(path.join(dir, "video.manifest.json"), "utf8"); assert.equal(await run(ledgerCommand, "weigh", "set", "w02", "--run", "3"), 1); assert.match(lines.at(-1), /ERR umtool ledger: run is a loss claim's/); assert.equal(await run(ledgerCommand, "weigh", "rm", "p01"), 1); assert.match(lines.at(-1), /p01 is still named: row p02 names it/); assert.equal(await run(ledgerCommand, "weigh", "rm", "w01"), 1); assert.match(lines.at(-1), /timeline entry c1 pins it/); assert.equal(await readFile(path.join(dir, "video.manifest.json"), "utf8"), before); assert.equal(await run(ledgerCommand, "weigh", "rm", "p02"), 0); m = await manifestOf(dir); assert.equal(m.ledger.some((r) => r.id === "p02"), false); }); test("umtool measure: the slice, scoped, with the odometer by his words", async () => { await project(); assert.equal(await run(ledgerCommand, "weigh", "set", "w04", "--retro", "true"), 0); assert.equal(await run(ledgerCommand, "weigh", "add", "w06", "--date", "2026-11-03", "--scope", "weight", "--population", "delta", "--value", "-58.2", "--scopeBasis", "his words"), 0); lines.length = 0; assert.equal(await run(measureCommand, "weigh", "--scopes", "weight,regime", "--as-of", "2026-11-05", "--json"), 0, lines.join("\n")); const sl = JSON.parse(lines.join("\n")); assert.deepEqual(sl.scopes, ["weight", "regime"]); assert.equal(sl.asOfRender, "2026-11-05"); assert.deepEqual(sl.events.map((e) => [e.row, e.tFrom]), [["w01", "quote"], ["w02", "quote"]]); assert.ok(![...sl.events, ...sl.hidden].some((e) => e.row === "d01"), "drinking is out of scope"); // One run: 5, then 60, then 58.2 -- its peak stays 60; the retro 64 is not counted. assert.equal(sl.totals.runs, 1); assert.equal(sl.totals.lost, 60); assert.deepEqual(sl.totals.net, { from: 368, to: 291.5, value: -76.5, fromRow: "w05", toRow: "w02" }); lines.length = 0; assert.equal(await run(measureCommand, "weigh", "--scopes", "weight,regime"), 0); assert.match(lines.join("\n"), /lost \(his words\): 60 lb across 1 run\(s\)/); assert.equal(await run(measureCommand, "weigh", "--scopes", "gym"), 1); assert.match(lines.at(-1), /"gym" is not a track/); });