import { test } from "node:test"; import assert from "node:assert/strict"; import { execFile } from "node:child_process"; import { mkdtemp, readdir, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { promisify } from "node:util"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test controller/liveJoin.test.ts // // The join of live-recording parts (controller/liveJoin.ts) with the REAL // tools — ffmpeg's cut and concat, ffprobe, the decode the recording clock // uses — over two small files made here from one seeded noise "stream": part // A is its seconds 0–90, part B its seconds 60–150 (a 30 s overlap), whose // recorder started 2 s after its sound (the clock says 62). No network. const run = promisify(execFile); const ROOT = await mkdtemp(path.join(tmpdir(), "live-join-")); test.after(() => rm(ROOT, { recursive: true, force: true })); const { joinLiveParts, ffmpegJoinDeps } = await import("./liveJoin"); const have = await run("ffmpeg", ["-version"]).then(() => true, () => false); async function part(name: string, from: number, seconds: number): Promise { const out = path.join(ROOT, name); await run("ffmpeg", [ "-v", "error", "-y", "-f", "lavfi", "-i", `anoisesrc=d=160:c=pink:r=16000:seed=42:a=0.5`, "-f", "lavfi", "-i", "testsrc=s=64x48:r=5:d=160", "-ss", String(from), "-t", String(seconds), "-map", "1:v", "-map", "0:a", "-c:v", "libx264", "-preset", "ultrafast", "-g", "10", "-c:a", "aac", "-b:a", "32k", out, ]); return out; } test("two overlapping parts are joined by ffmpeg at the overlap their sound shares, and the join is proven", { skip: !have && "no ffmpeg" }, async () => { const a = await part(".exmpl.live-s.ytdlp.mp4", 0, 90); const b = await part(".exmpl.live-s.ytdlp-edge.mp4", 60, 90); const start = Date.parse("2026-11-21T00:00:00.000Z"); const said: string[] = []; const r = await joinLiveParts({ parts: [ { stem: "a", file: a, seconds: 90, fromStart: true, startedAt: new Date(start).toISOString() }, { stem: "b", file: b, seconds: 90, fromStart: false, startedAt: new Date(start + 62_000).toISOString() }, ], streamStartedAt: new Date(start).toISOString(), outStem: path.join(ROOT, ".exmpl.live-s.joined"), deps: ffmpegJoinDeps({ ffmpegBin: "ffmpeg", ffprobeBin: "ffprobe" }), say: (s) => void said.push(s), }); assert.ok(r.ok, `joined: ${"why" in r ? r.why : ""} ${said.join(" | ")}`); if (!r.ok) return; assert.equal(r.joins.length, 1); assert.equal(r.joins[0]!.kind, "overlap"); assert.ok(Math.abs(r.joins[0]!.seconds - 30) < 0.1, `overlap ${r.joins[0]!.seconds}`); assert.ok(Math.abs(r.segments[1]!.streamFrom - 60) < 0.1, `B found at ${r.segments[1]!.streamFrom}`); assert.ok(Math.abs(r.seconds - (90 + 90 - r.segments[1]!.fileFrom)) < 1, `joined ${r.seconds} s`); assert.ok(r.seconds >= 149 && r.seconds <= 153, `the union: ${r.seconds} s`); const names = await readdir(ROOT); assert.ok(names.includes(path.basename(r.file)), "the joined file"); assert.equal(names.filter((n) => /\.temp\./.test(n)).length, 0, "no scratch left"); });