// The intro, edited and previewed (lane intro-play, 9 Nov 2026): introEdit.mjs (the Cut tab's intro editor and // `umtool intro`), and onscreen.mjs composeIntroPreview (Play's, Present's and the editor's page). import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { introObject, introPatch, introState, introValue, introWritten, setVersionIntro, takeIntroOff, writeIntro } from "./introEdit.mjs"; import { INTRO_SEEK_SCRIPT, composeIntroPreview } from "./onscreen.mjs"; import { introPreviewDir, introPreviewSrc, previewDirFor } from "./serve.mjs"; import { draftTake } from "./takeMaker.mjs"; import { resolveTakeManifest } from "./takeManifest.mjs"; import { introEntry } from "umtool-report-to-video/intro"; import { introCommand } from "./introCli.mjs"; import { PROJECT_KINDS } from "../projects/kinds.mjs"; const TAKES_KIND = PROJECT_KINDS.find((k) => k.makesTakes).id; const MANIFEST = { schemaVersion: 1, slug: "fixture", title: "Fixture, in its own words", generatedOn: "2026-10-29", render: { width: 640, height: 360, fps: 15, transition: 0, crf: 30, intro: "boot-dark" }, timeline: [{ type: "clip", id: "c01", channel: "chan", video: "vid", start: 100, end: 110, cite: 100 }], }; async function project(manifest = MANIFEST) { const root = await mkdtemp(path.join(tmpdir(), "introedit-")); const dir = path.join(root, "reports", "proj"); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(manifest, null, 2) + "\n"); return { root, dir, ref: { id: "proj", dir, kind: TAKES_KIND }, reportsRoot: path.join(root, "reports") }; } const read = async (dir) => JSON.parse(await readFile(path.join(dir, "video.manifest.json"), "utf8")); const exists = (p) => stat(p).then(() => true, () => false); test("introValue: seconds a number, lines a list (JSON or a|b), false words, an empty value clears", () => { assert.equal(introValue("seconds", "4.5"), 4.5); assert.deepEqual(introValue("lines", "29 October 2026|Part one"), ["29 October 2026", "Part one"]); assert.deepEqual(introValue("lines", '["a, b","c"]'), ["a, b", "c"]); assert.equal(introValue("sound", "false"), false); assert.equal(introValue("sound", "intro.wav"), "intro.wav"); assert.equal(introValue("url", "false"), false); assert.deepEqual(introValue("site", '{"name":"Jeralyzer"}'), { name: "Jeralyzer" }); assert.equal(introValue("title", ""), undefined); }); test("introPatch: onto the intro as written (a bare style its object), a new one from the brand sting; a stray key refused", () => { assert.deepEqual(introPatch("boot", [["title", "T"]]), { style: "boot", title: "T" }); assert.deepEqual(introPatch(null, [["seconds", "5"]]), { style: "boot-dark", seconds: 5 }); assert.deepEqual(introPatch({ style: "title-card", title: "T", lines: ["x"] }, [["title", ""], ["lines", ""]]), { style: "title-card" }); assert.throws(() => introPatch("boot", [["subtitle", "x"]]), /render\.intro\.subtitle is not a key/); assert.throws(() => introPatch("boot", [["style", ""]]), /--off/); assert.equal(introWritten({ style: "boot" }), "boot", "a lone style stays the bare name"); assert.deepEqual(introObject("boot"), { style: "boot" }); assert.equal(introObject(null), null); }); test("writeIntro: the look writer -- the build's checks refuse a bad intro, null takes it off", async () => { const { root, dir } = await project(); try { await writeIntro(dir, { style: "title-card", title: "Mine" }); assert.deepEqual((await read(dir)).render.intro, { style: "title-card", title: "Mine" }); await assert.rejects(writeIntro(dir, { style: "title-card", seconds: 9 }), /render\.intro\.seconds must be a number from 3\.5 to 8/); await writeIntro(dir, { style: "boot" }); assert.equal((await read(dir)).render.intro, "boot"); await writeIntro(dir, null); assert.ok(!("intro" in (await read(dir)).render)); } finally { await rm(root, { recursive: true, force: true }); } }); test("a version without the intro: its patch carries render.intro: null (the project's manifest untouched); on puts the cut's back", async () => { const { root, dir, ref } = await project(); try { await draftTake(dir, { id: "v1", label: "V1", patch: {} }); const before = await readFile(path.join(dir, "video.manifest.json"), "utf8"); const off = await setVersionIntro(dir, "v1", false); assert.equal(off.unchanged, false); const doc = JSON.parse(await readFile(path.join(dir, "takes", "v1", "take.json"), "utf8")); assert.equal(doc.patch.render.intro, null); assert.ok(takeIntroOff(doc.patch)); assert.equal(await readFile(path.join(dir, "video.manifest.json"), "utf8"), before, "the project's manifest is not touched"); assert.ok(!("intro" in ((await resolveTakeManifest(dir, "v1")).manifest.render ?? {})), "the version opens on no intro"); const state = await introState(ref); assert.deepEqual(state.versions.map((v) => [v.id, v.intro]), [["v1", "off"]]); assert.equal(state.entry?.style, "boot-dark"); assert.equal((await setVersionIntro(dir, "v1", false)).unchanged, true, "off twice writes nothing"); await setVersionIntro(dir, "v1", true); assert.equal((await resolveTakeManifest(dir, "v1")).manifest.render.intro, "boot-dark"); // On deletes the key (no stale copy of the cut's intro): the version follows what it is laid over. const onDoc = JSON.parse(await readFile(path.join(dir, "takes", "v1", "take.json"), "utf8")); assert.ok(!onDoc.patch || !("intro" in (onDoc.patch.render ?? {})), "no render.intro left in the patch"); assert.equal((await setVersionIntro(dir, "v1", true)).unchanged, true, "on twice writes nothing"); await writeIntro(dir, { style: "title-card" }); assert.equal((await resolveTakeManifest(dir, "v1")).manifest.render.intro, "title-card", "a legacy take follows the draft's intro"); await writeIntro(dir, null); await assert.rejects(setVersionIntro(dir, "v1", true), /the cut has no intro to put back/); } finally { await rm(root, { recursive: true, force: true }); } }); test("composeIntroPreview: the build's page with Play's seek, GSAP beside it, the face only when there are words; the draft its own file", async () => { const { root, dir } = await project(); try { const render = { width: 640, height: 360, fps: 15 }; const plain = introEntry({ intro: "boot-dark" }, {}); const c = await composeIntroPreview({ dir }, "full", plain, render); const pdir = introPreviewDir(dir, "full"); assert.equal(c.file, "index.html"); const html = await readFile(path.join(pdir, "index.html"), "utf8"); assert.ok(html.includes(INTRO_SEEK_SCRIPT), "Play's seek appended"); assert.ok(html.indexOf(INTRO_SEEK_SCRIPT) < html.lastIndexOf("")); assert.ok(await exists(path.join(pdir, "assets", "gsap.min.js"))); assert.ok(!(await exists(path.join(pdir, "assets", "IntroDisplay.ttf"))), "no words, no face"); const card = introEntry({ intro: "title-card" }, { manifest: MANIFEST }); const d = await composeIntroPreview({ dir }, "full", card, render, { draft: true }); assert.equal(d.file, "draft.html"); const draft = await readFile(path.join(pdir, "draft.html"), "utf8"); assert.ok(draft.includes("Fixture, in its own words") && draft.includes("29 October 2026")); assert.ok(await exists(path.join(pdir, "assets", "IntroDisplay.ttf"))); assert.equal(await readFile(path.join(pdir, "index.html"), "utf8"), html, "the draft never overwrites the cut's page"); // Served under intro-preview/ by the files route, a take's under its own prefix. assert.deepEqual(previewDirFor(dir, "full", ["intro-preview", "draft.html"], []), { dir: pdir, rest: ["draft.html"] }); assert.match(introPreviewSrc("reports/proj", "full", null), /\/full\/intro-preview\/index\.html$/); assert.match(introPreviewSrc("reports/proj", "full", "v1", "index.html"), /\/full\/@take\/v1\/intro-preview\/index\.html$/); } finally { await rm(root, { recursive: true, force: true }); } }); test("umtool intro: prints the intro, writes key=value, --off takes it off, --take --off a version's", async () => { const { root, dir, reportsRoot } = await project(); const out = []; const err = []; const run = (args) => introCommand(args, { root: reportsRoot, out: (s) => out.push(s), err: (s) => err.push(s) }); try { assert.equal(await run(["proj"]), 0); assert.match(out.join("\n"), /boot-dark/); assert.equal(await run(["proj", "style=title-card", "seconds=5", "lines=29 October 2026|Part one"]), 0); assert.deepEqual((await read(dir)).render.intro, { style: "title-card", seconds: 5, lines: ["29 October 2026", "Part one"] }); assert.equal(await run(["proj", "seconds=9"]), 1); assert.match(err.join("\n"), /seconds must be a number from 3\.5 to 8/); await draftTake(dir, { id: "v1", label: "V1", patch: {} }); assert.equal(await run(["proj", "--take", "v1", "--off"]), 0); assert.equal(JSON.parse(await readFile(path.join(dir, "takes", "v1", "take.json"), "utf8")).patch.render.intro, null); out.length = 0; assert.equal(await run(["proj", "--json"]), 0); const j = JSON.parse(out.join("\n")); assert.deepEqual(j.versions.map((v) => v.intro), ["off"]); assert.equal(await run(["proj", "--off"]), 0); assert.ok(!("intro" in (await read(dir)).render)); } finally { await rm(root, { recursive: true, force: true }); } });