// Claim groups (claimGroups.mjs, lane L4) through umtool's writers: the // validator's sentences, a manifest without groups left exactly as it was, // every structural edit keeping the groups (a removal drops the clip, a move // out of the run detaches it, a move alone into the middle joins, a whole // group into another's middle is refused), the writers (set, label, remove, // move, promote, import, order), one undo point each, and the CLI. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdtemp, readFile, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { clipOrder, groupModel, parseOrderText, syncGroups, validateGroups } from "./claimGroups.mjs"; import { seamsOf } from "./playCut.mjs"; import { groupLines, groupsView, timelineCommand } from "./timelineCli.mjs"; import { MANIFEST_NAME, StructureRefused, importGroups, labelGroup, manifestToken, moveEntry, moveGroup, moveSectionBoundary, orderEntries, placeEntries, promoteGroup, removeEntry, removeGroup, setGroup, undoStructural, addSection, } from "./manifest.mjs"; const clip = (id, i) => ({ type: "clip", id, video: "v1", start: 10 * i, end: 10 * i + 8 }); const IDS = ["a", "b", "c", "d", "e", "f", "g", "h"]; /** §0 a b c d | §1 "Two" e f g h; g1 = a b c, g2 = e f. */ const cut = ({ groups = true, markers = [{ after: "d", label: "Two", color: "#7fdc9a" }] } = {}) => ({ slug: "s", provenance: { siteOrigin: "https://example.test", channelSlug: "chan" }, render: { width: 1920, height: 1080, chrome: { engine: "hyperframes", layout: "deck", deck: markers ? { markers } : {} } }, timeline: IDS.map(clip), ...(groups ? { groups: [ { id: "g1", section: 0, claim: "The first claim", ids: ["a", "b", "c"] }, { id: "g2", section: 1, claim: "The second", ids: ["e", "f"] }, ], } : {}), }); async function project(manifest) { const dir = await mkdtemp(path.join(tmpdir(), "umtool-groups-")); 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 order = async (dir) => (await read(dir)).timeline.map((e) => e.id).join(" "); const groupsOf = async (dir) => (await read(dir)).groups.map((g) => `${g.id}@${g.section}:${g.ids.join(",")}`).join(" "); test("validateGroups: none is fine; each rule a sentence", () => { assert.deepEqual(validateGroups(cut({ groups: false })), []); assert.deepEqual(validateGroups(cut()), []); const m = cut(); m.groups = [ { id: "g1", section: 0, claim: "x", ids: ["a", "c"] }, { id: "g1", section: 1, claim: "", ids: ["d", "e"] }, { id: "g3", section: 1, claim: "y", ids: ["zz", "a"], extra: 1 }, { id: "g4", section: 0, claim: "z", ids: ["g"] }, ]; const errors = validateGroups(m); assert.ok(errors.some((e) => /g1 \("x"\)'s clips are not together: b stands between a and c/.test(e)), errors.join("\n")); assert.ok(errors.includes("group id g1 is used twice")); assert.ok(errors.some((e) => /needs its claim/.test(e))); assert.ok(errors.some((e) => /spans §0 \(the opening\) and §1 \(Two\): a group never crosses a section boundary/.test(e))); assert.ok(errors.some((e) => /unknown key extra/.test(e))); assert.ok(errors.some((e) => /names zz, which is not a clip of the cut/.test(e))); assert.ok(errors.some((e) => /a is in two groups: g1 and g3/.test(e))); assert.ok(errors.some((e) => /g4 \("z"\)'s section is 0, but its clips are in §1 \(Two\)/.test(e))); assert.deepEqual(validateGroups({ ...cut(), groups: {} }), ["groups must be a list of {id, section, claim, ids}"]); }); test("a manifest without groups is written as before: no groups key appears", async () => { const dir = await project(cut({ groups: false })); await moveEntry(dir, "b", 6); await removeEntry(dir, "c"); const m = await read(dir); assert.equal("groups" in m, false); assert.equal(syncGroups(m, clipOrder(m)).length, 0); }); test("a removal drops the clip from its group, and the group with its last clip", async () => { const dir = await project(cut()); const r = await removeEntry(dir, "b"); assert.ok(r.reanchored.includes('group g1 ("The first claim"): b dropped from it')); await removeEntry(dir, "e"); const r2 = await removeEntry(dir, "f"); assert.ok(r2.reanchored.includes('group g2 ("The second") dropped: f was its only clip')); assert.equal(await groupsOf(dir), "g1@0:a,c"); }); test("a clip moved out of its run is detached; moved alone into the middle of one, it joins; to its edge, nothing", async () => { const dir = await project(cut()); const r = await moveEntry(dir, "a", 7); assert.equal(await order(dir), "b c d e f g h a"); assert.deepEqual(r.groupNotes, ['a was moved out of group g1 ("The first claim")\'s run: detached from it']); assert.equal(await groupsOf(dir), "g1@0:b,c g2@1:e,f"); // h to the middle of g2 (between e and f): it joins. const j = await moveEntry(dir, "h", 4); assert.equal(await order(dir), "b c d e h f g a"); assert.ok(j.groupNotes.some((x) => /h joined group g2/.test(x))); assert.equal(await groupsOf(dir), "g1@0:b,c g2@1:e,h,f"); // a right after f (the run's edge): it stays out. await moveEntry(dir, "a", 6); assert.equal(await order(dir), "b c d e h f a g"); assert.equal(await groupsOf(dir), "g1@0:b,c g2@1:e,h,f"); // A clip moved within its own run stays in it. const w = await placeEntries(dir, ["f"], { before: "e", section: 1 }); assert.equal(w.groupNotes, undefined); assert.equal(await groupsOf(dir), "g1@0:b,c g2@1:f,e,h"); }); test("a section boundary dragged into a group is refused in words; one at its edge is fine", async () => { const dir = await project(cut()); const before = await raw(dir); await assert.rejects(moveSectionBoundary(dir, 0, "a"), (e) => e instanceof StructureRefused && /g1 \("The first claim"\) spans §0 \(the opening\) and §1 \(Two\)/.test(e.message)); assert.equal(await raw(dir), before); await moveSectionBoundary(dir, 0, "c"); // d is §1 now; the groups' sections follow the markers. assert.equal(await groupsOf(dir), "g1@0:a,b,c g2@1:e,f"); await assert.rejects(addSection(dir, "e", { label: "Mid" }), /g2 \("The second"\) spans/); }); test("groups move: the whole group, before a group, after one, or before a row; into another's middle is refused", async () => { const dir = await project(cut()); const r = await moveGroup(dir, "g2", { to: 0 }); assert.equal(await order(dir), "e f a b c d g h"); assert.equal(r.words, 'before g1 ("The first claim")'); // The markers follow so every other clip keeps its section; g2 is in §0 now. assert.equal((await read(dir)).render.chrome.deck.markers[0].after, "d"); assert.equal(await groupsOf(dir), "g1@0:a,b,c g2@0:e,f"); await moveGroup(dir, "g2", { after: "g1" }); assert.equal(await order(dir), "a b c e f d g h"); // Before a row (a bar dropped on it): in that row's section. await moveGroup(dir, "g1", { before: "h" }); assert.equal(await order(dir), "e f d g a b c h"); assert.equal(await groupsOf(dir), "g1@1:a,b,c g2@0:e,f"); // Into the middle of another group: refused, nothing written. const before = await raw(dir); await assert.rejects(moveGroup(dir, "g2", { before: "b" }), (e) => e instanceof StructureRefused && /g1 \("The first claim"\)'s clips are not together/.test(e.message)); assert.equal(await raw(dir), before); }); test("groups set, label, remove: a fresh id, clips taken from another group, refused when not together", async () => { const dir = await project(cut()); await assert.rejects(setGroup(dir, null, ["g", "h"], {}), /needs its claim/); const r = await setGroup(dir, null, ["g", "h"], { claim: "A third" }); assert.equal(r.id, "g3"); assert.equal(r.created, true); await assert.rejects(setGroup(dir, null, ["d", "f"], { claim: "No" }), /not together/); // Taking b, g1's middle clip, would leave a and c apart: refused. await assert.rejects(setGroup(dir, null, ["b"], { claim: "B" }), (e) => e instanceof StructureRefused && /g1 \("The first claim"\)'s clips are not together/.test(e.message)); // c leaves g1 for a new group with d (g1 stays together: a b). await setGroup(dir, "g9", ["c", "d"], { claim: "Two clips" }); assert.equal(await groupsOf(dir), "g1@0:a,b g2@1:e,f g3@1:g,h g9@0:c,d"); await labelGroup(dir, "g2", " The second, reworded "); assert.equal((await read(dir)).groups.find((g) => g.id === "g2").claim, "The second, reworded"); await assert.rejects(labelGroup(dir, "g2", "x".repeat(161)), /at most 160/); await removeGroup(dir, "g3"); assert.equal(await groupsOf(dir), "g1@0:a,b g2@1:e,f g9@0:c,d"); assert.equal(await order(dir), IDS.join(" ")); }); test("promote: a level-2 marker at the group's first clip, titled with its claim; the long, the opening and §0 refused", async () => { const dir = await project(cut()); await assert.rejects(promoteGroup(dir, "g2", {}), /no clip before|already opens/); await setGroup(dir, null, ["g", "h"], { claim: "Third: a claim far too long for a marker" }); await assert.rejects(promoteGroup(dir, "g3", {}), /a marker's label is at most 24 characters and the claim is 40 — give the subsection a shorter title \(--label\)/); const r = await promoteGroup(dir, "g3", { label: "Third" }); assert.equal(r.after, "f"); assert.deepEqual((await read(dir)).render.chrome.deck.markers, [ { after: "d", label: "Two", color: "#7fdc9a" }, { after: "f", label: "Third", level: 2 }, ]); assert.equal(await groupsOf(dir), "g1@0:a,b,c g2@1:e,f g3@2:g,h"); assert.equal(groupModel(await read(dir)).find((g) => g.id === "g3").promoted, true); await assert.rejects(promoteGroup(dir, "g3", { label: "Again" }), /already opens a subsection/); // g1 opens the cut; a group inside §0 would make the cut's first marker a subsection. await assert.rejects(promoteGroup(dir, "g1", {}), /opens the cut/); await setGroup(dir, "g1", ["b", "c"]); await assert.rejects(promoteGroup(dir, "g1", {}), /the cut's first marker opens a section/); }); const FILE = { sections: [ { label: "Lead", groups: [{ claim: "Opens", ids: ["c", "a"] }, { claim: "Then", ids: ["b", "d"] }] }, { label: "two", groups: [{ claim: "Second section", ids: ["h", "e"] }, { claim: "Gone", ids: ["zz"] }] }, ], }; test("import: the cut re-ordered to the file, its groups the cut's, in ONE undoable write", async () => { const dir = await project(cut({ groups: false })); const before = await raw(dir); await assert.rejects(importGroups(dir, FILE), /names 1 clip the cut does not have: zz — pass --skip-missing/); const r = await importGroups(dir, FILE, { skipMissing: true }); assert.equal(r.groups, 3); // "Lead" has no deck section of its name: its clips are in the opening. "two" is "Two". assert.deepEqual(r.sections.map((s) => [s.label, s.section]), [["Lead", 0], ["two", 1], ]); assert.ok(r.notes.some((x) => /"Lead" → §0 \(the opening\): the deck has no section of that name; most of its clips are there/.test(x))); assert.ok(r.notes.some((x) => /not in the file, kept at the end of their sections: f, g/.test(x))); assert.ok(r.notes.some((x) => /1 group left out/.test(x))); assert.equal(await order(dir), "c a b d h e f g"); assert.equal(await groupsOf(dir), "g1@0:c,a g2@0:b,d g3@1:h,e"); const view = groupsView(await read(dir)); assert.deepEqual(view.sections.map((s) => s.groups.map((g) => g.claim)), [["Opens", "Then"], ["Second section"]]); assert.match(groupLines(view).join("\n"), /§1 Two\n {2}g3 {3}Second section {2}\[h e\]\n {2}ungrouped: f g\n3 groups/); // Undo: the cut exactly as it was, byte for byte. await undoStructural(dir); assert.equal(await raw(dir), before); }); test("import: a section the file leaves empty is refused, or its marker removed with --drop-empty; out of order refused", async () => { const dir = await project(cut({ groups: false, markers: [{ after: "b", label: "Mid" }, { after: "d", label: "Two" }] })); const doc = { sections: [{ label: "Open", groups: [{ claim: "x", ids: ["a", "b", "c", "d"] }] }, { label: "Two", groups: [{ claim: "y", ids: ["e", "f", "g", "h"] }] }] }; await assert.rejects(importGroups(dir, doc), /leaves §1 \(Mid\) with no clip .* --drop-empty/); const r = await importGroups(dir, doc, { dropEmpty: true }); assert.deepEqual(r.dropped, [{ index: 1, label: "Mid" }]); assert.deepEqual((await read(dir)).render.chrome.deck.markers, [{ after: "d", label: "Two" }]); const flipped = { sections: [{ label: "Two", groups: [{ claim: "y", ids: ["e"] }] }, { label: "Mid", groups: [{ claim: "x", ids: ["a"] }] }] }; const dir2 = await project(cut({ groups: false, markers: [{ after: "b", label: "Mid" }, { after: "d", label: "Two" }] })); await assert.rejects(importGroups(dir2, flipped), /out of the cut's order: "Mid" \(§1 \(Mid\)\) comes after "Two"/); await assert.rejects(importGroups(dir2, { sections: [{ label: "x", groups: [{ claim: "y", ids: ["a", "a"] }] }] }), /a is named twice/); }); test("order: the named entries in the places they hold; a clip across a boundary refused; the groups follow", async () => { const dir = await project(cut()); await orderEntries(dir, ["c", "b", "a"]); assert.equal(await order(dir), "c b a d e f g h"); assert.equal(await groupsOf(dir), "g1@0:c,b,a g2@1:e,f"); await assert.rejects(orderEntries(dir, ["e", "d"]), /e \(§1 \(Two\)\) would stand after|d \(§0 \(the opening\)\)|would stand after/); // The whole order: g pulled between e and f joins g2 (it moved, they did not). const r = await orderEntries(dir, ["c", "b", "a", "d", "e", "g", "f", "h"]); assert.ok(r.groupNotes.some((x) => /g joined group g2/.test(x))); assert.equal(await groupsOf(dir), "g1@0:c,b,a g2@1:e,g,f"); assert.equal((await read(dir)).render.chrome.deck.markers[0].after, "d"); assert.deepEqual(parseOrderText("a, b\nc"), ["a", "b", "c"]); assert.deepEqual(parseOrderText('{"order": ["x"]}'), ["x"]); assert.deepEqual(parseOrderText(JSON.stringify(FILE)), ["c", "a", "b", "d", "h", "e", "zz"]); }); test("Play's seams: a group's lighter seam before its first clip, none where a section opens; without groups, as before", () => { const m = cut(); const entries = m.timeline.map((e) => ({ id: e.id, type: "clip" })); const markers = m.render.chrome.deck.markers; assert.deepEqual(seamsOf(entries, markers), seamsOf(entries, markers, [])); const s = seamsOf(entries, markers, [...m.groups, { id: "g3", claim: "Late", ids: ["g", "h"] }]); assert.deepEqual(s.map((x) => [x.before, x.kind ?? "section", x.label]), [ ["e", "section", "Two"], ["g", "group", "Late"], ]); }); test("umtool timeline
groups: import, ls --json in the file's shape, move, promote, undo", async () => { const root = await mkdtemp(path.join(tmpdir(), "umtool-groups-cli-")); const dir = path.join(root, "p"); await (await import("node:fs/promises")).mkdir(dir); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify({ ...cut({ groups: false }), title: "P" }, null, 2) + "\n"); const file = path.join(root, "claim-groups.json"); await writeFile(file, JSON.stringify(FILE)); const out = []; const err = []; const run = (...args) => timelineCommand(["p", ...args], { root, out: (s) => out.push(s), err: (s) => err.push(s) }); assert.equal(await run("groups", "import", file), 1); assert.match(err.join("\n"), /--skip-missing/); assert.equal(await run("groups", "import", file, "--skip-missing"), 0, err.join("\n")); assert.match(out.join("\n"), /imported 3 groups, the cut re-ordered to the file/); assert.match(out.join("\n"), /undo point: revisions\/.*auto-before-groups-import/); out.length = 0; assert.equal(await run("groups", "ls", "--json"), 0); const j = JSON.parse(out.join("\n")); assert.deepEqual(j.sections.map((s) => s.groups.map((g) => [g.claim, g.ids])), [[["Opens", ["c", "a"]], ["Then", ["b", "d"]]], [["Second section", ["h", "e"]]]]); out.length = 0; assert.equal(await run("groups", "move", "g2", "--to", "0"), 0, err.join("\n")); assert.match(out.join("\n"), /moved group g2 \(b d\) before g1/); assert.equal(await run("groups", "move", "g2", "--to", "after", "g1"), 0, err.join("\n")); out.length = 0; assert.equal(await run("groups", "promote", "g2", "--label", "Then"), 1); assert.match(err.join("\n"), /the cut's first marker opens a section/); assert.equal(await run("groups", "label", "g3", "Second,", "said", "again"), 0, err.join("\n")); assert.equal(await run("groups", "set", "-", "f", "g", "--claim", "Late"), 0, err.join("\n")); out.length = 0; assert.equal(await run("groups", "promote", "g4"), 0, err.join("\n")); assert.match(out.join("\n"), /group g4 opens a subsection of §1 \(Two\): marker 1 after e, "Late"/); assert.equal(await run("order", "a", "c"), 0, err.join("\n")); const tok = await manifestToken(dir); assert.ok(tok); });