// Walk a take: a draft recorded without rendering, the walk's edits written // into the take's patch (never the project's manifest) with an undo point // each, undone byte for byte, a draft rendered from its edited patch, the // walk's reading (walkData, what GET /api/report/walk and `umtool walk` // answer), and "Make this the cut" (promoteTake) with its one undo. // // Run with: node --test lib/report/takeEdit.test.mjs import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, readdir, rm, stat, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { StaleToken, StructureRefused, undoStructural } from "./manifest.mjs"; import { draftTake } from "./takeMaker.mjs"; import { PromoteStale, listTakeRevisions, moveTakeEntry, promoteTake, removeTakeEntry, takeToken, undoTakeEdit, updateTakeClip, } from "./takeEdit.mjs"; import { listTakes, readVerdicts } from "./takes.mjs"; import { resolveTakeManifest } from "./takeManifest.mjs"; import { takeCommand } from "./takeCli.mjs"; import { timelineCommand } from "./timelineCli.mjs"; import { walkCommand } from "./walkCli.mjs"; import { walkJson, walkNeighbours, walkOf } from "./walkData.mjs"; import { PROJECT_KINDS } from "../projects/kinds.mjs"; const TAKES_KIND = PROJECT_KINDS.find((k) => k.makesTakes).id; const clip = (id, start, end, extra = {}) => ({ type: "clip", id, channel: "chan", video: "vid", start, end, cite: start, ...extra }); const MANIFEST = { schemaVersion: 1, slug: "fixture", title: "Fixture", render: { width: 640, height: 360, fps: 15, transition: 0, crf: 30 }, timeline: [clip("c01", 100, 160, { verdict: "confirmed" }), clip("c02", 200, 260, { verdict: "confirmed" }), clip("c03", 300, 360)], }; /** The tight version: sub-windows of the master's clips, a whole `timeline` merge patch. */ const TIGHT = { timeline: [ clip("t01a", 105, 115, { verdict: "confirmed" }), clip("t01b", 130, 150, { verdict: "confirmed" }), clip("t02a", 210, 230), clip("t03a", 310, 320), ], }; async function project(manifest = MANIFEST) { const root = await mkdtemp(path.join(tmpdir(), "takeedit-")); const reportsRoot = path.join(root, "reports"); const dir = path.join(reportsRoot, "proj"); // One fetched window holding c01..c02's span (and so t01a, t01b, t02a); nothing for c03 / t03a. await mkdir(path.join(dir, "out", "clips-raw"), { recursive: true }); await writeFile(path.join(dir, "out", "clips-raw", "vid_90.00-270.00.mp4"), "media"); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(manifest, null, 2) + "\n"); const ref = { id: "proj", dir, kind: TAKES_KIND }; return { root, dir, reportsRoot, ref }; } const masterText = (dir) => readFile(path.join(dir, "video.manifest.json"), "utf8"); const takeJson = async (dir, id) => JSON.parse(await readFile(path.join(dir, "takes", id, "take.json"), "utf8")); function stubRender() { const calls = []; const render = async (job) => { const doc = JSON.parse(await readFile(job.manifestPath, "utf8")); calls.push({ ...job, doc }); const file = path.join(job.outDir, `${job.slug}.mp4`); await writeFile(file, "render"); return { file }; }; return { render, calls }; } test("a draft take is recorded, not rendered: listed as a draft, its manifest is the project's with its patch", async () => { const { root, dir } = await project(); try { const d = await draftTake(dir, { id: "tight", label: "Tight", patch: TIGHT }); assert.equal(d.clips, 4); const t = await takeJson(dir, "tight"); assert.equal(t.draft, true); assert.equal(t.builtAt, null); assert.equal(t.window, null, "a draft says its window when it is rendered"); assert.ok(t.baseHash); assert.deepEqual(await readdir(path.join(dir, "takes", "tight")), ["take.json"], "nothing rendered, no manifest copied"); const listed = (await listTakes(dir)).takes; assert.equal(listed.length, 1); assert.equal(listed[0].draft, true); assert.equal(listed[0].previewSize, null); const { manifest } = await resolveTakeManifest(dir, "tight"); assert.deepEqual(manifest.timeline.map((e) => e.id), ["t01a", "t01b", "t02a", "t03a"]); // Again: refused, then --force records it again; a RENDERED take is never drafted over. await assert.rejects(draftTake(dir, { id: "tight", label: "Tight", patch: TIGHT }), /exists \(a draft\)/); await draftTake(dir, { id: "tight", label: "Tight 2", patch: TIGHT }, { force: true }); assert.equal((await takeJson(dir, "tight")).label, "Tight 2"); await assert.rejects(draftTake(dir, { id: "x", label: "X", patch: { timeline: "no" } }), /no timeline/); } finally { await rm(root, { recursive: true, force: true }); } }); test("the walk's edits go into the take's patch with an undo point each; the project's manifest is untouched", async () => { const { root, dir } = await project(); try { await draftTake(dir, { id: "tight", label: "Tight", patch: TIGHT }); const before = await masterText(dir); const token = await takeToken(dir, "tight"); const r = await updateTakeClip(dir, "tight", "t02a", { end: 233.456, verdict: "confirmed" }, { token }); assert.equal(r.entry.end, 233.46, "2 dp, the project writer's rule"); assert.equal(r.before.end, 230); assert.notEqual(r.token, token); let t = await takeJson(dir, "tight"); const e = t.patch.timeline.find((x) => x.id === "t02a"); assert.equal(e.end, 233.46); assert.equal(e.verdict, "confirmed"); assert.ok(t.editedAt); assert.equal(t.draft, true, "still a draft"); assert.equal(await masterText(dir), before, "the project's manifest is never written"); // The project writer's own refusals. await assert.rejects(updateTakeClip(dir, "tight", "t03a", { verdict: "incorrect" }), /needs its note/); await assert.rejects(updateTakeClip(dir, "tight", "t03a", { start: 319.9 }), /at least half a second/); await assert.rejects(updateTakeClip(dir, "tight", "c01", { end: 150 }), /no timeline entry with id c01/, "the master's clip is not the take's"); // A stale token is refused, not written over. await assert.rejects(updateTakeClip(dir, "tight", "t03a", { verdict: "confirmed" }, { token }), StaleToken); // Structure: move and remove, checked by the build's rules. await moveTakeEntry(dir, "tight", "t03a", 0); await removeTakeEntry(dir, "tight", "t01b"); t = await takeJson(dir, "tight"); assert.deepEqual(t.patch.timeline.map((x) => x.id), ["t03a", "t01a", "t02a"]); const revs = await listTakeRevisions(dir, "tight"); assert.deepEqual(revs.map((x) => x.label), ["auto-before-remove", "auto-before-move", "auto-before-clip"]); // Undo: newest first, byte for byte, one step further each time. let u = await undoTakeEdit(dir, "tight"); assert.equal(u.op, "remove"); assert.deepEqual((await takeJson(dir, "tight")).patch.timeline.map((x) => x.id), ["t03a", "t01a", "t01b", "t02a"]); u = await undoTakeEdit(dir, "tight"); assert.equal(u.op, "move"); u = await undoTakeEdit(dir, "tight"); assert.equal(u.op, "clip"); assert.deepEqual((await takeJson(dir, "tight")).patch, TIGHT, "back to the patch as drafted"); await assert.rejects(undoTakeEdit(dir, "tight"), /nothing to undo/); assert.equal(await masterText(dir), before); } finally { await rm(root, { recursive: true, force: true }); } }); test("a take whose patch has no timeline (a style take) gets the cut's timeline on its first edit", async () => { const { root, dir } = await project(); try { await draftTake(dir, { id: "crisp", label: "Crisp", patch: { render: { crf: 33 } } }); await updateTakeClip(dir, "crisp", "c03", { verdict: "confirmed" }); const t = await takeJson(dir, "crisp"); assert.equal(t.patch.render.crf, 33, "the rest of the patch is kept"); assert.deepEqual(t.patch.timeline.map((x) => x.id), ["c01", "c02", "c03"]); assert.equal(t.patch.timeline[2].verdict, "confirmed"); assert.equal(JSON.parse(await masterText(dir)).timeline[2].verdict, undefined); } finally { await rm(root, { recursive: true, force: true }); } }); test("a refused structure edit writes nothing; removing a clip a post is attached to is the build's sentence", async () => { const { root, dir } = await project({ ...MANIFEST, posts: [{ id: "p1", platform: "x", date: "2026-01-01", text: "hi", url: "https://x.com/a/status/1", attachTo: "t01a" }] }); try { await draftTake(dir, { id: "tight", label: "Tight", patch: TIGHT }); const t0 = await readFile(path.join(dir, "takes", "tight", "take.json"), "utf8"); await assert.rejects(removeTakeEntry(dir, "tight", "t01a"), StructureRefused); assert.equal(await readFile(path.join(dir, "takes", "tight", "take.json"), "utf8"), t0); assert.deepEqual(await listTakeRevisions(dir, "tight"), []); } finally { await rm(root, { recursive: true, force: true }); } }); test("draft, walk, then render: the render reads the edited patch, and the take is made", async () => { const { root, dir, reportsRoot } = await project(); const r = stubRender(); const out = []; const err = []; const io = { reportsRoot, log: (s) => out.push(s), err: (s) => err.push(s), deps: { render: r.render, probe: async () => 42 } }; try { await writeFile(path.join(root, "patch.json"), JSON.stringify(TIGHT)); assert.equal(await takeCommand(["proj", "--id", "tight", "--patch", path.join(root, "patch.json"), "--draft", "--label", "Tight — walk"], io), 0, err.join("\n")); assert.match(out.join("\n"), /a draft, nothing rendered — 4 clip\(s\)/); assert.equal(r.calls.length, 0, "a draft renders nothing"); await updateTakeClip(dir, "tight", "t01a", { start: 104 }); await removeTakeEntry(dir, "tight", "t03a"); // `umtool take
--id X --full`: the draft, from what it stores. assert.equal(await takeCommand(["proj", "--id", "tight", "--full"], io), 0, err.join("\n")); assert.equal(r.calls.length, 1); assert.equal(r.calls[0].full, true); assert.deepEqual(r.calls[0].doc.timeline.map((e) => [e.id, e.start]), [["t01a", 104], ["t01b", 130], ["t02a", 210]]); const t = await takeJson(dir, "tight"); assert.equal(t.draft, undefined, "made now"); assert.equal(t.label, "Tight — walk"); assert.equal(t.window, "full"); assert.equal(t.seconds, 42); const listed = (await listTakes(dir)).takes[0]; assert.equal(listed.draft, false); assert.ok(listed.previewSize > 0); // --rebuild takes a window flag now. assert.equal(await takeCommand(["proj", "--id", "tight", "--rebuild", "--at", "0", "--dur", "5"], io), 0, err.join("\n")); assert.deepEqual((await takeJson(dir, "tight")).window, { at: 0, dur: 5 }); } finally { await rm(root, { recursive: true, force: true }); } }); test("walkOf: the take's clips in walk order, the walk visiting the fetched unjudged ones (all: every fetched one)", async () => { const { root, dir, ref } = await project(); try { await draftTake(dir, { id: "tight", label: "Tight", patch: TIGHT }); const w = await walkOf(ref, { take: "tight" }); assert.equal(w.take.id, "tight"); assert.equal(w.take.draft, true); assert.equal(w.take.editable, true); assert.deepEqual(w.order, ["t01a", "t01b", "t02a", "t03a"]); assert.deepEqual(w.clips.map((c) => [c.id, c.verdict, c.fetched, c.onWalk]), [ ["t01a", "confirmed", true, false], ["t01b", "confirmed", true, false], ["t02a", "unreviewed", true, true], ["t03a", "unreviewed", false, false], ]); assert.equal(w.first, "t02a"); assert.equal(w.needing, 2); assert.equal(w.ready, 1); assert.equal(w.token, await takeToken(dir, "tight")); assert.equal(w.seconds, 60); const all = await walkOf(ref, { take: "tight", all: true }); assert.equal(all.first, "t01a"); assert.deepEqual(walkNeighbours(all.order, all.onWalk, "t01b"), { prev: "t01a", next: "t02a" }); assert.deepEqual(walkNeighbours(all.order, all.onWalk, "t03a"), { prev: "t02a", next: null }); // The route's JSON: no manifest, no Set. const j = walkJson(all); assert.equal(j.manifest, undefined); assert.equal(j.onWalk, undefined); assert.ok(JSON.stringify(j)); // The project's own walk is unchanged by the take. const master = await walkOf(ref); assert.equal(master.take, null); assert.deepEqual(master.order, ["c01", "c02", "c03"]); await assert.rejects(walkOf(ref, { take: "nope" }), /no take nope/); } finally { await rm(root, { recursive: true, force: true }); } }); test("promoteTake: the take's patch over the project's manifest, one undo point; refused when the cut moved on, unless anyway", async () => { const { root, dir, ref } = await project(); try { await draftTake(dir, { id: "tight", label: "Tight", patch: TIGHT }); await updateTakeClip(dir, "tight", "t03a", { verdict: "incorrect", correction: "not her" }); const before = await masterText(dir); const r = await promoteTake(ref, "tight"); assert.equal(r.clips, 4); assert.match(r.snapshot.rel, /auto-before-promote-take/); const m = JSON.parse(await masterText(dir)); assert.deepEqual(m.timeline.map((e) => e.id), ["t01a", "t01b", "t02a", "t03a"]); assert.equal(m.timeline[3].correction, "not her", "the walk's edits are what is promoted"); assert.equal(m.render.crf, 30, "the rest of the manifest is the project's"); assert.equal((await readVerdicts(dir)).tight.picked, true); // One undo takes the whole promotion back, byte for byte. await undoStructural(dir); assert.equal(await masterText(dir), before); // The cut changed since the take was made: refused, then made all the same with anyway. const changed = JSON.parse(before); changed.timeline[2].verdict = "confirmed"; await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(changed, null, 2) + "\n"); await assert.rejects(promoteTake(ref, "tight"), PromoteStale); await promoteTake(ref, "tight", { anyway: true }); assert.equal(JSON.parse(await masterText(dir)).timeline[0].id, "t01a"); } finally { await rm(root, { recursive: true, force: true }); } }); test("the CLI twins: umtool walk --take, umtool timeline --take, umtool take --promote", async () => { const { root, dir, reportsRoot } = await project(); const out = []; const err = []; const io = { root: reportsRoot, out: (s) => out.push(s), err: (s) => err.push(s) }; try { await draftTake(dir, { id: "tight", label: "Tight", patch: TIGHT }); assert.equal(await walkCommand(["proj", "--take", "tight"], io), 0, err.join("\n")); const text = out.join("\n"); assert.match(text, /take tight "Tight" \(draft\) · 4 clip\(s\)/); assert.match(text, /→ +3 +t02a/); assert.match(text, /NOT FETCHED/); assert.match(text, /\/projects\/proj\/takes\/tight\/walk/); out.length = 0; assert.equal(await walkCommand(["proj", "--take", "tight", "--all", "--json"], io), 0, err.join("\n")); assert.equal(JSON.parse(out.join("\n")).first, "t01a"); out.length = 0; assert.equal(await timelineCommand(["proj", "--take", "tight", "move", "t03a", "--to", "0"], io), 0, err.join("\n")); assert.match(out.join("\n"), /take tight: moved t03a: 3 -> 0/); // A supported path (12 Nov): a write says nothing on stderr -- no deprecation line pointing at `takes open`. assert.deepEqual(err, []); err.length = 0; assert.equal(await timelineCommand(["proj", "--take", "tight", "undo"], io), 0, err.join("\n")); // "Add to this take only" (lane/ui-simplify): a clip into the take's timeline, never the project's. const masterBefore = await masterText(dir); out.length = 0; assert.equal(await timelineCommand(["proj", "--take", "tight", "insert", "--after", "-", "chan/vid@1-5"], io), 0, err.join("\n")); assert.match(out.join("\n"), /take tight: inserted/); assert.equal(await masterText(dir), masterBefore, "the project's manifest is untouched"); out.length = 0; assert.equal(await timelineCommand(["proj", "--take", "tight", "ls"], io), 0, err.join("\n")); assert.match(out.join("\n"), /5 entries in take tight of proj/); assert.equal(await timelineCommand(["proj", "--take", "tight", "undo"], io), 0, err.join("\n")); assert.equal(await timelineCommand(["proj", "--take", "tight", "insert", "chan/vid@1-5"], io), 2, "insert needs --after"); out.length = 0; err.length = 0; assert.equal(await timelineCommand(["proj", "--take", "tight", "ls"], io), 0, err.join("\n")); assert.match(out.join("\n"), /4 entries in take tight of proj$/, "the move was undone: nothing left to undo"); assert.deepEqual(err, [], "ls reads: no warning"); const log = []; const before = await masterText(dir); assert.equal(await takeCommand(["proj", "--id", "tight", "--promote"], { reportsRoot, log: (s) => log.push(s), err: (s) => err.push(s) }), 0, err.join("\n")); assert.match(log.join("\n"), /take tight is the cut now — 4 clip\(s\)/); assert.notEqual(await masterText(dir), before); const st = await stat(path.join(dir, "revisions")); assert.ok(st.isDirectory()); } finally { await rm(root, { recursive: true, force: true }); } });