// The clip finder in umtool (release 35 C1): `umtool new --seed reactions`, // `umtool clips analyze|propose|show` over a synthetic reaction stream, with // the test kit's fake tagger and fake embedder (no python, no model), the // no-model fallback, and the app's job step. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdtemp, readFile, rm, writeFile, stat } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; import { encodeWav16 } from "yt-dlp-transcript-common/lib/montage/voiceMatch.mjs"; import { reactionAudio, writeReactionCorpus } from "yt-dlp-transcript-common/testing/clips/reactionStream.mjs"; import { readReactions, setSpeakerRole } from "yt-dlp-transcript-common/lib/clips/reactions.mjs"; import { lintManifest } from "umtool-report-to-video/lint"; import { scaffoldReportVideo, seedsFor } from "../projects/scaffold.mjs"; import { clipsCommand, clipsSteps, loadRecording } from "./reactions.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const KIT = path.resolve(HERE, "..", "..", "..", "common", "testing", "audio"); const TAGGER = { python: process.execPath, script: path.join(KIT, "fakeAudioTag.mjs"), model: path.join(KIT, "fakeAudioTag.mjs"), labels: "none.csv", threads: 1, available: true }; const NO_TAGGER = { ...TAGGER, model: "/nonexistent/model.onnx", available: false }; const ENGINE = { python: process.execPath, script: path.join(KIT, "fakeSpeakerEmbed.mjs"), model: "fake.onnx", threads: 1 }; const hasFfmpeg = (() => { try { execFileSync("ffmpeg", ["-version"], { stdio: "ignore" }); return true; } catch { return false; } })(); const quiet = () => { const lines = []; return { lines, log: (s) => lines.push(s), err: (s) => lines.push(s) }; }; /** A corpus with the synthetic stream, and a reports root. */ async function fixture({ audio = false, diarization = true } = {}) { const root = await mkdtemp(path.join(tmpdir(), "clips-")); const channelsDir = path.join(root, "channels"); const reportsRoot = path.join(root, "reports"); writeReactionCorpus(channelsDir, { diarization, audio: audio ? encodeWav16(reactionAudio()) : null }); return { root, channelsDir, reportsRoot }; } /** Point a scaffolded project at the fixture's corpus (as a shadow tree would). */ async function pointAt(dir, channelsDir) { const file = path.join(dir, "video.manifest.json"); const m = JSON.parse(await readFile(file, "utf8")); m.provenance.channelsDir = path.relative(dir, channelsDir); await writeFile(file, JSON.stringify(m, null, 2)); return m; } const manifestOf = async (dir) => JSON.parse(await readFile(path.join(dir, "video.manifest.json"), "utf8")); test("--seed reactions: clips marked by the assistant, reactions.json beside the manifest, hard cuts, the README's checklist", async () => { const f = await fixture(); try { assert.deepEqual(await seedsFor({ channelsDir: f.channelsDir, from: "synth-reacts/react001" }), { video: true, chapters: false, frames: false, reactions: true }); const r = await scaffoldReportVideo({ root: f.reportsRoot, slug: "reacts", from: "synth-reacts/react001", seed: "reactions", channelsDir: f.channelsDir, target: 60 }); assert.ok(r.seeded >= 3, `seeded ${r.seeded}`); const m = await manifestOf(r.dir); assert.equal(m.render.transition, 0); assert.equal(m.provenance.channelSlug, "synth-reacts"); const clips = m.timeline; assert.ok(clips.every((c) => c.type === "clip" && /^r\d{3}$/.test(c.id) && c.channel === "synth-reacts" && c.video === "react001")); assert.ok(clips.every((c) => c.assist.by === "assistant" && /^p\d{3}$/.test(c.assist.proposal) && c.cutStart >= c.start && c.cutEnd <= c.end)); // Chronological. assert.deepEqual(clips.map((c) => c.cutStart), [...clips.map((c) => c.cutStart)].sort((a, b) => a - b)); const total = clips.reduce((n, c) => n + c.cutEnd - c.cutStart, 0); assert.ok(total <= 60 * 1.03, `total ${total}`); const doc = await readReactions(r.dir); assert.ok(doc); assert.equal(doc.source.channel, "synth-reacts"); assert.equal(doc.source.words, null); assert.equal(doc.source.wordsFrom, "cues"); assert.equal(doc.laughSource, "text"); assert.deepEqual(doc.speakers.map((s) => [s.index, s.role]), [[0, "host"], [1, "subject"]]); assert.equal(doc.params.target, 60); for (const c of clips) assert.equal(doc.proposals.find((p) => p.id === c.assist.proposal)?.clipId, c.id); assert.ok(doc.proposals.some((p) => !p.used && p.clipId === null), "the rest stay listed"); const readme = await readFile(path.join(r.dir, "README.md"), "utf8"); assert.match(readme, /Clips proposed from the recording/); assert.ok(r.notes.some((n) => /umtool clips analyze reacts/.test(n))); // The build's validators and lint accept it. const lint = lintManifest(m, { validators: true }); assert.deepEqual(lint.errors.filter((e) => /assist/.test(e)), []); m.timeline[0].assist.by = "robot"; assert.ok(lintManifest(m, { validators: true }).errors.some((e) => /assist\.by must be/.test(e))); // A recording with no speakers: refused, naming the command that makes them. await assert.rejects( scaffoldReportVideo({ root: f.reportsRoot, slug: "nope", from: "synth-reacts/missing", seed: "reactions", channelsDir: f.channelsDir }), /no video directory/, ); await assert.rejects(scaffoldReportVideo({ root: f.reportsRoot, slug: "nope2", seed: "reactions", channelsDir: f.channelsDir }), /--seed reactions needs --from/); await assert.rejects(scaffoldReportVideo({ root: f.reportsRoot, slug: "nope3", from: "synth-reacts/react001", seed: "reactions", channelsDir: f.channelsDir, target: 5 }), /from 10 to 7200/); } finally { await rm(f.root, { recursive: true, force: true }); } }); test("no diarization: the seed and the CLI say which archilyzer diarize backfill to run", async () => { const f = await fixture({ diarization: false }); try { await assert.rejects( scaffoldReportVideo({ root: f.reportsRoot, slug: "reacts", from: "synth-reacts/react001", seed: "reactions", channelsDir: f.channelsDir }), /archilyzer diarize backfill --scope video:synth-reacts\/react001/, ); assert.deepEqual(await seedsFor({ channelsDir: f.channelsDir, from: "synth-reacts/react001" }), { video: true, chapters: false, frames: false, reactions: false }); await assert.rejects(loadRecording({ channelsDir: f.channelsDir, channel: "synth-reacts", video: "react001" }), /diarize backfill/); } finally { await rm(f.root, { recursive: true, force: true }); } }); test("clips propose: params kept, a host flip is the operator's, ids stable, an edited clip never moved", async () => { const f = await fixture(); try { const r = await scaffoldReportVideo({ root: f.reportsRoot, slug: "reacts", from: "synth-reacts/react001", seed: "reactions", channelsDir: f.channelsDir, target: 60 }); await pointAt(r.dir, f.channelsDir); const before = await manifestOf(r.dir); // The same inputs again: nothing changes, every id stays. const q = quiet(); assert.equal(await clipsCommand(["propose", "reacts"], { reportsRoot: f.reportsRoot, ...q }), 0, q.lines.join("\n")); const same = await manifestOf(r.dir); assert.deepEqual(same.timeline.map((c) => [c.id, c.cutStart, c.cutEnd]), before.timeline.map((c) => [c.id, c.cutStart, c.cutEnd])); assert.match(q.lines.join("\n"), /clips \+0 ~0 -0/); // The operator trims the first clip: it is theirs now. const first = same.timeline[0]; first.cutStart = Math.round((first.cutStart + 0.5) * 100) / 100; first.assist.by = "operator"; first.assist.edits.push({ at: "2026-10-18T22:00:00.000Z", by: "operator", what: "trim", from: { cutStart: first.cutStart - 0.5 }, to: { cutStart: first.cutStart } }); await writeFile(path.join(r.dir, "video.manifest.json"), JSON.stringify(same, null, 2)); // A bigger target and a dry run first: nothing written. const d = quiet(); assert.equal(await clipsCommand(["propose", "reacts", "--target", "300", "--dry"], { reportsRoot: f.reportsRoot, ...d }), 0); assert.match(d.lines.join("\n"), /dry run, nothing written/); assert.equal((await readReactions(r.dir)).params.target, 60); assert.equal(await clipsCommand(["propose", "reacts", "--target", "300"], { reportsRoot: f.reportsRoot, ...quiet() }), 0); const wider = await manifestOf(r.dir); assert.ok(wider.timeline.length > same.timeline.length); const kept = wider.timeline.find((c) => c.id === first.id); assert.equal(kept.cutStart, first.cutStart); assert.equal(kept.assist.by, "operator"); for (const c of same.timeline.slice(1)) assert.equal(wider.timeline.find((x) => x.id === c.id)?.cutStart, c.cutStart, `${c.id} kept its id and cut`); const doc = await readReactions(r.dir); assert.equal(doc.params.target, 300); // The flip: speaker 1 is the host (the operator's word), kept by the next pass. assert.equal(await clipsCommand(["propose", "reacts", "--host", "1"], { reportsRoot: f.reportsRoot, ...quiet() }), 0); const flipped = await readReactions(r.dir); assert.deepEqual(flipped.speakers.map((s) => [s.index, s.role, s.how]), [[0, "subject", "auto"], [1, "host", "operator"]]); assert.equal(flipped.params.target, 300); assert.equal(await clipsCommand(["propose", "reacts"], { reportsRoot: f.reportsRoot, ...quiet() }), 0); assert.equal((await readReactions(r.dir)).speakers.find((s) => s.index === 1).how, "operator"); // The editor's writer: back to speaker 0, then a proposal pass. await setSpeakerRole(r.dir, 0, "host"); assert.equal(await clipsCommand(["propose", "reacts"], { reportsRoot: f.reportsRoot, ...quiet() }), 0); assert.deepEqual((await readReactions(r.dir)).speakers.map((s) => [s.index, s.role]), [[0, "host"], [1, "subject"]]); // show, and the refusals. const s = quiet(); assert.equal(await clipsCommand(["show", "reacts"], { reportsRoot: f.reportsRoot, ...s }), 0); assert.match(s.lines.join("\n"), /speaker 0 {2}Host/); const bad = quiet(); assert.equal(await clipsCommand(["propose", "reacts", "--host", "9"], { reportsRoot: f.reportsRoot, ...bad }), 2); assert.match(bad.lines.join("\n"), /no such speaker/); assert.equal(await clipsCommand(["propose", "reacts", "--laugh-min", "3"], { reportsRoot: f.reportsRoot, ...quiet() }), 2); assert.equal(await clipsCommand(["frobnicate", "reacts"], { reportsRoot: f.reportsRoot, ...quiet() }), 2); } finally { await rm(f.root, { recursive: true, force: true }); } }); test("the scope: --scope / --from S --to S, kept in reactions.json params, `all` clears it; the seed takes it", async () => { const f = await fixture(); try { const r = await scaffoldReportVideo({ root: f.reportsRoot, slug: "reacts", from: "synth-reacts/react001", seed: "reactions", channelsDir: f.channelsDir, scope: [300, 450], laughMin: 0.15 }); await pointAt(r.dir, f.channelsDir); let doc = await readReactions(r.dir); assert.deepEqual(doc.params.scope, [300, 450]); assert.equal(doc.params.laughMin, 0.15); assert.equal(doc.source.duration, 450); assert.ok(doc.proposals.every((p) => p.end > 300 && p.start < 450)); assert.equal(await clipsCommand(["propose", "reacts", "--from", "0", "--to", "200"], { reportsRoot: f.reportsRoot, ...quiet() }), 0); doc = await readReactions(r.dir); assert.deepEqual(doc.params.scope, [0, 200]); assert.ok(doc.turns.every((t) => t.end <= 200)); assert.equal(await clipsCommand(["propose", "reacts", "--to", "250"], { reportsRoot: f.reportsRoot, ...quiet() }), 0); assert.deepEqual((await readReactions(r.dir)).params.scope, [0, 250]); assert.equal(await clipsCommand(["propose", "reacts", "--scope", "400:"], { reportsRoot: f.reportsRoot, ...quiet() }), 0); assert.deepEqual((await readReactions(r.dir)).params.scope, [400, null]); assert.equal(await clipsCommand(["propose", "reacts", "--scope", "all"], { reportsRoot: f.reportsRoot, ...quiet() }), 0); assert.equal((await readReactions(r.dir)).params.scope, null); const bad = quiet(); assert.equal(await clipsCommand(["propose", "reacts", "--scope", "9-3"], { reportsRoot: f.reportsRoot, ...bad }), 2); assert.equal(await clipsCommand(["propose", "reacts", "--scope", "500:100"], { reportsRoot: f.reportsRoot, ...quiet() }), 2); await assert.rejects(scaffoldReportVideo({ root: f.reportsRoot, slug: "x1", from: "synth-reacts/react001", seed: "chapters", channelsDir: f.channelsDir, laughMin: 0.2 }), /go with --seed reactions/); } finally { await rm(f.root, { recursive: true, force: true }); } }); test("clips propose reads /words.json when there is one", async () => { const f = await fixture(); try { const r = await scaffoldReportVideo({ root: f.reportsRoot, slug: "reacts", from: "synth-reacts/react001", seed: "reactions", channelsDir: f.channelsDir }); await pointAt(r.dir, f.channelsDir); const { reactionStream } = await import("yt-dlp-transcript-common/testing/clips/reactionStream.mjs"); await writeFile(path.join(r.dir, "words.json"), JSON.stringify({ path: "/x", words: reactionStream().words })); assert.equal(await clipsCommand(["propose", "reacts"], { reportsRoot: f.reportsRoot, ...quiet() }), 0); const doc = await readReactions(r.dir); assert.equal(doc.source.words, "words.json"); assert.equal(doc.source.wordsFrom, "words.json"); } finally { await rm(f.root, { recursive: true, force: true }); } }); test("clips analyze (fake tagger + fake embedder): the scans, cached in the project; the model's laughs", { skip: !hasFfmpeg && "no ffmpeg" }, async () => { const f = await fixture({ audio: true }); try { const r = await scaffoldReportVideo({ root: f.reportsRoot, slug: "reacts", from: "synth-reacts/react001", seed: "reactions", channelsDir: f.channelsDir, target: 60 }); await pointAt(r.dir, f.channelsDir); const q = quiet(); assert.equal(await clipsCommand(["analyze", "reacts", "--progress", "ndjson"], { reportsRoot: f.reportsRoot, ...q, tagger: TAGGER, engine: ENGINE }), 0, q.lines.join("\n")); const events = q.lines.filter((l) => l.startsWith("{")).map((l) => JSON.parse(l)); assert.ok(events.some((e) => e.phase === "laughs" && e.event === "decode")); assert.ok(events.some((e) => e.phase === "voices")); assert.equal(events.at(-1).phase, "done"); const doc = await readReactions(r.dir); assert.equal(doc.laughSource, "model"); assert.equal(doc.laughs.length, 3); assert.ok(doc.laughs.every((l) => l.speaker === 0)); assert.equal(doc.scans.laughs.model, "fakeAudioTag.mjs"); assert.equal(doc.scans.voices.model, "fake-spectrum"); assert.equal(doc.source.audio, "audio.wav"); // Each laugh's voice, checked against the host's and the subject's (the fake embedder's "voices"). assert.ok(doc.laughs.every((l) => l.voice && Number.isFinite(l.voice.host) && Number.isFinite(l.voice.subject)), JSON.stringify(doc.laughs)); assert.equal(Object.keys(JSON.parse(await readFile(path.join(r.dir, "scans", "synth-reacts--react001.voices.json"), "utf8")).laughs).length, 3); await stat(path.join(r.dir, "scans", "synth-reacts--react001.audio.json")); await stat(path.join(r.dir, "scans", "synth-reacts--react001.voices.json")); // Again: both cached. const again = quiet(); assert.equal(await clipsCommand(["analyze", "reacts"], { reportsRoot: f.reportsRoot, ...again, tagger: TAGGER, engine: ENGINE }), 0); assert.match(again.lines.join("\n"), /laugh scan: cached/); assert.match(again.lines.join("\n"), /voices: cached/); } finally { await rm(f.root, { recursive: true, force: true }); } }); test("the no-model fallback: no laugh model — the sound's energy and the transcript's markers", { skip: !hasFfmpeg && "no ffmpeg" }, async () => { const f = await fixture({ audio: true }); try { const r = await scaffoldReportVideo({ root: f.reportsRoot, slug: "reacts", from: "synth-reacts/react001", seed: "reactions", channelsDir: f.channelsDir }); await pointAt(r.dir, f.channelsDir); const q = quiet(); assert.equal(await clipsCommand(["analyze", "reacts", "--no-voices"], { reportsRoot: f.reportsRoot, ...q, tagger: NO_TAGGER, engine: ENGINE }), 0, q.lines.join("\n")); assert.match(q.lines.join("\n"), /no laugh model at \/nonexistent\/model\.onnx — energy only/); const doc = await readReactions(r.dir); assert.equal(doc.laughSource, "text+energy"); assert.equal(doc.scans.laughs.model, null); assert.ok(doc.laughs.length >= 3); assert.ok(doc.laughs.some((l) => l.src === "text")); assert.ok(doc.proposals.some((p) => p.used)); } finally { await rm(f.root, { recursive: true, force: true }); } }); test("clipsSteps: one nice'd step running the CLI, its flags checked", () => { const plan = clipsSteps({ id: "reacts" }, { mode: "analyze", from: "synth-reacts/react001", target: 300, host: 1 }); assert.equal(plan.kind, "clips-analyze reacts"); assert.equal(plan.steps.length, 1); const argv = plan.steps[0].argv; assert.deepEqual(argv.slice(0, 3), ["nice", "-n", "10"]); assert.deepEqual(argv.slice(-10), ["clips", "analyze", "reacts", "--from", "synth-reacts/react001", "--target", "300", "--host", "1", "--progress", "ndjson"].slice(-10)); const bad = clipsSteps({ id: "reacts" }, { mode: "propose", from: "../etc", target: 2 }); assert.equal(bad.kind, "clips-propose reacts"); assert.ok(!bad.steps[0].argv.includes("--from") && !bad.steps[0].argv.includes("--target")); });