// Live pre-clip's project (lib/report/clipsLive.mjs): a stream's clip-finder // project made from the editor's live transcript, grown as the transcript // grows (ids kept), flagged by a landing check, then handed to the corpus. // Synthetic: no audio, no model, no network. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { lstat, mkdir, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { readReactions } from "yt-dlp-transcript-common/lib/clips/reactions.mjs"; import { scaffoldReportVideo } from "../projects/scaffold.mjs"; import { LIVE_PROJECT_FILE, liveClips, liveScanDoc, liveTurns, parseLiveBody } from "./clipsLive.mjs"; const CH = "synth-live"; const VID = "stream001"; /** A host talking, a line every 4 s, laughing ("hahaha") at the seconds given. */ function liveDoc(transcribedTo, laughsAt, restarts = 0) { const cues = []; const words = []; for (let t = 1; t + 3 < transcribedTo; t += 4) { const laugh = laughsAt.some((l) => l >= t && l < t + 4); const text = laugh ? "that is the funniest thing hahaha" : "and so the thing is we keep talking"; cues.push({ start: t, end: t + 3, text }); const ws = text.split(" "); ws.forEach((w, i) => words.push({ w, start: Math.round((t + (i * 3) / ws.length) * 1000) / 1000, end: Math.round((t + ((i + 0.8) * 3) / ws.length) * 1000) / 1000 })); } const n = Math.floor(transcribedTo / 0.5); return { version: 1, provisional: true, status: "live", basis: { stem: ".stream001.live-x.ytdlp", audioFile: ".stream001.live-x.ytdlp.ts", kind: "muxed" }, restarts, transcribedTo, cues, words, scan: { hop: 0.5, window: 1, energy: new Array(n).fill(-30), laugh: null, model: null }, chunks: [], fingerprint: null, updatedAt: "2026-10-27T12:00:00Z", }; } async function fixture() { const root = await mkdtemp(path.join(tmpdir(), "clips-live-")); const channelsDir = path.join(root, "channels"); const reportsRoot = path.join(root, "reports"); const sitesDir = path.join(root, "sites"); const videoDir = path.join(channelsDir, CH, "data", VID); await mkdir(videoDir, { recursive: true }); await mkdir(reportsRoot, { recursive: true }); await mkdir(sitesDir, { recursive: true }); await writeFile(path.join(channelsDir, CH, "config.json"), JSON.stringify({ name: "Synth", platform: "youtube" })); await writeFile(path.join(videoDir, "metadata.info.json"), JSON.stringify({ id: VID, title: "A live show", webpage_url: `https://www.youtube.com/watch?v=${VID}`, extractor_key: "Youtube", channel: "Synth", live_status: "is_live" })); const put = (doc) => writeFile(path.join(videoDir, "live-transcript.json"), JSON.stringify(doc)); return { root, channelsDir, reportsRoot, sitesDir, videoDir, put }; } test("liveTurns: cues joined across short gaps, one speaker", () => { assert.deepEqual( liveTurns([{ start: 0, end: 2 }, { start: 2.5, end: 4 }, { start: 9, end: 10 }]), [{ start: 0, end: 4, speaker: 0 }, { start: 9, end: 10, speaker: 0 }], ); assert.deepEqual(liveTurns([]), []); }); test("parseLiveBody: slug, from, final, check", () => { assert.deepEqual(parseLiveBody({ slug: "a-live", from: "ch/vid", final: true, check: { verdict: "shifted", why: "w", lagSeconds: 1.5 } }), { slug: "a-live", channel: "ch", video: "vid", final: true, check: { verdict: "shifted", why: "w", lagSeconds: 1.5 }, }); assert.throws(() => parseLiveBody({ slug: "A B", from: "ch/vid" }), /slug/); assert.throws(() => parseLiveBody({ slug: "ok", from: "nope" }), /from/); }); test("liveScanDoc: the scan cache reactions.mjs reads", () => { const s = liveScanDoc(liveDoc(10, [])); assert.equal(s.version, 1); assert.equal(s.t0, 0); assert.equal(s.windows, 20); assert.equal(s.laugh, null); assert.equal(liveScanDoc({ scan: null }), null); }); test("a live project: made, grown with its ids kept, flagged, then handed to the corpus", async () => { const f = await fixture(); const opts = { reportsRoot: f.reportsRoot, channelsDir: f.channelsDir, sitesDir: f.sitesDir, now: new Date("2026-10-27T12:10:00Z") }; try { await assert.rejects(liveClips({ slug: "stream001-live", from: `${CH}/${VID}` }, opts), (e) => e.status === 404); await f.put(liveDoc(600, [200, 400])); const a = await liveClips({ slug: "stream001-live", from: `${CH}/${VID}` }, opts); assert.equal(a.created, true); assert.equal(a.minedTo, 580); assert.ok(a.proposals > 0, "the laughs and the comments make proposals"); const dir = path.join(f.reportsRoot, "stream001-live"); const shadow = path.join(dir, ".shadow-channels", CH, "data", VID); const diar = JSON.parse(await readFile(path.join(shadow, "diarization.json"), "utf8")); assert.equal(diar.audioSeconds, 600); assert.ok(diar.turns.every((t) => t.speaker === 0)); assert.ok((await lstat(path.join(shadow, "clips"))).isSymbolicLink(), "the editor's clip cache, linked"); assert.equal(JSON.parse(await readFile(path.join(dir, "words.json"), "utf8")).provisional, true); let doc = await readReactions(dir); assert.deepEqual(doc.params.scope, [0, 580]); assert.ok(doc.proposals.every((p) => p.end <= 580)); const first = doc.proposals.map((p) => [p.id, p.start, p.end]); assert.ok(doc.laughs.length >= 2, "laughs from the transcript's markers (no model)"); // The transcript grows: a laugh at 900 s. The earlier proposals keep their ids. await f.put(liveDoc(1200, [200, 400, 900])); const b = await liveClips({ slug: "stream001-live", from: `${CH}/${VID}` }, { ...opts, now: new Date("2026-10-27T12:20:00Z") }); assert.equal(b.created, false); assert.ok(b.proposals >= a.proposals); doc = await readReactions(dir); assert.deepEqual(doc.params.scope, [0, 1180]); for (const [id, start, end] of first) { const same = doc.proposals.find((p) => p.id === id); assert.ok(same && Math.abs(same.start - start) < 0.01 && Math.abs(same.end - end) < 0.01, `${id} kept`); } assert.ok(doc.proposals.some((p) => p.start > 800 && p.start < 960), "a window near the new span"); const live = JSON.parse(await readFile(path.join(dir, LIVE_PROJECT_FILE), "utf8")); assert.equal(live.passes, 2); assert.deepEqual(live.flags, []); // Another project's slug is not taken over. await scaffoldReportVideo({ root: f.reportsRoot, slug: "someone-else", title: "x" }); await assert.rejects(liveClips({ slug: "someone-else", from: `${CH}/${VID}` }, opts), (e) => e.status === 409); // A restart on another clock, then a landing check that did not match: flagged. await f.put(liveDoc(1300, [200, 400, 900], 1)); const c = await liveClips({ slug: "stream001-live", from: `${CH}/${VID}`, check: { verdict: "shifted", why: "1.5 s later", lagSeconds: 1.5 } }, opts); assert.equal(c.flags.length, 2, JSON.stringify(c.flags)); assert.ok(c.flags.some((x) => /restarted on another clock/.test(x.why))); assert.ok(c.flags.some((x) => /landing check: shifted/.test(x.why))); // Final: the shadow goes, words.json becomes the corpus's, the windows stay. const clipsBefore = c.clips; await writeFile(path.join(f.videoDir, "words.json"), JSON.stringify({ words: [{ w: "final", start: 1, end: 1.3 }] })); const d = await liveClips({ slug: "stream001-live", from: `${CH}/${VID}`, final: true }, opts); assert.equal(d.final, true); assert.equal(d.clips, clipsBefore, "every window kept, by source second"); assert.equal(await stat(path.join(dir, ".shadow-channels")).then(() => true, () => false), false); assert.equal(JSON.parse(await readFile(path.join(dir, "words.json"), "utf8")).words[0].w, "final"); assert.equal(await stat(path.join(f.videoDir, "words.json")).then(() => true, () => false), true, "the corpus's file is untouched"); const fin = JSON.parse(await readFile(path.join(dir, LIVE_PROJECT_FILE), "utf8")); assert.equal(fin.status, "final"); assert.equal(fin.flags.length, 2); } finally { await rm(f.root, { recursive: true, force: true }); } }); test("a live share: the clip added at its seconds, the build's window asked of the editor, 2:20 at most", async () => { const { prepareLiveShare, shareSpan } = await import("./liveShare.mjs"); const f = await fixture(); const opts = { reportsRoot: f.reportsRoot, channelsDir: f.channelsDir, sitesDir: f.sitesDir }; try { await f.put(liveDoc(600, [200])); await liveClips({ slug: "stream001-live", from: `${CH}/${VID}` }, opts); const project = { id: "stream001-live", dir: path.join(f.reportsRoot, "stream001-live") }; const asked = []; const askWindow = async (req) => (asked.push(req), { ok: true, status: 200, body: { from: req.from, to: req.to, source: "live-recording" } }); const r = await prepareLiveShare(project, { start: 300, end: 345 }, { askWindow }); assert.equal(r.added, true); assert.equal(r.windowSource, "live-recording"); assert.equal(asked[0].slug, CH); assert.equal(asked[0].id, VID); // The clip's extent (the cut ± 1.5 s) plus the build's fetch pad. const m = JSON.parse(await readFile(path.join(project.dir, "video.manifest.json"), "utf8")); const clip = m.timeline.find((e) => e.id === r.clip); assert.deepEqual([clip.cutStart, clip.cutEnd, clip.start, clip.end], [300, 345, 298.5, 346.5]); assert.deepEqual({ from: asked[0].from, to: asked[0].to }, shareSpan(clip, m.render)); assert.ok(asked[0].from <= 298.5 && asked[0].to >= 346.5); // By id; the editor's "not yet recorded" passes through as a 409. const late = async () => ({ ok: false, status: 409, body: { error: "the recording holds 10:00 so far; the window ends at 10:03 — ask again in 5 s" } }); await assert.rejects(prepareLiveShare(project, { clip: r.clip }, { askWindow: late }), (e) => e.status === 409 && /ask again in 5 s/.test(e.message)); await assert.rejects(prepareLiveShare(project, { start: 0, end: 200 }, { askWindow }), /2:20/); await assert.rejects(prepareLiveShare(project, { clip: "nope" }, { askWindow }), (e) => e.status === 404); } finally { await rm(f.root, { recursive: true, force: true }); } });