// The named montage styles (release 33 M3): the shared interface with umtool's // take maker -- names, plain-words labels, merge patches under render.montage, // deep copies -- and RFC 7386 merge patching. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; import { applyMontageStyle, mergePatch, MONTAGE_STYLE_NAMES, MONTAGE_STYLES, montageStyle } from "./montage-styles.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); test("MONTAGE_STYLES: at least the six named styles, each a label, a summary and a patch under render.montage", () => { for (const name of ["straight", "flash-count", "accelerate", "stutter", "wall-finale", "whip-years"]) { assert.ok(MONTAGE_STYLES[name], name); } assert.ok(MONTAGE_STYLE_NAMES.length >= 6); for (const [name, s] of Object.entries(MONTAGE_STYLES)) { assert.equal(typeof s.label, "string", name); assert.ok(s.label.length > 0 && s.label.length <= 40, name); assert.ok(s.summary.length > 10 && !/[{}`]/.test(s.summary), `${name}: plain words`); assert.deepEqual(Object.keys(s.patch), ["render"], name); assert.deepEqual(Object.keys(s.patch.render), ["montage"], name); assert.equal(s.patch.render.montage.tighten, "phrase", name); } }); test("every style names every montage key, so one style over another leaves exactly the second", () => { const keys = (s) => Object.keys(s.patch.render.montage).sort(); const first = keys(MONTAGE_STYLES.straight); for (const s of Object.values(MONTAGE_STYLES)) assert.deepEqual(keys(s), first); const base = { slug: "x", render: { fps: 30, montage: { stutter: { every: 2, times: 4 }, junkKeepsNothing: 1 } }, timeline: [] }; for (const a of MONTAGE_STYLE_NAMES) { for (const b of MONTAGE_STYLE_NAMES) { assert.deepEqual( applyMontageStyle(applyMontageStyle(base, a), b).render.montage, { ...applyMontageStyle({ render: {} }, b).render.montage, junkKeepsNothing: 1 }, `${a} then ${b}`, ); } } }); test("the styles say what the brief asks of each", () => { const m = (n) => applyMontageStyle({}, n).render.montage; assert.equal(m("straight").cut, "hard"); assert.equal(m("straight").counter.style, "chip"); assert.equal(m("flash-count").cut, "flash"); assert.equal(m("flash-count").counter.style, "big"); assert.deepEqual(m("accelerate").ramp, { from: 4, to: 1.4 }); assert.equal(m("accelerate").punch.mode, "drift"); assert.equal(m("accelerate").counter.style, "tally"); assert.equal(m("stutter").stutter.every, 5); assert.equal(m("stutter").punch.mode, "snap"); assert.equal(m("wall-finale").wall.at, "end"); assert.equal(m("wall-finale").wall.tiles, 16); assert.equal(m("wall-finale").counter.style, "chip"); assert.equal(m("whip-years").cut, "whip"); assert.equal(m("whip-years").yearStamp, true); assert.equal(m("whip-years").counter.style, "chip"); // Off is absent (null removed the key) or false. assert.equal("wall" in m("straight"), false); assert.equal(m("straight").yearStamp, false); }); test("montageStyle: a deep copy, or null", () => { const a = montageStyle("straight"); a.patch.render.montage.cut = "whip"; a.label = "changed"; assert.equal(MONTAGE_STYLES.straight.patch.render.montage.cut, "hard"); assert.equal(MONTAGE_STYLES.straight.label, "Straight"); assert.equal(montageStyle("nope"), null); assert.equal(montageStyle("toString"), null, "not an inherited key"); assert.throws(() => applyMontageStyle({}, "nope"), /no montage style "nope" — the styles are straight, /); }); test("mergePatch: RFC 7386 -- objects merge, null removes, anything else replaces; inputs untouched", () => { const target = { a: "b", c: { d: "e", f: "g" }, list: [1, 2] }; const patch = { a: "z", c: { f: null, h: 1 }, list: [3], n: { deep: { x: 1 } } }; assert.deepEqual(mergePatch(target, patch), { a: "z", c: { d: "e", h: 1 }, list: [3], n: { deep: { x: 1 } } }); assert.deepEqual(target, { a: "b", c: { d: "e", f: "g" }, list: [1, 2] }); // The RFC's own examples. assert.deepEqual(mergePatch({ a: "b" }, { a: "c" }), { a: "c" }); assert.deepEqual(mergePatch({ a: "b" }, { b: "c" }), { a: "b", b: "c" }); assert.deepEqual(mergePatch({ a: "b" }, { a: null }), {}); assert.deepEqual(mergePatch({ a: ["b"] }, { a: "c" }), { a: "c" }); assert.deepEqual(mergePatch({ a: "c" }, { a: ["b"] }), { a: ["b"] }); assert.deepEqual(mergePatch({ a: { b: "c" } }, { a: { b: "d", c: null } }), { a: { b: "d" } }); assert.deepEqual(mergePatch(["a", "b"], ["c", "d"]), ["c", "d"]); assert.deepEqual(mergePatch({ a: "b" }, ["c"]), ["c"]); assert.equal(mergePatch({ a: "foo" }, null), null); assert.deepEqual(mergePatch([1, 2], { a: "b", c: null }), { a: "b" }); }); test("the CLI: list, and apply to a file", () => { const list = spawnSync(process.execPath, [path.join(HERE, "montage-styles.mjs"), "list"]); assert.equal(list.status, 0, String(list.stderr)); assert.match(String(list.stdout), /^straight\s+Straight — /m); const json = spawnSync(process.execPath, [path.join(HERE, "montage-styles.mjs"), "list", "--json"]); assert.deepEqual(JSON.parse(String(json.stdout)), JSON.parse(JSON.stringify(MONTAGE_STYLES))); const dir = mkdtempSync(path.join(tmpdir(), "montage-styles-")); try { const src = path.join(dir, "video.manifest.json"); const out = path.join(dir, "styled.json"); writeFileSync(src, JSON.stringify({ slug: "x", render: { fps: 30 }, timeline: [] })); const r = spawnSync(process.execPath, [path.join(HERE, "montage-styles.mjs"), "apply", "flash-count", src, "--out", out]); assert.equal(r.status, 0, String(r.stderr)); const got = JSON.parse(readFileSync(out, "utf8")); assert.equal(got.render.fps, 30); assert.equal(got.render.montage.cut, "flash"); assert.equal(JSON.parse(readFileSync(src, "utf8")).render.montage, undefined, "the source is untouched"); const bad = spawnSync(process.execPath, [path.join(HERE, "montage-styles.mjs"), "apply", "nope", src]); assert.equal(bad.status, 1); assert.match(String(bad.stderr), /no montage style "nope"/); } finally { rmSync(dir, { recursive: true, force: true }); } });