// The yt-dlp services' browser-cookie ages (browserCookieAge.ts), read BY NAME // from a Firefox cookies.sqlite. Every store is a fixture written through // node:sqlite under a temp dir (social/__fixtures__/firefoxCookieStore.ts) — no // test reads a real browser profile, and nothing asks any service. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/browserCookieAge.test.ts import { test, after, beforeEach } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync } from "node:fs"; import { mkdir, readdir, rm, utimes, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { BROWSER_COOKIE_TIMES_SQL, readBrowserCookieAges, readFirefoxServiceCookieTimes, resetBrowserCookieAgeCache, STALE_COPY_MS, } from "./browserCookieAge"; import { writeFirefoxCookieStore } from "../social/__fixtures__/firefoxCookieStore"; const TMP = mkdtempSync(path.join(os.tmpdir(), "cookieage-")); after(() => rm(TMP, { recursive: true, force: true })); beforeEach(() => resetBrowserCookieAgeCache()); const us = (iso: string) => Date.parse(iso) * 1000; test("the query never selects a cookie's value", () => { // The column list is everything between SELECT and FROM. const cols = /^SELECT (.*) FROM /.exec(BROWSER_COOKIE_TIMES_SQL)?.[1] ?? ""; assert.ok(cols.length > 0); assert.doesNotMatch(cols, /\bvalue\b/i); assert.doesNotMatch(cols, /\*/); assert.deepEqual( cols.split(",").map((c) => c.trim()), ["host", "name", "creationTime", "lastAccessed", "expiry", "originAttributes"], ); }); test("rumble's clearance: issued and expiry; youtube's sign-in: last used; odysee with none", async () => { const dir = path.join(TMP, "p1"); await writeFirefoxCookieStore(dir, [ { host: ".rumble.com", name: "cf_clearance", value: "fixture-not-a-real-clearance", creationTime: us("2026-11-16T09:00:00Z"), lastAccessed: us("2026-11-16T09:10:00Z"), expiry: Date.parse("2026-11-16T10:00:00Z") / 1000, }, // An older clearance on a subdomain: the newest wins. { host: "a.rumble.com", name: "cf_clearance", value: "x", creationTime: us("2026-11-15T09:00:00Z") }, // Not the site's (a lookalike host). { host: ".notrumble.com", name: "cf_clearance", value: "x", creationTime: us("2026-11-16T09:30:00Z") }, { host: ".youtube.com", name: "SID", value: "x", lastAccessed: us("2026-11-16T08:00:00Z"), creationTime: us("2026-01-01T00:00:00Z") }, { host: ".youtube.com", name: "__Secure-3PSID", value: "x", lastAccessed: us("2026-11-16T08:30:00Z") }, { host: ".youtube.com", name: "PREF", value: "x", lastAccessed: us("2026-11-16T09:59:00Z") }, ]); const out = await readFirefoxServiceCookieTimes(path.join(dir, "cookies.sqlite"), { tmpRoot: TMP }); assert.deepEqual(out.rumble, { service: "rumble", basis: "clearance", found: true, at: "2026-11-16T09:00:00.000Z", expiresAt: "2026-11-16T10:00:00.000Z", }); assert.equal(out.youtube.basis, "login-used"); assert.equal(out.youtube.at, "2026-11-16T08:30:00.000Z"); assert.equal(out.youtube.expiresAt, undefined); assert.deepEqual(out.odysee, { service: "odysee", basis: "clearance", found: false, at: null }); // Nothing returned carries a value. assert.doesNotMatch(JSON.stringify(out), /fixture-not-a-real/); }); test("a cookie only in the write-ahead log counts; a container the spec does not read does not", async () => { const dir = path.join(TMP, "p2"); const { db } = await writeFirefoxCookieStore( dir, [ { host: ".rumble.com", name: "cf_clearance", value: "x", creationTime: us("2026-11-16T07:00:00Z") }, { host: ".rumble.com", name: "cf_clearance", value: "x", creationTime: us("2026-11-16T09:45:00Z"), originAttributes: "^userContextId=2", }, ], { keepOpen: true }, ); try { const out = await readFirefoxServiceCookieTimes(path.join(dir, "cookies.sqlite"), { tmpRoot: TMP }); assert.equal(out.rumble.at, "2026-11-16T07:00:00.000Z"); const all = await readFirefoxServiceCookieTimes(path.join(dir, "cookies.sqlite"), { tmpRoot: TMP, container: "all" }); assert.equal(all.rumble.at, "2026-11-16T09:45:00.000Z"); } finally { db?.close(); } }); test("readBrowserCookieAges: a profile path spec, the cache, and fresh", async () => { const dir = path.join(TMP, "p3"); await writeFirefoxCookieStore(dir, [ { host: ".rumble.com", name: "cf_clearance", value: "x", creationTime: us("2026-11-16T09:00:00Z") }, ]); const spec = `firefox:${dir}`; const a = await readBrowserCookieAges(spec, { home: TMP, tmpRoot: TMP, now: 1000 }); assert.equal(a.services.rumble.at, "2026-11-16T09:00:00.000Z"); // A rewrite of the store is not seen inside the cache window... await rm(path.join(dir, "cookies.sqlite")); await writeFirefoxCookieStore(dir, [ { host: ".rumble.com", name: "cf_clearance", value: "x", creationTime: us("2026-11-16T09:50:00Z") }, ]); const b = await readBrowserCookieAges(spec, { home: TMP, tmpRoot: TMP, now: 2000 }); assert.equal(b.services.rumble.at, "2026-11-16T09:00:00.000Z"); // ...and is with fresh. const c = await readBrowserCookieAges(spec, { home: TMP, tmpRoot: TMP, now: 3000, fresh: true }); assert.equal(c.services.rumble.at, "2026-11-16T09:50:00.000Z"); }); test("unreadable sources are answers, never throws", async () => { const none = await readBrowserCookieAges(undefined, { home: TMP }); assert.equal(none.services.rumble.found, null); assert.match(none.services.rumble.unknown ?? "", /No cookiesFromBrowser/); const chrome = await readBrowserCookieAges("chrome", { home: TMP }); assert.match(chrome.services.youtube.unknown ?? "", /encrypted/); const missing = await readBrowserCookieAges(`firefox:${path.join(TMP, "no-such-profile")}`, { home: TMP }); assert.equal(missing.services.odysee.at, null); assert.ok(missing.services.odysee.unknown); const corrupt = path.join(TMP, "p4"); await writeFirefoxCookieStore(corrupt, []); await writeFile(path.join(corrupt, "cookies.sqlite"), "not a database"); const bad = await readBrowserCookieAges(`firefox:${corrupt}`, { home: TMP, tmpRoot: TMP }); assert.match(bad.services.rumble.unknown ?? "", /Could not read Firefox's cookie store/); }); test("a file: spec is aged by the file's mtime", async () => { const file = path.join(TMP, "cookies.txt"); await writeFile(file, "# Netscape HTTP Cookie File\n"); const t = new Date("2026-11-16T06:00:00Z"); await utimes(file, t, t); const out = await readBrowserCookieAges(`file:${file}`); assert.deepEqual(out.services.youtube, { service: "youtube", basis: "file", found: true, at: t.toISOString() }); }); test("a read sweeps this module's stale copies (they hold values), and leaves fresh ones and others alone", async () => { const root = path.join(TMP, "sweep"); await mkdir(root); const old = path.join(root, "archilyzer-cookieage-old1"); const fresh = path.join(root, "archilyzer-cookieage-new1"); const other = path.join(root, "someone-elses-old"); for (const d of [old, fresh, other]) await mkdir(d); const past = new Date(Date.now() - STALE_COPY_MS - 60_000); await utimes(old, past, past); await utimes(other, past, past); const dir = path.join(TMP, "p5"); await writeFirefoxCookieStore(dir, []); await readFirefoxServiceCookieTimes(path.join(dir, "cookies.sqlite"), { tmpRoot: root }); // The read's own copy is gone too, once it is done. assert.deepEqual((await readdir(root)).sort(), ["archilyzer-cookieage-new1", "someone-elses-old"]); });