// X's jar age, from disk (xJarAge.ts): the profile jar's mtime, or the // browser's x.com auth_token last-sent time read BY NAME from a Firefox // cookies.sqlite. Every store is a fixture written through node:sqlite under a // temp HOME (__fixtures__/firefoxCookieStore.ts) — no test reads a real // browser profile, and nothing asks X. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test social/xJarAge.test.ts import { test, after } 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 type { Paths } from "../lib/paths"; import { readFirefoxXAuthTimes, readXJarAge, xJarStatusFrom } from "./xJarAge"; import { writeFirefoxCookieStore, type FixtureCookie } from "./__fixtures__/firefoxCookieStore"; const TMP = mkdtempSync(path.join(os.tmpdir(), "xjarage-")); after(() => rm(TMP, { recursive: true, force: true })); let n = 0; const fresh = (label: string) => path.join(TMP, `${label}-${++n}`); const NOW = Date.UTC(2026, 9, 31, 23, 0, 0); const us = (msAgo: number) => (NOW - msAgo) * 1000; const SECRET = "fixture-auth-value-never-read"; function login(lastUsedMsAgo: number, extra: FixtureCookie[] = []): FixtureCookie[] { return [ { host: ".x.com", name: "auth_token", value: SECRET, lastAccessed: us(lastUsedMsAgo), creationTime: us(30 * 86_400_000) }, { host: ".x.com", name: "ct0", value: "csrf" }, { host: "notx.com", name: "auth_token", value: "lookalike", lastAccessed: us(0) }, ...extra, ]; } function pathsAt(dir: string): Paths { return { transcriptsDir: dir } as unknown as Paths; } test("the name-only read: the x.com auth_token's times, its value never selected", async () => { const { file } = await writeFirefoxCookieStore(fresh("ff"), login(30 * 60_000)); const t = await readFirefoxXAuthTimes(file, { tmpRoot: TMP }); assert.equal(t.found, true); assert.equal(t.lastAccessedMs, NOW - 30 * 60_000); assert.equal(t.creationMs, NOW - 30 * 86_400_000); assert.equal(JSON.stringify(t).includes(SECRET), false); // The private copy is gone. assert.deepEqual((await readdir(TMP)).filter((f) => f.startsWith("archilyzer-xjar-")), []); }); test("a login only Firefox's write-ahead log holds counts; one in an unread container does not", async () => { const { file, db } = await writeFirefoxCookieStore(fresh("wal"), login(5 * 60_000), { keepOpen: true }); try { assert.equal((await readFirefoxXAuthTimes(file, { tmpRoot: TMP })).found, true); } finally { db?.close(); } const { file: f2 } = await writeFirefoxCookieStore(fresh("container"), [ { host: ".x.com", name: "auth_token", value: SECRET, lastAccessed: us(60_000), originAttributes: "^userContextId=2" }, ]); assert.equal((await readFirefoxXAuthTimes(f2, { tmpRoot: TMP })).found, false); assert.equal((await readFirefoxXAuthTimes(f2, { tmpRoot: TMP, container: "all" })).found, true); }); test("browser source: fresh within the limit, stale past it, no limit at 0; cached between reads unless asked fresh", async () => { const home = fresh("home"); const profile = path.join(home, ".mozilla", "firefox", "abc.default-release"); await writeFirefoxCookieStore(profile, login(30 * 60_000)); const settings = { cookiesFromBrowser: "firefox", social: { x: { cookieSource: "browser" as const } } }; const p = pathsAt(fresh("transcripts")); const ok = await readXJarAge(p, settings, { maxAgeMinutes: 60, now: NOW, home, tmpRoot: TMP, fresh: true }); assert.equal(ok.source, "browser"); assert.equal(ok.loggedIn, true); assert.equal(ok.ageMinutes, 30); assert.equal(ok.fresh, true); assert.match(ok.summary, /last used 30 min ago \(limit 60 min\)\.$/); assert.equal(JSON.stringify(ok).includes(SECRET), false); const stale = await readXJarAge(p, settings, { maxAgeMinutes: 20, now: NOW, home, tmpRoot: TMP, fresh: true }); assert.equal(stale.fresh, false); assert.match(stale.summary, /open x\.com in that browser/); assert.equal((await readXJarAge(p, settings, { maxAgeMinutes: 0, now: NOW, home, tmpRoot: TMP, fresh: true })).fresh, true); // The store changes (the operator opened x.com) — a cached read does not see // it; a fresh read does. await rm(profile, { recursive: true, force: true }); await writeFirefoxCookieStore(profile, login(60_000)); const cached = await readXJarAge(p, settings, { maxAgeMinutes: 60, now: NOW + 60_000, home, tmpRoot: TMP }); assert.equal(cached.ageMinutes, 31, "re-derived from the cached reading at the new time"); const reread = await readXJarAge(p, settings, { maxAgeMinutes: 60, now: NOW, home, tmpRoot: TMP, fresh: true }); assert.equal(reread.ageMinutes, 1); }); test("profile source: the exported jar's mtime and its auth_token", async () => { const dir = fresh("transcripts"); const jar = path.join(dir, ".x-session", "cookies.txt"); await mkdir(path.join(dir, ".x-session", "profile"), { recursive: true }); await writeFile(jar, "# Netscape HTTP Cookie File\n.x.com\tTRUE\t/\tTRUE\t0\tauth_token\tv\n"); const t = new Date(NOW - 3 * 3600_000); await utimes(jar, t, t); const settings = { social: { x: { cookieSource: "profile" as const } } }; const v = await readXJarAge(pathsAt(dir), settings, { maxAgeMinutes: 60, now: NOW }); assert.equal(v.source, "profile"); assert.equal(v.ageMinutes, 180); assert.equal(v.fresh, false); assert.match(v.summary, /^The X cookie jar was written 3 h ago \(limit 60 min\): press Refresh cookies/); await utimes(jar, new Date(NOW - 60_000), new Date(NOW - 60_000)); assert.equal((await readXJarAge(pathsAt(dir), settings, { maxAgeMinutes: 60, now: NOW })).fresh, true); }); test("the verdict, pure: no login is never fresh; an unreadable source says why", () => { assert.equal(xJarStatusFrom({ source: "profile", at: null, loggedIn: false, maxAgeMinutes: 0, now: NOW }).fresh, false); const u = xJarStatusFrom({ source: "browser", at: null, loggedIn: null, maxAgeMinutes: 60, now: NOW, unreadable: "No Firefox cookie store found." }); assert.equal(u.fresh, false); assert.equal(u.summary, "No Firefox cookie store found."); });