// Cookie refresh alerts (plans/cookie-refresh-alerts.md): the classifier on // real stderr (2026-10-21, Rumble's embed 403s that a fresh cf_clearance // cleared), the two-videos-in-a-row rule, clearing, coercion, the view. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/serviceCookies.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { CLEARED_KEEP_MS, OK_AGAIN_SHOW_MS, PENDING_SIGNAL_TTL_MS, applyServiceCookieClean, applyServiceCookieSignal, buildServiceCookieAlerts, coerceServiceCookieState, emptyServiceCookieState, forumServiceKey, isAlertRaised, isServiceKey, noteLastGood, pruneServiceCookieState, rumbleMissingConfigLine, serviceCookieSignalFromStderr, serviceFamily, serviceFixText, serviceStoppedLine, } from "./serviceCookies"; import { classifyDownloadFailure } from "./availability"; const FIX = path.join(path.dirname(fileURLToPath(import.meta.url)), "__fixtures__"); const DOWNLOAD_LOG = readFileSync(path.join(FIX, "rumble-embed-403.download.log"), "utf8"); const WINDOWS_LOG = readFileSync(path.join(FIX, "rumble-fetch-windows-stopped.log"), "utf8"); // The fetch-windows log, one yt-dlp run per `$ yt-dlp` block. function runsOf(log: string): string[] { return log .split(/^\$ yt-dlp /m) .slice(1) .map((b) => b.split("\n").slice(1).join("\n")); } const T0 = Date.parse("2026-10-21T17:02:00.000Z"); test("the real Rumble embed 403 is a rumble embed-403 signal naming the embed id", () => { const sig = serviceCookieSignalFromStderr(DOWNLOAD_LOG, "rumble"); assert.ok(sig); assert.equal(sig.service, "rumble"); assert.equal(sig.kind, "embed-403"); assert.equal(sig.videoId, "v7ez8hg"); assert.match(sig.line, /^ERROR: \[RumbleEmbed\] v7ez8hg: Unable to download JSON metadata: HTTP Error 403/); // What it was before this feature: a bare network failure. assert.equal(classifyDownloadFailure(DOWNLOAD_LOG, "error"), "network"); }); test("the fetch-windows log: two windows fetched, then two DIFFERENT videos' embeds 403 — the second raises the alert", () => { const runs = runsOf(WINDOWS_LOG); assert.equal(runs.length, 3); const sigs = runs.map((r) => serviceCookieSignalFromStderr(r, "rumble")); assert.equal(sigs[0], null, "a fetched window is no signal"); assert.deepEqual( sigs.slice(1).map((s) => [s?.service, s?.kind, s?.videoId]), [ ["rumble", "embed-403", "v7d7myw"], ["rumble", "embed-403", "v7d2lxe"], ], ); const state = emptyServiceCookieState(); const first = applyServiceCookieSignal(state, sigs[1]!, { jobId: "J1", jobKind: "fetch-windows" }, T0); assert.equal(first.raisedNow, false, "one embed 403 is not enough — a removed video answers 403 too"); assert.equal(isAlertRaised(state.services.rumble), false); const second = applyServiceCookieSignal(state, sigs[2]!, { jobId: "J1", jobKind: "fetch-windows" }, T0 + 141_000); assert.equal(second.raisedNow, true); assert.equal(isAlertRaised(state.services.rumble), true); assert.deepEqual(state.services.rumble.videos, ["v7d7myw", "v7d2lxe"]); assert.deepEqual(state.services.rumble.jobs, [{ id: "J1", kind: "fetch-windows" }]); }); test("the same video refused twice (prefetch, then the download) stays one video: no alert", () => { const state = emptyServiceCookieState(); const sig = serviceCookieSignalFromStderr(DOWNLOAD_LOG, "rumble")!; applyServiceCookieSignal(state, sig, {}, T0); const again = applyServiceCookieSignal(state, sig, {}, T0 + 8_000); assert.equal(again.raisedNow, false); assert.equal(state.services.rumble.count, 2); assert.deepEqual(state.services.rumble.videos, ["v7ez8hg"]); }); test("a removed Rumble page's 403 and a media 403 are not cookie signals", () => { assert.equal( serviceCookieSignalFromStderr( "ERROR: [Rumble] v7gone1: Unable to download webpage: HTTP Error 403: Forbidden (caused by )", "rumble", ), null, ); assert.equal(serviceCookieSignalFromStderr("ERROR: [download] Got error: HTTP Error 403: Forbidden", "rumble"), null); assert.equal(serviceCookieSignalFromStderr("", "rumble"), null); }); test("a clean Rumble request between two embed 403s breaks the streak", () => { const state = emptyServiceCookieState(); applyServiceCookieSignal(state, { service: "rumble", kind: "embed-403", line: "a", videoId: "v1" }, {}, T0); assert.equal(applyServiceCookieClean(state, "rumble", T0 + 1000), "pending-dropped"); const r = applyServiceCookieSignal(state, { service: "rumble", kind: "embed-403", line: "b", videoId: "v2" }, {}, T0 + 2000); assert.equal(r.raisedNow, false); }); test("a pending first 403 older than an hour is forgotten", () => { const state = emptyServiceCookieState(); applyServiceCookieSignal(state, { service: "rumble", kind: "embed-403", line: "a", videoId: "v1" }, {}, T0); const r = applyServiceCookieSignal( state, { service: "rumble", kind: "embed-403", line: "b", videoId: "v2" }, {}, T0 + PENDING_SIGNAL_TTL_MS + 1, ); assert.equal(r.raisedNow, false); assert.deepEqual(state.services.rumble.videos, ["v2"]); }); test("YouTube's bot check raises at once; its video id is read", () => { const stderr = "ERROR: [youtube] vid00000001: Sign in to confirm you’re not a bot. Use --cookies-from-browser or --cookies for the authentication."; const sig = serviceCookieSignalFromStderr(stderr, "youtube"); assert.deepEqual([sig?.service, sig?.kind, sig?.videoId], ["youtube", "bot-check", "vid00000001"]); const state = emptyServiceCookieState(); assert.equal(applyServiceCookieSignal(state, sig!, {}, T0).raisedNow, true); }); test("Odysee: a 403 alone is not a signal; a 403 with Cloudflare's challenge words is (no real sample yet)", () => { assert.equal( serviceCookieSignalFromStderr("ERROR: [lbry] abc: Unable to download JSON metadata: HTTP Error 403: Forbidden", "odysee"), null, ); const sig = serviceCookieSignalFromStderr( "ERROR: [lbry] abc: Unable to download webpage: HTTP Error 403: Forbidden\nJust a moment...", "odysee", ); assert.deepEqual([sig?.service, sig?.kind], ["odysee", "challenge"]); }); test("clearing: a raised alert is cleared and kept a day; a second clean changes nothing; prune drops it after", () => { const state = emptyServiceCookieState(); applyServiceCookieSignal(state, { service: "youtube", kind: "bot-check", line: "x" }, {}, T0); assert.equal(applyServiceCookieClean(state, "youtube", T0 + 60_000, "recheck"), "cleared"); assert.equal(state.services.youtube.clearedBy, "recheck"); assert.equal(applyServiceCookieClean(state, "youtube", T0 + 120_000), null); assert.equal(pruneServiceCookieState(state, T0 + 60_000 + CLEARED_KEEP_MS - 1), false); assert.equal(pruneServiceCookieState(state, T0 + 60_000 + CLEARED_KEEP_MS + 1), true); assert.equal(state.services.youtube, undefined); }); test("a new signal after the clearing starts a fresh alert", () => { const state = emptyServiceCookieState(); applyServiceCookieSignal(state, { service: "youtube", kind: "bot-check", line: "x" }, { jobId: "A" }, T0); applyServiceCookieClean(state, "youtube", T0 + 1000); const r = applyServiceCookieSignal(state, { service: "youtube", kind: "bot-check", line: "y" }, { jobId: "B" }, T0 + 2000); assert.equal(r.raisedNow, true); assert.equal(state.services.youtube.clearedAt, undefined); assert.deepEqual(state.services.youtube.jobs.map((j) => j.id), ["B"]); }); test("lastGood is refreshed at most every ten minutes", () => { const state = emptyServiceCookieState(); assert.equal(noteLastGood(state, "rumble", { url: "https://rumble.com/a", channel: "c" }, T0), true); assert.equal(noteLastGood(state, "rumble", { url: "https://rumble.com/b" }, T0 + 60_000), false); assert.equal(noteLastGood(state, "rumble", { url: "https://rumble.com/b" }, T0 + 11 * 60_000), true); assert.equal(state.lastGood.rumble.url, "https://rumble.com/b"); }); test("coerce: garbage is an empty state; bad entries are dropped, good ones kept", () => { assert.deepEqual(coerceServiceCookieState(null), emptyServiceCookieState()); assert.deepEqual(coerceServiceCookieState("nope"), emptyServiceCookieState()); const s = coerceServiceCookieState({ services: { rumble: { kind: "embed-403", since: "2026-10-21T17:02:00.000Z", count: 2, videos: ["a", 3, "b"], evidence: { line: "L", jobId: "J" }, raisedAt: "2026-10-21T17:04:00.000Z", jobs: [{ id: "J", kind: "fetch-windows" }, { nope: 1 }] }, "../../etc": { kind: "embed-403", since: "2026-10-21T17:02:00.000Z" }, youtube: { kind: "made-up", since: "2026-10-21T17:02:00.000Z" }, odysee: { kind: "challenge", since: "not a date" }, }, lastGood: { rumble: { url: "https://rumble.com/x", at: "2026-10-21T16:00:00.000Z" }, youtube: { url: 5 } }, }); assert.deepEqual(Object.keys(s.services), ["rumble"]); assert.deepEqual(s.services.rumble.videos, ["a", "b"]); assert.deepEqual(s.services.rumble.jobs, [{ id: "J", kind: "fetch-windows" }]); assert.equal(s.services.rumble.lastSeen, "2026-10-21T17:02:00.000Z"); assert.deepEqual(Object.keys(s.lastGood), ["rumble"]); }); test("service keys, families and the forum key", () => { assert.equal(isServiceKey("rumble"), true); assert.equal(isServiceKey("archive.today"), true); assert.equal(isServiceKey("kiwifarms.st"), true); assert.equal(isServiceKey("you tube/../x"), false); assert.equal(isServiceKey("localhost"), false); assert.equal(serviceFamily("odysee"), "ytdlp"); assert.equal(serviceFamily("archive.today"), "archive"); assert.equal(serviceFamily("kiwifarms.st"), "forum"); assert.equal(forumServiceKey("WWW.KiwiFarms.st"), "kiwifarms.st"); }); test("the fix names the configured browser; the job's stopped line says what and how", () => { assert.match(serviceFixText("rumble", "firefox"), /^open rumble\.com in firefox/); assert.match(serviceFixText("rumble", "chrome:Profile 1"), /in chrome /); assert.match(serviceFixText("rumble"), /the browser yt-dlp reads cookies from/); assert.equal( serviceStoppedLine("rumble", "firefox"), "stopped: Rumble cookies look stale — open rumble.com in firefox (a fresh Cloudflare clearance cookie), then Recheck on the dashboard.", ); assert.match(serviceFixText("kiwifarms.st"), /Connect forum session/); assert.match(serviceFixText("archive.today"), /Connect archive session/); }); test("the view: a stale banner with its fix; OK again offers the stopped jobs, by status; a re-run hides the offer", () => { const state = emptyServiceCookieState(); applyServiceCookieSignal(state, { service: "rumble", kind: "embed-403", line: "a", videoId: "v1" }, { jobId: "J1", jobKind: "fetch-windows", channel: "huck" }, T0); applyServiceCookieSignal(state, { service: "rumble", kind: "embed-403", line: "b", videoId: "v2" }, { jobId: "J2", jobKind: "download-one", url: "https://rumble.com/v2" }, T0 + 60_000); applyServiceCookieSignal(state, { service: "youtube", kind: "bot-check", line: "c" }, {}, T0); const pendingOnly = emptyServiceCookieState(); applyServiceCookieSignal(pendingOnly, { service: "rumble", kind: "embed-403", line: "a", videoId: "v1" }, {}, T0); assert.deepEqual(buildServiceCookieAlerts(pendingOnly, { now: T0 + 1000 }).alerts, []); assert.equal(buildServiceCookieAlerts(pendingOnly, { now: T0 + 1000 }).pending.length, 1); const statuses: Record = { J1: "failed", J2: "done" }; let v = buildServiceCookieAlerts(state, { now: T0 + 120_000, browser: "firefox", statusOf: (id) => statuses[id] }); const rumble = v.alerts.find((a) => a.service === "rumble")!; assert.equal(rumble.state, "stale"); assert.equal(rumble.headline, "Rumble needs fresh cookies: open rumble.com in firefox (a fresh Cloudflare clearance cookie)"); assert.match(rumble.detail, /^2 embed 403s on different videos since 17:02 UTC, job download-one J2/); assert.equal(rumble.canRecheck, true); applyServiceCookieClean(state, "rumble", T0 + 180_000, "recheck"); v = buildServiceCookieAlerts(state, { now: T0 + 240_000, statusOf: (id) => statuses[id] }); const ok = v.alerts.find((a) => a.service === "rumble")!; assert.equal(ok.state, "ok-again"); assert.deepEqual(ok.stoppedJobs.map((j) => j.id), ["J1"], "a job that finished done was not stopped"); assert.equal(ok.headline, "Rumble cookies OK again — re-run the 1 job it stopped"); assert.equal(v.alerts[0].state, "stale", "stale banners come first"); // Still offered the next morning while there is a job to re-run... assert.ok(buildServiceCookieAlerts(state, { now: T0 + 20 * 3600_000, statusOf: (id) => statuses[id] }).alerts.some((a) => a.service === "rumble")); // ...and once re-run, shown only briefly after the clearing. state.services.rumble.rerunAt = new Date(T0 + 200_000).toISOString(); state.services.rumble.rerunJobIds = ["J9"]; const after = buildServiceCookieAlerts(state, { now: T0 + 180_000 + OK_AGAIN_SHOW_MS + 1, statusOf: (id) => statuses[id] }); assert.equal(after.alerts.some((a) => a.service === "rumble"), false); }); // plans/pacing-guard.md: X is a service, its family "x", Recheck always offered. test("X is a cookie-alert service: key, label, family, fix; one challenge raises it", async () => { const sc = await import("./serviceCookies"); assert.equal(sc.isServiceKey("x"), true); assert.equal(sc.serviceLabel("x"), "X"); assert.equal(sc.serviceFamily("x"), "x"); assert.match(sc.serviceFixText("x"), /Refresh cookies/); const state = sc.emptyServiceCookieState(); const r = sc.applyServiceCookieSignal(state, { service: "x", kind: "challenge", line: "[twitter][warning] Cloudflare challenge" }, {}, Date.parse("2026-10-23T03:00:00Z")); assert.equal(r.raisedNow, true); const view = sc.buildServiceCookieAlerts(state, { now: Date.parse("2026-10-23T03:01:00Z") }); assert.equal(view.alerts[0]?.service, "x"); assert.equal(view.alerts[0]?.canRecheck, true); assert.match(view.alerts[0]?.headline ?? "", /^X needs fresh cookies: /); }); test("rumbleMissingConfigLine: names what the channel's config lacks on any Rumble 403, and nothing otherwise", () => { const err = "ERROR: [RumbleChannel] x: Unable to download webpage: HTTP Error 403: Forbidden"; assert.match( rumbleMissingConfigLine(err, "rekietalaw-rumble", {}) ?? "", /^Rumble refused \(HTTP 403\): rekietalaw-rumble has no cookiesFromBrowser, no cookieMode, no --impersonate in ytdlpExtraArgs; Rumble needs cookies from firefox/, ); // Only the missing part; a wrong mode is named with its value. const partial = rumbleMissingConfigLine(err, "c", { cookiesFromBrowser: "firefox", cookieMode: "defer", ytdlpExtraArgs: ["--impersonate=firefox"] }); assert.match(partial ?? "", /c has cookieMode defer;/); assert.equal(partial?.includes("cookiesFromBrowser"), false); // Complete config, a non-Rumble line, a non-403, an unreadable config: no line. const full = { cookiesFromBrowser: "firefox", cookieMode: "always" as const, ytdlpExtraArgs: ["--impersonate", "firefox"] }; assert.equal(rumbleMissingConfigLine(err, "c", full), null); assert.equal(rumbleMissingConfigLine("ERROR: [youtube] abc: HTTP Error 403", "c", {}), null); assert.equal(rumbleMissingConfigLine("ERROR: [RumbleChannel] x: HTTP Error 429", "c", {}), null); assert.equal(rumbleMissingConfigLine(err, "c", null), null); });