// yt-dlp is never started on an instagram.com URL (review 1, S11): the pass // every yt-dlp argv takes before a spawn throws, so no process starts. import { test } from "node:test"; import assert from "node:assert/strict"; import { readFile, readdir } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { jitterYtdlpSleeps } from "../jitter"; import { jitterSleepArgs } from "../jitter.mjs"; import { scanBatchIds } from "../../ytdlp/metadataScan"; import { INSTAGRAM_YTDLP_REFUSAL, instagramUrlInArgs, refuseInstagramYtdlp } from "./ytdlp"; test("an argv naming an instagram.com URL is refused, wherever the URL sits; any other is passed on", () => { const reel = "https://www.instagram.com/reel/CabcDEF1234/"; assert.equal(instagramUrlInArgs(["--skip-download", "--", reel]), reel); assert.equal(instagramUrlInArgs(["-J", "https://instagram.com/example.account/"]), "https://instagram.com/example.account/"); assert.equal(instagramUrlInArgs(["--referer", "x", "https://www.youtube.com/watch?v=aaaaaaaaaaa"]), null); // A word that merely mentions it is not a URL on it. assert.equal(instagramUrlInArgs(["--match-filter", "title~=instagram.com", "https://example.com/instagram.com"]), null); assert.throws(() => refuseInstagramYtdlp(["--", reel]), new Error(INSTAGRAM_YTDLP_REFUSAL)); assert.throws(() => jitterYtdlpSleeps(["--sleep-requests", "2", "--", reel]), new Error(INSTAGRAM_YTDLP_REFUSAL)); assert.deepEqual(jitterYtdlpSleeps(["-J", "--", "https://www.youtube.com/watch?v=aaaaaaaaaaa"]), ["-J", "--", "https://www.youtube.com/watch?v=aaaaaaaaaaa"]); assert.match(INSTAGRAM_YTDLP_REFUSAL, /Instagram is read as a posts channel; a single reel import comes with a later slice/); }); test("every module of common/ that spawns yt-dlp passes its argv through that refusal", async () => { const root = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", ".."); const offenders: string[] = []; for (const dir of ["controller", "ytdlp", "jobs", "lib", "social", "publish", "bin"]) { for (const name of await readdir(path.join(root, dir))) { if (!name.endsWith(".ts") || name.endsWith(".test.ts") || name.endsWith(".fixture.ts")) continue; // The doctor asks yt-dlp for its --version: no URL is ever in that argv. if (dir === "bin" && name === "doctor.ts") continue; const src = await readFile(path.join(root, dir, name), "utf8"); // A module that spawns the yt-dlp binary itself… if (!/(execa|spawn|runRecorder)\(\s*(?:[\w.]*\.)?ytdlpBin\b|bin:\s*paths\.ytdlpBin/.test(src)) continue; // …passes the argv through one of the two. if (!/jitterYtdlpSleeps\(|refuseInstagramYtdlp\(/.test(src)) offenders.push(`${dir}/${name}`); } } assert.deepEqual(offenders, []); }); test("the refusal is in the shared plain-JS pass, so umtool's scripts are held to it too", async () => { const reel = "https://www.instagram.com/reel/CabcDEF1234/"; assert.throws(() => jitterSleepArgs(["--simulate", "--", reel]), new Error(INSTAGRAM_YTDLP_REFUSAL)); assert.throws(() => jitterSleepArgs(["--simulate", "--", reel], 0), new Error(INSTAGRAM_YTDLP_REFUSAL), "with jitter off too"); assert.deepEqual(jitterSleepArgs(["--simulate", "--", "https://www.youtube.com/watch?v=aaaaaaaaaaa"], 0), ["--simulate", "--", "https://www.youtube.com/watch?v=aaaaaaaaaaa"]); // The two umtool scripts that start yt-dlp hand it only an argv that went through that pass. const umtool = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "..", "..", "umtool", "report-to-video"); for (const [file, bin] of [["check-availability.mjs", "ytdlp"], ["build-video.mjs", "YTDLP"]] as const) { const src = await readFile(path.join(umtool, file), "utf8"); const calls = [...src.matchAll(new RegExp(`execFileP\\(${bin},\\s*([^)]{0,40})`, "g"))].map((m) => m[1]!); assert.ok(calls.length > 0, `${file} starts yt-dlp`); for (const c of calls) assert.ok(c.startsWith("jitterSleepArgs("), `${file}: ${c}`); } }); test("a metadata scan's batch file never holds an instagram.com URL: left out, in order, and said", () => { const urls: Record = { a: "https://www.youtube.com/watch?v=aaaaaaaaaaa", b: "https://www.instagram.com/reel/CabcDEF1234/", c: "https://www.youtube.com/watch?v=ccccccccccc", d: "https://instagram.com/p/CabcDEF1235/", }; const r = scanBatchIds(["a", "b", "c", "d"], (id) => urls[id]!); assert.deepEqual(r.ids, ["a", "c"]); assert.equal(r.leftOut, `2 instagram.com URL(s) left out of this scan: ${INSTAGRAM_YTDLP_REFUSAL}\n`); assert.deepEqual(scanBatchIds(["a", "c"], (id) => urls[id]!), { ids: ["a", "c"], leftOut: null }); assert.deepEqual(scanBatchIds(["b"], (id) => urls[id]!).ids, []); });