// `umtool take … --from ` (takeFrom.mjs): the other take's segments, // chrome frames (or the ones kept aside) and crossfade batches are hard-linked // into this take's out/sourced/ before the render; a bad --from is refused // before anything is written. A stub renderer; no media. // // Run with: node --test umtool/lib/report/takeFrom.test.mjs import assert from "node:assert/strict"; import { existsSync, statSync } from "node:fs"; import { mkdir, mkdtemp, readFile, rm, utimes, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { makeTake } from "./takeMaker.mjs"; import { linkFromTake } from "./takeFrom.mjs"; import { renderArgv } from "./takeRender.mjs"; import { takeCommand } from "./takeCli.mjs"; import { PROJECT_KINDS } from "../projects/kinds.mjs"; const TAKES_KIND = PROJECT_KINDS.find((k) => k.makesTakes).id; const clip = (id, start, end) => ({ type: "clip", id, video: `v-${id}`, start, end, cite: start, section: 0 }); const MANIFEST = { schemaVersion: 1, slug: "fixture", title: "Fixture", render: { width: 640, height: 360, fps: 15, transition: 0.5 }, timeline: [clip("s01", 0, 10), clip("s02", 20, 25)], }; async function project() { const root = await mkdtemp(path.join(tmpdir(), "takefrom-")); 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"); await writeFile(path.join(dir, "project.json"), JSON.stringify({ kind: TAKES_KIND, title: "Proj" }) + "\n"); // The other take, built: its segments, a pruned layer kept aside, a live one, a batch. const src = path.join(dir, "takes", "blurred", "out", "sourced"); for (const d of ["segments", "chrome/deck-frames", "chrome/deck", "chrome/feed-frames-nokey", "xfade-batches"]) await mkdir(path.join(src, d), { recursive: true }); for (const f of ["s01.mp4", "s01.cut.json", "s01.attrib.txt", "s01.blur-0.txt", "s02.mp4", "s02.cut.json", "brand-intro.mp4", "brand-intro.intro.json"]) await writeFile(path.join(src, "segments", f), `blurred ${f}`); await writeFile(path.join(src, "chrome", "deck-frames", "frame_000001.png"), "png"); await writeFile(path.join(src, "chrome", "deck-frames", ".key"), "k1\n"); await writeFile(path.join(src, "chrome", "deck", "index.html"), ""); await writeFile(path.join(src, "chrome", "feed-frames-nokey", "frame_000001.png"), "png"); await writeFile(path.join(src, "xfade-batches", "batch-0123abcd.mov"), "batch"); await writeFile(path.join(src, "xfade-batches", "batch-0123abcd.partial.mov"), "half"); const kept = path.join(dir, "takes", "blurred", "out", ".keep-frames", "stamp-frames"); await mkdir(kept, { recursive: true }); await writeFile(path.join(kept, "frame_000001.png"), "stamp"); await writeFile(path.join(kept, ".key"), "k2\n"); await writeFile(path.join(dir, "takes", "blurred", "take.json"), JSON.stringify({ id: "blurred" })); return { root, dir, src, reportsRoot: path.join(root, "reports") }; } const sameFile = (a, b) => { const x = statSync(a); const y = statSync(b); return x.ino === y.ino && x.dev === y.dev; }; test("linkFromTake: segments, keyed chrome frames (kept-aside ones too) and finished batches, as hard links", async () => { const p = await project(); try { const out = path.join(p.dir, "takes", "twin", "out"); await mkdir(path.join(out, "sourced", "segments"), { recursive: true }); // An OLDER file of the twin's own is replaced by the link (through a rename). await writeFile(path.join(out, "sourced", "segments", "s02.mp4"), "the twin's own"); await utimes(path.join(out, "sourced", "segments", "s02.mp4"), new Date(0), new Date(0)); const got = await linkFromTake(p.dir, "twin", "blurred", out); const d = path.join(out, "sourced"); for (const f of ["s01.mp4", "s01.cut.json", "s01.attrib.txt", "s02.mp4", "s02.cut.json", "brand-intro.mp4", "brand-intro.intro.json"]) { assert.ok(sameFile(path.join(p.src, "segments", f), path.join(d, "segments", f)), f); } assert.equal(got.segments, 7); assert.ok(!existsSync(path.join(d, "segments", "s01.blur-0.txt")), "a file the build writes in place is not linked"); assert.deepEqual(got.frames, ["deck-frames", "stamp-frames"]); assert.ok(sameFile(path.join(p.src, "chrome", "deck-frames", "frame_000001.png"), path.join(d, "chrome", "deck-frames", "frame_000001.png"))); assert.ok(sameFile(path.join(p.src, "chrome", "deck-frames", ".key"), path.join(d, "chrome", "deck-frames", ".key"))); assert.equal(await readFile(path.join(d, "chrome", "stamp-frames", ".key"), "utf8"), "k2\n"); assert.ok(!existsSync(path.join(d, "chrome", "deck")), "a composition is not linked"); // A layer in both places: the newer .key wins (chrome/ holds an older cut's here). const stale = path.join(p.src, "chrome", "stamp-frames"); await mkdir(stale, { recursive: true }); await writeFile(path.join(stale, ".key"), "old\n"); await utimes(path.join(stale, ".key"), new Date(0), new Date(0)); await linkFromTake(p.dir, "twin", "blurred", out); assert.equal(await readFile(path.join(d, "chrome", "stamp-frames", ".key"), "utf8"), "k2\n"); assert.ok(!existsSync(path.join(d, "chrome", "feed-frames-nokey")), "a sequence with no key is not linked"); assert.equal(got.batches, 1); assert.ok(sameFile(path.join(p.src, "xfade-batches", "batch-0123abcd.mov"), path.join(d, "xfade-batches", "batch-0123abcd.mov"))); assert.ok(!existsSync(path.join(d, "xfade-batches", "batch-0123abcd.partial.mov"))); assert.deepEqual(JSON.parse(await readFile(path.join(d, "xfade-batches", ".from.json"), "utf8")), { take: "blurred", segments: path.join(p.src, "segments") }); assert.equal(await readFile(path.join(p.src, "segments", "s02.mp4"), "utf8"), "blurred s02.mp4", "the other take's file is as it was"); // Again: nothing new to link, nothing broken. const again = await linkFromTake(p.dir, "twin", "blurred", out); assert.equal(again.segments, 0); assert.deepEqual(again.frames, ["deck-frames", "stamp-frames"]); assert.equal(again.batches, 0); } finally { await rm(p.root, { recursive: true, force: true }); } }); test("linkFromTake: this take's own NEWER build (a re-run after a stop) is kept, theirs beside it", async () => { const p = await project(); try { const out = path.join(p.dir, "takes", "twin", "out"); const d = path.join(out, "sourced"); await mkdir(path.join(d, "segments"), { recursive: true }); await mkdir(path.join(d, "chrome", "deck-frames"), { recursive: true }); // The twin's own run, stopped mid-chrome: s01 and the deck rendered for // ITS cut, after the other take's. const later = new Date(Date.now() + 60_000); for (const f of ["s01.mp4", "s01.cut.json"]) { await writeFile(path.join(d, "segments", f), `twin ${f}`); await utimes(path.join(d, "segments", f), later, later); } await writeFile(path.join(d, "chrome", "deck-frames", "frame_000001.png"), "twin png"); await writeFile(path.join(d, "chrome", "deck-frames", ".key"), "twin-key\n"); await utimes(path.join(d, "chrome", "deck-frames", ".key"), later, later); const got = await linkFromTake(p.dir, "twin", "blurred", out); assert.deepEqual(got.keptOwn, ["s01"]); assert.equal(await readFile(path.join(d, "segments", "s01.mp4"), "utf8"), "twin s01.mp4"); assert.equal(await readFile(path.join(d, "segments", "s01.cut.json"), "utf8"), "twin s01.cut.json", "the record stays with its segment"); assert.ok(!existsSync(path.join(d, "segments", "s01.attrib.txt")), "none of theirs beside our s01"); assert.ok(sameFile(path.join(p.src, "segments", "s02.mp4"), path.join(d, "segments", "s02.mp4")), "a segment we lack is linked"); assert.deepEqual(got.keptOwnFrames, ["deck-frames"]); assert.ok(!got.frames.includes("deck-frames")); assert.equal(await readFile(path.join(d, "chrome", "deck-frames", ".key"), "utf8"), "twin-key\n", "our deck is not replaced"); assert.ok(sameFile(path.join(p.src, "chrome", "deck-frames", "frame_000001.png"), path.join(d, "chrome", "work-from-deck-frames", "frame_000001.png")), "theirs kept for frame reuse"); // An OLDER own layer is replaced, and kept beside as well. await utimes(path.join(d, "chrome", "deck-frames", ".key"), new Date(0), new Date(0)); const again = await linkFromTake(p.dir, "twin", "blurred", out); assert.ok(again.frames.includes("deck-frames")); assert.equal(await readFile(path.join(d, "chrome", "deck-frames", ".key"), "utf8"), "k1\n"); assert.equal(await readFile(path.join(d, "chrome", "work-from-own-deck-frames", ".key"), "utf8"), "twin-key\n"); } finally { await rm(p.root, { recursive: true, force: true }); } }); test("linkFromTake: the chunk record (unless ours is newer), the full variant, the ladder chunks", async () => { const p = await project(); try { // The other take's chunks and their record, and a built full variant with a set-aside layer. await mkdir(path.join(p.src, "chunks"), { recursive: true }); await writeFile(path.join(p.src, "chunks", "chunk-0a1b.mp4"), "chunk"); await writeFile(path.join(p.src, "chunks", "chunks.json"), '{"version":1,"chunks":[]}\n'); await writeFile(path.join(p.src, "chunks", "concat.txt"), "file 'chunk-0a1b.mp4'\n"); const other = path.dirname(p.src); const full = path.join(other, "full"); await mkdir(path.join(full, "segments"), { recursive: true }); await mkdir(path.join(full, "chunks"), { recursive: true }); await writeFile(path.join(full, "segments", "s01.mp4"), "full s01"); await writeFile(path.join(full, "segments", "s01.cut.json"), "{}"); await writeFile(path.join(full, "chunks", "chunk-ffee.mp4"), "full chunk"); await writeFile(path.join(full, "chunks", "chunks.json"), "{}\n"); const keptFull = path.join(other, ".keep-frames", "full", "deck-frames"); await mkdir(keptFull, { recursive: true }); await writeFile(path.join(keptFull, "frame_000001.png"), "full deck"); await writeFile(path.join(keptFull, ".key"), "kf\n"); const out = path.join(p.dir, "takes", "twin", "out"); const got = await linkFromTake(p.dir, "twin", "blurred", out); assert.deepEqual(got.variants, ["sourced", "full"]); assert.equal(got.records, 2); assert.equal(got.chunks, 2); assert.ok(sameFile(path.join(p.src, "chunks", "chunks.json"), path.join(out, "sourced", "chunks", "chunks.json"))); assert.ok(!existsSync(path.join(out, "sourced", "chunks", "concat.txt")), "the concat list is written in place by the build: not linked"); assert.ok(sameFile(path.join(full, "segments", "s01.mp4"), path.join(out, "full", "segments", "s01.mp4"))); assert.ok(sameFile(path.join(full, "chunks", "chunk-ffee.mp4"), path.join(out, "full", "chunks", "chunk-ffee.mp4"))); assert.ok(got.frames.includes("full/deck-frames"), got.frames.join(", ")); assert.equal(await readFile(path.join(out, "full", "chrome", "deck-frames", ".key"), "utf8"), "kf\n"); assert.equal(await readFile(path.join(out, "sourced", "chrome", "deck-frames", "frame_000001.png"), "utf8"), "png", "sourced's deck is sourced's"); // Our own NEWER record is kept. const ours = path.join(out, "sourced", "chunks", "chunks.json"); await rm(ours); await writeFile(ours, '{"version":1,"ours":true}\n'); const later = new Date(Date.now() + 60_000); await utimes(ours, later, later); const again = await linkFromTake(p.dir, "twin", "blurred", out); assert.equal(again.records, 0); assert.match(await readFile(ours, "utf8"), /"ours":true/); // The other take's ladder chunks (its out/ladder/chunks/), by key; nothing else of the ladder. const ladder = path.join(other, "ladder", "chunks"); await mkdir(ladder, { recursive: true }); const rungFile = `rung-${"ab".repeat(12)}.mp4`; await writeFile(path.join(ladder, rungFile), "rung"); await writeFile(path.join(ladder, `.rung-${"cd".repeat(12)}.1.partial.mp4`), "half"); const basis = { maxMiB: 24, seconds: 781.37, srcHeight: 1080, maxHeight: null }; await writeFile(path.join(other, "ladder", "ladder.json"), JSON.stringify({ version: 1, master: { id: "x" }, rungs: { "720p": { keys: ["a"], audio: "copy", master: "x", file: "f" }, fit24: { keys: ["b"], audio: "c", master: "x", file: "g", fit: { basis, scale: 0.94 } } }, })); const withLadder = await linkFromTake(p.dir, "twin", "blurred", out); assert.equal(withLadder.ladder, 1); assert.ok(sameFile(path.join(ladder, rungFile), path.join(out, "ladder", "chunks", rungFile))); assert.deepEqual((await import("node:fs")).readdirSync(path.join(out, "ladder", "chunks")), [rungFile]); // Their record is not linked; their size-capped rung's scale seeds ours. assert.deepEqual(withLadder.ladderFit, ["fit24"]); const seededRec = JSON.parse(await readFile(path.join(out, "ladder", "ladder.json"), "utf8")); assert.deepEqual(seededRec.rungs, { fit24: { keys: [], audio: null, master: null, file: null, fit: { basis, scale: 0.94 } } }); assert.ok(!sameFile(path.join(other, "ladder", "ladder.json"), path.join(out, "ladder", "ladder.json"))); // Our own scale is never overwritten. seededRec.rungs.fit24.fit.scale = 0.88; await writeFile(path.join(out, "ladder", "ladder.json"), JSON.stringify(seededRec)); const again2 = await linkFromTake(p.dir, "twin", "blurred", out); assert.equal(again2.ladder, 0); assert.deepEqual(again2.ladderFit, []); assert.equal(JSON.parse(await readFile(path.join(out, "ladder", "ladder.json"), "utf8")).rungs.fit24.fit.scale, 0.88); // No full variant on the other side: only sourced. await rm(full, { recursive: true }); assert.deepEqual((await linkFromTake(p.dir, "twin", "blurred", out)).variants, ["sourced"]); } finally { await rm(p.root, { recursive: true, force: true }); } }); test("makeTake --from: linked before the render; --keep-frames reaches the build; a bad --from is refused before anything is written", async () => { const p = await project(); try { const seen = []; const render = async (job) => { seen.push({ keepFrames: job.keepFrames, linked: existsSync(path.join(job.outDir, "sourced", "segments", "s01.mp4")) }); const file = path.join(job.outDir, `${job.slug}.mp4`); await writeFile(file, "render"); return { file }; }; const opts = { render, probe: async () => 1, styles: {} }; await makeTake(p.dir, { id: "twin", label: "Twin", patch: { render: { blur: "off" } }, window: "full" }, { ...opts, from: "blurred", keepFrames: true }); assert.deepEqual(seen, [{ keepFrames: true, linked: true }]); // --keep-frames marks the take: a later build of it sets its frames aside. assert.ok(existsSync(path.join(p.dir, "takes", "twin", "out", ".keep-frames", "asked.json"))); assert.ok(!existsSync(path.join(p.dir, "takes", "blurred", "out", ".keep-frames", "asked.json")), "the other take is not marked"); for (const [from, re] of [["twin", /that is this take/], ["nope", /no such take/]]) { await assert.rejects(makeTake(p.dir, { id: "twin", label: "Twin", window: "full" }, { ...opts, force: true, from }), re); } // A take that was never rendered. await mkdir(path.join(p.dir, "takes", "empty"), { recursive: true }); await assert.rejects(makeTake(p.dir, { id: "fresh", label: "Fresh", window: "full" }, { ...opts, from: "empty" }), /never rendered/); assert.ok(!existsSync(path.join(p.dir, "takes", "fresh")), "nothing written for a refused --from"); assert.equal(seen.length, 1); } finally { await rm(p.root, { recursive: true, force: true }); } }); test("the CLI: --from and --keep-frames; a bad --from exits non-zero with the reason", async () => { const p = await project(); try { const errs = []; const deps = { render: async (job) => { const file = path.join(job.outDir, `${job.slug}.mp4`); await writeFile(file, "render"); return { file }; }, probe: async () => 1, }; const code = await takeCommand(["proj", "--id", "twin", "--label", "Twin", "--set", "render.blur=off", "--full", "--from", "blurred", "--keep-frames"], { reportsRoot: p.reportsRoot, log: () => {}, err: (s) => errs.push(s), deps, }); assert.equal(code, 0, errs.join("\n")); assert.ok(existsSync(path.join(p.dir, "takes", "twin", "out", "sourced", "segments", "s01.mp4"))); const bad = await takeCommand(["proj", "--id", "other", "--label", "O", "--full", "--from", "missing"], { reportsRoot: p.reportsRoot, log: () => {}, err: (s) => errs.push(s), deps, }); assert.equal(bad, 1); assert.match(errs.at(-1), /--from missing: no such take/); // The help names both. const help = []; await takeCommand(["--help"], { log: (s) => help.push(s) }); assert.match(help.join("\n"), /--from TAKE/); assert.match(help.join("\n"), /--keep-frames/); } finally { await rm(p.root, { recursive: true, force: true }); } }); test("renderArgv: --keep-frames only when asked", () => { const base = { manifestPath: "/m.json", outDir: "/o", full: true, xfade: true, queueLock: null }; assert.ok(!renderArgv(base).includes("--keep-frames")); assert.ok(renderArgv({ ...base, keepFrames: true }).includes("--keep-frames")); });