// The working draft and its saved versions (versions.mjs): a version freezes, // the draft knows its parent, opening a version never loses the draft or an // undo point, and a legacy take resolves exactly as it always did. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { createHash } from "node:crypto"; import { mkdir, mkdtemp, readdir, readFile, rm, stat, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { spawnSync } from "node:child_process"; import { freezeTakes, judgeDraft, nextVersionId, openVersion, readDraft, saveVersion } from "./versions.mjs"; import { composeTake, checkSpec, makeTake, pickTake } from "./takeMaker.mjs"; import { resolveTakeManifest } from "./takeManifest.mjs"; import { isWorkingTake, listTakes, readVerdicts, setTakePicked } from "./takes.mjs"; import { draftLine, readWorkingCut, workingCutLine } from "./workingCut.mjs"; import { createSnapshot, listSnapshots } from "./snapshots.mjs"; import { moveEntry, undoStructural, updateClip } from "./manifest.mjs"; import { PROJECT_KINDS } from "../projects/kinds.mjs"; import { takesCommand } from "./takeCli.mjs"; const KIND = PROJECT_KINDS.find((k) => k.makesTakes).id; const hash = (t) => createHash("sha256").update(t).digest("hex"); const clip = (id, start, end, extra = {}) => ({ type: "clip", id, video: `v-${id}`, start, end, cite: start, section: 0, ...extra }); const MANIFEST = { schemaVersion: 1, slug: "fixture", title: "Fixture", render: { width: 640, height: 360, fps: 15, transition: 0, crf: 30 }, timeline: [clip("a", 0, 10), clip("b", 20, 25), clip("c", 40, 50)], }; async function project(manifest = MANIFEST) { const root = await mkdtemp(path.join(tmpdir(), "versions-")); const dir = path.join(root, "reports", "proj"); await mkdir(path.join(dir, "out", "clips-raw"), { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(manifest, null, 2) + "\n"); return { root, dir, proj: { id: "proj", dir, kind: KIND }, reportsRoot: path.join(root, "reports") }; } const text = (dir) => readFile(path.join(dir, "video.manifest.json"), "utf8"); const json = async (f) => JSON.parse(await readFile(f, "utf8")); const ids = async (dir) => (await json(path.join(dir, "video.manifest.json"))).timeline.map((e) => e.id).join(" "); test("saveVersion: v1 is the draft byte for byte, frozen; the draft knows it is from v1; an edit counts; no edits saves nothing", async () => { const { root, dir, proj } = await project(); try { let w = await readWorkingCut(dir); assert.equal(w.parent, null); assert.equal(draftLine(w), "Draft · unsaved"); const before = await text(dir); const r = await saveVersion(proj, { label: "First pass" }); assert.equal(r.saved, true); assert.equal(r.id, "v1"); assert.equal(await readFile(path.join(dir, "takes", "v1", "base.manifest.json"), "utf8"), before); const doc = await json(path.join(dir, "takes", "v1", "take.json")); assert.deepEqual([doc.version, doc.draft, doc.patch, doc.window, doc.baseHash, doc.label], [true, true, null, "full", hash(before), "First pass"]); assert.equal(isWorkingTake(doc), true, "a version is the cut, rendered or not"); assert.deepEqual(await readDraft(dir), { from: "v1", hash: hash(before), at: doc.savedAt }); w = await readWorkingCut(dir); assert.equal(w.parent?.id, "v1"); assert.equal(w.latest, null, "a version not rendered is not the latest render"); assert.equal(draftLine(w), "Draft · from v1 · no edits"); assert.match(workingCutLine(w), /Draft from v1 · no edits since/); // Nothing to save at no edits; --force saves anyway. const none = await saveVersion(proj); assert.equal(none.saved, false); assert.equal(none.id, "v1"); // An edit to the draft: v1 does not follow it. await moveEntry(dir, "c", 0); w = await readWorkingCut(dir); assert.equal(w.edits?.count, 1); assert.equal(draftLine(w), "Draft · from v1 · 1 edit"); const v1 = (await resolveTakeManifest(dir, "v1")).manifest; assert.deepEqual(v1.timeline.map((e) => e.id), ["a", "b", "c"], "the version is frozen"); const r2 = await saveVersion(proj); assert.equal(r2.id, "v2"); assert.equal((await json(path.join(dir, "takes", "v2", "take.json"))).draftedFrom, "v1"); assert.deepEqual((await listTakes(dir)).takes.map((t) => t.id), ["v1", "v2"]); } finally { await rm(root, { recursive: true, force: true }); } }); test("nextVersionId: above every v directory, made or not; never a name in use", async () => { const { root, dir } = await project(); try { assert.equal(await nextVersionId(dir), "v1"); await mkdir(path.join(dir, "takes", "v3"), { recursive: true }); await mkdir(path.join(dir, "takes", "v10-tight"), { recursive: true }); assert.equal(await nextVersionId(dir), "v4"); } finally { await rm(root, { recursive: true, force: true }); } }); test("openVersion: the draft saved first as the next version, the version copied in, one undo point that puts the draft AND its parent back", async () => { const { root, dir, proj } = await project(); try { await saveVersion(proj); // v1: a b c const v1Text = await text(dir); await moveEntry(dir, "c", 0); // c a b // A pinned pick elsewhere survives. await mkdir(path.join(dir, "takes", "old"), { recursive: true }); await writeFile(path.join(dir, "takes", "old", "take.json"), JSON.stringify({ id: "old", group: "takes", order: 0, label: "Old", kind: "reference", preview: "preview.mp4", window: "full", baseHash: "x" })); await setTakePicked(dir, "old", ["old", "v1"], { pinned: true }); const preOpen = await text(dir); const r = await openVersion(proj, "v1"); assert.equal(r.saved, "v2", "the draft's edit is kept as v2"); assert.equal(r.how, "frozen"); assert.equal(await readFile(path.join(dir, "takes", "v2", "base.manifest.json"), "utf8"), preOpen); assert.equal(await text(dir), v1Text, "the draft is v1 again, byte for byte"); assert.equal((await readDraft(dir))?.from, "v1"); assert.match(r.snapshot.rel, /auto-before-open-v1\.manifest\.json$/); let w = await readWorkingCut(dir); assert.equal(w.parent?.id, "v1"); assert.equal(w.edits?.count, 0); const v = await readVerdicts(dir); assert.deepEqual([v.old?.picked, v.old?.pinned], [true, true], "opening never moves or unpins the pick"); // Opening it again: nothing written. assert.equal((await openVersion(proj, "v1")).unchanged, true); // Undo: the pre-open draft, and the draft's parent is v2 (what it was saved as). const u = await undoStructural(dir); assert.equal(u.op, "open-v1"); assert.equal(await text(dir), preOpen); assert.equal((await readDraft(dir))?.from, "v2"); w = await readWorkingCut(dir); assert.equal(w.edits?.count, 0); } finally { await rm(root, { recursive: true, force: true }); } }); test("openVersion of a legacy take: a rendered one is the manifest it rendered; one with its own timeline merges its patch (promote)", async () => { const { root, dir, proj } = await project(); try { const t0 = await text(dir); const mk = async (id, doc, rendered = 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", window: "full", baseHash: hash(t0), ...doc })); if (rendered) await writeFile(path.join(dir, "takes", id, "video.manifest.json"), JSON.stringify(rendered, null, 2) + "\n"); }; // Rendered from an older manifest (its baseHash is not the draft's), with a look patch. await mk("rendered", { builtAt: "2026-11-01T00:00:00Z", baseHash: "older", patch: { title: "Rendered" } }, { ...MANIFEST, title: "Rendered", timeline: [clip("a", 0, 10)] }); await mk("own", { draft: true, patch: { timeline: [clip("b", 21, 24), clip("z", 1, 2)] } }); // Legacy takes resolve as they always did: their patch over the project's manifest now. assert.deepEqual((await resolveTakeManifest(dir, "rendered")).manifest.timeline.map((e) => e.id), ["a", "b", "c"]); const r = await openVersion(proj, "rendered"); assert.equal(r.how, "rendered"); assert.equal(r.saved, "v1", "the draft differs from its parent (the latest render): saved first"); assert.equal(await ids(dir), "a"); assert.equal((await json(path.join(dir, "video.manifest.json"))).title, "Rendered"); const o = await openVersion(proj, "own"); assert.equal(o.how, "merge"); assert.equal(await ids(dir), "b z"); assert.equal((await json(path.join(dir, "takes", "own", "take.json"))).patch.timeline.length, 2, "the take itself is untouched"); // Its edits are counted from what the open wrote (it has no frozen form of its own). let w = await readWorkingCut(dir); assert.deepEqual([w.parent?.id, w.edits?.count], ["own", 0]); await moveEntry(dir, "z", 0); w = await readWorkingCut(dir); assert.deepEqual([w.parent?.id, w.edits?.count], ["own", 1]); } finally { await rm(root, { recursive: true, force: true }); } }); test("a version renders its frozen base, and pickTake uses an unrendered version", async () => { const { root, dir, proj } = await project(); try { await saveVersion(proj); // v1: a b c await moveEntry(dir, "c", 0); const c = await composeTake(dir, checkSpec({ id: "v1", label: "v1", window: "full" })); assert.equal(c.frozen, true); assert.deepEqual(c.manifest.timeline.map((e) => e.id), ["a", "b", "c"]); // Rendered with a stub: still a version, its baseHash the base's, its frozen manifest what it rendered. const calls = []; const render = async (job) => { const doc = JSON.parse(await readFile(job.manifestPath, "utf8")); calls.push(doc.timeline.map((e) => e.id).join(" ")); const file = path.join(job.outDir, `${job.slug}.mp4`); await writeFile(file, "mp4"); return { file }; }; const base = await readFile(path.join(dir, "takes", "v1", "base.manifest.json"), "utf8"); await makeTake(dir, { id: "v1", label: "v1", window: "full" }, { force: true, render, styles: {}, probe: async () => 9, record: false, pruneAfter: false }); assert.deepEqual(calls, ["a b c"]); const doc = await json(path.join(dir, "takes", "v1", "take.json")); assert.deepEqual([doc.version, doc.baseHash, doc.draft], [true, hash(base), undefined]); assert.equal((await readDraft(dir))?.from, "v1", "a frozen version re-rendered does not move the draft's parent"); // An unrendered version is picked from its base. await saveVersion(proj); // v2: c a b await moveEntry(dir, "a", 0); await pickTake(proj, "v2", { anyway: true }); assert.equal(await ids(dir), "c a b"); } finally { await rm(root, { recursive: true, force: true }); } }); test("freezeTakes: a dry run writes nothing; --apply gives a legacy take the base it was rendered from", async () => { const { root, dir, proj } = await project(); try { const t0 = await text(dir); await mkdir(path.join(dir, "takes", "d1"), { recursive: true }); await writeFile(path.join(dir, "takes", "d1", "take.json"), JSON.stringify({ id: "d1", group: "takes", order: 1, label: "d1", kind: "reference", preview: "preview.mp4", window: "full", baseHash: hash(t0), builtAt: "2026-11-01T00:00:00Z" })); await writeFile(path.join(dir, "takes", "d1", "video.manifest.json"), t0); await moveEntry(dir, "c", 0); const dry = await freezeTakes(proj, { ids: null }); assert.deepEqual(dry.rows.map((x) => [x.id, x.action, x.source, x.differs]), [["d1", "would-freeze", "rendered", 1]]); await assert.rejects(stat(path.join(dir, "takes", "d1", "base.manifest.json"))); assert.deepEqual((await resolveTakeManifest(dir, "d1")).manifest.timeline.map((e) => e.id), ["c", "a", "b"], "unfrozen, it follows the draft"); const done = await freezeTakes(proj, { ids: ["d1"], apply: true }); assert.equal(done.rows[0].action, "frozen"); assert.equal(await readFile(path.join(dir, "takes", "d1", "base.manifest.json"), "utf8"), t0); assert.deepEqual((await resolveTakeManifest(dir, "d1")).manifest.timeline.map((e) => e.id), ["a", "b", "c"]); assert.equal((await freezeTakes(proj, { ids: ["d1"] })).rows[0].action, "already"); } finally { await rm(root, { recursive: true, force: true }); } }); test("snapshots in one second are two undo points, newest first", async () => { const { root, dir } = await project(); try { const now = new Date("2026-11-09T10:00:00.120Z"); const a = await createSnapshot(dir, { label: "auto-before-trim", now }); const b = await createSnapshot(dir, { label: "auto-before-trim", now: new Date("2026-11-09T10:00:00.480Z") }); const c = await createSnapshot(dir, { label: "auto-before-move", now: new Date("2026-11-09T10:00:00.480Z") }); assert.notEqual(a.name, b.name); assert.notEqual(b.name, c.name); const list = await listSnapshots(dir); assert.deepEqual(list.map((s) => s.name), [c.name, b.name, a.name]); assert.equal(list[0].label, "auto-before-move"); } finally { await rm(root, { recursive: true, force: true }); } }); test("umtool takes save | open | freeze, with --json", async () => { const { root, dir, reportsRoot } = await project(); try { const out = []; const err = []; const run = (args) => takesCommand(args, { reportsRoot, log: (s) => out.push(s), err: (s) => err.push(s) }); assert.equal(await run(["save", "proj", "--label", "Pass one", "--json"]), 0); assert.equal(JSON.parse(out.pop()).id, "v1"); await updateClip(dir, "a", { end: 9 }); assert.equal(await run(["save", "proj"]), 0); assert.match(out.pop(), /saved as v2/); assert.equal(await run(["open", "proj", "v1", "--json"]), 0); const o = JSON.parse(out.pop()); assert.deepEqual([o.opened, o.saved], ["v1", null]); assert.equal(await run(["freeze", "proj", "--all"]), 0); assert.equal(await run(["freeze", "proj"]), 2); assert.deepEqual((await readdir(path.join(dir, "takes"))).sort(), ["draft.json", "v1", "v2"]); } finally { await rm(root, { recursive: true, force: true }); } }); test("umtool takes verdict: Play and judge's twin -- like | maybe | no | clear, with a note; an unknown take or word refused", async () => { const { root, dir, reportsRoot } = await project(); try { const out = []; const err = []; const run = (args) => takesCommand(args, { reportsRoot, log: (s) => out.push(s), err: (s) => err.push(s) }); assert.equal(await run(["save", "proj"]), 0); assert.equal(await run(["verdict", "proj", "v1", "maybe", "--note", "the cold open drags"]), 0); assert.equal(out.pop(), "v1: maybe — the cold open drags"); const { readVerdicts } = await import("./takes.mjs"); assert.deepEqual([(await readVerdicts(dir)).v1.verdict, (await readVerdicts(dir)).v1.note], ["maybe", "the cold open drags"]); assert.equal(await run(["verdict", "proj", "v1", "clear", "--json"]), 0); assert.equal(JSON.parse(out.pop()).entry?.verdict ?? null, null); assert.equal(await run(["verdict", "proj", "v9", "like"]), 2); assert.equal(await run(["verdict", "proj", "v1", "great"]), 2); } finally { await rm(root, { recursive: true, force: true }); } }); test("one undo stack: in the app (setUndoEveryMs(0)) two trims a moment apart are two undo points, undone one by one; the terminal -- and a step the app spawns -- keeps the throttle", async () => { const { root, dir } = await project(); const { setUndoEveryMs } = await import("./manifest.mjs"); try { const t0 = await text(dir); // The terminal: the second trim of a burst takes no undo point of its own. setUndoEveryMs(null); const a = await updateClip(dir, "a", { end: 9 }); const b = await updateClip(dir, "a", { end: 8 }); assert.equal(a.snapshot?.skipped, false); assert.equal(b.snapshot?.skipped, true); await writeFile(path.join(dir, "video.manifest.json"), t0); await rm(path.join(dir, "revisions"), { recursive: true, force: true }); // The app: each its own. setUndoEveryMs(0); // ...and a child process the app spawns (lib/jobs.ts passes process.env) does NOT inherit it. assert.equal(process.env.UMTOOL_UNDO_EVERY_MS, undefined); const child = spawnSync(process.execPath, ["--input-type=module", "-e", `import("${new URL("./manifest.mjs", import.meta.url).href}").then((m) => console.log(m.undoEveryMs()))`], { env: process.env, encoding: "utf8" }); assert.equal(child.stdout.trim(), "120000", child.stderr); await updateClip(dir, "a", { end: 9 }); const t1 = await text(dir); await updateClip(dir, "a", { end: 8 }); // A verdict alone is not an undo point (the bench keeps its own undo for those). const v = await updateClip(dir, "b", { verdict: "confirmed" }, { edit: "verdict" }); assert.equal(v.snapshot, undefined); await undoStructural(dir); const back1 = JSON.parse(await text(dir)); assert.equal(back1.timeline[0].end, 9); assert.deepEqual(JSON.parse(t1).timeline[0], back1.timeline[0]); await undoStructural(dir); assert.equal(JSON.parse(await text(dir)).timeline[0].end, 10); } finally { setUndoEveryMs(null); await rm(root, { recursive: true, force: true }); } }); test("undo points are pruned to the newest N; a named snapshot is never pruned", async () => { const { root, dir } = await project(); try { const { autoSnapshot, pruneAutoSnapshots } = await import("./manifest.mjs"); const named = await createSnapshot(dir, { label: "keep-me" }); for (let i = 0; i < 5; i += 1) await autoSnapshot(dir, `op${i}`, { every: 0, keep: 1000 }); assert.equal((await listSnapshots(dir)).length, 6); const gone = await pruneAutoSnapshots(dir, 3); assert.equal(gone.length, 2); const left = await listSnapshots(dir); assert.deepEqual(left.filter((x) => x.label?.startsWith("auto-before-")).map((x) => x.label), ["auto-before-op4", "auto-before-op3", "auto-before-op2"]); assert.ok(left.some((x) => x.name === named.name)); } finally { await rm(root, { recursive: true, force: true }); } }); // ---- the review's fixes (L3 review, 9 Nov) ---- test("a version's patch never masks a draft edit: v2 'No intro' and an intro in the draft is an edit; open(v2) brings its No intro in (no byte-identical save of its base); open(v1) after another intro saves it first", async () => { const { root, dir, proj } = await project(); const setIntro = async (on) => { const m = JSON.parse(await text(dir)); if (on) m.render.intro = "boot-dark"; else delete m.render.intro; await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(m, null, 2) + "\n"); }; try { await saveVersion(proj); // v1: no intro await moveEntry(dir, "c", 0); await setIntro(true); await saveVersion(proj); // v2: with the intro // v2 "No intro" since (its patch): the draft still has the intro -- that IS a difference from v2. const v2doc = await json(path.join(dir, "takes", "v2", "take.json")); v2doc.patch = { render: { intro: null } }; await writeFile(path.join(dir, "takes", "v2", "take.json"), JSON.stringify(v2doc, null, 2) + "\n"); let w = await readWorkingCut(dir); assert.equal(w.parent?.id, "v2"); assert.equal(w.edits?.count, 1, "the draft's intro is not masked by v2's patch"); const withIntro = await text(dir); const o2 = await openVersion(proj, "v2"); assert.equal(o2.unchanged, false, "not 'unchanged': v2's No intro is not in the draft"); assert.equal(o2.saved, null, "the draft IS v2's base byte for byte: no second copy of it"); assert.equal(await readFile(path.join(dir, "takes", "v2", "base.manifest.json"), "utf8"), withIntro, "kept there"); assert.ok(!("intro" in JSON.parse(await text(dir)).render), "v2's No intro reached the draft"); assert.equal((await openVersion(proj, "v2")).unchanged, true, "now the draft holds it"); w = await readWorkingCut(dir); assert.deepEqual([w.parent?.id, w.edits?.count], ["v2", 0]); // The intro added to the draft again: an edit (v2's patch does not hide it) -- but the draft is v2's base // byte for byte, so open(v1) keeps no second copy of it. await setIntro(true); assert.equal((await readWorkingCut(dir)).edits?.count, 1); assert.equal(await text(dir), withIntro); assert.equal((await openVersion(proj, "v1")).saved, null); // A real edit since v1: saved first. await moveEntry(dir, "a", 2); const edited = await text(dir); const o1 = await openVersion(proj, "v1"); assert.equal(o1.saved, "v3"); assert.equal(await readFile(path.join(dir, "takes", "v3", "base.manifest.json"), "utf8"), edited); } finally { await rm(root, { recursive: true, force: true }); } }); test("a legacy rendered take whose patch was edited after its render opens by its patch, not the stale render", async () => { const { root, dir, proj } = await project(); try { await mkdir(path.join(dir, "takes", "r"), { recursive: true }); await writeFile(path.join(dir, "takes", "r", "video.manifest.json"), JSON.stringify({ ...MANIFEST, title: "Rendered" }, null, 2)); const doc = { id: "r", group: "takes", order: 1, label: "r", kind: "different", preview: "preview.mp4", window: "full", baseHash: "x", builtAt: "2026-11-01T00:00:00Z", patch: { title: "Walked since" } }; await writeFile(path.join(dir, "takes", "r", "take.json"), JSON.stringify({ ...doc, editedAt: "2026-11-02T00:00:00Z" })); const o = await openVersion(proj, "r"); assert.equal(o.how, "merge"); assert.equal(JSON.parse(await text(dir)).title, "Walked since"); // Not edited since its render: the rendered manifest, as before. await writeFile(path.join(dir, "takes", "r", "take.json"), JSON.stringify({ ...doc, editedAt: "2026-10-31T00:00:00Z" })); const o2 = await openVersion(proj, "r"); assert.equal(o2.how, "rendered"); assert.equal(JSON.parse(await text(dir)).title, "Rendered"); } finally { await rm(root, { recursive: true, force: true }); } }); test("version ids: claimed exclusively (two claims at once get two ids); a number a pointer or an undo sidecar names is never reused", async () => { const { root, dir } = await project(); try { const { claimVersionId } = await import("./versions.mjs"); const [a, b] = await Promise.all([claimVersionId(dir), claimVersionId(dir)]); assert.notEqual(a.id, b.id); assert.deepEqual([a.id, b.id].sort(), ["v1", "v2"]); // v5 deleted, but an undo point's sidecar still names it: never v5 again. await mkdir(path.join(dir, "revisions"), { recursive: true }); await writeFile(path.join(dir, "revisions", "20261109-100000-auto-before-open-v1.draft.json"), JSON.stringify({ from: "v5" })); assert.equal(await nextVersionId(dir), "v6"); await writeFile(path.join(dir, "takes", "draft.json"), JSON.stringify({ from: "v9" })); assert.equal((await claimVersionId(dir)).id, "v10"); } finally { await rm(root, { recursive: true, force: true }); } }); test("openVersion: a write from another process between its read and its write is a StaleToken -- nothing lost, the version it saved removed, the pointer put back", async () => { const { root, dir, proj } = await project(); try { await saveVersion(proj); // v1 await moveEntry(dir, "c", 0); // an edit: open(v1) saves first const pointer = await readDraft(dir); let theirs = ""; await assert.rejects( openVersion(proj, "v1", { _beforeWrite: async () => { // The CLI, another process: a write the lock cannot see (its mtime moves the token). await new Promise((r) => setTimeout(r, 20)); const m = JSON.parse(await text(dir)); m.title = "Written by the CLI"; theirs = JSON.stringify(m, null, 2) + "\n"; await writeFile(path.join(dir, "video.manifest.json"), theirs); }, }), (e) => e?.name === "StaleToken", ); assert.equal(await text(dir), theirs, "the other write stands"); await assert.rejects(stat(path.join(dir, "takes", "v2")), "no stray v2"); assert.deepEqual(await readDraft(dir), pointer, "the pointer as it was"); } finally { await rm(root, { recursive: true, force: true }); } }); test("a working take rendered while the draft was opened leaves the opened pointer alone", async () => { const { root, dir, proj } = await project(); try { const render = async (job) => { // Mid-render, somebody opens a version (the pointer moves). await writeFile(path.join(dir, "takes", "draft.json"), JSON.stringify({ from: "v1", hash: null, at: "2026-11-09T00:00:00Z" })); const file = path.join(job.outDir, `${job.slug}.mp4`); await writeFile(file, "mp4"); return { file }; }; await mkdir(path.join(dir, "takes"), { recursive: true }); await makeTake(dir, { id: "full-1", label: "Full", window: "full" }, { render, styles: {}, probe: async () => 9, record: false, pruneAfter: false }); assert.equal((await readDraft(dir))?.from, "v1", "the open's pointer stands"); // Nothing moved it this time: the new working take is the draft's parent. await makeTake(dir, { id: "full-2", label: "Full 2", window: "full" }, { render: async (job) => { const file = path.join(job.outDir, `${job.slug}.mp4`); await writeFile(file, "mp4"); return { file }; }, styles: {}, probe: async () => 9, record: false, pruneAfter: false }); assert.equal((await readDraft(dir))?.from, "full-2"); void proj; } finally { await rm(root, { recursive: true, force: true }); } }); test("a project naming a PRESET: its latest legacy take with no edits opens as nothing (straight on); opened after an edit, the draft keeps naming the preset -- never the resolved manifest", async () => { const { resolvePresetLayers } = await import("umtool-report-to-video/presets"); const { openIsNoop, sameContent } = await import("./versions.mjs"); const { root, dir, proj } = await project({ ...MANIFEST, preset: "plain-deck", render: { crf: 30 } }); try { const render = async (job) => { const file = path.join(job.outDir, `${job.slug}.mp4`); await writeFile(file, "mp4"); return { file }; }; await makeTake(dir, { id: "d1", label: "Draft 1", window: "full" }, { render, styles: {}, probe: async () => 9, record: false, pruneAfter: false }); const rendered = await json(path.join(dir, "takes", "d1", "video.manifest.json")); assert.equal(rendered.presetApplied, "plain-deck", "the render's file is preset-resolved"); // Make the pointer "latest" again (main's shape: no draft.json), the draft untouched since the render. await rm(path.join(dir, "takes", "draft.json"), { force: true }); let w = await readWorkingCut(dir); assert.deepEqual([w.parent?.id, w.edits?.count], ["d1", 0]); assert.equal(await openIsNoop(dir, "d1"), true, "the 307 straight-on is kept"); const before = await text(dir); assert.equal((await openVersion(proj, "d1")).unchanged, true); assert.equal(await text(dir), before, "nothing written over the draft"); // An edit, then open d1: the rendered cut comes back NAMING its preset. await moveEntry(dir, "c", 0); const o = await openVersion(proj, "d1"); assert.equal(o.how, "rendered"); const now = JSON.parse(await text(dir)); assert.equal(now.preset, "plain-deck"); assert.equal(now.presetApplied, undefined); assert.deepEqual(now.render, { crf: 30 }, "the draft's override stays; the preset's keys are the preset's"); assert.deepEqual(now.timeline.map((e) => e.id), ["a", "b", "c"]); assert.ok(sameContent(resolvePresetLayers(now), rendered), "it resolves to exactly what was rendered"); // A legacy take with its own timeline, merged: the preset stays named too. await mkdir(path.join(dir, "takes", "own"), { recursive: true }); await writeFile(path.join(dir, "takes", "own", "take.json"), JSON.stringify({ id: "own", group: "takes", order: 5, label: "own", kind: "different", preview: "preview.mp4", draft: true, patch: { timeline: [clip("b", 21, 24)] } })); const m = await openVersion(proj, "own"); assert.equal(m.how, "merge"); const after = JSON.parse(await text(dir)); assert.deepEqual([after.preset, after.presetApplied, after.render], ["plain-deck", undefined, { crf: 30 }]); } finally { await rm(root, { recursive: true, force: true }); } }); test("judgeDraft: a clear never saves a version -- the parent's is cleared while the draft is that version; a draft with edits (or never saved) is refused, nothing written", async () => { const { root, dir, proj } = await project(); try { // Never saved: nothing to clear, and no v1 made for it. await assert.rejects(judgeDraft(proj, null), /never been saved as a version/); assert.deepEqual((await listTakes(dir)).takes.filter((t) => t.version).map((t) => t.id), []); await saveVersion(proj); await judgeDraft(proj, "like"); // The draft is v1 (a walk verdict only): the clear is v1's. await updateClip(dir, "a", { verdict: "confirmed" }); let r = await judgeDraft(proj, null); assert.deepEqual([r.judged, r.saved], ["v1", false]); assert.equal((await readVerdicts(dir)).v1?.verdict ?? null, null); // An edge moved: the draft is not v1 -- refused, no v2. await judgeDraft(proj, "like"); await updateClip(dir, "c", { end: 48 }); await assert.rejects(judgeDraft(proj, null), /edits since v1/); assert.deepEqual((await listTakes(dir)).takes.filter((t) => t.version).map((t) => t.id), ["v1"]); assert.equal((await readVerdicts(dir)).v1.verdict, "like", "v1's verdict untouched"); } finally { await rm(root, { recursive: true, force: true }); } }); test("judgeDraft: a draft that differs from its parent only by the walk's and the review pass's fields judges the PARENT and saves nothing; any other edit saves the next version and judges it", async () => { const { root, dir, proj } = await project(); try { await saveVersion(proj); // A Keep (the walk's verdict), a correction, a walk note, the star: the draft is still v1. await updateClip(dir, "a", { verdict: "confirmed" }); await updateClip(dir, "b", { walkNote: "fine", star: true }); let r = await judgeDraft(proj, "like"); assert.deepEqual([r.judged, r.saved], ["v1", false]); assert.equal((await readVerdicts(dir)).v1.verdict, "like"); assert.deepEqual((await listTakes(dir)).takes.filter((t) => t.version).map((t) => t.id), ["v1"]); // An edge moved: another cut -- saved as v2, and v2 is judged. await updateClip(dir, "c", { end: 48 }); r = await judgeDraft(proj, "maybe"); assert.deepEqual([r.judged, r.saved], ["v2", true]); const v = await readVerdicts(dir); assert.deepEqual([v.v1.verdict, v.v2.verdict], ["like", "maybe"]); assert.equal((await readDraft(dir)).from, "v2"); // No parent at all: saved as the first version, judged. const fresh = await project(); try { r = await judgeDraft(fresh.proj, "no"); assert.deepEqual([r.judged, r.saved], ["v1", true]); assert.equal((await readVerdicts(fresh.dir)).v1.verdict, "no"); } finally { await rm(fresh.root, { recursive: true, force: true }); } } finally { await rm(root, { recursive: true, force: true }); } });