// The video side of an article's iterations (release 27 S7): the parent // seed and the dedupe. Temp dirs only. // // Run with: node --test umtool/lib/report/iterations.test.mjs import assert from "node:assert/strict"; import test, { after } from "node:test"; import { mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync, existsSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { DEDUPE_MIN_BYTES, dedupeMedia, seedFromParent } from "./iterations.mjs"; const ROOT = mkdtempSync(path.join(os.tmpdir(), "umtool-iterations-")); after(() => rmSync(ROOT, { recursive: true, force: true })); const put = (file, data) => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, typeof data === "string" || Buffer.isBuffer(data) ? data : `${JSON.stringify(data, null, 2)}\n`); }; const json = (file) => JSON.parse(readFileSync(file, "utf8")); const ino = (file) => statSync(file).ino; const card = (id, heading, narration) => ({ type: "card", id, style: "chapter", seconds: narration ? "auto" : 3.5, heading, ...(narration ? { narration: { text: narration } } : {}), }); test("seedFromParent: narration carried by id, else by heading; voices linked; a card with its own words kept", async () => { const dir = path.join(ROOT, "seed"); const parent = path.join(dir, "i1"); const child = path.join(dir, "i2"); put(path.join(parent, "video.manifest.json"), { timeline: [card("title", "The title", "Opening words."), card("s1", "The shop", "About the shop."), card("s2", "Gone", "A card the child lost.")], }); put(path.join(parent, "out", "narration", "abc.wav"), Buffer.alloc(100, 1)); put(path.join(parent, "out", "narration", "abc.json"), { key: "abc" }); put(path.join(parent, "out", "narration", "notes.txt"), "not a voice"); put(path.join(child, "video.manifest.json"), { timeline: [ card("title", "The title"), // Renumbered, same heading. card("s9", "THE SHOP "), card("s3", "New section", "Its own words."), { type: "clip", id: "c01", channel: "synth-kitchen", video: "v1", start: 1, end: 2 }, ], }); // A voice the child already has is not replaced. put(path.join(child, "out", "narration", "abc.json"), { key: "mine" }); const r = await seedFromParent(parent, child); assert.deepEqual(r, { carried: 2, linked: 1 }); const m = json(path.join(child, "video.manifest.json")); assert.deepEqual(m.timeline.map((e) => e.narration?.text ?? null), ["Opening words.", "About the shop.", "Its own words.", null]); assert.equal(m.timeline[1].seconds, "auto"); assert.equal(ino(path.join(child, "out", "narration", "abc.wav")), ino(path.join(parent, "out", "narration", "abc.wav"))); assert.deepEqual(json(path.join(child, "out", "narration", "abc.json")), { key: "mine" }); assert.equal(existsSync(path.join(child, "out", "narration", "notes.txt")), false); }); test("seedFromParent: a parent with no voices and nothing to carry writes nothing", async () => { const dir = path.join(ROOT, "seed-empty"); put(path.join(dir, "i1", "video.manifest.json"), { timeline: [card("s1", "One")] }); put(path.join(dir, "i2", "video.manifest.json"), { timeline: [card("s1", "One")] }); const before = readFileSync(path.join(dir, "i2", "video.manifest.json"), "utf8"); assert.deepEqual(await seedFromParent(path.join(dir, "i1"), path.join(dir, "i2")), { carried: 0, linked: 0 }); assert.equal(readFileSync(path.join(dir, "i2", "video.manifest.json"), "utf8"), before); }); test("dedupeMedia: identical segments and voices become one file; nothing else is touched; a dry run links nothing", async () => { const root = path.join(ROOT, "dedupe", "my-article"); const seg = Buffer.alloc(DEDUPE_MIN_BYTES + 10, 7); const other = Buffer.alloc(DEDUPE_MIN_BYTES + 10, 8); const voice = Buffer.alloc(200, 3); put(path.join(root, "i1", "out", "sourced", "segments", "c01.mp4"), seg); put(path.join(root, "i2", "out", "sourced", "segments", "c01.mp4"), seg); put(path.join(root, "i3", "out", "sourced", "segments", "c01.mp4"), other); put(path.join(root, "i2", "out", "full", "segments", "c02.mp4"), seg); put(path.join(root, "i1", "out", "narration", "k.wav"), voice); put(path.join(root, "i2", "out", "narration", "k.wav"), voice); // Small segments, the final cut and anything outside the two places are never linked. put(path.join(root, "i1", "out", "sourced", "segments", "tiny.mp4"), "x"); put(path.join(root, "i2", "out", "sourced", "segments", "tiny.mp4"), "x"); put(path.join(root, "i1", "out", "sourced", "my-article.mp4"), seg); put(path.join(root, "i2", "out", "sourced", "my-article.mp4"), seg); put(path.join(root, "i1", "video.manifest.json"), seg); put(path.join(root, "i2", "video.manifest.json"), seg); const dry = await dedupeMedia(root, { dry: true }); assert.deepEqual(dry, { links: 3, freedBytes: 2 * seg.length + voice.length }); assert.notEqual(ino(path.join(root, "i2", "out", "sourced", "segments", "c01.mp4")), ino(path.join(root, "i1", "out", "sourced", "segments", "c01.mp4"))); const done = await dedupeMedia(root); assert.deepEqual(done, dry); const a = ino(path.join(root, "i1", "out", "sourced", "segments", "c01.mp4")); assert.equal(ino(path.join(root, "i2", "out", "sourced", "segments", "c01.mp4")), a); assert.equal(ino(path.join(root, "i2", "out", "full", "segments", "c02.mp4")), a); assert.notEqual(ino(path.join(root, "i3", "out", "sourced", "segments", "c01.mp4")), a); assert.equal(ino(path.join(root, "i2", "out", "narration", "k.wav")), ino(path.join(root, "i1", "out", "narration", "k.wav"))); assert.notEqual(ino(path.join(root, "i2", "out", "sourced", "segments", "tiny.mp4")), ino(path.join(root, "i1", "out", "sourced", "segments", "tiny.mp4"))); assert.notEqual(ino(path.join(root, "i2", "out", "sourced", "my-article.mp4")), ino(path.join(root, "i1", "out", "sourced", "my-article.mp4"))); assert.notEqual(ino(path.join(root, "i2", "video.manifest.json")), ino(path.join(root, "i1", "video.manifest.json"))); // Run again: already one file, nothing to do. assert.deepEqual(await dedupeMedia(root), { links: 0, freedBytes: 0 }); // No temp left behind. assert.equal(existsSync(path.join(root, "i2", "out", "narration", "k.wav.dedupe-tmp")), false); // A root that is not there is nothing to do. assert.deepEqual(await dedupeMedia(path.join(ROOT, "nowhere")), { links: 0, freedBytes: 0 }); });