// `umtool modules` and `umtool preset` (modulesCli.mjs) over a temp tree. import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { modulesCommand, presetCommand } from "./modulesCli.mjs"; const clip = (id, start) => ({ type: "clip", id, channel: "c", video: "v", start, end: start + 3 }); function tree() { const root = mkdtempSync(path.join(os.tmpdir(), "modules-cli-")); const dir = path.join(root, "proj"); mkdirSync(dir, { recursive: true }); writeFileSync(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: "proj", title: "Proj", provenance: {}, render: { width: 1920, height: 1080, fps: 30 }, timeline: [clip("a", 0), clip("b", 10)], })); writeFileSync(path.join(dir, "module-config.json"), JSON.stringify({ preset: "plain-deck", modules: { chapter: [{ atClip: "b", title: "Two" }], shell: { title: "x" } }, })); return { root, dir }; } const sink = () => { const lines = []; return { lines, log: (s) => lines.push(s), err: (s) => lines.push(`ERR ${s}`) }; }; test("modules apply --dry-run says the patch and what has no home, and writes nothing", async () => { const { root, dir } = tree(); const before = readFileSync(path.join(dir, "video.manifest.json"), "utf8"); const s = sink(); assert.equal(await modulesCommand(["apply", dir, "--dry-run", "--json"], { reportsRoot: root, ...s }), 0, s.lines.join("\n")); const r = JSON.parse(s.lines[0]); assert.equal(r.patch.preset, "plain-deck"); assert.deepEqual(r.patch.render.chrome.chapter, { list: [{ at: "b", title: "Two" }] }); assert.ok(r.unmapped.some((u) => u.startsWith("modules.shell.title"))); assert.equal(readFileSync(path.join(dir, "video.manifest.json"), "utf8"), before); }); test("modules apply --take writes the take's patch and makes the take from it; the manifest untouched", async () => { const { root, dir } = tree(); const before = readFileSync(path.join(dir, "video.manifest.json"), "utf8"); const s = sink(); let got = null; const take = async (args) => { got = args; return 0; }; assert.equal(await modulesCommand(["apply", dir, "--take", "modules-1", "--at", "0", "--dur", "20", "--quick"], { reportsRoot: root, ...s, take }), 0, s.lines.join("\n")); const patchFile = path.join(dir, "takes", "modules-1.modules-patch.json"); assert.deepEqual(got.slice(1, 3), ["--id", "modules-1"]); assert.ok(got.includes("--patch") && got[got.indexOf("--patch") + 1] === patchFile); assert.ok(got.includes("--quick") && got.includes("--at")); assert.equal(JSON.parse(readFileSync(patchFile, "utf8")).preset, "plain-deck"); assert.equal(readFileSync(path.join(dir, "video.manifest.json"), "utf8"), before); }); test("modules apply writes the manifest through the writer; preset apply none is refused while the chrome needs it", async () => { const { root, dir } = tree(); const s = sink(); assert.equal(await modulesCommand(["apply", dir], { reportsRoot: root, ...s }), 0, s.lines.join("\n")); const m = JSON.parse(readFileSync(path.join(dir, "video.manifest.json"), "utf8")); assert.equal(m.preset, "plain-deck"); assert.deepEqual(m.render.chrome, { chapter: { list: [{ at: "b", title: "Two" }] } }); const t = sink(); assert.equal(await presetCommand(["apply", "none", dir], { reportsRoot: root, ...t }), 1); assert.match(t.lines.join("\n"), /render\.chrome\.engine/); assert.equal(await presetCommand(["apply", "archive-catalog", dir], { reportsRoot: root, ...sink() }), 0); assert.equal(JSON.parse(readFileSync(path.join(dir, "video.manifest.json"), "utf8")).preset, "archive-catalog"); }); test("a config the cut could not build is refused, naming why", async () => { const { root, dir } = tree(); writeFileSync(path.join(dir, "module-config.json"), JSON.stringify({ modules: { chapter: [{ atClip: "b", title: "Two" }] } })); const s = sink(); // No preset and no deck: the chapter has no deck to stand in. assert.equal(await modulesCommand(["apply", dir, "--dry-run"], { reportsRoot: root, ...s }), 1); assert.match(s.lines.join("\n"), /refused/); }); test("modules list and preset list", async () => { const s = sink(); assert.equal(await modulesCommand(["list", "--json"], s), 0); const ids = JSON.parse(s.lines[0]).map((m) => m.id); for (const id of ["pair", "chapter", "status", "finale", "intro"]) assert.ok(ids.includes(id), id); const t = sink(); assert.equal(await presetCommand(["list", "--json"], t), 0); assert.deepEqual(JSON.parse(t.lines[0]).map((p) => p.name).sort(), ["archive-catalog", "basic", "ember-deck", "huck-deck", "plain-deck"]); }); test("umtool look takes the story modules' keys, through updateChrome", async () => { const { lookCommand } = await import("./knobsCli.mjs"); const { root, dir } = tree(); assert.equal(await modulesCommand(["apply", dir], { reportsRoot: root, ...sink() }), 0); const s = sink(); const code = await lookCommand([dir, "chapter.style=lower", "pair={\"style\":\"split\"}"], { root, out: s.log, err: s.err }); assert.equal(code, 0, s.lines.join("\n")); const m = JSON.parse(readFileSync(path.join(dir, "video.manifest.json"), "utf8")); assert.equal(m.render.chrome.chapter.style, "lower"); assert.deepEqual(m.render.chrome.chapter.list, [{ at: "b", title: "Two" }]); assert.deepEqual(m.render.chrome.pair, { style: "split" }); const bad = sink(); assert.equal(await lookCommand([dir, "status.rows=40"], { root, out: bad.log, err: bad.err }), 1); assert.match(bad.lines.join("\n"), /status\.rows/); });