// Sections as sections (sectionEdit.mjs) and their writers in manifest.mjs: // move to section, new section here, drag / remove a boundary, the take // patches' markers kept in step, flash-forward a span, and a split that keeps // its section. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { addSectionIn, moveBoundaryIn, moveToSectionIn, removeBoundaryIn, sectionModel, setSectionIn } from "./sectionEdit.mjs"; import { syncSectionMarkers } from "./patchMarkers.mjs"; import { MANIFEST_NAME, StructureRefused, addSection, flashForward, flashForwardIn, moveSectionBoundary, moveToSection, removeSection, splitClip, undoStructural, } from "./manifest.mjs"; const clip = (id, start, end, extra = {}) => ({ type: "clip", id, video: "v1", start, end, ...extra }); // §0 a b | §1 "Two" c d | card k | §2 "Three" e f const deckCut = () => ({ slug: "s", provenance: { siteOrigin: "https://example.test", channelSlug: "chan" }, render: { width: 1920, height: 1080, chrome: { engine: "hyperframes", layout: "deck", deck: { markers: [ { after: "b", label: "Two", color: "#ff0000" }, { after: "d", label: "Three", color: "#00ff00" }, ], }, }, }, timeline: [ clip("a", 10, 20), clip("b", 30, 40), clip("c", 50, 60), clip("d", 70, 80), { type: "card", id: "k", seconds: 3 }, clip("e", 90, 100), clip("f", 110, 120), ], }); const ids = (m) => m.timeline.map((e) => e.id); const afters = (m) => (m.render.chrome.deck.markers ?? []).map((mk) => mk.after); test("sectionModel reads the sections off the markers", () => { const { sections, sectionOf, entrySection } = sectionModel(deckCut()); assert.deepEqual(sections.map((s) => [s.index, s.label, s.clips]), [[0, null, ["a", "b"]], [1, "Two", ["c", "d"]], [2, "Three", ["e", "f"]]]); assert.equal(sectionOf.get("e"), 2); assert.equal(entrySection[4], 2, "the card after d's boundary stands in §2"); }); test("move to section: the clip lands at the section's end and the markers follow", () => { const m = deckCut(); const r = moveToSectionIn(m, ["a"], 1); assert.deepEqual(ids(m), ["b", "c", "d", "a", "k", "e", "f"]); assert.deepEqual(afters(m), ["b", "a"]); assert.equal(r.section, 1); assert.deepEqual(r.reanchored, ['marker 1 ("Three") moved from after d to after a']); }); test("move the clip a marker follows: the marker moves to the clip before it", () => { const m = deckCut(); moveToSectionIn(m, ["d"], 2, { position: "start" }); assert.deepEqual(ids(m), ["a", "b", "c", "d", "k", "e", "f"], "d is already where §2 starts — the boundary moves instead"); assert.deepEqual(afters(m), ["b", "c"]); const m2 = deckCut(); moveToSectionIn(m2, ["b", "c"], 2); assert.deepEqual(ids(m2), ["a", "d", "k", "e", "f", "b", "c"]); assert.deepEqual(afters(m2), ["a", "d"]); }); test("a section left with no clip is refused, in words", () => { const m = deckCut(); assert.throws(() => moveToSectionIn(m, ["c", "d"], 0), /§1 \(Two\) would have no clip left/); assert.throws(() => moveToSectionIn(deckCut(), ["k"], 0), /a section move takes clips/); assert.throws(() => moveToSectionIn(deckCut(), ["a"], 3), /section must be 0–2/); }); test("new section here: a marker after the clip, in the cut's order, with its colour", () => { const m = deckCut(); const r = addSectionIn(m, "e", { label: "Four", color: "#123456" }); assert.deepEqual(m.render.chrome.deck.markers.map((x) => [x.after, x.label ?? null]), [["b", "Two"], ["d", "Three"], ["e", "Four"]]); assert.equal(r.opens, "f"); assert.throws(() => addSectionIn(deckCut(), "f", { label: "x" }), /the last clip/); assert.throws(() => addSectionIn(deckCut(), "b", { label: "x" }), /already starts after b/); const m2 = deckCut(); addSectionIn(m2, "a", {}); assert.deepEqual(afters(m2), ["a", "b", "d"], "an unlabelled one first in the list too"); }); test("drag a boundary: between its neighbours, never crossing them", () => { const m = deckCut(); const r = moveBoundaryIn(m, 1, "c"); assert.deepEqual(afters(m), ["b", "c"]); assert.deepEqual(r.moved, ["d"]); assert.throws(() => moveBoundaryIn(deckCut(), 1, "b"), /would cross or touch/); assert.throws(() => moveBoundaryIn(deckCut(), 1, "f"), /would have no clip/); assert.throws(() => moveBoundaryIn(deckCut(), 0, "d"), /would have no clip/); const m2 = deckCut(); moveBoundaryIn(m2, 1, "e"); assert.deepEqual(sectionModel(m2).sections[2].clips, ["f"]); }); test("remove a boundary joins two sections; set relabels and recolours", () => { const m = deckCut(); removeBoundaryIn(m, 0); assert.deepEqual(sectionModel(m).sections.map((s) => s.clips.length), [4, 2]); const m2 = deckCut(); setSectionIn(m2, 1, { label: "Drei", color: "" }); assert.deepEqual(m2.render.chrome.deck.markers[1], { after: "d", label: "Drei" }); }); test("a cut with no deck says so", () => { const m = deckCut(); delete m.render.chrome; assert.throws(() => addSectionIn(m, "a", { label: "x" }), /no deck — turn the deck on first/); }); async function project(manifest, extra = {}) { const dir = await mkdtemp(path.join(tmpdir(), "umtool-sections-")); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify(manifest, null, 2) + "\n"); for (const [name, doc] of Object.entries(extra)) await writeFile(path.join(dir, name), typeof doc === "string" ? doc : JSON.stringify(doc, null, 2) + "\n"); return dir; } const read = async (dir, name = MANIFEST_NAME) => JSON.parse(await readFile(path.join(dir, name), "utf8")); const patchOf = (markers) => ({ render: { chrome: { deck: { markers } } }, title: "T" }); test("the writers keep in-step take patches in step, and leave a variant alone", async () => { const m = deckCut(); const dir = await project(m, { "draft-1.patch.json": patchOf(m.render.chrome.deck.markers), "variant.patch.json": patchOf([{ after: "a", label: "Other" }]), "plain.patch.json": { title: "no markers" }, }); try { const r = await moveToSection(dir, ["a"], 1); assert.deepEqual(r.sectionPatches.synced, ["draft-1.patch.json"]); assert.deepEqual(r.sectionPatches.skipped.map((x) => x.file), ["variant.patch.json"]); assert.deepEqual((await read(dir, "draft-1.patch.json")).render.chrome.deck.markers.map((x) => x.after), ["b", "a"]); assert.deepEqual((await read(dir, "variant.patch.json")).render.chrome.deck.markers, [{ after: "a", label: "Other" }]); assert.deepEqual(await read(dir, "plain.patch.json"), { title: "no markers" }); const r2 = await addSection(dir, "e", { label: "Four", color: "amber" }); assert.deepEqual(r2.sectionPatches.synced, ["draft-1.patch.json"]); assert.equal((await read(dir, "draft-1.patch.json")).render.chrome.deck.markers.length, 3); await moveSectionBoundary(dir, 1, "c"); await removeSection(dir, 2); const p = await read(dir, "draft-1.patch.json"); assert.deepEqual(p.render.chrome.deck.markers, (await read(dir)).render.chrome.deck.markers); // Undo restores the manifest (the patch is the operator's to re-sync: it says so in the check). await undoStructural(dir); } finally { await rm(dir, { recursive: true, force: true }); } }); test("a marker past the deck's limits is refused in its words, nothing written", async () => { const m = deckCut(); const dir = await project(m); try { const before = await readFile(path.join(dir, MANIFEST_NAME), "utf8"); await assert.rejects(addSection(dir, "a", { label: "a label far longer than twenty-four characters" }), (e) => e instanceof StructureRefused && /at most 24/.test(e.message)); assert.equal(await readFile(path.join(dir, MANIFEST_NAME), "utf8"), before); } finally { await rm(dir, { recursive: true, force: true }); } }); test("syncSectionMarkers keeps the patch's own indent", async () => { const dir = await project(deckCut(), { "one.patch.json": JSON.stringify(patchOf([{ after: "b" }]), null, 1) + "\n" }); try { await syncSectionMarkers(dir, [{ after: "b" }], [{ after: "c" }]); const text = await readFile(path.join(dir, "one.patch.json"), "utf8"); assert.match(text, /^\{\n "render"/); assert.equal(JSON.parse(text).render.chrome.deck.markers[0].after, "c"); } finally { await rm(dir, { recursive: true, force: true }); } }); test("split keeps the section: a marker after the clip follows its second half; the QR follows the second", async () => { const m = deckCut(); m.timeline[1] = { ...m.timeline[1], cite: 30, citeUrl: "https://archive.example/?v=chan%2Fv1&t=30", lockEnd: true, title: "Stream", date: "2026-10-27" }; const dir = await project(m, { "draft-1.patch.json": patchOf(m.render.chrome.deck.markers) }); try { const r = await splitClip(dir, "b", 35); const after = await read(dir); assert.deepEqual(ids(after), ["a", "b", "b-2", "c", "d", "k", "e", "f"]); assert.deepEqual(afters(after), ["b-2", "d"]); const b2 = after.timeline[2]; assert.equal(b2.cite, 35); assert.match(b2.citeUrl, /t=35$/); assert.equal(b2.lockEnd, true); assert.equal(b2.title, "Stream"); assert.deepEqual(r.sectionPatches.synced, ["draft-1.patch.json"]); } finally { await rm(dir, { recursive: true, force: true }); } }); test("flash-forward copies a span earlier with its attribution and locks, as a repeat", () => { const m = deckCut(); Object.assign(m.timeline[5], { channel: "chan", cite: 90, citeUrl: "https://archive.example/?v=chan%2Fv1&t=90", lock: true, title: "Stream", date: "2026-10-27", quote: "the whole thing", speakers: ["Host"], claim: { id: "e", verdict: "reason" }, gaps: [{ from: 92, to: 93 }, { from: 97, to: 98 }], }); const r = flashForwardIn(m, "e", { from: 95.5, to: 99.25, after: "a", quote: "the tight line" }); assert.equal(r.id, "et"); assert.deepEqual(ids(m), ["a", "et", "b", "c", "d", "k", "e", "f"]); const c = m.timeline[1]; assert.equal(c.start, 95.5); assert.equal(c.end, 99.25); assert.equal(c.cite, 95); assert.match(c.citeUrl, /t=95$/); assert.equal(c.repeatOf, "e"); assert.equal(c.lock, true); assert.equal(c.title, "Stream"); assert.equal(c.date, "2026-10-27"); assert.deepEqual(c.speakers, ["Host"]); assert.equal(c.quote, "the tight line"); assert.equal(c.claim, undefined, "one claim, one entry"); assert.deepEqual(c.gaps, [{ from: 97, to: 98 }]); assert.equal(r.earlier, true); assert.equal(m.timeline[6].end, 100, "the clip itself is untouched"); }); test("flash-forward into a section, and a moved span takes the clip's edge with it", () => { const m = deckCut(); m.timeline[3].cite = 70; const r = flashForwardIn(m, "d", { from: 75, to: 80, section: 0, move: true, restQuote: "the first half" }); assert.deepEqual(ids(m), ["a", "b", "dt", "c", "d", "k", "e", "f"]); assert.deepEqual(afters(m), ["dt", "d"]); assert.equal(r.move, true); assert.equal(m.timeline[2].repeatOf, undefined); assert.deepEqual(r.rest, { start: 70, end: 75 }); assert.equal(m.timeline[4].quote, "the first half"); assert.throws(() => flashForwardIn(deckCut(), "d", { from: 72, to: 78, move: true }), /in the middle of d/); assert.throws(() => flashForwardIn(deckCut(), "d", { from: 60, to: 78 }), /not inside d's window/); }); test("flashForward writes once, with an undo point", async () => { const dir = await project(deckCut()); try { const r = await flashForward(dir, "f", { from: 111, to: 115, after: "a" }); assert.equal(r.id, "ft"); assert.ok(r.snapshot?.rel?.includes("auto-before-flash-forward")); await undoStructural(dir); assert.deepEqual(ids(await read(dir)), ids(deckCut())); } finally { await rm(dir, { recursive: true, force: true }); } }); test("flash-forward to the start of §0 when the copy already lands there", () => { const m = deckCut(); const r = flashForwardIn(m, "c", { from: 51, to: 55, section: 0, position: "start" }); assert.deepEqual(ids(m).slice(0, 2), ["ct", "a"]); assert.equal(r.section.index, 0); });