// Stand-ins (release 35 O1): a montage clip whose window the editor refused is // switched to its instance's first public alternate, on the clip, reversibly; // its ruling and its voice-match row move with it, and back. import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { alternateRank, applyStandIns, eligibleAlternates, refusedWindows, restoreStandIn, restoredClip, standInClip, standInPlan } from "./standIns.mjs"; import { fallbackCommand } from "./standInsCli.mjs"; const write = async (file, doc) => { await mkdir(path.dirname(file), { recursive: true }); await writeFile(file, typeof doc === "string" ? doc : JSON.stringify(doc, null, 2) + "\n"); }; const clip = (id, channel, video, cutStart, extra = {}) => ({ type: "clip", id, channel, video, start: cutStart - 2.5, end: cutStart + 5.5, cutStart, cutEnd: cutStart + 3, cite: cutStart, quote: "kind of a slow news day today", onscreen: { title: "slow news day", subtitle: "2 Jan 2023" }, montage: { n: 1, of: 3, date: "20230102", platform: channel.includes("rumble") ? "rumble" : "youtube", match: "slow news day", speakerCheck: false }, ...extra, }); const ALT_YT = { slug: "syt", videoId: "yalt", start: 210, end: 213, platform: "youtube", availability: null }; const ALT_ODY = { slug: "cornbreadman", videoId: "c1", start: 50, end: 53, platform: "odysee", availability: "public" }; test("the ranking: YouTube, then Odysee / cornbreadman, then the rest; public or unknown only; never one refused", () => { const alts = [ { slug: "x", videoId: "k1", start: 1, end: 2, platform: "kick", availability: null }, ALT_ODY, { slug: "syt", videoId: "gone", start: 1, end: 2, platform: "youtube", availability: "deleted" }, ALT_YT, { slug: "syt", videoId: "y403", start: 1, end: 2, platform: "youtube", availability: "unknown" }, ]; assert.deepEqual(eligibleAlternates(alts, { refused: new Set(["syt/y403"]) }).map((a) => a.videoId), ["yalt", "c1", "k1"]); assert.equal(alternateRank({ slug: "cornbreadman", platform: "rumble" }), 1); }); test("standInClip / restoredClip: the alternate's moment with the clip's own margins; everything put back exactly", () => { const before = clip("s02", "srumble", "vrefuse", 200, { note: "also in syt/yalt" }); const next = standInClip(before, ALT_YT, { refusal: { class: "network", message: "HTTP Error 403: Forbidden" } }); assert.equal(next.channel, "syt"); assert.equal(next.video, "yalt"); assert.deepEqual([next.start, next.end, next.cutStart, next.cutEnd, next.cite], [207.5, 215.5, 210, 213, 210]); assert.equal(next.note, "from the YouTube upload: the Rumble copy refused (403)"); assert.equal(next.montage.platform, "youtube"); assert.deepEqual(next.fallbackFrom, { channel: "srumble", video: "vrefuse", start: 197.5, end: 205.5, cutStart: 200, cutEnd: 203, cite: 200, note: "also in syt/yalt", platform: "rumble", reason: "403" }); assert.equal(JSON.stringify(restoredClip(next)), JSON.stringify(before), "key order and all"); // A clip with no note: the note goes again on Restore. const bare = clip("s03", "srumble", "v3", 10); assert.equal(JSON.stringify(restoredClip(standInClip(bare, ALT_ODY, { refusal: { class: "unavailable", message: "gone" } }))), JSON.stringify(bare)); assert.match(standInClip(bare, ALT_ODY, { refusal: { class: "unavailable", message: "gone" } }).note, /the Odysee upload: the Rumble copy refused \(unavailable\)/); }); test("refusedWindows: the newest word per clip; a rate limit or our own drive is not a refusal", () => { const doc = { runs: [ { at: "1", summary: { unresolved: [{ clipId: "s05", slug: "a", id: "b", error: "the recording is deleted" }] }, jobs: {} }, { at: "2", jobs: { J1: { status: "failed", failed: [ { clipId: "s02", slug: "srumble", id: "vrefuse", class: "network", message: "HTTP Error 403" }, { clipId: "s01", slug: "srumble", id: "v1", class: "rate_limit", message: "429" }, { clipId: "s04", slug: "srumble", id: "v4", class: "unreachable", message: "drive" }, ] } } }, ], }; const r = refusedWindows(doc); assert.deepEqual([...r.keys()].sort(), ["s02", "s05"]); assert.equal(r.get("s02").jobId, "J1"); assert.equal(r.get("s05").class, "unresolved"); }); async function project(root, { scanAlternates = true } = {}) { const dir = path.join(root, "reports", "synth-reel"); const channelsDir = path.join(root, "channels"); // The corpus: a Rumble stream refused, its YouTube twin with the same words. const cues = (start, text) => ({ uploadDate: "20230102", duration: 1000, cues: [{ start, end: start + 3, text }] }); await write(path.join(channelsDir, "srumble", "data", "v1", "transcript.cues.json"), { ...cues(100, "kind of a slow news day today folks"), platform: "rumble", title: "monday show" }); await write(path.join(channelsDir, "srumble", "data", "vrefuse", "transcript.cues.json"), { ...cues(200, "honestly a slow news day out there people"), platform: "rumble", title: "tuesday show", uploadDate: "20230103" }); await write(path.join(channelsDir, "syt", "data", "yalt", "transcript.cues.json"), { ...cues(210, "honestly a slow news day out there people"), platform: "youtube", title: "tuesday show", uploadDate: "20230103", duration: 1005 }); await write(path.join(channelsDir, "srumble", "data", "v3", "transcript.cues.json"), { ...cues(10, "what a slow news week"), platform: "rumble", title: "wed", uploadDate: "20230104" }); // s01's window is on disk. await write(path.join(channelsDir, "srumble", "data", "v1", "clips", "97.50-105.50.mp4"), ""); const manifest = { schemaVersion: 1, slug: "synth-reel", title: "Synth reel", provenance: { siteOrigin: "https://archive.example", channelSlug: "srumble", channelsDir }, render: { fetchPad: 0, transition: 0 }, timeline: [clip("s01", "srumble", "v1", 100), clip("s02", "srumble", "vrefuse", 200), clip("s03", "srumble", "v3", 10)], }; await write(path.join(dir, "video.manifest.json"), manifest); const inst = (slug, videoId, start, alternates) => ({ slug, videoId, start, end: start + 3, date: "20230103", platform: "rumble", ...(scanAlternates ? { alternates } : {}) }); await write(path.join(dir, "phrase-scan.json"), { phrase: String.raw`slow[\s,]+news`, channels: ["syt"], prefer: "srumble", exclude: null, scanned: 4, hits: 4, instances: [inst("srumble", "v1", 100, []), inst("srumble", "vrefuse", 200, [ALT_YT]), inst("srumble", "v3", 10, [])], dropped: [], }); await write(path.join(dir, "fetch-runs.json"), { runs: [{ at: "2026-10-18T22:00:00.000Z", bodyHash: "h", dryRun: false, items: 3, jobIds: ["J1"], summary: { jobs: [{ jobId: "J1", platform: "rumble", items: 3 }], unresolved: [] }, jobs: { J1: { status: "failed", platform: "rumble", at: "x", failed: [ { clipId: "s02", slug: "srumble", id: "vrefuse", from: 197.5, to: 205.5, class: "network", message: "ERROR: [Rumble] vrefuse: HTTP Error 403: Forbidden" }, { clipId: "s03", slug: "srumble", id: "v3", from: 7.5, to: 15.5, class: "network", message: "HTTP Error 403: Forbidden" }, ] } } }], }); await write(path.join(dir, "montage-rulings.json"), { version: 1, rulings: { "srumble/vrefuse@200.00": { verdict: "keep", reason: "", at: "2026-10-18T21:00:00.000Z" } } }); await write(path.join(dir, "voice-match.json"), { version: 1, clips: { "srumble/vrefuse@200.00": { score: 0.7, label: "likely", speechSeconds: 2 } } }); return { id: "synth-reel", dir, channelsDir, manifest }; } test("the plan, the switch and Restore: on the clip, the ruling and the voice row moved and back, Undo-able", async () => { const root = await mkdtemp(path.join(os.tmpdir(), "umtool-standins-")); try { const p = await project(root); const plan = await standInPlan(p, { cacheDir: path.join(root, "cache") }); assert.deepEqual(plan.candidates.map((c) => [c.clipId, c.alternate.videoId]), [["s02", "yalt"]]); assert.deepEqual(plan.none.map((n) => n.clipId), ["s03"], "refused, no alternate"); assert.equal(plan.source, "scan"); const res = await applyStandIns(p, { cacheDir: path.join(root, "cache") }); assert.deepEqual(res.switched.map((s) => [s.clipId, s.from, s.to, s.ruling, s.voice]), [["s02", "srumble/vrefuse@200.00", "syt/yalt@210.00", true, true]]); const m = JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); assert.equal(m.timeline[1].video, "yalt"); assert.equal(m.timeline[1].fallbackFrom.video, "vrefuse"); assert.equal(m.timeline[1].note, "from the YouTube upload: the Rumble copy refused (403)"); assert.deepEqual(m.timeline[0], p.manifest.timeline[0], "the others untouched"); const rulings = JSON.parse(await readFile(path.join(p.dir, "montage-rulings.json"), "utf8")).rulings; assert.deepEqual(Object.keys(rulings), ["syt/yalt@210.00"]); assert.deepEqual(Object.keys(JSON.parse(await readFile(path.join(p.dir, "voice-match.json"), "utf8")).clips), ["syt/yalt@210.00"]); assert.ok((await readdir(path.join(p.dir, "revisions"))).some((f) => f.includes("auto-before-stand-in")), "the Cut tab's Undo can take it back"); const after = await standInPlan(p, { cacheDir: path.join(root, "cache") }); assert.deepEqual(after.switched.map((s) => s.clipId), ["s02"]); assert.equal(after.candidates.length, 0); const back = await restoreStandIn(p, "s02"); assert.deepEqual([back.from, back.to, back.ruling, back.voice], ["syt/yalt@210.00", "srumble/vrefuse@200.00", true, true]); const m2 = JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); assert.equal(JSON.stringify(m2), JSON.stringify(p.manifest), "the manifest exactly as it was"); assert.deepEqual(Object.keys(JSON.parse(await readFile(path.join(p.dir, "montage-rulings.json"), "utf8")).rulings), ["srumble/vrefuse@200.00"]); await assert.rejects(restoreStandIn(p, "s01"), /no stand-in to restore/); } finally { await rm(root, { recursive: true, force: true }); } }); test("an older phrase-scan.json with no alternates is scanned again to find them; the CLI's dry run, switch and restore", async () => { const root = await mkdtemp(path.join(os.tmpdir(), "umtool-standins-")); try { const p = await project(root, { scanAlternates: false }); const lines = []; const run = (args) => fallbackCommand(args, { root: path.join(root, "reports"), out: (s) => lines.push(s), err: (s) => lines.push(s), cfg: { url: "http://x", token: "" }, cacheDir: path.join(root, "cache") }); assert.equal(await run(["synth-reel", "--dry-run"]), 0); assert.match(lines.join("\n"), /1 stand-in\(s\), 1 refused with none, 0 already switched \(alternates from a fresh scan/); assert.match(lines.join("\n"), /s02 {2}srumble\/vrefuse refused \(network\) -> syt\/yalt @210.00 \(YouTube\)/); lines.length = 0; assert.equal(await run(["synth-reel", "--clip", "s02"]), 0); assert.match(lines.join("\n"), /s02 -> syt\/yalt @210.00 \(YouTube\) · ruling moved · voice score moved/); assert.match(lines.join("\n"), /umtool fetch synth-reel --dry-run/); lines.length = 0; assert.equal(await run(["synth-reel", "--clip", "s03"]), 1, "no stand-in for s03"); lines.length = 0; assert.equal(await run(["synth-reel", "--restore", "s02"]), 0); assert.match(lines.join("\n"), /s02 restored \(srumble\/vrefuse@200.00\), its ruling moved back, its voice score moved back/); } finally { await rm(root, { recursive: true, force: true }); } });