// RECHECK A SERVICE'S COOKIES (cookie refresh alerts, // plans/cookie-refresh-alerts.md): one request through the service's own path, // the alert cleared when it comes back clean. Every test in a temp transcripts // dir; yt-dlp and the forum browser are injected. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/recheckServiceCookies.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; import { noteForumServiceOutcome, noteServiceCookieSignal, readServiceCookieState, } from "../lib/serviceCookies-server"; import { isAlertRaised } from "../lib/serviceCookies"; import { CHALLENGE_PAGE } from "../social/__fixtures__/xenforoPages"; import { recheckServiceCookies } from "./recheckServiceCookies"; const FIX = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "lib", "__fixtures__"); const EMBED_403 = readFileSync(path.join(FIX, "rumble-embed-403.download.log"), "utf8"); const embed403For = (id: string) => EMBED_403.replaceAll("v7ez8hg", id); type RecheckPaths = Parameters[0]["paths"]; async function tmpPaths(): Promise { const transcriptsDir = await mkdtemp(path.join(os.tmpdir(), "service-cookies-recheck-")); return { transcriptsDir, channelsDir: path.join(transcriptsDir, "channels"), ytdlpBin: "yt-dlp" } as RecheckPaths; } test("Recheck (Rumble): one metadata request for the request that failed, with the channel's args and cookies; clean clears and settles the backoff", async () => { const paths = await tmpPaths(); try { await noteServiceCookieSignal(paths, { service: "rumble", kind: "embed-403", line: "a", videoId: "e1" }, { url: "https://rumble.com/v1-a.html" }); await noteServiceCookieSignal(paths, { service: "rumble", kind: "embed-403", line: "b", videoId: "e2" }, { url: "https://rumble.com/v2-b.html", channel: "huck" }); const calls: { cwd: string; args: string[] }[] = []; const settled: string[] = []; let answer = { exitCode: 1, stderrTail: embed403For("e2"), archiveLine: null }; const deps = { runYtdlp: async (cwd: string, args: string[]) => { calls.push({ cwd, args }); return answer; }, readChannelConfig: async () => ({ name: "Huck", url: "https://rumble.com/c/huck", cookiesFromBrowser: "firefox" }) as never, cookieSpec: () => "firefox", recordPlatformClean: async (p: string) => { settled.push(p); return "rumble answered cleanly: its rate-limit backoff is cleared.\n"; }, }; const stale = await recheckServiceCookies({ paths, service: "rumble", deps }); assert.equal(stale.result, "stale"); assert.equal(calls.length, 1); const args = calls[0].args; assert.equal(args.at(-1), "https://rumble.com/v2-b.html"); assert.ok(args.includes("--skip-download")); assert.deepEqual(args.slice(args.indexOf("--print"), args.indexOf("--print") + 2), ["--print", "id"]); assert.deepEqual(args.slice(args.indexOf("--cookies-from-browser"), args.indexOf("--cookies-from-browser") + 2), ["--cookies-from-browser", "firefox"]); assert.ok(args.includes("--impersonate"), "Rumble's own platform args"); assert.ok(args.indexOf("--no-write-info-json") > args.indexOf("--cookies-from-browser"), "refusals after the channel's args"); assert.equal(calls[0].cwd, path.join(paths.channelsDir, "huck")); assert.equal(isAlertRaised((await readServiceCookieState(paths)).services.rumble), true); answer = { exitCode: 0, stderrTail: "", archiveLine: null }; const clean = await recheckServiceCookies({ paths, service: "rumble", deps }); assert.equal(clean.result, "clean"); const state = await readServiceCookieState(paths); assert.equal(state.services.rumble.clearedBy, "recheck"); assert.equal(state.lastGood.rumble.url, "https://rumble.com/v2-b.html"); assert.deepEqual(settled, ["rumble"]); const nothing = await recheckServiceCookies({ paths, service: "odysee", deps }); assert.equal(nothing.result, "failed"); } finally { await rm(paths.transcriptsDir, { recursive: true, force: true }); } }); test("Recheck (forum): one page through the profile, judged by classifyForumPage", async () => { const paths = await tmpPaths(); try { const url = "https://kiwifarms.st/threads/x.1/"; await noteForumServiceOutcome(paths, url, { block: "challenge" }); let html = CHALLENGE_PAGE; const loaded: string[] = []; const deps = { loadForumPage: async (_p: unknown, u: string) => { loaded.push(u); return { html }; }, }; assert.equal((await recheckServiceCookies({ paths, service: "kiwifarms.st", deps })).result, "stale"); html = `
`; assert.equal((await recheckServiceCookies({ paths, service: "kiwifarms.st", deps })).result, "clean"); assert.deepEqual(loaded, [url, url]); } finally { await rm(paths.transcriptsDir, { recursive: true, force: true }); } }); test("Recheck (archive.today): the request that met the check, again; a check still standing keeps it", async () => { const paths = await tmpPaths(); try { const url = "https://archive.ph/timemap/https://example.com/page"; await noteServiceCookieSignal(paths, { service: "archive.today", kind: "check-page", line: "check" }, { url }); const asked: string[] = []; let refuse = true; const deps = { archiveTodayRequest: async (u: string) => { asked.push(u); if (refuse) throw Object.assign(new Error("a CAPTCHA"), { code: "challenge" }); }, }; assert.equal((await recheckServiceCookies({ paths, service: "archive.today", deps })).result, "stale"); refuse = false; assert.equal((await recheckServiceCookies({ paths, service: "archive.today", deps })).result, "clean"); assert.deepEqual(asked, [url, url]); assert.equal((await readServiceCookieState(paths)).services["archive.today"].clearedBy, "recheck"); } finally { await rm(paths.transcriptsDir, { recursive: true, force: true }); } }); test("Recheck (X): no request to X — stale until the login is renewed after the alert, then clean", async () => { const paths = await tmpPaths(); try { const { noteXServiceOutcome } = await import("../lib/serviceCookies-server"); await noteXServiceOutcome(paths, { challengeLine: "[twitter][warning] Cloudflare challenge", url: "https://x.com/a", channel: "a" }); const raisedAt = (await readServiceCookieState(paths)).services.x?.raisedAt; assert.ok(raisedAt, "one challenge raises the X alert"); const asked: string[] = []; let login = { source: "profile" as const, renewedAt: new Date(Date.parse(raisedAt!) - 60_000).toISOString(), loggedIn: true }; const deps = { runYtdlp: async () => { asked.push("yt-dlp"); return { exitCode: 0, stderrTail: "", archiveLine: null }; }, loadForumPage: async () => { asked.push("page"); return { html: "" }; }, xLogin: async () => login, }; const stale = await recheckServiceCookies({ paths, service: "x", deps }); assert.equal(stale.result, "stale"); assert.match(stale.message, /Refresh cookies/); assert.match(stale.message, /Recheck sends X no request/); assert.equal(isAlertRaised((await readServiceCookieState(paths)).services.x), true); login = { source: "profile", renewedAt: new Date(Date.parse(raisedAt!) + 60_000).toISOString(), loggedIn: true }; const clean = await recheckServiceCookies({ paths, service: "x", deps }); assert.equal(clean.result, "clean"); const after = (await readServiceCookieState(paths)).services.x; assert.equal(after?.clearedBy, "recheck"); assert.deepEqual(asked, [], "Recheck asked nothing of anyone"); } finally { await rm(paths.transcriptsDir, { recursive: true, force: true }); } }); test("Recheck (X, jar-age): the jar's age on disk, no request — stale while old, clean once fresh; a challenge is never overwritten by it", async () => { const paths = await tmpPaths(); try { const { noteXJarStale, noteXJarFresh, noteXServiceOutcome } = await import("../lib/serviceCookies-server"); assert.equal(await noteXJarStale(paths, { line: "X cookies were last refreshed 3 h ago (limit 60 min): …", channel: "ember-x", job: { id: "J1", kind: "fetch-posts" } }), true); let x = (await readServiceCookieState(paths)).services.x; assert.equal(x?.kind, "jar-age"); assert.deepEqual(x?.jobs, [{ id: "J1", kind: "fetch-posts" }]); const asked: string[] = []; let fresh = false; const deps = { runYtdlp: async () => { asked.push("yt-dlp"); return { exitCode: 0, stderrTail: "", archiveLine: null }; }, xLogin: async () => { asked.push("login"); return { source: "profile" as const, loggedIn: true }; }, xJar: async () => ({ source: "profile" as const, at: new Date().toISOString(), ageMinutes: fresh ? 1 : 180, maxAgeMinutes: 60, loggedIn: true, fresh, readAt: new Date().toISOString(), summary: fresh ? "The X cookie jar was written 1 min ago (limit 60 min)." : "The X cookie jar was written 3 h ago (limit 60 min): press Refresh cookies (or Connect X account) on /cookies.", }), }; const stale = await recheckServiceCookies({ paths, service: "x", deps }); assert.equal(stale.result, "stale"); assert.match(stale.message, /3 h ago .*Recheck sends X no request\.$/); fresh = true; const clean = await recheckServiceCookies({ paths, service: "x", deps }); assert.equal(clean.result, "clean"); x = (await readServiceCookieState(paths)).services.x; assert.equal(x?.clearedBy, "recheck"); assert.deepEqual(asked, [], "neither yt-dlp nor the challenge path was asked"); // Nothing raised: fresh is a no-op. assert.equal(await noteXJarFresh(paths), null); // A challenge is up: the jar-age path records the job but never replaces it. await noteXServiceOutcome(paths, { challengeLine: "[twitter][warning] Cloudflare challenge", kind: "challenge" }); assert.equal(await noteXJarStale(paths, { line: "old jar", job: { id: "J2" } }), true); x = (await readServiceCookieState(paths)).services.x; assert.equal(x?.kind, "challenge"); assert.equal(x?.evidence.line, "[twitter][warning] Cloudflare challenge"); assert.deepEqual(x?.jobs.map((j) => j.id), ["J2"]); assert.equal(await noteXJarFresh(paths), null, "a fresh jar does not clear a challenge"); assert.equal(isAlertRaised((await readServiceCookieState(paths)).services.x), true); } finally { await rm(paths.transcriptsDir, { recursive: true, force: true }); } }); test("X's login failure is its own kind: an auth-failure alert, worded so", async () => { const paths = await tmpPaths(); try { const { noteXServiceOutcome } = await import("../lib/serviceCookies-server"); await noteXServiceOutcome(paths, { challengeLine: "[twitter][error] AuthRequired: authenticated cookies needed to access this timeline", kind: "auth-failure" }); const x = (await readServiceCookieState(paths)).services.x; assert.equal(x?.kind, "auth-failure"); assert.equal(isAlertRaised(x), true); const { buildServiceCookieAlerts } = await import("../lib/serviceCookies"); const view = buildServiceCookieAlerts(await readServiceCookieState(paths), { now: Date.now() }); assert.match(view.alerts[0]?.detail ?? "", /^1 login failure since /); } finally { await rm(paths.transcriptsDir, { recursive: true, force: true }); } });