// A take's manifest without rendering it (takeManifest.mjs): the project's // manifest now + the style's patch + the take's own, from take.json or a // recipe; the frozen manifest when the style is gone; the play manifest. // // Run with: node --test lib/report/takeManifest.test.mjs 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 { cachedTakeManifest, playableTakes, playManifestPath, resolveTakeManifest, takeSpecOf, writePlayManifest } from "./takeManifest.mjs"; const base = () => ({ slug: "p", render: { transition: 0.5, blur: "on" }, timeline: [ { type: "clip", id: "c01", video: "v", start: 1, end: 3 }, { type: "clip", id: "c02", video: "v", start: 4, end: 6 }, { type: "clip", id: "c03", video: "v", start: 7, end: 9 }, ], }); async function project() { const dir = await mkdtemp(path.join(tmpdir(), "take-manifest-")); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(base())); await mkdir(path.join(dir, "takes", "short"), { recursive: true }); await writeFile( path.join(dir, "takes", "short", "take.json"), JSON.stringify({ id: "short", group: "takes", order: 1, label: "Short", kind: "different", preview: "preview.mp4", style: null, // A take with its own timeline: c03 then c01, uncensored. patch: { render: { blur: "off" }, timeline: [base().timeline[2], base().timeline[0]] }, }), ); await writeFile( path.join(dir, "takes", "recipe.json"), JSON.stringify({ takes: [{ id: "short", label: "dup" }, { id: "quick-joins", label: "Quick joins", patch: { render: { transition: 0.15 } } }, { id: "styled", label: "Styled", style: "grey" }] }), ); return dir; } test("a made take: the project's manifest NOW with its patch (its own timeline)", async () => { const dir = await project(); try { const r = await resolveTakeManifest(dir, "short", { styles: {} }); assert.deepEqual(r.manifest.timeline.map((e) => e.id), ["c03", "c01"]); assert.equal(r.manifest.render.blur, "off"); assert.equal(r.manifest.render.transition, 0.5); assert.equal(r.take.made, true); assert.equal(r.take.from, "takes/short/take.json"); assert.equal(r.note, null); // An edit to the project after the take was made is in what plays. const m = base(); m.render.transition = 0.3; await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(m)); assert.equal((await resolveTakeManifest(dir, "short", { styles: {} })).manifest.render.transition, 0.3); } finally { await rm(dir, { recursive: true, force: true }); } }); test("a take a recipe names, not made yet; a style's patch before the take's", async () => { const dir = await project(); try { const r = await resolveTakeManifest(dir, "quick-joins", { styles: {} }); assert.equal(r.manifest.render.transition, 0.15); assert.deepEqual(r.manifest.timeline.map((e) => e.id), ["c01", "c02", "c03"]); assert.equal(r.take.made, false); assert.equal(r.take.from, "takes/recipe.json"); const s = await resolveTakeManifest(dir, "styled", { styles: { grey: { patch: { render: { grade: "grey" } } } } }); assert.equal(s.manifest.render.grade, "grey"); // The made take wins over the recipe's line of the same id. assert.equal((await takeSpecOf(dir, "short"))?.label, "Short"); assert.deepEqual(await playableTakes(dir), [ { id: "short", label: "Short", made: true }, { id: "quick-joins", label: "Quick joins", made: false }, { id: "styled", label: "Styled", made: false }, ]); } finally { await rm(dir, { recursive: true, force: true }); } }); test("refusals: no such take, a bad id, a gone style with nothing rendered", async () => { const dir = await project(); try { await assert.rejects(resolveTakeManifest(dir, "nope", { styles: {} }), /no take nope/); await assert.rejects(resolveTakeManifest(dir, "../x", { styles: {} }), /not a take id/); await assert.rejects(resolveTakeManifest(dir, "styled", { styles: {} }), /style "grey" is not installed/); } finally { await rm(dir, { recursive: true, force: true }); } }); test("a gone style on a made take: the manifest it was rendered from", async () => { const dir = await project(); try { const frozen = { ...base(), slug: "frozen" }; await writeFile(path.join(dir, "takes", "short", "video.manifest.json"), JSON.stringify(frozen)); const doc = JSON.parse(await readFile(path.join(dir, "takes", "short", "take.json"), "utf8")); await writeFile(path.join(dir, "takes", "short", "take.json"), JSON.stringify({ ...doc, style: "gone" })); const r = await resolveTakeManifest(dir, "short", { styles: {} }); assert.equal(r.manifest.slug, "frozen"); assert.match(String(r.note), /not installed/); } finally { await rm(dir, { recursive: true, force: true }); } }); test("the play manifest is written beside the project's; the cache follows the files", async () => { const dir = await project(); try { const file = await writePlayManifest(dir, "short", { timeline: [] }); assert.equal(file, playManifestPath(dir, "short")); assert.equal(path.dirname(file), dir); assert.deepEqual(JSON.parse(await readFile(file, "utf8")), { timeline: [] }); const a = await cachedTakeManifest(dir, "quick-joins"); assert.equal(a.manifest.render.transition, 0.15); assert.equal(await cachedTakeManifest(dir, "quick-joins"), a); } finally { await rm(dir, { recursive: true, force: true }); } }); test("two writes of one take's play manifest at once both land (Play's preview and intro ask together)", async () => { const dir = await project(); try { // Before, both used `..tmp`: one rename took the other's tmp and the second threw ENOENT. const docs = Array.from({ length: 12 }, (_, i) => ({ timeline: [], n: i })); const files = await Promise.all(docs.map((d) => writePlayManifest(dir, "short", d))); assert.ok(files.every((f) => f === playManifestPath(dir, "short"))); const n = JSON.parse(await readFile(playManifestPath(dir, "short"), "utf8")).n; assert.ok(Number.isInteger(n) && n >= 0 && n < docs.length); const { readdir } = await import("node:fs/promises"); assert.deepEqual((await readdir(dir)).filter((f) => f.endsWith(".tmp")), []); } finally { await rm(dir, { recursive: true, force: true }); } });