// The working cut's name (workingCut.mjs) and the auto-pick (takes.mjs autoPickTake). import assert from "node:assert/strict"; import { createHash } from "node:crypto"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { test } from "node:test"; import { autoPickTake, isWorkingTake, readVerdicts, setTakePicked } from "./takes.mjs"; import { cutDiff, latestWorkingTake, readWorkingCut, workingCutLine } from "./workingCut.mjs"; const hash = (t) => createHash("sha256").update(t).digest("hex"); const clip = (id, start = 0, end = 5) => ({ id, type: "clip", video: "v", start, end }); test("isWorkingTake: a whole-cut render of the manifest's own content only", () => { const ok = { window: "full", baseHash: "h", draft: false }; assert.equal(isWorkingTake(ok), true); assert.equal(isWorkingTake({ ...ok, patch: { title: "x", render: {} } }), true, "a look patch is still the cut"); assert.equal(isWorkingTake({ ...ok, window: { at: 0, dur: 30 } }), false, "a window preview"); assert.equal(isWorkingTake({ ...ok, draft: true }), false, "a draft (not rendered)"); assert.equal(isWorkingTake({ ...ok, quick: true }), false, "a quick render"); assert.equal(isWorkingTake({ ...ok, style: "punchy" }), false, "a montage style"); assert.equal(isWorkingTake({ ...ok, patch: { timeline: [] } }), false, "its own timeline"); assert.equal(isWorkingTake({ ...ok, baseHash: null }), false, "made by hand"); }); test("latestWorkingTake: the newest working take by builtAt", () => { const t = (id, builtAt, extra = {}) => ({ id, builtAt, order: 1, window: "full", baseHash: "h", ...extra }); const takes = [t("draft-21", "2026-10-31T10:00:00Z"), t("draft-22", "2026-11-01T14:57:00Z"), t("preview", "2026-11-01T16:00:00Z", { window: { at: 0, dur: 30 } }), t("mods", "2026-11-01T17:00:00Z", { patch: { timeline: [] } })]; assert.equal(latestWorkingTake(takes)?.id, "draft-22"); assert.equal(latestWorkingTake([]), null); }); test("cutDiff: added, removed, moved (off the longest kept run) and changed, in words", () => { const before = { title: "a", timeline: [clip("a"), clip("b"), clip("c"), clip("d"), clip("e")] }; const after = { title: "a", timeline: [clip("a"), clip("c"), clip("b", 0, 4), clip("e"), clip("f")] }; const d = cutDiff(before, after); assert.deepEqual(d.removed, ["d"]); assert.deepEqual(d.added, ["f"]); assert.equal(d.moved.length, 1, "one of b/c moved, not both"); assert.deepEqual(d.changed, [{ id: "b", what: "trimmed" }]); assert.equal(d.rest, false); assert.equal(d.count, 4); assert.ok(d.words.includes("d removed") && d.words.includes("f added")); assert.equal(cutDiff(before, before).count, 0); assert.equal(cutDiff(before, { ...before, title: "b" }).count, 1, "the rest of the manifest counts once"); }); async function project() { const dir = await mkdtemp(path.join(os.tmpdir(), "working-cut-")); await mkdir(path.join(dir, "takes"), { recursive: true }); return dir; } async function writeTake(dir, id, doc, manifest = null) { await mkdir(path.join(dir, "takes", id), { recursive: true }); await writeFile(path.join(dir, "takes", id, "take.json"), JSON.stringify({ id, group: "takes", order: 1, label: id, kind: "different", preview: "preview.mp4", ...doc })); if (manifest) await writeFile(path.join(dir, "takes", id, "video.manifest.json"), JSON.stringify(manifest)); } test("readWorkingCut: the latest take, no edits when the manifest is its base, else the edits in words", async () => { const dir = await project(); try { const m1 = { timeline: [clip("a"), clip("b"), clip("c")] }; const text1 = JSON.stringify(m1); await writeFile(path.join(dir, "video.manifest.json"), text1); await writeTake(dir, "draft-1", { window: "full", baseHash: "old", builtAt: "2026-10-01T00:00:00Z" }); await writeTake(dir, "draft-2", { window: "full", baseHash: hash(text1), builtAt: "2026-11-01T00:00:00Z", seconds: 125 }, m1); let w = await readWorkingCut(dir); assert.equal(w.latest?.id, "draft-2"); assert.deepEqual(w.edits, { count: 0, words: [], same: true }); assert.equal(workingCutLine(w), "Latest: draft-2 · 2:05 · no edits since"); // An edit since: b moved after c, a removed. await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ timeline: [clip("c"), clip("b")] })); w = await readWorkingCut(dir); assert.equal(w.edits?.same, false); assert.equal(w.edits?.count, 2); assert.match(workingCutLine(w), /\+2 edits since/); assert.ok(w.edits?.words.includes("a removed")); } finally { await rm(dir, { recursive: true, force: true }); } }); test("autoPickTake: a working take is picked; a pinned pick wins; a partial take never", async () => { const dir = await project(); try { const group = ["draft-1", "draft-2", "prev"]; const w1 = { id: "draft-1", window: "full", baseHash: "h1" }; assert.deepEqual((await autoPickTake(dir, w1, group)).picked, true); assert.equal((await readVerdicts(dir))["draft-1"]?.picked, true); // A window preview: never picked. const r = await autoPickTake(dir, { id: "prev", window: { at: 0, dur: 30 }, baseHash: "h2" }, group); assert.equal(r.picked, false); assert.equal((await readVerdicts(dir))["draft-1"]?.picked, true); // The next working take moves the pick. await autoPickTake(dir, { id: "draft-2", window: "full", baseHash: "h2" }, group); let v = await readVerdicts(dir); assert.equal(v["draft-2"]?.picked, true); assert.equal(v["draft-1"], undefined, "a cleared row that says nothing is removed"); // Pinned to draft-1: a newer working take does not move it. await setTakePicked(dir, "draft-1", group, { pinned: true }); const r3 = await autoPickTake(dir, { id: "draft-3", window: "full", baseHash: "h3" }, [...group, "draft-3"]); assert.equal(r3.picked, false); assert.match(r3.why, /pinned to draft-1/); v = await readVerdicts(dir); assert.equal(v["draft-1"]?.pinned, true); assert.equal(v["draft-3"], undefined); // Unpinned: the next one is picked again, and the pin is gone with the pick. await setTakePicked(dir, "draft-1", group, { pinned: false }); assert.equal((await autoPickTake(dir, { id: "draft-3", window: "full", baseHash: "h3" }, [...group, "draft-3"])).picked, true); v = JSON.parse(await readFile(path.join(dir, "takes", "verdicts.json"), "utf8")); assert.equal(v["draft-3"].picked, true); assert.equal(v["draft-1"], undefined); } finally { await rm(dir, { recursive: true, force: true }); } });