// The audio check on a clip window (release 31): a window from Odysee (or a channel // that opts in) runs the audio-checked pipeline's final probe; a malformed window is // fetched once more, and twice malformed it fails and nothing is kept. Fakes for // yt-dlp and ffmpeg (the probe's verdict is ffmpeg's exit code). // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test ytdlp/fetchWindowAudioCheck.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { chmod, mkdir, mkdtemp, readdir, readFile, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { FetchWindowError, fetchWindowManaged } from "./fetchWindowManaged"; import { audioCheckApplies } from "../lib/audioCheckPolicy"; import type { Paths } from "../lib/paths"; import type { ChannelConfig } from "../lib/channelConfig"; const ROOT = await mkdtemp(path.join(os.tmpdir(), "fetchwindow-ac-")); const YTDLP = path.join(ROOT, "fake-ytdlp.mjs"); const FFMPEG = path.join(ROOT, "fake-ffmpeg.mjs"); const COUNT = path.join(ROOT, "ffmpeg-count"); process.env.FAKE_FFMPEG_COUNT = COUNT; await writeFile( YTDLP, `#!/usr/bin/env node import { writeFileSync } from "node:fs"; const args = process.argv.slice(2); writeFileSync(args[args.indexOf("-o") + 1], "mp4"); `, ); // FAKE_FFMPEG: "clean" exits 0; "malformed" exits 1 every time; "malformed-once" // exits 1 on its first run, then 0. await writeFile( FFMPEG, `#!/usr/bin/env node import { readFileSync, writeFileSync } from "node:fs"; const f = process.env.FAKE_FFMPEG_COUNT; let n = 0; try { n = Number(readFileSync(f, "utf8")) || 0; } catch {} writeFileSync(f, String(n + 1)); const mode = process.env.FAKE_FFMPEG; if (mode === "malformed" || (mode === "malformed-once" && n === 0)) { process.stderr.write("[aac @ 0x1] Sample rate index in program config element does not match\\n"); process.exit(1); } process.exit(0); `, ); await chmod(YTDLP, 0o755); await chmod(FFMPEG, 0o755); let seq = 0; async function setup() { const videoDir = path.join(ROOT, `c${++seq}`, "data", "v1"); await mkdir(videoDir, { recursive: true }); await writeFile(COUNT, "0"); return videoDir; } const probes = async () => Number(await readFile(COUNT, "utf8")); function opts(videoDir: string, videoUrl: string, channelConfig: ChannelConfig = {} as ChannelConfig) { return { channelSlug: "c", channelConfig, paths: { ytdlpBin: YTDLP, ffmpegBin: FFMPEG } as unknown as Paths, videoDir, videoId: "v1", videoUrl, from: 10, to: 20, provenance: { requestedBy: "test" }, onLog: () => {}, signal: new AbortController().signal, corruptRetryGapMs: 0, }; } const ODYSEE = "https://odysee.com/@x:1/stream:abc"; test("policy: on for Odysee unless the channel turns it off; elsewhere the channel's opt-in", () => { assert.equal(audioCheckApplies({}, ODYSEE), true); assert.equal(audioCheckApplies({ audioCheck: { enabled: false } }, ODYSEE), false); assert.equal(audioCheckApplies({}, "https://www.youtube.com/watch?v=x"), false); assert.equal(audioCheckApplies({ audioCheck: { enabled: true } }, "https://www.youtube.com/watch?v=x"), true); assert.equal(audioCheckApplies({}, undefined), false); }); test("an Odysee window is probed; clean, it is kept with the verdict in its provenance", async () => { process.env.FAKE_FFMPEG = "clean"; const dir = await setup(); const res = await fetchWindowManaged(opts(dir, ODYSEE)); assert.equal(await probes(), 1); assert.deepEqual(res.provenance?.audioCheck, { verdict: "clean", attempts: 1 }); }); test("malformed once: deleted, fetched again, kept on the second attempt", async () => { process.env.FAKE_FFMPEG = "malformed-once"; const dir = await setup(); const logs: string[] = []; const res = await fetchWindowManaged({ ...opts(dir, ODYSEE), onLog: (s: string) => logs.push(s) }); assert.equal(await probes(), 2); assert.deepEqual(res.provenance?.audioCheck, { verdict: "clean", attempts: 2 }); assert.match(logs.join(""), /malformed; deleted, fetching it again/); const clips = await readdir(path.join(dir, "clips")); assert.ok(clips.some((f) => f === "10.00-20.00.mp4"), clips.join(",")); assert.ok(!clips.some((f) => f.includes(".part")), clips.join(",")); }); test("malformed twice: the fetch fails per_video and no window is kept", async () => { process.env.FAKE_FFMPEG = "malformed"; const dir = await setup(); await assert.rejects(fetchWindowManaged(opts(dir, ODYSEE)), (err: unknown) => { assert.ok(err instanceof FetchWindowError); assert.equal(err.failureClass, "per_video"); assert.match(err.message, /corrupt audio/); return true; }); assert.equal(await probes(), 2); const clips = await readdir(path.join(dir, "clips")).catch(() => []); assert.ok(!clips.some((f) => f.endsWith(".mp4")), clips.join(",")); }); test("no probe where the check does not apply: another platform, or Odysee turned off", async () => { process.env.FAKE_FFMPEG = "malformed"; let dir = await setup(); let res = await fetchWindowManaged(opts(dir, "https://www.youtube.com/watch?v=x")); assert.equal(await probes(), 0); assert.equal(res.provenance?.audioCheck, undefined); dir = await setup(); res = await fetchWindowManaged(opts(dir, ODYSEE, { audioCheck: { enabled: false } } as ChannelConfig)); assert.equal(await probes(), 0); assert.equal(res.cached, false); });