// `umtool timeline
[--take ] add-analysis | set-analysis` (timelineCli.mjs analysisVerb): an analysis item into
// the draft or a take's timeline only, checked against the ledger as the build checks it, every write an undo point;
// --dry-run prints every figure with its row and writes nothing.
//
// Run with: node --test lib/report/analysisCli.test.mjs
import assert from "node:assert/strict";
import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import test from "node:test";
import { draftTake } from "./takeMaker.mjs";
import { resolveTakeManifest } from "./takeManifest.mjs";
import { timelineCommand } from "./timelineCli.mjs";
import { ledgerCommand } from "./ledgerCli.mjs";
const W = (id, population, date, value, extra = {}) => ({ id, scope: "weight", population, date, value, ...extra });
const MANIFEST = {
schemaVersion: 1,
slug: "fixture",
title: "Fixture",
render: { width: 640, height: 360, fps: 15, transition: 0, fontRegular: "/fonts/Regular.ttf", fontBold: "/fonts/Bold.ttf" },
ledgerSchema: {
kind: "measure", unit: "lb", bandGapDays: 60, tracks: [{ key: "weight", role: "value", label: "Weight" }],
fields: [{ key: "qualifier", label: "Bound", kind: "enum", options: [">", "<", "≈"], render: "cell" }],
},
ledger: [
W("w83", "delta", "2026-07-23", -5),
W("w91", "past", "2026-08-03", 349, { asOf: "2026-07-19" }),
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("w118", "current", "2026-10-26", 291.5, { asOf: "2026-10-23" }),
],
timeline: [
{ type: "clip", id: "c01", channel: "chan", video: "vid", start: 100, end: 160, cite: 100 },
{ type: "clip", id: "c02", channel: "chan", video: "vid", start: 200, end: 260, cite: 200 },
],
};
const ITEM = {
title: "What his numbers say", kicker: "THE RUN",
series: { rows: ["w91", "w104", "w110", "w115", "w118"] },
pace: [{ from: "w110", to: "w115" }, { from: "w115", to: "w118" }],
reading: "The loss is steady.",
};
async function project() {
const root = await mkdtemp(path.join(tmpdir(), "analysis-cli-"));
const reportsRoot = path.join(root, "reports");
const dir = path.join(reportsRoot, "proj");
await mkdir(dir, { recursive: true });
await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(MANIFEST, null, 2) + "\n");
await writeFile(path.join(root, "item.json"), JSON.stringify(ITEM));
const out = [];
const err = [];
const run = (...args) => timelineCommand(args, { root: reportsRoot, out: (s) => out.push(s), err: (s) => err.push(s) });
return { root, dir, run, out, err, file: `@${path.join(root, "item.json")}` };
}
const manifestOf = async (dir) => JSON.parse(await readFile(path.join(dir, "video.manifest.json"), "utf8"));
test("add-analysis --dry-run prints every figure with its row and writes nothing", async () => {
const p = await project();
try {
const before = await readFile(path.join(p.dir, "video.manifest.json"), "utf8");
assert.equal(await p.run("proj", "add-analysis", p.file, "--after", "c01", "--dry-run"), 0, p.err.join("\n"));
const text = p.out.join("\n");
assert.match(text, /would add analysis analysis at 1 \(after c01\) in proj:/);
assert.match(text, /point w104: 317\.5 on 2026-09-04 \(-31\.5 from w91 349\)/);
assert.match(text, /pace\[0\] 24 Sep → 15 Oct: MORE THAN 10\.5 lb lost \(w110 310\.5 → w115 < 300\), 21 days -- a bound: w115/);
assert.match(text, /pace\[1\] 15 → 23 Oct: AT MOST 8\.5 lb lost/);
assert.match(text, /warning: .*pace\[0\] ends on a bound/);
assert.equal(await readFile(path.join(p.dir, "video.manifest.json"), "utf8"), before);
} finally {
await rm(p.root, { recursive: true, force: true });
}
});
test("add-analysis and set-analysis on the draft: written, an undo point each, refused for a row the ledger lacks", async () => {
const p = await project();
try {
assert.equal(await p.run("proj", "add-analysis", p.file, "--id", "end", "--json"), 0, p.err.join("\n"));
const j = JSON.parse(p.out.join("\n"));
assert.equal(j.inserted.id, "end");
assert.equal(j.inserted.at, 2);
let m = await manifestOf(p.dir);
assert.deepEqual(m.timeline.map((e) => e.id), ["c01", "c02", "end"]);
assert.equal(m.timeline[2].type, "analysis");
const snaps = await readdir(path.join(p.dir, "revisions")).catch(() => []);
assert.ok(snaps.some((f) => /auto-before-insert/.test(f)), snaps.join(", "));
p.out.length = 0;
assert.equal(await p.run("proj", "set-analysis", "end", JSON.stringify({ footnote: "His own words.", kicker: null })), 0, p.err.join("\n"));
assert.match(p.out.join("\n"), /set analysis end at 2 in proj/);
m = await manifestOf(p.dir);
assert.equal(m.timeline[2].footnote, "His own words.");
assert.equal("kicker" in m.timeline[2], false);
assert.ok((await readdir(path.join(p.dir, "revisions"))).some((f) => /auto-before-analysis/.test(f)));
// Refusals are exit 1 (cli.md): an id already in the cut, a patch that changes the id.
assert.equal(await p.run("proj", "add-analysis", p.file, "--id", "end"), 1);
assert.match(p.err.join("\n"), /end is already in proj/);
assert.equal(await p.run("proj", "set-analysis", "end", JSON.stringify({ id: "zz" })), 1);
assert.match(p.err.join("\n"), /does not change its id/);
// A ledger row the item draws cannot be removed from under it (umtool ledger rm).
const lines = [];
assert.equal(await ledgerCommand(["proj", "rm", "w115"], { root: path.dirname(p.dir), out: (s) => lines.push(s), err: (s) => lines.push(s) }), 1);
assert.match(lines.join("\n"), /w115 is still named: analysis item end draws it/);
p.err.length = 0;
assert.equal(await p.run("proj", "set-analysis", "end", JSON.stringify({ series: { rows: ["w91", "w999"] } })), 1);
assert.match(p.err.join("\n"), /series row w999 is not in the ledger/);
assert.equal(await p.run("proj", "set-analysis", "c01", "{}"), 1);
assert.equal(await p.run("proj", "add-analysis", JSON.stringify({ ...ITEM, seconds: 12 }), "--id", "x"), 1);
assert.match(p.err.join("\n"), /held 12 s with no audio/);
// The item's audio clip cannot be taken out from under it.
p.err.length = 0;
assert.equal(await p.run("proj", "set-analysis", "end", JSON.stringify({ audio: { clip: "c02", start: 210, end: 224 } })), 0, p.err.join("\n"));
assert.equal(await p.run("proj", "remove", "c02"), 1);
assert.match(p.err.join("\n"), /audio\.clip c02 is not an entry of the cut/);
} finally {
await rm(p.root, { recursive: true, force: true });
}
});
test("--take: into the take's timeline only, with the take's own undo point", async () => {
const p = await project();
try {
await draftTake(p.dir, { id: "tk", label: "Take", patch: {} });
const master = await readFile(path.join(p.dir, "video.manifest.json"), "utf8");
assert.equal(await p.run("proj", "--take", "tk", "add-analysis", p.file, "--after", "c01"), 0, p.err.join("\n"));
assert.match(p.out.join("\n"), /added analysis analysis at 1 in take tk of proj/);
assert.match(p.out.join("\n"), /undo point: takes\/tk\//);
assert.equal(await readFile(path.join(p.dir, "video.manifest.json"), "utf8"), master, "the draft is untouched");
let m = (await resolveTakeManifest(p.dir, "tk")).manifest;
assert.deepEqual(m.timeline.map((e) => e.id), ["c01", "analysis", "c02"]);
p.out.length = 0;
assert.equal(await p.run("proj", "--take", "tk", "set-analysis", "analysis", JSON.stringify({ reading: "Steady, then a stall." })), 0, p.err.join("\n"));
m = (await resolveTakeManifest(p.dir, "tk")).manifest;
assert.equal(m.timeline[1].reading, "Steady, then a stall.");
assert.equal(await p.run("proj", "--take", "tk", "undo"), 0, p.err.join("\n"));
m = (await resolveTakeManifest(p.dir, "tk")).manifest;
assert.equal(m.timeline[1].reading, "The loss is steady.");
assert.equal(await readFile(path.join(p.dir, "video.manifest.json"), "utf8"), master);
} finally {
await rm(p.root, { recursive: true, force: true });
}
});