// `umtool take
--id --preview --at S --dur S` (12 Nov 2026, rev9-a9: `--at 440 --dur 170` rendered 92 s of
// the cut's early clips with the chrome reset, over the full preview's file). --at/--dur are seconds of the TAKE's
// own cut: the take's measured schedule says which entries they cover; the whole cut is handed to the render with
// the window (build-video --cut-window), so the deck draws its state at that second; the file is the window's own.
import { test } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync } from "node:fs";
import { mkdir, readFile, utimes, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { fastKindOf, fastRenders, previewTake, previewWindowPlan, windowPreviewMp4 } from "./takePreview.mjs";
import { renderArgv } from "./takeRender.mjs";
import { cutWindowFile, cutWindowFlag, cutWindowSpan } from "umtool-report-to-video/build-video";
const clip = (id, len) => ({ type: "clip", id, video: "v", cutStart: 0, cutEnd: len });
/** A project whose draft holds a–f (100 s each) and a take whose own cut opens with x (300 s), measured. */
async function fixture() {
const dir = mkdtempSync(path.join(tmpdir(), "take-window-"));
const base = { slug: "s", title: "T", render: { width: 1920, height: 1080, fps: 30 }, timeline: ["a", "b", "c", "d", "e", "f"].map((id) => clip(id, 100)) };
await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(base));
const takeDir = path.join(dir, "takes", "t1");
const timeline = [clip("x", 300), ...base.timeline];
await mkdir(path.join(takeDir, "out", "preview", "sourced"), { recursive: true });
await writeFile(path.join(takeDir, "take.json"), JSON.stringify({ id: "t1", label: "T1", draft: true, window: "full", patch: { timeline } }));
await writeFile(path.join(takeDir, "out", "s.preview.mp4"), "FULL");
let t = 0;
const segments = timeline.map((e) => {
const s = { id: e.id, start: t, duration: e.cutEnd };
t += e.cutEnd;
return s;
});
await writeFile(path.join(takeDir, "out", "preview", "sourced", "schedule.json"), JSON.stringify({ estimated: false, total: t, segments }));
return { dir, takeDir };
}
test("previewWindowPlan: --at/--dur are seconds of the take's own cut, timed by its measured schedule", () => {
const timeline = [clip("x", 300), clip("a", 100), clip("b", 100), clip("c", 100), clip("d", 100)];
const schedule = { segments: timeline.map((e, i) => ({ id: e.id, start: i === 0 ? 0 : 300 + (i - 1) * 100, duration: e.cutEnd })) };
const p = previewWindowPlan(timeline, { at: 440, dur: 170 }, schedule);
assert.deepEqual(p.ids, ["b", "c", "d"]);
assert.equal(p.basis, "schedule");
// Past the take's end: refused, naming the unit and the cut's length.
assert.throws(() => previewWindowPlan(timeline, { at: 900, dur: 10 }, schedule), /--at 900 is past the end of the take's cut \(700 s of cut time/);
// No schedule: the take's own lengths, said to be estimates.
const e = previewWindowPlan(timeline, { at: 0, dur: 10 }, null);
assert.equal(e.basis, "estimate");
assert.deepEqual(e.ids, ["x"]);
});
test("take --preview --at 440 --dur 170: the whole take's cut with the window, to its own file; the full preview untouched", async () => {
const { dir, takeDir } = await fixture();
try {
/** @type {any[]} */
const jobs = [];
const lines = [];
const render = async (job) => {
const doc = JSON.parse(await readFile(job.manifestPath, "utf8"));
jobs.push({ ...job, doc });
const file = cutWindowFile(job.outDir, job.slug, "sourced");
await mkdir(path.dirname(file), { recursive: true });
await writeFile(file, "WINDOW");
return { file };
};
const r = await previewTake(dir, "t1", { window: { at: 440, dur: 170 }, render, log: (l) => lines.push(l) });
assert.equal(jobs.length, 1);
// The window is the render's to lay: every entry of the take's cut is in its manifest (the chrome's state at 440 s).
assert.deepEqual(jobs[0].doc.timeline.map((e) => e.id), ["x", "a", "b", "c", "d", "e", "f"]);
assert.deepEqual(jobs[0].cutWindow, { at: 440, dur: 170 });
assert.equal(jobs[0].doc.render.tier?.name, "preview");
// Its own build dir, never the full preview's.
assert.equal(jobs[0].outDir, path.join(takeDir, "out", "window"));
// The entries the take's schedule puts in 440–610: b (400–500), c (500–600), d (600–700).
assert.deepEqual(r.covers, ["b", "c", "d"]);
assert.ok(lines.some((l) => /cut seconds 440–610/.test(l) && /b, c, d/.test(l)), lines.join("\n"));
assert.equal(r.mp4, path.join(takeDir, "out", "s.preview.440-610.mp4"));
assert.equal(r.mp4, windowPreviewMp4(takeDir, "s", { at: 440, dur: 170 }));
assert.equal(await readFile(r.mp4, "utf8"), "WINDOW");
assert.equal(await readFile(path.join(takeDir, "out", "s.preview.mp4"), "utf8"), "FULL");
// Past the take's end: refused before anything renders.
await assert.rejects(previewTake(dir, "t1", { window: { at: 2000, dur: 10 }, render }), /--at 2000 is past the end/);
assert.equal(jobs.length, 1);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("--cut-window: build-video's flag, its span against the cut, and the take render's argv", () => {
assert.equal(cutWindowFlag(["m.json"]), null);
assert.deepEqual(cutWindowFlag(["m.json", "--cut-window", "440", "170"]), { at: 440, dur: 170 });
assert.throws(() => cutWindowFlag(["m.json", "--cut-window", "7:20", "170"]), /seconds of the cut/);
assert.throws(() => cutWindowFlag(["m.json", "--cut-window", "4", "5", "--chrome-only"]), /not with --chrome-only/);
assert.deepEqual(cutWindowSpan({ at: 600, dur: 170 }, 668.3), { at: 600, dur: 668.3 - 600 });
assert.throws(() => cutWindowSpan({ at: 700, dur: 10 }, 668.3), /past the cut's end \(668\.3 s/);
const argv = renderArgv({ manifestPath: "m.json", outDir: "/o", full: false, xfade: false, queueLock: null, cutWindow: { at: 440, dur: 170 } });
assert.deepEqual(argv.slice(argv.indexOf("--cut-window"), argv.indexOf("--cut-window") + 3), ["--cut-window", "440", "170"]);
assert.ok(!renderArgv({ manifestPath: "m.json", outDir: "/o", full: false, xfade: false, queueLock: null }).includes("--cut-window"));
assert.equal(cutWindowFile("/o", "s", "sourced"), "/o/sourced/s.window.mp4");
});
// Review round 1 (S1–S6).
test("a schedule older than take.json is not the take's: its entries' lengths, said as estimates -- and an --at past their end refused before anything builds (S1, S2)", async () => {
const { dir, takeDir } = await fixture();
try {
// The take grew since its last preview: x is 500 s now (the schedule says 300). Its take.json is the newer file.
const tj = JSON.parse(await readFile(path.join(takeDir, "take.json"), "utf8"));
tj.patch.timeline[0] = clip("x", 500);
await writeFile(path.join(takeDir, "take.json"), JSON.stringify(tj));
const old = new Date(Date.now() - 3600_000);
await utimes(path.join(takeDir, "out", "preview", "sourced", "schedule.json"), old, old);
const jobs = [];
const render = async (job) => {
jobs.push(job);
const file = cutWindowFile(job.outDir, job.slug, "sourced");
await mkdir(path.dirname(file), { recursive: true });
await writeFile(file, "W");
return { file };
};
// 650 is inside the take's 1100 s now (by its lengths), past the stale schedule's 900 -- not refused.
const r = await previewTake(dir, "t1", { window: { at: 650, dur: 50 }, render });
assert.equal(r.basis, "estimate");
assert.deepEqual(r.covers, ["b"]);
// Past the estimated end: refused, saying it is an estimate and the length, before any render.
await assert.rejects(previewTake(dir, "t1", { window: { at: 5000, dur: 50 }, render }), /--at 5000 is past the end of the take's cut by its entries' own lengths \(an estimate: 1100 s/);
assert.equal(jobs.length, 1);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("the file names the window rendered (clipped at the cut's end); a second window while one runs is refused; take status reads the window's record (S3, S4, S5)", async () => {
const { dir, takeDir } = await fixture();
try {
// The build reports the window it laid (build-video's done event): 650–700, the cut ends at 700.
const render = async (job) => {
job.emit({ ev: "done", out: "x", failures: [], window: { at: 650, dur: 50 } });
const file = cutWindowFile(job.outDir, job.slug, "sourced");
await mkdir(path.dirname(file), { recursive: true });
await writeFile(file, "W");
return { file };
};
const r = await previewTake(dir, "t1", { window: { at: 650, dur: 100 }, render });
assert.equal(path.basename(r.mp4), "s.preview.650-700.mp4");
// With no report from the build, the plan's own end clips it.
const r2 = await previewTake(dir, "t1", { window: { at: 600, dur: 500 }, render: async (job) => {
const file = cutWindowFile(job.outDir, job.slug, "sourced");
await mkdir(path.dirname(file), { recursive: true });
await writeFile(file, "W");
return { file };
} });
assert.equal(path.basename(r2.mp4), "s.preview.600-900.mp4");
// take status: the window's own record, labelled, never the full preview's.
const fast = await fastRenders(takeDir, "s");
assert.equal(fast.window?.state, "done");
assert.equal(fast.window?.span, "600-1100");
assert.equal(path.basename(fast.window?.mp4 ?? ""), "s.preview.600-900.mp4");
assert.equal(fast.preview?.mp4, path.join(takeDir, "out", "s.preview.mp4"));
assert.equal(fastKindOf(["take", "p", "--id", "t1", "--preview", "--at", "1", "--dur", "2"]), "window");
assert.equal(fastKindOf(["take", "p", "--id", "t1", "--preview"]), "preview");
const { takeStatus } = await import("../queue/cli.mjs");
const said = [];
await takeStatus({ id: "p", dir }, { take: "t1", json: false, showLog: false, tail: null, reportsRoot: dir, env: {}, log: (l) => said.push(l), err: (l) => said.push(l) });
assert.match(said.join("\n"), /window 600-1100: DONE — .*s\.preview\.600-900\.mp4/);
assert.match(said.join("\n"), /preview: DONE — .*out\/s\.preview\.mp4/);
// A window running now (its record live, this very process): the second is refused, naming it.
const rec = JSON.parse(await readFile(path.join(takeDir, "out", "window", "render.json"), "utf8"));
await writeFile(path.join(takeDir, "out", "window", "render.json"), JSON.stringify({ ...rec, state: "running", pid: process.pid, pidStart: null, endedAt: null }));
await assert.rejects(previewTake(dir, "t1", { window: { at: 10, dur: 5 }, render }), new RegExp(`a window preview of t1 is running \\(pid ${process.pid}, window 600-1100`));
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("--cut-window refuses the runs that are not a build of the cut (S6)", () => {
for (const f of [["--clip-preview", "c1"], ["--fetch-only", "c1"], ["--short", "c1"], ["--join-preview"], ["--thumbnail"], ["--chapters-only"]]) {
assert.throws(() => cutWindowFlag(["m.json", "--cut-window", "1", "2", ...f]), new RegExp(`not with ${f[0]}`), f[0]);
}
});