import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { execa } from "execa"; import type { ChannelConfig } from "../lib/channelConfig"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test controller/liveClip.test.ts // // A share of a stream still live (controller/liveClip.ts): the window cut now // from a recording in progress (a real, tiny MPEG-TS), the cache, the live // edge; and the ops job's conversation with umtool (a stub fetch). const ROOT = await mkdtemp(path.join(tmpdir(), "live-clip-")); const CORPUS = path.join(ROOT, "transcripts"); process.env.TRANSCRIPTS_DIR = CORPUS; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); await mkdir(CORPUS, { recursive: true }); await writeFile(process.env.SETTINGS_FILE, "{}"); const { getPaths } = await import("../lib/paths"); const { liveWindow, parseLiveClipBody, runLiveClip } = await import("./liveClip"); const { liveRecordingSidecar } = await import("../lib/mediaImport-server"); const paths = getPaths(); test.after(() => rm(ROOT, { recursive: true, force: true })); const SLUG = "synth-live"; const CONFIG = { handling: "transcribe", url: "https://www.youtube.com/@synth", platform: "youtube", name: "Synth" } as ChannelConfig; test("parseLiveClipBody: start/end or clip, 2:20, the project's default", () => { assert.deepEqual(parseLiveClipBody({ slug: "s", id: "Ab1", start: 10, end: 70 }), { ok: true, value: { slug: "s", id: "Ab1", project: "ab1-live", start: 10, end: 70 } }); assert.deepEqual(parseLiveClipBody({ slug: "s", id: "Ab1", clip: "r003", project: "mine" }), { ok: true, value: { slug: "s", id: "Ab1", project: "mine", clip: "r003" } }); assert.match((parseLiveClipBody({ slug: "s", id: "a", start: 0, end: 200 }) as { error: string }).error, /2:20/); assert.match((parseLiveClipBody({ slug: "s", id: "a", clip: "r1", start: 1, end: 2 }) as { error: string }).error, /not both/); assert.match((parseLiveClipBody({ slug: "s", id: "a" }) as { error: string }).error, /"start" and "end"/); }); test("liveWindow: cut now from the part being recorded, then the cache; past the edge says how long to wait", async (t) => { const id = "win001"; const videoDir = path.join(CORPUS, "channels", SLUG, "data", id); await mkdir(videoDir, { recursive: true }); const store = path.join(paths.savedVideosDir, SLUG); await mkdir(store, { recursive: true }); const stem = `.${id}.live-2026-10-27T12-00-00-000Z.ytdlp`; try { await execa(paths.ffmpegBin, [ "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=s=160x90:r=10:d=40", "-f", "lavfi", "-i", "sine=f=300:d=40:r=16000", "-c:v", "libx264", "-preset", "ultrafast", "-g", "20", "-c:a", "aac", "-b:a", "48k", "-f", "mpegts", path.join(store, `${stem}.ts`), ]); } catch { return t.skip("no ffmpeg with libx264 here"); } await liveRecordingSidecar.write(videoDir, { status: "recording", url: "u", platform: "youtube", fromStart: true, startedAt: "2026-10-27T12:00:00Z", parts: [{ method: "ytdlp", url: "u", fromStart: true, startedAt: "2026-10-27T12:00:00Z", stem }], }); const a = await liveWindow({ paths, slug: SLUG, id, channelConfig: CONFIG, from: 10, to: 20 }); assert.ok(a.ok, JSON.stringify(a)); if (!a.ok) return; assert.equal(a.source, "live-recording"); assert.equal(a.window.file, "10.00-20.00.mp4"); const again = await liveWindow({ paths, slug: SLUG, id, channelConfig: CONFIG, from: 12, to: 18 }); assert.ok(again.ok && again.source === "cache", "the tightest window already cut serves it"); const late = await liveWindow({ paths, slug: SLUG, id, channelConfig: CONFIG, from: 30, to: 45 }); assert.ok(!late.ok && late.status === 409 && /ask again in \d+ s/.test(late.error), JSON.stringify(late)); const none = await liveWindow({ paths, slug: SLUG, id: "nothing", channelConfig: CONFIG, from: 1, to: 5 }); assert.ok(!none.ok && none.status === 404); }); function umtoolStub(o: { states: string[]; size: number }) { const seen: { method: string; url: string; body?: unknown }[] = []; const doFetch = (async (url: string, init?: RequestInit) => { const u = String(url); seen.push({ method: init?.method ?? "GET", url: u, ...(init?.body ? { body: JSON.parse(String(init.body)) } : {}) }); const json = (b: unknown, status = 200) => new Response(JSON.stringify(b), { status, headers: { "content-type": "application/json" } }); if (u.endsWith("/api/report/live-share")) return json({ ok: true, clip: "stream-3605", added: true, window: { from: 3600.5, to: 3666.5 }, windowSource: "live-recording", job: { id: "j1" } }); if (u.includes("?job=j1")) { const state = o.states.shift() ?? "done"; return json({ state, log: [`state ${state}`], next: 1 }); } if (u.includes("/api/report/shorts?project=")) return json({ shorts: [{ clip: "stream-3605", size: o.size }] }); return json({ error: "?" }, 404); }) as typeof fetch; return { seen, doFetch }; } test("runLiveClip: asks umtool's live share, follows the short, gives its URL; over 512 MiB is refused", async () => { const s = umtoolStub({ states: ["running", "done"], size: 40 * 1024 * 1024 }); const lines: string[] = []; const r = await runLiveClip( { slug: SLUG, id: "stream", project: "stream-live", start: 3605, end: 3660 }, { onLog: (l) => lines.push(l), signal: new AbortController().signal, deps: { fetch: s.doFetch, umtoolUrl: "http://umtool", workerToken: "tok", pollMs: 0, sleep: async () => {} } }, ); assert.equal(r.clip, "stream-3605"); assert.equal(r.bytes, 40 * 1024 * 1024); assert.match(r.url, /shorts\?project=stream-live&clip=stream-3605&file=1/); assert.deepEqual(s.seen[0]!.body, { project: "stream-live", from: `${SLUG}/stream`, start: 3605, end: 3660 }); assert.ok(lines.some((l) => /window 3600.5–3666.5 s cut from the live-recording/.test(l))); const big = umtoolStub({ states: ["done"], size: 600 * 1024 * 1024 }); await assert.rejects( runLiveClip({ slug: SLUG, id: "stream", project: "stream-live", clip: "stream-3605" }, { onLog: () => {}, signal: new AbortController().signal, deps: { fetch: big.doFetch, umtoolUrl: "http://umtool", workerToken: "tok", pollMs: 0, sleep: async () => {} } }), /over X's 512 MiB/, ); const failed = umtoolStub({ states: ["failed"], size: 1 }); await assert.rejects( runLiveClip({ slug: SLUG, id: "stream", project: "stream-live", clip: "c" }, { onLog: () => {}, signal: new AbortController().signal, deps: { fetch: failed.doFetch, umtoolUrl: "http://umtool", workerToken: "tok", pollMs: 0, sleep: async () => {} } }), /job j1 failed/, ); });