// The /cookies screen's rows (cookieStatus.ts): pure, from what the editor read. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/cookieStatus.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { ageSentence, buildCookieStatus, durationText, forumThreadFor, HEADED_NOTE, type CookieStatusInput } from "./cookieStatus"; import { emptyServiceCookieState, type ServiceCookieAlertView } from "./serviceCookies"; import type { BrowserCookieAge } from "./browserCookieAge"; const NOW = Date.parse("2026-11-16T10:00:00Z"); const age = (service: "rumble" | "youtube" | "odysee", over: Partial = {}): BrowserCookieAge => ({ service, basis: service === "youtube" ? "login-used" : "clearance", found: false, at: null, ...over, }); // The privacy displayers, as settings.privacy would draw them. const display = { path: (p: string) => p.replace("/home/someone", "/home/user"), text: (s: string) => s.replace("/home/someone", "/home/user"), }; function input(over: Partial = {}): CookieStatusInput { return { now: NOW, alerts: { alerts: [], pending: [] }, state: emptyServiceCookieState(), x: { source: "profile", profileDir: "/home/someone/t/.x-session/profile", hasProfile: true }, ytdlp: { rumble: { spec: "firefox", specFrom: "settings", age: age("rumble") }, youtube: { spec: "firefox", specFrom: "settings", age: age("youtube") }, odysee: { spec: "firefox", specFrom: "settings", age: age("odysee") }, }, archive: { dir: "/home/someone/t/.archive-session", hasProfile: false }, forums: [], running: {}, display, ...over, }; } const row = (p: ReturnType, s: string) => { const r = p.rows.find((x) => x.service === s); assert.ok(r, `no row for ${s}`); return r; }; test("durations and age sentences", () => { assert.equal(durationText(12), "12 min"); assert.equal(durationText(180), "3 h"); assert.equal(durationText(60 * 24 * 3), "3 days"); assert.equal(ageSentence("jar written", "2026-11-16T09:48:00Z", NOW), "jar written 12 min ago"); assert.equal(ageSentence("jar written", "2026-11-16T09:59:40Z", NOW), "jar written just now"); assert.equal(ageSentence("jar written", null, NOW), null); }); test("the fixed rows, in order, with paths drawn through the displayers", () => { const p = buildCookieStatus(input()); assert.deepEqual(p.rows.map((r) => r.service), ["x", "rumble", "youtube", "odysee", "archive.today"]); assert.equal(row(p, "x").source, "Connected profile (/home/user/t/.x-session/profile)"); assert.equal(row(p, "archive.today").source, "Archive profile (/home/user/t/.archive-session)"); assert.doesNotMatch(JSON.stringify(p), /someone/); }); test("X with a connected profile: Refresh cookies, the jar's age and its limit", () => { const p = buildCookieStatus( input({ x: { source: "profile", profileDir: "/p", hasProfile: true, jar: { source: "profile", at: "2026-11-16T09:48:00Z", ageMinutes: 12, maxAgeMinutes: 60, loggedIn: true, fresh: true, readAt: "", summary: "" }, }, }), ); const x = row(p, "x"); assert.deepEqual(x.button, { action: "refresh", label: "Refresh cookies" }); assert.equal(x.age, "jar written 12 min ago"); assert.equal(x.ageNote, "limit 60 min"); assert.equal(x.state, "fine"); assert.equal(x.headed, undefined); }); test("X with no profile: Connect, said to open on the editor's machine", () => { const x = row(buildCookieStatus(input({ x: { source: "profile", profileDir: "/p", hasProfile: false } })), "x"); assert.deepEqual(x.button, { action: "connect", label: "Connect X account" }); assert.equal(x.headed, true); assert.equal(x.todo, HEADED_NOTE); assert.equal(x.state, "unknown"); assert.equal(x.ageUnknown, "No X profile connected yet."); }); test("X with the browser login: open x.com, then Check again", () => { const x = row( buildCookieStatus(input({ x: { source: "browser", browserSpec: "firefox:/home/someone/ff", profileDir: "/p", hasProfile: false } })), "x", ); assert.deepEqual(x.button, { action: "recheck", label: "Check again" }); assert.equal(x.todo, "Open x.com in firefox, then Check again (no request is sent to X)."); assert.equal(x.source, "Browser login (firefox:/home/user/ff)"); }); test("Rumble: a stale alert's headline, the clearance's age and expiry, Check again", () => { const state = emptyServiceCookieState(); state.lastGood.rumble = { url: "https://rumble.com/v1", channel: "c", at: "2026-11-16T08:00:00Z" }; state.actions = { rumble: { at: "2026-11-16T09:30:00Z", action: "recheck", ok: true, result: "stale", message: "still refused" } }; const alert = { service: "rumble", state: "stale", headline: "Rumble needs fresh cookies: open rumble.com in firefox" } as ServiceCookieAlertView; const p = buildCookieStatus( input({ state, alerts: { alerts: [alert], pending: [] }, ytdlp: { ...input().ytdlp, rumble: { spec: "firefox", specFrom: "channel", channel: "c", age: age("rumble", { found: true, at: "2026-11-16T09:20:00Z", expiresAt: "2026-11-16T10:30:00Z" }), }, }, running: { rumble: { action: "recheck", since: "2026-11-16T09:59:00Z" } }, }), ); const r = row(p, "rumble"); assert.equal(r.state, "stale"); assert.equal(r.alert, alert.headline); assert.equal(r.age, "clearance issued 40 min ago"); assert.equal(r.ageNote, "expires in 30 min"); assert.equal(r.source, "firefox (channel c)"); assert.equal(r.todo, "Open rumble.com in firefox, then Check again."); assert.deepEqual(r.button, { action: "recheck", label: "Check again" }); assert.equal(r.last?.result, "stale"); assert.deepEqual(r.running, { action: "recheck", since: "2026-11-16T09:59:00Z" }); }); test("a yt-dlp service with nothing recorded: unknown, and Check again says why it cannot ask", () => { const p = buildCookieStatus(input()); const o = row(p, "odysee"); assert.equal(o.state, "unknown"); assert.equal(o.ageUnknown, "No clearance cookie for odysee.com in firefox."); assert.equal(o.button?.disabled, "No Odysee request is recorded yet to ask again."); const y = row(p, "youtube"); assert.equal(y.todo, "Open youtube.com in firefox signed in, then Check again."); }); test("a pending first signal is said, not raised", () => { const p = buildCookieStatus( input({ alerts: { alerts: [], pending: [{ service: "rumble", label: "Rumble", kind: "embed-403", videos: ["a"], lastSeen: "2026-11-16T09:12:00Z" }] } }), ); assert.equal(row(p, "rumble").alert, "One embed 403 at 09:12 UTC; a second video raises the alert."); }); test("forum hosts: from the profiles and from the state, sorted; Connect needs a thread", () => { const state = emptyServiceCookieState(); state.lastGood["kiwifarms.st"] = { url: "https://kiwifarms.st/threads/t.1/", at: "2026-11-16T08:00:00Z" }; const p = buildCookieStatus( input({ state, forums: [ { host: "kiwifarms.st", dir: "/home/someone/t/.forum-session/kiwifarms.st", hasProfile: true, connectedAt: "2026-11-15T10:00:00Z", cleared: true }, { host: "forum.example.org", dir: "/d", hasProfile: true }, ], }), ); assert.deepEqual(p.rows.slice(5).map((r) => r.service), ["forum.example.org", "kiwifarms.st"]); const kf = row(p, "kiwifarms.st"); assert.equal(kf.label, "Kiwi Farms (kiwifarms.st)"); assert.equal(kf.age, "connected 24 h ago"); assert.equal(kf.ageNote, "check passed"); assert.equal(kf.headed, true); assert.equal(kf.button?.disabled, undefined); assert.match(row(p, "forum.example.org").button?.disabled ?? "", /No thread on this forum/); assert.deepEqual(forumThreadFor(state, "kiwifarms.st"), { url: "https://kiwifarms.st/threads/t.1/" }); assert.equal(forumThreadFor(state, "forum.example.org"), null); }); test("X with a profile that holds no login: the one press is Connect, not Refresh", () => { const x = row(buildCookieStatus(input({ x: { source: "profile", profileDir: "/p", hasProfile: true, loggedIn: false } })), "x"); assert.deepEqual(x.button, { action: "connect", label: "Connect X account" }); assert.equal(x.headed, true); const logged = row(buildCookieStatus(input({ x: { source: "profile", profileDir: "/p", hasProfile: true, loggedIn: true } })), "x"); assert.equal(logged.button?.action, "refresh"); }); test("forum thread channels add their hosts, name their channels, and give Connect a thread", () => { const forumChannels = [ { host: "www.forum.example.org", slug: "b-thread", url: "https://forum.example.org/threads/b.2/" }, { host: "forum.example.org", slug: "a-thread", url: "https://forum.example.org/threads/a.1/" }, ]; const p = buildCookieStatus(input({ forumChannels })); const f = row(p, "forum.example.org"); assert.deepEqual(f.channels, ["a-thread", "b-thread"]); assert.equal(f.button?.disabled, undefined); assert.equal(f.source, "No forum profile yet"); assert.deepEqual(forumThreadFor(emptyServiceCookieState(), "forum.example.org", forumChannels), { url: "https://forum.example.org/threads/b.2/", channel: "b-thread", }); });