// PLACE: a drop on Play's reel strip (sectionEdit.mjs placeIn, manifest.mjs placeEntries, // `umtool timeline

place`): before an entry, into a section, the markers following. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { execFile } from "node:child_process"; import { mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; import { promisify } from "node:util"; import { placeIn, sectionModel } from "./sectionEdit.mjs"; import { MANIFEST_NAME, placeEntries, undoStructural } from "./manifest.mjs"; const clip = (id, start, end) => ({ type: "clip", id, video: "v1", start, end }); // §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" }, { after: "d", label: "Three" }] } }, }, 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); const secs = (m) => sectionModel(m).sections.map((s) => s.clips.join("")); test("within a section: the order changes, no boundary moves", () => { const m = deckCut(); const r = placeIn(m, ["a"], { before: "c", section: 0 }); assert.deepEqual(ids(m), ["b", "a", "c", "d", "k", "e", "f"]); assert.deepEqual(afters(m), ["a", "d"], "the marker that followed b follows a, the clip now last in §0"); assert.deepEqual(secs(m), ["ba", "cd", "ef"]); assert.equal(r.section, 0); assert.equal(r.from, 0); assert.equal(r.to, 1); }); test("moving the clip a marker follows keeps the boundary where it was", () => { const m = deckCut(); placeIn(m, ["b"], { before: "a" }); assert.deepEqual(ids(m), ["b", "a", "c", "d", "k", "e", "f"]); assert.deepEqual(secs(m), ["ba", "cd", "ef"], "c and d stay in §1 (a plain `move` took the boundary with b)"); }); test("at a seam the section decides: the end of §0 or the start of §1, the same slot", () => { const end = deckCut(); placeIn(end, ["e"], { before: "c", section: 0 }); assert.deepEqual(ids(end), ["a", "b", "e", "c", "d", "k", "f"]); assert.deepEqual(secs(end), ["abe", "cd", "f"]); const start = deckCut(); placeIn(start, ["e"], { before: "c", section: 1 }); assert.deepEqual(ids(start), ["a", "b", "e", "c", "d", "k", "f"]); assert.deepEqual(secs(start), ["ab", "ecd", "f"]); // The default is the section of the clip it lands after. const def = deckCut(); assert.equal(placeIn(def, ["e"], { before: "c" }).section, 0); }); test("across sections, inside one: the drop's section is the slot's", () => { const m = deckCut(); const r = placeIn(m, ["a"], { before: "f" }); assert.deepEqual(ids(m), ["b", "c", "d", "k", "e", "a", "f"]); assert.deepEqual(secs(m), ["b", "cd", "eaf"]); assert.equal(r.section, 2); assert.deepEqual(r.fromSections, { a: 0 }); // At the end. const m2 = deckCut(); placeIn(m2, ["c"], { before: null }); assert.deepEqual(ids(m2).slice(-1), ["c"]); assert.deepEqual(secs(m2), ["ab", "d", "efc"]); }); test("refused in words: an emptied section, a section the slot cannot be in, nothing to do", () => { const m = deckCut(); placeIn(m, ["c"], { before: "e", section: 2 }); assert.throws(() => placeIn(m, ["d"], { before: "a", section: 0 }), /§1 \(Two\) would have no clip left/); assert.throws(() => placeIn(deckCut(), ["a"], { before: "f", section: 0 }), /cannot join §0 \(the opening\)/); assert.throws(() => placeIn(deckCut(), ["a"], { before: "b", section: 0 }), /already stands there in §0/); assert.throws(() => placeIn(deckCut(), ["a"], { before: "a" }), /one of the entries being placed/); assert.throws(() => placeIn(deckCut(), ["zz"], { before: "a" }), /no timeline entry with id zz/); assert.throws(() => placeIn(deckCut(), ["a"], { before: "zz" }), /no timeline entry with id zz to place before/); }); test("a section's only clip may move within its own seam; a card moves to the slot", () => { const m = deckCut(); // §1 down to one clip (d), then d moves across the card: still §1, alone. placeIn(m, ["c"], { before: "a", section: 0 }); placeIn(m, ["d"], { before: "e", section: 1 }); assert.deepEqual(secs(m), ["cab", "d", "ef"]); const k = deckCut(); placeIn(k, ["k"], { before: "a" }); assert.deepEqual(ids(k), ["k", "a", "b", "c", "d", "e", "f"]); assert.deepEqual(secs(k), ["ab", "cd", "ef"]); }); test("no deck: a plain move to the slot", () => { const m = deckCut(); delete m.render.chrome; const r = placeIn(m, ["f"], { before: "a", section: 2 }); assert.deepEqual(ids(m), ["f", "a", "b", "c", "d", "k", "e"]); assert.equal(r.section, null); }); async function project() { const dir = await mkdtemp(path.join(tmpdir(), "place-")); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify(deckCut(), null, 2) + "\n"); // A take patch in step with the manifest's markers. await writeFile(path.join(dir, "draft-1.patch.json"), JSON.stringify({ title: "x", render: { chrome: { deck: { markers: deckCut().render.chrome.deck.markers } } } }, null, 2) + "\n"); return dir; } test("placeEntries: one write, its own undo point every time, the take patch's markers in step", async () => { const dir = await project(); try { const r1 = await placeEntries(dir, ["b"], { before: "f" }); assert.ok(r1.snapshot?.rel && !r1.snapshot.skipped, "an undo point"); assert.deepEqual(r1.sectionPatches.synced, ["draft-1.patch.json"]); const patch = JSON.parse(await readFile(path.join(dir, "draft-1.patch.json"), "utf8")); assert.deepEqual(patch.render.chrome.deck.markers.map((x) => x.after), ["a", "d"], "the boundary b followed is after a now, in the patch too"); // A second drop straight after is its own undo point (no 2-minute coalescing). await new Promise((res) => setTimeout(res, 1100)); const r2 = await placeEntries(dir, ["c"], { before: "a", section: 0, token: r1.token }); assert.ok(r2.snapshot?.rel && !r2.snapshot.skipped && r2.snapshot.rel !== r1.snapshot.rel); // Undo takes back exactly the second. await undoStructural(dir, { token: r2.token }); const m = JSON.parse(await readFile(path.join(dir, MANIFEST_NAME), "utf8")); assert.deepEqual(ids(m), ["a", "c", "d", "k", "e", "b", "f"]); const snaps = (await readdir(path.join(dir, "revisions"))).filter((n) => n.includes("place")); assert.equal(snaps.length, 2); } finally { await rm(dir, { recursive: true, force: true }); } }); test("umtool timeline place: the same writer from a terminal", async () => { const dir = await project(); const root = path.dirname(dir); try { const bin = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "..", "bin", "umtool.mjs"); const { stdout } = await promisify(execFile)(process.execPath, [bin, "timeline", dir, "place", "e", "--before", "c", "--section", "1"], { env: { ...process.env, REPORTS_DIR: root } }); assert.match(stdout, /placed e before c \(index 5 -> 2\) in §1 \(Two\)/); assert.match(stdout, /undo point: revisions\//); const m = JSON.parse(await readFile(path.join(dir, MANIFEST_NAME), "utf8")); assert.deepEqual(secs(m), ["ab", "ecd", "f"]); } finally { await rm(dir, { recursive: true, force: true }); } });