// Subsections (`level: 2` on a deck marker, 2026-11-01) through umtool's // writers: a subsection is a section to every op (it is dragged, moved into, // never left empty), it sits inside its parent (the colour, "Section › Sub" // in words), the cut's first marker must be a section -- refused in words, // nothing written -- and the deck takes up to 32 markers. // // 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 { MARKER_LIMITS } from "umtool-report-to-video/marker-limits"; import { sectionModel, sectionWords } from "./sectionEdit.mjs"; import { sectionName } from "./stripDrag.mjs"; import { sectionLines } from "./timelineCli.mjs"; import { MANIFEST_NAME, StructureRefused, addSection, manifestToken, moveToSection, removeSection, updateDeckMarkers, updateSection, } from "./manifest.mjs"; const clip = (id, start, end) => ({ type: "clip", id, video: "v1", start, end }); const cut = (markers, n = 8) => ({ slug: "s", provenance: { siteOrigin: "https://example.test", channelSlug: "chan" }, render: { width: 1920, height: 1080, chrome: { engine: "hyperframes", layout: "deck", deck: markers ? { markers } : {} } }, timeline: Array.from({ length: n }, (_, i) => clip(`c${i}`, 10 * i, 10 * i + 8)), }); async function project(manifest) { const dir = await mkdtemp(path.join(tmpdir(), "umtool-levels-")); 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 markersOf = async (dir) => JSON.parse(await raw(dir)).render.chrome.deck.markers ?? []; const NESTED = [ { after: "c1", label: "Who she is", color: "#e070d0" }, { after: "c3", label: "Early days", level: 2 }, { after: "c5", label: "After", color: "#7fdc9a" }, ]; test("sectionModel: a subsection is a section, with its level, its parent and its parent's colour; in words \"Section › Sub\"", () => { const { sections, sectionOf } = sectionModel(cut(NESTED)); assert.deepEqual(sections.map((s) => [s.index, s.level, s.label, s.color, s.parent ?? null, s.clips.join(" ")]), [ [0, 1, null, null, null, "c0 c1"], [1, 1, "Who she is", "#e070d0", null, "c2 c3"], [2, 2, "Early days", "#e070d0", 1, "c4 c5"], [3, 1, "After", "#7fdc9a", null, "c6 c7"], ]); assert.equal(sectionOf.get("c4"), 2); assert.equal(sectionWords(sections[2]), "§2 (Who she is › Early days)"); assert.equal(sectionWords(sections[1]), "§1 (Who she is)"); // The strip's drop words and the CLI's `sections` say it too. assert.equal(sectionName([{ index: 2, label: "Early days", level: 2, within: "Who she is" }], 2), "§2 Who she is › Early days"); assert.match(sectionLines(sections)[2], / › Early days/); }); test("updateDeckMarkers: level 1 is left out, \"2\" is 2, 3 is refused; a leading subsection is refused, nothing written", async () => { const dir = await project(cut(null)); try { await updateDeckMarkers(dir, { markers: [{ after: "c1", label: "A", level: 1 }, { after: "c3", label: "a", level: "2" }] }, { token: await manifestToken(dir) }); assert.deepEqual(await markersOf(dir), [{ after: "c1", label: "A" }, { after: "c3", label: "a", level: 2 }]); const before = await raw(dir); await assert.rejects(updateDeckMarkers(dir, { markers: [{ after: "c1", label: "A", level: 3 }] }), (e) => e instanceof StructureRefused && /level must be 1 \(a section\) or 2/.test(e.message)); // Listed second, but it stands first in the cut. await assert.rejects( updateDeckMarkers(dir, { markers: [{ after: "c3", label: "A" }, { after: "c1", label: "a", level: 2 }] }), (e) => e instanceof StructureRefused && /markers\[1\] \(after c1\) is a subsection \(level 2\) with no section before it/.test(e.message), ); assert.equal(await raw(dir), before); } finally { await rm(dir, { recursive: true, force: true }); } }); test("the cap: 32 markers are written, 33 refused", async () => { const dir = await project(cut(null, 40)); try { const marks = (n) => Array.from({ length: n }, (_, i) => ({ after: `c${i}`, label: `S${i}`, ...(i % 3 === 2 ? { level: 2 } : {}) })); await updateDeckMarkers(dir, { markers: marks(MARKER_LIMITS.count) }); assert.equal((await markersOf(dir)).length, 32); await assert.rejects(updateDeckMarkers(dir, { markers: marks(33) }), /33 markers \(at most 32\)/); } finally { await rm(dir, { recursive: true, force: true }); } }); test("section ops: add a subsection, set a level, move a clip into one; an op that leaves a subsection first is refused", async () => { const dir = await project(cut([{ after: "c1", label: "Who she is" }, { after: "c5", label: "After" }])); try { const r = await addSection(dir, "c3", { label: "Early days", level: "2" }); assert.equal(r.level, 2); assert.deepEqual((await markersOf(dir)).map((m) => [m.after, m.level ?? 1]), [["c1", 1], ["c3", 2], ["c5", 1]]); // Moved into the subsection like into any section; the markers follow. await moveToSection(dir, ["c0"], 2); const m1 = JSON.parse(await raw(dir)); assert.deepEqual(sectionModel(m1).sections[2].clips, ["c4", "c5", "c0"]); // Removing its section's boundary would leave it the cut's first marker: refused, nothing written. const before = await raw(dir); await assert.rejects(removeSection(dir, 0), (e) => e instanceof StructureRefused && /subsection \(level 2\) with no section before it/.test(e.message)); assert.equal(await raw(dir), before); // Back to a section: the key goes. await updateSection(dir, 1, { level: 1 }); assert.equal("level" in (await markersOf(dir))[1], false); await updateSection(dir, 1, { level: 2 }); assert.equal((await markersOf(dir))[1].level, 2); } finally { await rm(dir, { recursive: true, force: true }); } }); test("a leading subsection at its birth: a level-2 section added before every other marker is refused", async () => { const dir = await project(cut([{ after: "c4", label: "Later" }])); try { const before = await raw(dir); await assert.rejects(addSection(dir, "c1", { label: "Early", level: 2 }), (e) => e instanceof StructureRefused && /no section before it/.test(e.message)); assert.equal(await raw(dir), before); } finally { await rm(dir, { recursive: true, force: true }); } });