// `umtool timeline` (2026-10-25): the cut's structure from a terminal, through // the writers POST /api/report/timeline runs -- the same snapshots, the same // build validators, the same edit notes on a generated manifest. // // Run with: pnpm test:scripts 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"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-timeline-cli-")); process.env.REPORTS_DIR = ROOT; const { attributionPatch, insertConvention, insertNeighbour, splitTimelineArgs, timelineCommand, timelineLines, timelineRows } = await import("./timelineCli.mjs"); const MANIFEST = { schemaVersion: 1, slug: "cut", title: "Cut", provenance: { siteOrigin: "https://example.test", channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30 }, timeline: [ { type: "teaser", id: "t1", lines: ["One", "Two"] }, { type: "clip", id: "c01", video: "v1", start: 10, end: 20, date: "2026-08-24", title: "First" }, { type: "clip", id: "c02", channel: "other", video: "v2", start: 30, end: 41, cutStart: 31, cutEnd: 40 }, ], posts: [{ id: "x-1", platform: "x", date: "2026-08-24T10:00:00.000Z", text: "words", url: "https://x.com/a/status/1", attachTo: "c02" }], }; async function project(name, extra = {}) { const dir = path.join(ROOT, name); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ ...MANIFEST, ...extra }, null, 2) + "\n"); return dir; } const read = async (dir) => JSON.parse(await readFile(path.join(dir, "video.manifest.json"), "utf8")); const ids = async (dir) => (await read(dir)).timeline.map((e) => e.id); const runner = () => { const lines = []; return { lines, run: (...args) => timelineCommand(args, { root: ROOT, out: (s) => lines.push(s), err: (s) => lines.push(`ERR ${s}`) }) }; }; test.after(() => rm(ROOT, { recursive: true, force: true })); test("splitTimelineArgs: a valued flag takes the next word, even `-`", () => { const { words, flags } = splitTimelineArgs(["p", "insert", "--after", "-", "chan/v@1-2", "--title", "T", "--json"]); assert.deepEqual(words, ["p", "insert", "chan/v@1-2"]); assert.equal(flags.get("--after"), "-"); assert.equal(flags.get("--title"), "T"); assert.equal(flags.get("--json"), true); }); test("attributionPatch: the window flags; a bad date or cite refused before anything is written", () => { const f = (o) => new Map(Object.entries(o)); assert.deepEqual(attributionPatch(f({ "--title": "T", "--date": "2026-08-24", "--cite": "12.5", "--quote": "q" })), { title: "T", date: "2026-08-24", cite: 12.5, quote: "q", }); assert.throws(() => attributionPatch(f({ "--date": "2026-02-30" })), /real calendar date/); assert.throws(() => attributionPatch(f({ "--cite": "soon" })), /--cite must be/); }); test("ls: index, id, type, video, start–end, date title", async () => { await project("ls"); const rows = timelineRows(MANIFEST); assert.deepEqual(rows.map((r) => [r.index, r.id, r.type]), [[0, "t1", "teaser"], [1, "c01", "clip"], [2, "c02", "clip"]]); assert.equal(rows[1].channel, "chan"); assert.equal(rows[2].channel, "other"); assert.deepEqual([rows[2].cutStart, rows[2].cutEnd], [31, 40]); const lines = timelineLines(rows); assert.match(lines[1], /^\s+1\s+c01\s+clip\s+v1\s+10\.00–20\.00\s+2026-08-24 First$/); assert.match(lines[2], /cut 31\.00–40\.00/); const { lines: out, run } = runner(); assert.equal(await run("ls", "--json"), 0); const doc = JSON.parse(out[0]); assert.equal(doc.timeline.length, 3); assert.equal(typeof doc.token, "string"); assert.equal(doc.posts[0].id, "x-1"); }); test("insert: a clip after an id (or `-`, the start), its attribution through updateClip; snapshots first", async () => { const dir = await project("insert"); const { lines, run } = runner(); assert.equal(await run("insert", "insert", "--after", "c01", "chan/v9@5-12.5", "--id", "new", "--title", "Nine", "--date", "2026-09-01", "--quote", "said it", "--cite", "6"), 0); let m = await read(dir); assert.deepEqual(m.timeline.map((e) => e.id), ["t1", "c01", "new", "c02"]); assert.deepEqual(m.timeline[2], { type: "clip", id: "new", channel: "chan", video: "v9", start: 5, end: 12.5, title: "Nine", quote: "said it", date: "2026-09-01", cite: 6 }); assert.ok((await readdir(path.join(dir, "revisions"))).some((n) => n.endsWith("-auto-before-insert.manifest.json"))); // The id taken: a fresh one, and said so. assert.equal(await run("insert", "insert", "--after", "-", "chan/v9@1-3", "--id", "new"), 0); assert.match(lines.at(-2) + lines.at(-1), /new was taken: it is new-2/); assert.equal((await ids(dir))[0], "new-2"); // A bad date: refused before the insert, nothing written. const before = await read(dir); assert.equal(await run("insert", "insert", "--after", "c01", "chan/v9@20-30", "--date", "2026-13-01"), 2); assert.deepEqual(await read(dir), before); assert.equal(await run("insert", "insert", "chan/v9@20-30"), 2); assert.match(lines.at(-1), /needs --after/); assert.equal(await run("insert", "insert", "--after", "c01", "nonsense"), 1); assert.match(lines.at(-1), /\/