// The inspector's writers (umtool redesign L6): provenance, title, a teaser inserted, the published embed ids, the // ledger's undo points, and a still grabbed from a fetched window -- each one write to the draft with its own // undo point, refused in the build's words. Run with: pnpm test:scripts import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { mkdir, mkdtemp, readFile, readdir, rm, stat, 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-inspector-")); process.env.REPORTS_DIR = path.join(ROOT, "reports"); process.env.CHANNELS_DIR = path.join(ROOT, "channels"); process.env.CHANNELS_FALLBACK = "1"; const ed = await import("./inspectorEdits.mjs"); const { embedIdFixes, embedIdRows } = await import("./embedIds.mjs"); const { grabStill, listShots, shotFileOf, stillNameOf } = await import("./stillGrab.mjs"); const { addLedgerRow, manifestToken, setUndoEveryMs, undoStructural, StaleToken, StructureRefused } = await import("./manifest.mjs"); // The app's rule (instrumentation.ts): every op its own undo point. setUndoEveryMs(0); test.after(() => rm(ROOT, { recursive: true, force: true })); const MANIFEST = () => ({ schemaVersion: 1, slug: "insp", title: "The Inspector", provenance: { siteOrigin: "https://old.example", channelSlug: "chan" }, render: { fps: 30, transition: 0 }, timeline: [ { type: "clip", id: "c01", video: "vid1", start: 1, end: 3, cite: 1 }, { type: "clip", id: "c02", video: "v7slug1", channel: "rumblechan", start: 4, end: 6, cite: 4 }, { type: "clip", id: "c03", video: "v7slug2", channel: "rumblechan", start: 1, end: 2, cite: 1, citeUrl: "https://site.example/?v=rumblechan/v7slug2&t=1" }, ], }); let n = 0; async function project(m = MANIFEST()) { n += 1; const dir = path.join(ROOT, "reports", `p${n}`); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(m, null, 2) + "\n"); return { id: `p${n}`, dir }; } const read = async (dir) => JSON.parse(await readFile(path.join(dir, "video.manifest.json"), "utf8")); const undoPoints = async (dir) => (await readdir(path.join(dir, "revisions")).catch(() => [])).filter((f) => f.includes("auto-before-")); test("provenance: checked, normalised, one write with its own undo point", async () => { assert.deepEqual(ed.provenancePatchOf({ siteOrigin: "https://jeralyzer.pages.dev/", channelSlug: " the-quartering ", channel: "" }), { siteOrigin: "https://jeralyzer.pages.dev", channelSlug: "the-quartering", channel: null, }); assert.throws(() => ed.provenancePatchOf({ siteOrigin: "https://x.example/path" }), /no path/); assert.throws(() => ed.provenancePatchOf({ siteOrigin: "ftp://x" }), /http/); assert.throws(() => ed.provenancePatchOf({ channelSlug: "a b" }), /slug/); assert.throws(() => ed.provenancePatchOf({ linkPolicy: {} }), /link-policy/); assert.throws(() => ed.provenancePatchOf({}), /nothing to change/); const p = await project(); const token = await manifestToken(p.dir); const r = await ed.updateProvenance(p.dir, { siteOrigin: "https://new.example", channel: "The Channel" }, { token }); assert.deepEqual(r.provenance, { siteOrigin: "https://new.example", channelSlug: "chan", channel: "The Channel" }); assert.deepEqual(r.changed.siteOrigin, ["https://old.example", "https://new.example"]); const m = await read(p.dir); assert.equal(m.provenance.siteOrigin, "https://new.example"); assert.equal(m.provenance.channel, "The Channel"); assert.equal((await undoPoints(p.dir)).filter((f) => f.endsWith("auto-before-provenance.manifest.json")).length, 1); // A stale token is refused, the draft untouched. await assert.rejects(ed.updateProvenance(p.dir, { channelSlug: "x" }, { token }), (e) => e instanceof StaleToken); // Undo takes exactly it back. await undoStructural(p.dir); assert.equal((await read(p.dir)).provenance.siteOrigin, "https://old.example"); }); test("title and subtitle: one line each, the subtitle removable", async () => { assert.throws(() => ed.titlePatchOf({ title: " " }), /words/); assert.throws(() => ed.titlePatchOf({ title: "a\nb" }), /one line/); assert.throws(() => ed.titlePatchOf({}), /nothing to change/); const p = await project(); let r = await ed.updateTitle(p.dir, { title: " A New Title ", subtitle: "and its line" }); assert.deepEqual(r, { ...r, title: "A New Title", subtitle: "and its line" }); r = await ed.updateTitle(p.dir, { subtitle: "" }); assert.equal(r.subtitle, null); const m = await read(p.dir); assert.equal(m.title, "A New Title"); assert.equal("subtitle" in m, false); assert.equal((await undoPoints(p.dir)).length, 2); }); test("teaser insert: start, end, after; the id fresh; refused in the build's words", async () => { const lines = ["The 291 scale bet", { text: "Oct 23: bet at 291", hold: "" }, { text: "Scale video: not posted as of Nov 8", break: "as of Nov 8", hold: 1.6 }]; assert.deepEqual(ed.teaserLineOf({ text: " x ", break: "", hold: "" }), "x"); assert.deepEqual(ed.teaserLineOf({ text: "a b", break: "b", hold: "1.234" }), { text: "a b", break: "b", hold: 1.23 }); assert.throws(() => ed.teaserLineOf({ text: "a", colour: "red" }), /not a teaser line key/); const p = await project(); let r = await ed.insertTeaser(p.dir, { lines, beat: 1.3 }, "end"); assert.equal(r.id, "close"); assert.equal(r.at, 3); r = await ed.insertTeaser(p.dir, { lines: ["Cold open"] }, "start"); assert.equal(r.id, "cold"); r = await ed.insertTeaser(p.dir, { lines: ["Between"] }, { after: "c01" }); assert.equal(r.id, "teaser"); r = await ed.insertTeaser(p.dir, { lines: ["Again"] }, "start"); assert.equal(r.id, "cold-2"); const m = await read(p.dir); assert.deepEqual(m.timeline.map((e) => e.id), ["cold-2", "cold", "c01", "teaser", "c02", "c03", "close"]); assert.deepEqual(m.timeline.at(-1), { type: "teaser", id: "close", lines: ["The 291 scale bet", "Oct 23: bet at 291", { text: "Scale video: not posted as of Nov 8", break: "as of Nov 8", hold: 1.6 }], beat: 1.3 }); assert.equal((await undoPoints(p.dir)).filter((f) => f.includes("teaser-insert")).length, 4); await assert.rejects(ed.insertTeaser(p.dir, { lines: [] }, "end"), (e) => e instanceof StructureRefused); await assert.rejects(ed.insertTeaser(p.dir, { id: "c01", lines: ["x"] }, "end"), /already has an entry c01/); await assert.rejects(ed.insertTeaser(p.dir, { lines: [{ text: "abc", break: "zzz" }] }, "end"), /break must be the end of its text/); await assert.rejects(ed.insertTeaser(p.dir, { lines: ["x"], beat: 9 }, "end"), /beat must be from/); await assert.rejects(ed.insertTeaser(p.dir, { lines: ["x"] }, { after: "nope" }), /no timeline entry with id nope/); assert.equal((await read(p.dir)).timeline.length, 7); }); async function rumbleChannel() { const ch = path.join(ROOT, "channels", "rumblechan"); for (const [dirId, id] of [["v7slug1", "v7emb01"], ["v7slug2", "v7emb02"], ["v7same3", "v7same3"]]) { await mkdir(path.join(ch, "data", dirId), { recursive: true }); await writeFile(path.join(ch, "data", dirId, "metadata.info.json"), JSON.stringify({ id, extractor: "Rumble", webpage_url: `https://rumble.com/${dirId}-x.html` })); } await writeFile(path.join(ch, "config.json"), JSON.stringify({ platform: "rumble", url: "https://rumble.com/c/rumblechan" })); } test("the published embed id: found on disk, written as siteVideo (a citeUrl naming the folder moved), one undo point", async () => { await rumbleChannel(); const p = await project(); const m = await read(p.dir); const rows = await embedIdRows(p.dir, m, { layers: [path.join(ROOT, "channels")] }); assert.deepEqual(rows.map((r) => [r.clip, r.video, r.siteVideo]), [["c02", "v7slug1", "v7emb01"], ["c03", "v7slug2", "v7emb02"]]); assert.equal(rows[1].citeUrl, "https://site.example/?v=rumblechan/v7emb02&t=1"); assert.equal(rows[1].fixedUrl, "https://site.example/?v=rumblechan/v7emb02&t=1"); assert.equal(rows[0].state, "unchecked"); assert.throws(() => embedIdFixes(rows, ["c01"]), /no published embed id/); const r = await ed.setSiteVideos(p.dir, embedIdFixes(rows, ["c02"])); assert.deepEqual(r.fixed, [{ clip: "c02", video: "v7slug1", siteVideo: "v7emb01" }]); let after = await read(p.dir); assert.equal(after.timeline[1].siteVideo, "v7emb01"); assert.equal(after.timeline[1].video, "v7slug1"); // The rest, in one write. const left = await embedIdRows(p.dir, after, { layers: [path.join(ROOT, "channels")] }); assert.deepEqual(left.map((x) => x.clip), ["c03"]); await ed.setSiteVideos(p.dir, embedIdFixes(left)); after = await read(p.dir); assert.equal(after.timeline[2].siteVideo, "v7emb02"); assert.equal(after.timeline[2].citeUrl, "https://site.example/?v=rumblechan/v7emb02&t=1"); assert.deepEqual(await embedIdRows(p.dir, after, { layers: [path.join(ROOT, "channels")] }), []); assert.equal((await undoPoints(p.dir)).filter((f) => f.includes("embed-ids")).length, 2); }); test("the ledger's writers take an undo point each", async () => { const m = MANIFEST(); m.ledgerSchema = { kind: "measure", unit: "lb", yMin: 200, yMax: 400, tracks: [{ key: "weight", role: "value", label: "Weight" }], forms: ["current"], regimes: [], fields: [] }; m.ledger = []; const p = await project(m); await addLedgerRow(p.dir, { id: "w01", date: "2026-10-01", scope: "weight", population: "current", value: 291, scopeBasis: "his words" }); assert.equal((await undoPoints(p.dir)).filter((f) => f.endsWith("auto-before-ledger.manifest.json")).length, 1); await undoStructural(p.dir); assert.deepEqual((await read(p.dir)).ledger, []); }); const hasFfmpeg = spawnSync("ffmpeg", ["-version"]).status === 0; test("a still at second N: one frame of the fetched window into shots/, listed; a second no window holds refused", { skip: !hasFfmpeg && "no ffmpeg" }, async () => { const p = await project(); await mkdir(path.join(p.dir, "out", "clips-raw"), { recursive: true }); const win = path.join(p.dir, "out", "clips-raw", "vid1_0.00-9.00.mp4"); const ff = spawnSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=size=160x90:rate=10:duration=9", "-pix_fmt", "yuv420p", win]); assert.equal(ff.status, 0, String(ff.stderr)); const m = await read(p.dir); const before = await readFile(path.join(p.dir, "video.manifest.json"), "utf8"); const r = await grabStill(p, m, "c01", 2.5); assert.equal(r.rel, `shots/${stillNameOf("c01", 2.5)}`); assert.equal(r.rel, "shots/still-c01-2_50s.png"); assert.ok((await stat(r.file)).size > 100); // A still never edits the cut. assert.equal(await readFile(path.join(p.dir, "video.manifest.json"), "utf8"), before); const shots = await listShots(p.dir); assert.deepEqual(shots.map((s) => [s.rel, s.still]), [["shots/still-c01-2_50s.png", { clip: "c01", at: 2.5 }]]); assert.equal(shotFileOf(p.dir, "shots/still-c01-2_50s.png"), path.join(p.dir, "shots", "still-c01-2_50s.png")); assert.equal(shotFileOf(p.dir, "shots/../video.manifest.json"), null); assert.equal(shotFileOf(p.dir, "posts-media/x.png"), null); await assert.rejects(grabStill(p, m, "c01", 30), /no fetched window/); await assert.rejects(grabStill(p, m, "nope", 1), /no clip nope/); }); /** The mean luma of a box of a PNG (0–255). */ function pngLuma(file, { x, y, w, h }) { const r = spawnSync("ffmpeg", ["-v", "error", "-i", file, "-vf", `crop=${w}:${h}:${x}:${y},format=gray`, "-f", "rawvideo", "-"]); assert.equal(r.status, 0, String(r.stderr)); return r.stdout.reduce((n, v) => n + v, 0) / r.stdout.length; } test("a still of a clip that hides regions: the boxes over its second hidden in the PNG; outside their span, as recorded", { skip: !hasFfmpeg && "no ffmpeg" }, async () => { const m0 = MANIFEST(); m0.render.censor = [{ video: "vid1", boxes: [{ box: [0.5, 0, 0.5, 1], from: 1, to: 2 }], style: "black" }]; const p = await project(m0); await mkdir(path.join(p.dir, "out", "clips-raw"), { recursive: true }); const win = path.join(p.dir, "out", "clips-raw", "vid1_0.00-9.00.mp4"); const ff = spawnSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "color=c=white:size=160x90:rate=10:duration=9", "-pix_fmt", "yuv420p", win]); assert.equal(ff.status, 0, String(ff.stderr)); const m = await read(p.dir); const RIGHT = { x: 100, y: 10, w: 50, h: 70 }; const LEFT = { x: 10, y: 10, w: 50, h: 70 }; const inside = await grabStill(p, m, "c01", 1.5); assert.ok(pngLuma(inside.file, RIGHT) < 30, `hidden (${pngLuma(inside.file, RIGHT)})`); assert.ok(pngLuma(inside.file, LEFT) > 220, "the rest as recorded"); const outside = await grabStill(p, m, "c01", 2.5); assert.ok(pngLuma(outside.file, RIGHT) > 220, "past its span: as recorded"); // A clip's own box (no span: the whole clip), whatever the recording's. m.timeline[0].censor = { boxes: [[0, 0, 0.5, 1]], style: "black" }; const own = await grabStill(p, m, "c01", 2.5, { name: "still-own.png" }); assert.ok(pngLuma(own.file, LEFT) < 30 && pngLuma(own.file, RIGHT) > 220, "its own box"); }); // ---- the CLI twins (inspectorCli.mjs; timelineCli.mjs teaser-insert; linksCli.mjs --embed-ids) ---- const cli = await import("./inspectorCli.mjs"); const { timelineCommand } = await import("./timelineCli.mjs"); const { linksCommand } = await import("./linksCli.mjs"); const run = async (fn, args) => { const outs = []; const errs = []; const code = await fn(args, { root: path.join(ROOT, "reports"), out: (s) => outs.push(s), err: (s) => errs.push(s) }); return { code, out: outs.join("\n"), err: errs.join("\n") }; }; test("umtool provenance / title: print, set, refuse", async () => { const p = await project(); let r = await run(cli.provenanceCommand, [p.id, "--json"]); assert.equal(r.code, 0, r.err); assert.equal(JSON.parse(r.out).provenance.siteOrigin, "https://old.example"); r = await run(cli.provenanceCommand, [p.id, "set", "--site-origin", "https://jeralyzer.pages.dev/", "--channel-slug", "the-quartering", "--json"]); assert.equal(r.code, 0, r.err); assert.equal(JSON.parse(r.out).provenance.channelSlug, "the-quartering"); r = await run(cli.provenanceCommand, [p.id, "set", "--site-origin", "nope"]); assert.equal(r.code, 1); assert.match(r.err, /siteOrigin must be/); r = await run(cli.provenanceCommand, [p.id, "set", "--bogus", "x"]); assert.equal(r.code, 2); r = await run(cli.titleCommand, [p.id, "Losing the same hundred pounds", "--subtitle", "Every weight", "--json"]); assert.equal(r.code, 0, r.err); assert.deepEqual([JSON.parse(r.out).title, JSON.parse(r.out).subtitle], ["Losing the same hundred pounds", "Every weight"]); r = await run(cli.titleCommand, [p.id, "--no-subtitle"]); assert.equal(r.code, 0, r.err); const m = await read(p.dir); assert.equal(m.provenance.siteOrigin, "https://jeralyzer.pages.dev"); assert.equal(m.title, "Losing the same hundred pounds"); assert.equal("subtitle" in m, false); }); test("umtool timeline teaser-insert: --line/--break/--hold, at the end by default", async () => { const p = await project(); let r = await run(timelineCommand, [p.id, "teaser-insert", "--line", "The 291 scale bet", "--line", "Scale video: not posted as of Nov 8", "--break", "as of Nov 8", "--hold", "1.6", "--beat", "1.3", "--json"]); assert.equal(r.code, 0, r.err); const j = JSON.parse(r.out); assert.equal(j.inserted.id, "close"); assert.deepEqual(j.inserted.entry, { type: "teaser", id: "close", lines: ["The 291 scale bet", { text: "Scale video: not posted as of Nov 8", break: "as of Nov 8", hold: 1.6 }], beat: 1.3 }); r = await run(timelineCommand, [p.id, "teaser-insert", "--at", "start", "--lines-json", '["Cold open", {"text": "a b", "break": "b"}]']); assert.equal(r.code, 0, r.err); assert.match(r.out, /inserted teaser cold at 0/); r = await run(timelineCommand, [p.id, "teaser-insert", "--break", "x"]); assert.equal(r.code, 2); r = await run(timelineCommand, [p.id, "teaser-insert", "--line", "abc", "--break", "zzz"]); assert.equal(r.code, 1); assert.match(r.err, /refused/); assert.deepEqual((await read(p.dir)).timeline.map((e) => e.id), ["cold", "c01", "c02", "c03", "close"]); }); test("umtool links --embed-ids: dry run, then the fix; no network", async () => { await rumbleChannel(); const p = await project(); let r = await run(linksCommand, [p.id, "--embed-ids", "--dry-run", "--json"]); assert.equal(r.code, 0, r.err); assert.deepEqual(JSON.parse(r.out).would.map((f) => f.clip), ["c02", "c03"]); r = await run(linksCommand, [p.id, "--embed-ids", "--clips", "c02"]); assert.equal(r.code, 0, r.err); assert.match(r.out, /fixed c02: v7slug1 -> siteVideo v7emb01/); assert.equal((await read(p.dir)).timeline[2].siteVideo, undefined); r = await run(linksCommand, [p.id, "--embed-ids", "--clips", "c01"]); assert.equal(r.code, 1); }); test("an attachment switched off keeps its knobs in mediaOff, and on puts it back as it was", async () => { const { updatePostItem } = await import("./manifest.mjs"); const { validatePostItem } = await import("umtool-report-to-video/post-item"); const m = MANIFEST(); const vid = { src: "posts-media/x/3/3_1.mp4", kind: "video", seconds: 4, width: 320, height: 180, audio: true }; const pic = { src: "posts-media/x/3/3_2.png", kind: "image", width: 320, height: 180 }; m.timeline.push({ type: "post", id: "p-3", post: "x/3", platform: "x", author: "A", handle: "a", date: "2026-10-30T12:00:00.000Z", text: "words", media: [vid, pic] }); const p = await project(m); await updatePostItem(p.dir, "p-3", { media: { 0: { crop: [0.1, 0.1, 0.8, 0.8], mute: true, start: 1 } } }); let r = await updatePostItem(p.dir, "p-3", { mediaHide: [0] }); assert.deepEqual(r.entry.media, [pic]); assert.deepEqual(r.entry.mediaOff, [{ ...vid, crop: [0.1, 0.1, 0.8, 0.8], mute: true, start: 1 }]); assert.deepEqual(validatePostItem(r.entry), []); r = await updatePostItem(p.dir, "p-3", { mediaShow: [0] }); assert.deepEqual(r.entry.media, [pic, { ...vid, crop: [0.1, 0.1, 0.8, 0.8], mute: true, start: 1 }]); assert.equal("mediaOff" in r.entry, false); await assert.rejects(updatePostItem(p.dir, "p-3", { mediaShow: [0] }), /no attachment switched off at 0/); await assert.rejects(updatePostItem(p.dir, "p-3", { mediaHide: [5] }), /shows no attachment 5/); await assert.rejects(updatePostItem(p.dir, "p-3", { mediaHide: [0], hold: 3 }), /go alone/); // The build's check: a bad switched-off attachment is refused as a shown one is. assert.match(validatePostItem({ ...r.entry, mediaOff: [{ src: 7 }] }).join(" "), /mediaOff\[0\]/); assert.match(validatePostItem({ ...r.entry, mediaOff: [] }).join(" "), /mediaOff must be a list/); });