// Gaps inside a clip, written (umtool-report-to-video gaps.mjs; the one writer, // manifest.mjs patchClipEntry): `gaps` / `gapAdd` / `gapRemove` through // updateClip, checked against the entry after the patch -- a window, cut or // mark save that leaves a gap outside, or a mark in one, is refused and the // file is left as it was; an undo point first; split and merge carry them; // `umtool set gap.add=…`; `umtool check`'s clip-gap-invalid row. // // 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-gaps-writer-")); process.env.REPORTS_DIR = ROOT; const { MANIFEST_NAME, mergeClips, splitClip, updateClip, updateMarks } = await import("./manifest.mjs"); const { setCommand } = await import("./knobsCli.mjs"); const { timelineLines, timelineRows } = await import("./timelineCli.mjs"); const MANIFEST = { schemaVersion: 1, slug: "gaps", title: "Gaps", provenance: { siteOrigin: "https://example.test", channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30 }, timeline: [ { type: "clip", id: "c01", video: "v1", start: 10, end: 30, cutStart: 12, cutEnd: 28 }, { type: "clip", id: "c02", video: "v1", start: 30, end: 40 }, ], }; let n = 0; async function project(manifest = MANIFEST) { const dir = path.join(ROOT, `p${(n += 1)}`); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify(manifest, null, 2) + "\n"); return dir; } const raw = (dir) => readFile(path.join(dir, MANIFEST_NAME), "utf8"); const read = async (dir) => JSON.parse(await raw(dir)); const clip = async (dir, id) => (await read(dir)).timeline.find((e) => e.id === id); test.after(() => rm(ROOT, { recursive: true, force: true })); test("updateClip: gapAdd (a span or a list), merged with a gap it touches; gapRemove by a second inside; gaps sets, [] or '' clears", async () => { const dir = await project(); let r = await updateClip(dir, "c01", { gapAdd: "15.004-17.333" }); assert.deepEqual(r.entry.gaps, [{ from: 15, to: 17.33 }]); r = await updateClip(dir, "c01", { gapAdd: [{ from: 20, to: 21 }, "17-17.6"] }); assert.deepEqual(r.entry.gaps, [{ from: 15, to: 17.6 }, { from: 20, to: 21 }]); r = await updateClip(dir, "c01", { gapRemove: 16 }); assert.deepEqual(r.entry.gaps, [{ from: 20, to: 21 }]); r = await updateClip(dir, "c01", { gaps: [{ from: 22, to: 23 }, { from: 14, to: 14.5 }] }); assert.deepEqual((await clip(dir, "c01")).gaps, [{ from: 14, to: 14.5 }, { from: 22, to: 23 }], "sorted on write"); await updateClip(dir, "c01", { gaps: [] }); assert.equal("gaps" in (await clip(dir, "c01")), false, "an empty list removes the key"); await updateClip(dir, "c01", { gapAdd: "15-16" }); await updateClip(dir, "c01", { gaps: "" }); assert.equal("gaps" in (await clip(dir, "c01")), false); // Removing the last gap removes the key too. await updateClip(dir, "c01", { gapAdd: "15-16" }); await updateClip(dir, "c01", { gapRemove: 15.5 }); assert.equal("gaps" in (await clip(dir, "c01")), false); }); test("updateClip: a gap the build would refuse is refused, and the file is left byte for byte", async () => { const dir = await project(); await updateClip(dir, "c01", { gapAdd: "15-17" }); const before = await raw(dir); const refuses = async (patch, re) => { await assert.rejects(updateClip(dir, "c01", patch), re); assert.equal(await raw(dir), before, `${JSON.stringify(patch)} left the file as it was`); }; await refuses({ gapAdd: "11-13" }, /outside the clip's cut 12–28/); await refuses({ gapAdd: "12.2-13" }, /after the cut's start; keep at least 0.5 s/); await refuses({ gapAdd: "20-20.1" }, /under 0.2 s/); await refuses({ gapAdd: "17.2-18" }, /merge|keep at least/); await refuses({ gapAdd: "nonsense" }, /a gap is FROM-TO/); await refuses({ gapRemove: 25 }, /no gap at 25/); await refuses({ gaps: "15-17" }, /must be a list/); // A cut save that leaves the gap outside (or too close to the new edge). await refuses({ cutStart: 16, cutEnd: 28 }, /outside the clip's cut 16–28/); await refuses({ cutStart: 14.8 }, /after the cut's start/); // A mute mark inside the gap. await refuses({ muteFrom: 16 }, /muteFrom 16 lies inside the gap 15–17, which never plays/); // On its edge it is fine. const r = await updateClip(dir, "c01", { muteFrom: 17 }); assert.equal(r.entry.muteFrom, 17); }); test("updateClip: a gap edit leaves an undo point (auto-before-gaps); another edit does not", async () => { const dir = await project(); await updateClip(dir, "c01", { title: "x" }); const has = async () => (await readdir(path.join(dir, "revisions")).catch(() => [])).some((f) => f.includes("auto-before-gaps")); assert.equal(await has(), false); const r = await updateClip(dir, "c01", { gapAdd: "15-17" }); assert.equal(await has(), true); assert.ok(r.snapshot, "the answer names it"); }); test("the bench's mark settings leave an undo point, as a gap write does: a clip's gapCue (window route), the project's (look / Modules)", async () => { const { updateRender } = await import("./manifest.mjs"); const count = async (d) => (await readdir(path.join(d, "revisions")).catch(() => [])).filter((f) => f.includes("auto-before-gaps")).length; const dir = await project(); const r = await updateClip(dir, "c01", { gapCue: { mark: true } }); assert.ok(r.snapshot, "the clip's: an undo point first"); assert.equal(await count(dir), 1); const dir2 = await project(); const l = await updateRender(dir2, { gapCue: { mark: true, label: "{s} later" } }); assert.ok(l.snapshot, "the project's: an undo point first"); assert.equal(await count(dir2), 1); // Another look key leaves none. const dir3 = await project(); const o = await updateRender(dir3, { headerHeight: 0 }); assert.equal(o.snapshot, undefined); assert.equal(await count(dir3), 0); // A write that changes nothing is no edit and no undo point (review 1, 13): gapCue null where there is none. const dir4 = await project(); assert.equal((await updateClip(dir4, "c01", { gapCue: null })).snapshot, undefined); assert.equal((await updateRender(dir4, { gapCue: null })).snapshot, undefined); assert.equal(await count(dir4), 0); }); test("updateMarks: a mark's second inside a gap is refused", async () => { const dir = await project(); await updateClip(dir, "c01", { gapAdd: "15-17" }); const before = await raw(dir); await assert.rejects(updateMarks(dir, "c01", { add: { verdict: "CONTRADICTED", at: 16 } }), /lies inside the gap 15–17, which never plays.* nothing was written/); assert.equal(await raw(dir), before); const r = await updateMarks(dir, "c01", { add: { verdict: "CONTRADICTED", at: 18 } }); assert.equal(r.marks.length, 1); }); test("splitClip: each half keeps the gaps on its side; a split inside a gap is refused; merge joins them", async () => { const dir = await project(); await updateClip(dir, "c01", { gapAdd: ["14-15", "24-25"] }); await assert.rejects(splitClip(dir, "c01", 14.5), /inside the gap 14–15/); const s = await splitClip(dir, "c01", 20); const a = await clip(dir, "c01"); const b = await clip(dir, s.second); assert.deepEqual(a.gaps, [{ from: 14, to: 15 }]); assert.deepEqual(b.gaps, [{ from: 24, to: 25 }]); // Too close to the new edge: refused, by the gap rule. const dir2 = await project(); await updateClip(dir2, "c01", { gapAdd: "18-19.8" }); await assert.rejects(splitClip(dir2, "c01", 20), /split at 20: .*keep at least 0.5 s/); // Merge back: both lists, in order. await mergeClips(dir, "c01"); assert.deepEqual((await clip(dir, "c01")).gaps, [{ from: 14, to: 15 }, { from: 24, to: 25 }]); }); test("the mark at the joins: a gap's own mark and why (gap.mark=, gap.why=, gapEdit), the clip's gapCue (gapCue.*), the project's (umtool look gapCue=)", async () => { const dir = await project(); const lines = []; const io = { root: ROOT, out: (s) => lines.push(s), err: (s) => lines.push(`ERR ${s}`) }; const run = (...args) => setCommand(args, io); const name = path.basename(dir); assert.equal(await run(name, "c01", "gap.add=15-17", "gap.add=20-21"), 0); assert.equal(await run(name, "c01", "gap.mark=16:off", "gap.why=20.5:dead-air"), 0); assert.deepEqual((await clip(dir, "c01")).gaps, [{ from: 15, to: 17, mark: false }, { from: 20, to: 21, why: "dead-air" }]); assert.match(lines.at(-1), /c01: gaps 15–17 \(mark off\), 20–21 \(dead-air\)/); // Another gap added keeps theirs; default drops one; its seconds never move. assert.equal(await run(name, "c01", "gap.add=24-25", "gap.mark=16:default"), 0); assert.deepEqual((await clip(dir, "c01")).gaps, [{ from: 15, to: 17 }, { from: 20, to: 21, why: "dead-air" }, { from: 24, to: 25 }]); assert.equal(await run(name, "c01", "gap.mark=19:on"), 1, "no gap at 19"); assert.equal(await run(name, "c01", "gap.why=20.5:boring"), 2); // Its reason (the mark says it) and its own corner (lane gap-cue rulings, 20 Nov); `:` drops the words. assert.equal(await run(name, "c01", "gap.why=16:idle", "gap.reason=16:walks off", "gap.corner=16:tr"), 0); assert.deepEqual((await clip(dir, "c01")).gaps[0], { from: 15, to: 17, why: "idle", reason: "walks off", corner: "tr" }); assert.match(lines.at(-1), /15–17 \(idle, "walks off", at tr\)/); assert.equal(await run(name, "c01", "gap.reason=16:", "gap.corner=16:default", "gap.why=16:dead-air"), 0); assert.deepEqual((await clip(dir, "c01")).gaps[0], { from: 15, to: 17, why: "dead-air" }); assert.equal(await run(name, "c01", "gap.corner=16:middle"), 2); assert.equal(await run(name, "c01", "gap.reason=16:a reason far too long for one small mark"), 1); await assert.rejects(updateClip(dir, "c01", { gaps: [{ from: 15, to: 17, corner: "middle" }] }), /corner is one of tl, tr, bl, br/); // The clip's own settings, merged; one cleared; all cleared. assert.equal(await run(name, "c01", "gapCue.mark=true", "gapCue.corner=tr", "gapCue.label={s} later"), 0); assert.deepEqual((await clip(dir, "c01")).gapCue, { mark: true, corner: "tr", label: "{s} later" }); assert.equal(await run(name, "c01", "gapCue.corner="), 0); assert.deepEqual((await clip(dir, "c01")).gapCue, { mark: true, label: "{s} later" }); assert.equal(await run(name, "c01", "gapCue.min=99"), 1); assert.match(lines.at(-1), /^ERR gapCue\.min is seconds from 0\.5 to 60/); assert.equal(await run(name, "c01", "gapCue="), 0); assert.equal("gapCue" in (await clip(dir, "c01")), false); // The window route's gapEdit, through the one writer: an undo point, as every gap write. const r = await updateClip(dir, "c01", { gapEdit: { at: 24.5, mark: true } }); assert.deepEqual(r.entry.gaps[2], { from: 24, to: 25, mark: true }); assert.ok(r.snapshot, "an undo point first"); // The project's: a look key. const { lookCommand } = await import("./knobsCli.mjs"); assert.equal(await lookCommand([name, 'gapCue={"mark":true,"min":4}'], io), 0); assert.deepEqual((await read(dir)).render.gapCue, { mark: true, min: 4 }); assert.equal(await lookCommand([name, 'gapCue={"min":null}'], io), 0); assert.deepEqual((await read(dir)).render.gapCue, { mark: true }); assert.equal(await lookCommand([name, 'gapCue={"corner":"middle"}'], io), 1); // The border (variant B): the project's through the look writer, the clip's through set; refused when no colour. assert.equal(await lookCommand([name, 'gapCue={"border":"#3aa0ff"}'], io), 0); assert.deepEqual((await read(dir)).render.gapCue, { mark: true, border: "#3aa0ff" }); assert.equal(await lookCommand([name, 'gapCue={"border":"blue"}'], io), 1); assert.equal(await lookCommand([name, 'gapCue={"border":null}'], io), 0); assert.deepEqual((await read(dir)).render.gapCue, { mark: true }, "null drops it: the accent again"); assert.equal(await run(name, "c01", "gapCue.border=#ff00aa"), 0); assert.deepEqual((await clip(dir, "c01")).gapCue, { border: "#ff00aa" }); assert.equal(await run(name, "c01", "gapCue.border=accent"), 0); assert.deepEqual((await clip(dir, "c01")).gapCue, { border: "accent" }); assert.equal(await run(name, "c01", "gapCue.border=#fff"), 1); assert.match(lines.at(-1), /^ERR gapCue\.border is accent \(the deck's accent colour\) or a #rrggbb colour/); assert.equal(await run(name, "c01", "gapCue.border="), 0); assert.equal("gapCue" in (await clip(dir, "c01")), false, "cleared: the project's (absent reads as accent)"); }); test("umtool set: gap.add=, gap.remove=, gaps= (a list, JSON, or empty); timeline ls shows them", async () => { const dir = await project(); const lines = []; const run = (...args) => setCommand(args, { root: ROOT, out: (s) => lines.push(s), err: (s) => lines.push(`ERR ${s}`) }); const name = path.basename(dir); assert.equal(await run(name, "c01", "gap.add=15-17", "gap.add=20-21"), 0); assert.deepEqual((await clip(dir, "c01")).gaps, [{ from: 15, to: 17 }, { from: 20, to: 21 }]); assert.match(lines.at(-1), /c01: gaps 15–17, 20–21/); assert.equal(await run(name, "c01", "gap.remove=20.5"), 0); assert.deepEqual((await clip(dir, "c01")).gaps, [{ from: 15, to: 17 }]); const rows = timelineRows(await read(dir)); assert.deepEqual(rows[0].gaps, [{ from: 15, to: 17 }]); assert.equal(rows[0].plays, 14); assert.match(timelineLines(rows)[0], /gaps −15\.00–17\.00 \(plays 14\.00s\)/); assert.equal(await run(name, "c01", "gaps=13-14,22-23"), 0); assert.deepEqual((await clip(dir, "c01")).gaps, [{ from: 13, to: 14 }, { from: 22, to: 23 }]); assert.equal(await run(name, "c01", 'gaps=[{"from":18,"to":19}]'), 0); assert.deepEqual((await clip(dir, "c01")).gaps, [{ from: 18, to: 19 }]); assert.equal(await run(name, "c01", "gaps="), 0); assert.equal("gaps" in (await clip(dir, "c01")), false); assert.match(lines.at(-1), /no gaps/); assert.equal(await run(name, "c01", "gaps=12-zz"), 2); assert.equal(await run(name, "c01", "gap.add=11-12.5"), 1); assert.match(lines.at(-1), /^ERR .*outside the clip's cut/); });