// node --test umtool/lib/ingredients/usedIn.test.mjs import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, utimes, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { buildUsageIndex, loadUsageIndex, manifestFilesOf, rowsInWindow, usesOf, usesOfManifest } from "./usedIn.mjs"; const clip = (id, video, start, end, extra = {}) => ({ type: "clip", id, video, start, end, ...extra }); const manifest = (timeline, channelSlug = "chan") => ({ schemaVersion: 1, provenance: { channelSlug }, render: {}, timeline }); async function project(root, rel, m, takes = {}) { const dir = path.join(root, rel); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(m)); for (const [id, t] of Object.entries(takes)) { await mkdir(path.join(dir, "takes", id), { recursive: true }); await writeFile(path.join(dir, "takes", id, "take.json"), "{}"); if (t.base) await writeFile(path.join(dir, "takes", id, "base.manifest.json"), JSON.stringify(t.base)); if (t.rendered) await writeFile(path.join(dir, "takes", id, "video.manifest.json"), JSON.stringify(t.rendered)); } return dir; } async function fixture() { const root = await mkdtemp(path.join(tmpdir(), "usedin-")); const reports = path.join(root, "reports"); const channels = path.join(root, "channels"); await mkdir(reports, { recursive: true }); // A Rumble channel: its folder is the URL slug v7slug1, its archive (embed) id v7emb01. await mkdir(path.join(channels, "rumch", "data", "v7slug1"), { recursive: true }); await writeFile(path.join(channels, "rumch", "config.json"), JSON.stringify({ platform: "rumble" })); await writeFile(path.join(channels, "rumch", "data", "v7slug1", "metadata.info.json"), JSON.stringify({ id: "v7emb01", title: "t" })); return { root, reports, channels }; } test("usesOfManifest: clips with a recording, the provenance's channel as the default", () => { const u = usesOfManifest(manifest([clip("a", "vid", 1, 2), { type: "card", id: "k" }, clip("b", "", 1, 2), clip("c", "v2", 3, 4, { channel: "other", cutStart: 3.5 }), clip("d", "v3", 1, 2, { src: "x.mp4" })])); assert.deepEqual(u.map((x) => `${x.ch}/${x.video}#${x.clipId}`), ["chan/vid#a", "other/v2#c"]); assert.equal(u[1].cutStart, 3.5); }); test("manifestFilesOf: the draft, a version's base, a legacy rendered take; a patch-only take adds nothing", async () => { const { root, reports } = await fixture(); try { const dir = await project(reports, "f/p", manifest([]), { v1: { base: manifest([]) }, a2: { rendered: manifest([]) }, b3: {} }); const files = await manifestFilesOf(dir); assert.deepEqual(files.map((f) => `${f.version}:${f.kind}:${path.basename(f.file)}`), ["draft:draft:video.manifest.json", "a2:rendered:video.manifest.json", "v1:version:base.manifest.json"]); } finally { await rm(root, { recursive: true, force: true }); } }); test("two projects, a version, one row per project clip with its versions", async () => { const { root, reports, channels } = await fixture(); try { await project(reports, "f/a", manifest([clip("a1", "vid", 10, 20), clip("a2", "vid", 100, 110, { cutStart: 102, cutEnd: 108 })]), { v1: { base: manifest([clip("a1", "vid", 11, 19)]) }, }); await project(reports, "f/b", manifest([clip("b1", "vid", 15, 25)])); const { index, stats } = await buildUsageIndex({ reportsRoot: reports, channelsDir: channels }); assert.equal(stats.projects, 2); assert.equal(stats.manifests, 3); const rows = usesOf(index, "chan", "vid"); assert.deepEqual(rows.map((r) => `${r.project}#${r.clipId}`), ["f/a#a1", "f/a#a2", "f/b#b1"]); const a1 = rows[0]; assert.equal(a1.inDraft, true); assert.equal(a1.start, 10); assert.deepEqual(a1.versions.map((v) => `${v.id}:${v.kind}:${v.start}`), ["v1:version:11"]); // A window 100-130 serves a2 only; 0-30 serves a1 and b1. assert.deepEqual(rowsInWindow(rows, { from: 100, to: 130 }).map((r) => r.clipId), ["a2"]); assert.deepEqual(rowsInWindow(rows, { from: 0, to: 30 }).map((r) => r.clipId), ["a1", "b1"]); } finally { await rm(root, { recursive: true, force: true }); } }); test("incremental: an unchanged manifest is reused, a changed one parsed again, a deleted project drops out", async () => { const { root, reports, channels } = await fixture(); const file = path.join(root, "state", "usage-index.json"); try { const a = await project(reports, "f/a", manifest([clip("a1", "vid", 10, 20)])); await project(reports, "f/b", manifest([clip("b1", "vid", 15, 25)])); const first = await loadUsageIndex({ reportsRoot: reports, channelsDir: channels, file }); assert.equal(first.stats.parsed, 2); assert.equal(first.written, true); const again = await loadUsageIndex({ reportsRoot: reports, channelsDir: channels, file }); assert.deepEqual([again.stats.parsed, again.stats.reused, again.written], [0, 2, false]); // f/a's draft changes (a new mtime): only it is read again. await writeFile(path.join(a, "video.manifest.json"), JSON.stringify(manifest([clip("a1", "vid", 10, 20), clip("a9", "vid", 50, 55)]))); const later = new Date(Date.now() + 5000); await utimes(path.join(a, "video.manifest.json"), later, later); const changed = await loadUsageIndex({ reportsRoot: reports, channelsDir: channels, file }); assert.deepEqual([changed.stats.parsed, changed.stats.reused], [1, 1]); assert.deepEqual(usesOf(changed.index, "chan", "vid").map((r) => r.clipId), ["a1", "a9", "b1"]); // f/b is deleted: its rows go with it. await rm(path.join(reports, "f", "b"), { recursive: true, force: true }); const gone = await loadUsageIndex({ reportsRoot: reports, channelsDir: channels, file }); assert.equal(gone.stats.dropped, 1); assert.deepEqual(usesOf(gone.index, "chan", "vid").map((r) => `${r.project}#${r.clipId}`), ["f/a#a1", "f/a#a9"]); } finally { await rm(root, { recursive: true, force: true }); } }); test("Rumble: a clip citing the embed id and one citing the folder slug are ONE ingredient, keyed by the folder", async () => { const { root, reports, channels } = await fixture(); try { await project(reports, "f/emb", manifest([clip("e1", "v7emb01", 10, 20)], "rumch")); await project(reports, "f/slug", manifest([clip("s1", "v7slug1", 30, 40, { siteVideo: "v7emb01" })], "rumch")); const { index } = await buildUsageIndex({ reportsRoot: reports, channelsDir: channels }); assert.deepEqual(Object.keys(index.byIngredient), ["rumch/v7slug1"]); const rows = usesOf(index, "rumch", "v7slug1"); assert.deepEqual(rows.map((r) => `${r.project}#${r.clipId}`), ["f/emb#e1", "f/slug#s1"]); assert.deepEqual(rows[0].cites, ["v7emb01"]); assert.deepEqual(rows[1].cites, ["v7slug1", "v7emb01"]); } finally { await rm(root, { recursive: true, force: true }); } }); test("write: false never writes the cache", async () => { const { root, reports, channels } = await fixture(); const file = path.join(root, "state", "usage-index.json"); try { await project(reports, "f/a", manifest([clip("a1", "vid", 10, 20)])); const r = await loadUsageIndex({ reportsRoot: reports, channelsDir: channels, file, write: false }); assert.equal(r.written, false); assert.equal(usesOf(r.index, "chan", "vid").length, 1); const { readUsageIndex } = await import("./usedIn.mjs"); assert.equal(await readUsageIndex(file), null); } finally { await rm(root, { recursive: true, force: true }); } });