// The clip deck's store (release 34 N1): one writer, a queue, tmp + rename, a // bad file never overwritten; the import of the seed's edits.json mapped to // moments (the clip within 30 s, else the seed's instances.json), offered once. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import test from "node:test"; import { earlierRulingsOffer, hasMontageClips, importEarlierRulings, mapEarlierRulings, momentKeysOf, normaliseRuling, readRulingsState, resolveRulingKey, setMontageRuling, verdictFromWhy, } from "./montageRulings.mjs"; const NOW = new Date("2026-10-18T12:00:00.000Z"); const clip = (id, channel, video, cutStart) => ({ type: "clip", id, channel, video, start: cutStart - 2.5, end: cutStart + 5, cutStart, cutEnd: cutStart + 2, montage: { n: 1, of: 3, match: "slow news day" }, }); const MANIFEST = { provenance: { channelSlug: "chan" }, timeline: [clip("s01", "chan", "aaa", 100), clip("s02", "chan-rumble", "v1a2b3c", 3597.6), clip("s03", "chan", "bbb", 12.25), { type: "card", id: "k1" }], }; const tmp = () => mkdtemp(path.join(os.tmpdir(), "umtool-rulings-")); const file = (dir) => path.join(dir, "montage-rulings.json"); test("keys: every clip's moment; a clip id or a key resolves, anything else is refused", () => { assert.deepEqual([...momentKeysOf(MANIFEST)], [["s01", "chan/aaa@100.00"], ["s02", "chan-rumble/v1a2b3c@3597.60"], ["s03", "chan/bbb@12.25"]]); assert.equal(resolveRulingKey(MANIFEST, "s02"), "chan-rumble/v1a2b3c@3597.60"); assert.equal(resolveRulingKey(MANIFEST, "chan/bbb@12.25"), "chan/bbb@12.25"); assert.equal(resolveRulingKey(MANIFEST, "chan/zzz@1.00"), null); assert.equal(hasMontageClips(MANIFEST), true); assert.equal(hasMontageClips({ timeline: [{ type: "clip", id: "c", video: "v", start: 1, end: 2 }] }), false); }); test("normaliseRuling: checked, trimmed, stamped; a bad one is a sentence", () => { assert.deepEqual(normaliseRuling({ verdict: "not-him", reason: " guest " }, NOW), { verdict: "not-him", reason: "guest", at: NOW.toISOString() }); assert.deepEqual(normaliseRuling({ verdict: "keep", match: " it's not that " }, NOW), { verdict: "keep", reason: "", match: "it's not that", at: NOW.toISOString() }); assert.throws(() => normaliseRuling({ verdict: "maybe" }), /verdict must be one of/); assert.throws(() => normaliseRuling({ verdict: "cut", reason: 3 }), /reason must be a string/); assert.throws(() => normaliseRuling(null), /a ruling is/); }); test("setMontageRuling: set, replace, remove; writes queue; no temp file is left; the shape on disk", async () => { const dir = await tmp(); try { assert.deepEqual(await readRulingsState(dir), { exists: false, doc: { version: 1, rulings: {} }, error: null }); await Promise.all([ setMontageRuling(dir, "chan/aaa@100.00", { verdict: "cut", reason: "wrong moment" }, { now: NOW }), setMontageRuling(dir, "chan/bbb@12.25", { verdict: "read-aloud" }, { now: NOW }), setMontageRuling(dir, "chan-rumble/v1a2b3c@3597.60", { verdict: "keep", match: "it's not that it's a slow news cycle" }, { now: NOW }), ]); let disk = JSON.parse(await readFile(file(dir), "utf8")); assert.equal(disk.version, 1); assert.deepEqual(Object.keys(disk.rulings), ["chan-rumble/v1a2b3c@3597.60", "chan/aaa@100.00", "chan/bbb@12.25"]); assert.deepEqual(disk.rulings["chan/aaa@100.00"], { verdict: "cut", reason: "wrong moment", at: NOW.toISOString() }); await setMontageRuling(dir, "chan/aaa@100.00", { verdict: "not-him" }, { now: NOW }); await setMontageRuling(dir, "chan/bbb@12.25", null); disk = JSON.parse(await readFile(file(dir), "utf8")); assert.equal(disk.rulings["chan/aaa@100.00"].verdict, "not-him"); assert.equal(disk.rulings["chan/bbb@12.25"], undefined); assert.deepEqual((await readdir(dir)).sort(), ["montage-rulings.json"]); await assert.rejects(setMontageRuling(dir, "s01", { verdict: "cut" }), /not a moment key/); } finally { await rm(dir, { recursive: true, force: true }); } }); test("a file that does not parse is an error: read as such, never overwritten", async () => { const dir = await tmp(); try { await writeFile(file(dir), "{ broken"); const st = await readRulingsState(dir); assert.equal(st.exists, true); assert.match(st.error, /does not parse/); await assert.rejects(setMontageRuling(dir, "chan/aaa@100.00", { verdict: "cut" }), /not overwriting it/); assert.equal(await readFile(file(dir), "utf8"), "{ broken"); } finally { await rm(dir, { recursive: true, force: true }); } }); test("verdictFromWhy: an exclude's words", () => { assert.equal(verdictFromWhy("another speaker"), "not-him"); assert.equal(verdictFromWhy("the co-host says it"), "not-him"); assert.equal(verdictFromWhy("a tweet read out, not his own line"), "read-aloud"); assert.equal(verdictFromWhy("reading chat"), "read-aloud"); assert.equal(verdictFromWhy("too long"), "cut"); assert.equal(verdictFromWhy(undefined), "cut"); }); const EDITS = { exclude: [ { videoId: "aaa", near: 112, why: "another speaker" }, { videoId: "gone", near: 454, why: "a tweet read out, not his own line" }, { videoId: "nowhere", near: 1, why: "x" }, ], match: [{ videoId: "v1a2b3c", near: 3597, match: "it's not that it's a slow news cycle", why: "the rebuttal" }], }; const INSTANCES = [{ slug: "chan", videoId: "gone", start: 454.259 }, { slug: "chan", videoId: "aaa", start: 100 }]; test("mapEarlierRulings: the clip whose video matches within 30 s; a moment the seed left out, from instances.json; the rest unmapped", () => { const m = mapEarlierRulings(MANIFEST, EDITS, INSTANCES); assert.deepEqual(m.rulings, [ { key: "chan/aaa@100.00", clip: "s01", ruling: { verdict: "not-him", reason: "another speaker" } }, { key: "chan/gone@454.26", clip: null, ruling: { verdict: "read-aloud", reason: "a tweet read out, not his own line" } }, { key: "chan-rumble/v1a2b3c@3597.60", clip: "s02", ruling: { verdict: "keep", reason: "the rebuttal", match: "it's not that it's a slow news cycle" } }, ]); assert.deepEqual(m.unmapped, [{ videoId: "nowhere", near: 1, why: "x" }]); // 30 s is the edge. assert.equal(mapEarlierRulings(MANIFEST, { exclude: [{ videoId: "aaa", near: 130.1, why: "" }] }).rulings.length, 0); assert.deepEqual(mapEarlierRulings(MANIFEST, null), { rulings: [], unmapped: [] }); }); test("the import: offered while something is new, written once, then not offered again; a ruled moment is left as it is", async () => { const dir = await tmp(); try { assert.deepEqual(await earlierRulingsOffer(dir, MANIFEST), { exists: false, rulings: [], unmapped: [] }); await writeFile(path.join(dir, "edits.json"), JSON.stringify(EDITS)); await writeFile(path.join(dir, "instances.json"), JSON.stringify(INSTANCES)); await setMontageRuling(dir, "chan/aaa@100.00", { verdict: "keep" }, { now: NOW }); const offer = await earlierRulingsOffer(dir, MANIFEST); assert.deepEqual(offer.rulings.map((r) => r.key), ["chan/gone@454.26", "chan-rumble/v1a2b3c@3597.60"]); const dry = await importEarlierRulings(dir, MANIFEST, { dryRun: true }); assert.deepEqual(dry.written, []); const done = await importEarlierRulings(dir, MANIFEST, { now: NOW }); assert.deepEqual(done.written, ["chan/gone@454.26", "chan-rumble/v1a2b3c@3597.60"]); const disk = (await readRulingsState(dir)).doc.rulings; assert.equal(disk["chan/aaa@100.00"].verdict, "keep", "the deck's ruling stands"); assert.equal(disk["chan-rumble/v1a2b3c@3597.60"].match, "it's not that it's a slow news cycle"); assert.equal((await earlierRulingsOffer(dir, MANIFEST)).rulings.length, 0); } finally { await rm(dir, { recursive: true, force: true }); } }); test("umtool rulings: show, set, clear, import -- the same writer", async () => { const { rulingsCommand } = await import("./rulingsCli.mjs"); const { mkdir } = await import("node:fs/promises"); const root = await tmp(); const dir = path.join(root, "slow"); try { await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: "slow", title: "Slow", render: {}, ...MANIFEST })); const lines = []; const run = (...args) => rulingsCommand(args, { root, out: (s) => lines.push(s), err: (s) => lines.push(`ERR ${s}`), now: NOW }); assert.equal(await run("slow", "set", "s01", "not-him", "--reason", "guest"), 0); assert.equal(await run("slow", "set", "s02", "keep", "--match", "it's not that"), 0); assert.equal(await run("slow", "set", "s09", "cut"), 1); assert.match(lines.at(-1), /no montage clip "s09"/); lines.length = 0; assert.equal(await run("slow", "--json"), 0); const shown = JSON.parse(lines.join("\n")); assert.deepEqual(shown.plan.kept.map((k) => [k.id, k.n, k.match]), [["s02", 1, "it's not that"], ["s03", 2, "slow news day"]]); assert.equal(await run("slow", "clear", "s01"), 0); assert.equal((await readRulingsState(dir)).doc.rulings["chan/aaa@100.00"], undefined); await writeFile(path.join(dir, "edits.json"), JSON.stringify(EDITS)); lines.length = 0; assert.equal(await run("slow", "import", "--dry-run"), 0); assert.match(lines.at(-1), /would import 1 earlier ruling/); assert.equal(await run("slow", "import"), 0); assert.equal((await readRulingsState(dir)).doc.rulings["chan/aaa@100.00"].verdict, "not-him"); } finally { await rm(root, { recursive: true, force: true }); } });