// The clip editor's live preview, server side (plans/live-preview.md): the // preview's estimate on demand, the finale's preview composition and where it // is served from, the editor's `onscreen` op, and Render this clip's job step // and file. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { MANIFEST_NAME, StaleToken } from "./manifest.mjs"; import { clipEditorView, runClipEditorOp } from "./clipEditor.mjs"; import { clipRenderFile, clipRenderSteps, readClipRender, windowMoved } from "./clipRender.mjs"; import { composeFinalePreview, composeTeaserPreviews, renderSeconds, scheduleForPreview, TEASER_SEEK_SCRIPT, withTeaserSeek } from "./onscreen.mjs"; import { finalePreviewDir, finalePreviewSrc, previewDirFor, teaserPreviewDir, teaserPreviewSrc } from "./serve.mjs"; import { PIPELINE_DIR, RENDER_BUDGET, buildSteps, buildTimeoutMs } from "./driver.mjs"; import { takeTimeoutMs } from "./takeMaker.mjs"; const manifest = () => ({ schemaVersion: 1, slug: "lp", provenance: { channelSlug: "chan", siteOrigin: "https://example.test" }, render: { width: 1920, height: 1080, fps: 30, palette: { bg: "#101219", fg: "#f2f0ea", muted: "#979caa", accent: "#e5534b", amber: "#ffc860" }, chrome: { engine: "hyperframes", layout: "deck", deck: {} } }, timeline: [ { type: "clip", id: "a", video: "v1", start: 10, end: 20, cutStart: 11, cutEnd: 19 }, { type: "clip", id: "b", video: "v1", start: 30, end: 40, onscreen: { title: "1 Oct 2026" }, layout: "finale" }, ], }); async function project() { const dir = await mkdtemp(path.join(tmpdir(), "umtool-live-preview-")); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify(manifest(), null, 2) + "\n"); return { id: "reports/lp", dir }; } const read = async (dir) => JSON.parse(await readFile(path.join(dir, MANIFEST_NAME), "utf8")); const noLinks = { find: async () => null }; test("scheduleForPreview: `estimate: true` never reads the build's schedule", async () => { const p = await project(); try { const m = await read(p.dir); // A build's schedule that still matches the cut, with seconds the estimate would never give. const built = { version: 1, kind: "deck", estimated: false, fps: 30, transition: 0.5, total: 99, multiChannel: false, segments: [ { id: "a", type: "clip", start: 0, duration: 50, end: 50, title: "", subtitle: "built", qrUrl: null, hideDeck: false }, { id: "b", type: "clip", start: 49.5, duration: 49.5, end: 99, title: "1 Oct 2026", subtitle: "built", qrUrl: null, hideDeck: false }, ], }; await mkdir(path.join(p.dir, "out", "sourced"), { recursive: true }); await writeFile(path.join(p.dir, "out", "sourced", "schedule.json"), JSON.stringify(built)); const asBuilt = await scheduleForPreview(p, m, "sourced", new Map(), {}, { resolver: noLinks }); assert.equal(asBuilt.schedule.total, 99); const est = await scheduleForPreview(p, m, "sourced", new Map(), {}, { resolver: noLinks, estimate: true }); assert.equal(est.schedule.estimated, true); // a: its cut with the 0.4 s lead-in (10.6–19), b: its extent; one 0.5 s join. assert.equal(est.schedule.segments[0].duration, 8.4); assert.equal(est.schedule.total, 8.4 + 10 - 0.5); assert.ok(est.schedule.finale, "the finale is placed"); } finally { await rm(p.dir, { recursive: true, force: true }); } }); test("composeFinalePreview: compose-chrome's finale region, into finale-preview, refused anywhere else", async () => { const calls = []; const proj = { dir: "/proj" }; const schedule = { finale: { from: 1, to: 2 } }; await composeFinalePreview(proj, "sourced", schedule, { compose: async (args) => (calls.push(args), { projDir: path.join(args.outDir, "chrome", "finale-preview") }), }); assert.deepEqual(calls[0], { manifestPath: path.join("/proj", "video.manifest.json"), outDir: path.join("/proj", "out", "sourced"), variant: "sourced", region: "finale", schedule, preview: true, }); await assert.rejects( composeFinalePreview(proj, "sourced", schedule, { compose: async () => ({ projDir: "/elsewhere" }) }), /not \/proj\/out\/sourced\/chrome\/finale-preview/, ); }); test("the finale's preview is served from finale-preview/ under the files route", () => { assert.equal(finalePreviewDir("/p", "sourced"), path.join("/p", "out", "sourced", "chrome", "finale-preview")); assert.match(finalePreviewSrc("reports/x", "sourced"), /^\/api\/report\/chrome\/files\/[A-Za-z0-9_-]+\/sourced\/finale-preview\/index\.html$/); assert.deepEqual(previewDirFor("/p", "sourced", ["finale-preview", "assets", "qr00.png"], []), { dir: path.join("/p", "out", "sourced", "chrome", "finale-preview"), rest: ["assets", "qr00.png"], }); }); test("composeTeaserPreviews: each teaser of the cut through compose-chrome's teaser region, into teaser-preview-, Play's seek appended", async () => { const dir = await mkdtemp(path.join(tmpdir(), "teaser-preview-")); try { const calls = []; const timeline = [ { type: "clip", id: "a" }, { type: "teaser", id: "x1", lines: ["One"] }, { type: "teaser", id: "x2", lines: ["Two"] }, ]; const r = await composeTeaserPreviews({ dir }, "sourced", timeline, { compose: async (args) => { calls.push(args); if (args.segment === "x2") throw new Error("teaser x2: no lines"); const projDir = path.join(args.outDir, "chrome", `teaser-preview-${args.segment}`); await mkdir(projDir, { recursive: true }); await writeFile(path.join(projDir, "index.html"), "

card

\n"); return { projDir }; }, }); assert.deepEqual(calls.map((c) => [c.region, c.segment, c.preview]), [["teaser", "x1", true], ["teaser", "x2", true]]); assert.deepEqual(r, [{ id: "x1", ok: true }, { id: "x2", ok: false, error: "teaser x2: no lines" }]); const html = await readFile(path.join(teaserPreviewDir(dir, "sourced", "x1"), "index.html"), "utf8"); assert.ok(html.includes(TEASER_SEEK_SCRIPT), "the preview copy answers Play's seek"); assert.ok(html.indexOf(TEASER_SEEK_SCRIPT) < html.indexOf(""), "before "); assert.ok(html.startsWith("

card

"), "the build's page otherwise"); // Refused when compose-chrome writes anywhere else. const bad = await composeTeaserPreviews({ dir }, "sourced", [timeline[1]], { compose: async () => ({ projDir: "/elsewhere" }) }); assert.match(bad[0].error ?? "", /not .*teaser-preview-x1/); } finally { await rm(dir, { recursive: true, force: true }); } }); test("a teaser's preview is served from teaser-preview-/ when is an entry of the cut", () => { assert.equal(teaserPreviewDir("/p", "sourced", "x-d28"), path.join("/p", "out", "sourced", "chrome", "teaser-preview-x-d28")); assert.match(teaserPreviewSrc("reports/x", "sourced", "x-d28", "a-take"), /^\/api\/report\/chrome\/files\/[A-Za-z0-9_-]+\/sourced\/@take\/a-take\/teaser-preview-x-d28\/index\.html$/); assert.deepEqual(previewDirFor("/p", "sourced", ["teaser-preview-x-d28", "assets", "gsap.min.js"], ["x-d28"]), { dir: path.join("/p", "out", "sourced", "chrome", "teaser-preview-x-d28"), rest: ["assets", "gsap.min.js"], }); assert.equal(previewDirFor("/p", "sourced", ["teaser-preview-nope", "index.html"], ["x-d28"]), null); assert.equal(previewDirFor("/p", "sourced", ["teaser-preview-..", "index.html"], [".."]), null); // A page with no still gets it, at its end. assert.ok(withTeaserSeek("

x

").endsWith(TEASER_SEEK_SCRIPT)); }); test("the editor's `onscreen` op: the clip's deck words through the On-screen writer; null is automatic; the view carries them", async () => { const p = await project(); try { const v0 = await clipEditorView(p); assert.equal(v0.clips.find((c) => c.id === "a").onscreen, null); assert.deepEqual(v0.clips.find((c) => c.id === "b").onscreen, { title: "1 Oct 2026" }); const r = await runClipEditorOp(p, { op: "onscreen", clip: "a", token: v0.token, onscreen: { title: "24 Oct 2020", subtitle: "a line" } }); assert.deepEqual((await read(p.dir)).timeline[0].onscreen, { title: "24 Oct 2020", subtitle: "a line" }); assert.deepEqual(r.view.clips.find((c) => c.id === "a").onscreen, { title: "24 Oct 2020", subtitle: "a line" }); await runClipEditorOp(p, { op: "onscreen", clip: "a", token: r.view.token, onscreen: null }); assert.equal("onscreen" in (await read(p.dir)).timeline[0], false); await assert.rejects(runClipEditorOp(p, { op: "onscreen", clip: "a", onscreen: "words" }), /onscreen is/); await assert.rejects(runClipEditorOp(p, { op: "onscreen", clip: "a", token: "stale", onscreen: null }), StaleToken); await assert.rejects(runClipEditorOp(p, { op: "onscreen", clip: "zz", onscreen: null }), /no clip zz/); } finally { await rm(p.dir, { recursive: true, force: true }); } }); test("Render this clip: one step, build-video --clip-preview from what is on disk; a stub by env; ids only", () => { const p = { dir: "/proj" }; const [step] = clipRenderSteps(p, "fed", { env: {} }); assert.equal(step.cwd, PIPELINE_DIR); assert.deepEqual(step.env, {}); assert.deepEqual(step.argv, [ "node", path.join(PIPELINE_DIR, "build-video.mjs"), path.join("/proj", "video.manifest.json"), "--out", path.join("/proj", "out"), "--skip-fetch", "--clip-preview", "fed", "--progress", "ndjson", ]); assert.equal(step.ndjson, true); assert.deepEqual(clipRenderSteps(p, "fed", { env: {}, variant: "full" })[0].argv.slice(5, 7), ["--variant", "full"]); assert.equal(clipRenderSteps(p, "fed", { env: { E2E_UMTOOL_CLIP_RENDER_SCRIPT: "/stub.mjs" } })[0].argv[1], "/stub.mjs"); assert.throws(() => clipRenderSteps(p, "../x", { env: {} }), /not a clip id/); assert.equal(clipRenderFile("/proj", "sourced", "fed"), path.join("/proj", "out", "sourced", "clip-preview", "fed.mp4")); assert.equal(clipRenderFile("/proj", "sourced", "../fed"), null); assert.equal(clipRenderFile("/proj", "../x", "fed"), null); }); test("a deck cut's build is budgeted by its length too: the Huck cut's final is not stopped at 72 minutes", () => { const m = manifest(); assert.equal(renderSeconds(m), Math.round(8.4 + 10)); assert.equal(renderSeconds({ ...m, render: { ...m.render, chrome: undefined } }), 0); assert.equal(renderSeconds(null), 0); // 36 clips, 25 minutes of cut: the per-clip budget alone was 72 minutes. assert.equal(buildTimeoutMs(36, true), 72 * 60_000); assert.equal(buildTimeoutMs(36, true, 1515), 1515 * RENDER_BUDGET * 1000); assert.ok(buildTimeoutMs(36, true, 1515) > 2 * 60 * 60_000); assert.equal(buildTimeoutMs(3, false, 0), 15 * 60_000); const final = buildSteps({ id: "p", dir: "/p" }, { preset: "final", clipCount: 36, seconds: 1515 }); assert.equal(final.find((s) => s.label === "final").timeoutMs, 1515 * RENDER_BUDGET * 1000); // One clip's preview build is not the cut's. const one = buildSteps({ id: "p", dir: "/p" }, { preset: "preview", only: "c1", clipCount: 36, seconds: 1515 }); assert.equal(one.at(-1).timeoutMs, 36 * 60_000); assert.equal(takeTimeoutMs(true, 36, 1515), 1515 * RENDER_BUDGET * 1000); assert.equal(takeTimeoutMs(true, 36), 36 * 60_000); assert.equal(takeTimeoutMs(false, 36, 1515), 15 * 60_000); }); test("a clip's render on disk, and whether its window moved since", async () => { const p = await project(); try { assert.equal(await readClipRender(p.dir, "sourced", "a"), null); const dir = path.join(p.dir, "out", "sourced", "clip-preview"); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "a.mp4"), "mp4"); const window = { start: 10, end: 20, cutStart: 11, cutEnd: 19 }; await writeFile(path.join(dir, "a.json"), JSON.stringify({ version: 1, id: "a", window })); const entry = (await read(p.dir)).timeline[0]; const r = await readClipRender(p.dir, "sourced", "a", entry); assert.equal(r.size, 3); assert.equal(r.stale, false); assert.deepEqual(r.record.window, window); assert.equal((await readClipRender(p.dir, "sourced", "a", { ...entry, cutEnd: 18.5 })).stale, true); assert.equal(windowMoved(null, entry), false); assert.equal(windowMoved({ window: { start: 30, end: 40, cutStart: null, cutEnd: null } }, { start: 30, end: 40 }), false); assert.equal(windowMoved({ window: { start: 30, end: 40, cutStart: null, cutEnd: null } }, { start: 30, end: 40, cutStart: 31, cutEnd: 39 }), true); } finally { await rm(p.dir, { recursive: true, force: true }); } });