// The /cookies screen's full controls (cookieControls.ts), the forum-thread // channels behind its forum rows (forumChannels.ts) and yt-dlp's cookie keys // saved alone (settings/cookieSourceActions.ts), over a temp corpus. // // Run with: pnpm -C editor exec tsx --test app/lib/cookieControls.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, readFile, rm, writeFile } from "node:fs/promises"; import path from "node:path"; import { setupOpsCorpus } from "../api/ops/_testCorpus"; const corpus = await setupOpsCorpus("one-youtube-channel-with-data"); await corpus.writeSettings({ cookiesFromBrowser: "firefox", cookieMode: "always", minFreeDiskGB: 0 }); test.after(() => corpus.cleanup()); const { getPaths } = await import("yt-dlp-transcript-common/lib/paths"); const { listForumChannels } = await import("./forumChannels"); const { loadCookieControls } = await import("./cookieControls"); const { sectionFailed } = await import("./cookieControlsTypes"); function ok(v: T | { error: string }): T { if (sectionFailed(v)) throw new Error(v.error); return v as T; } const { saveYtdlpCookieSourceAction } = await import("../settings/cookieSourceActions"); async function forumChannel(slug: string, url: string) { const dir = path.join(corpus.transcripts, "channels", slug); await mkdir(dir, { recursive: true }); await writeFile( path.join(dir, "config.json"), JSON.stringify({ handling: "youtube", name: slug, sourceKind: "social", platform: "xenforo", postFetcher: "xenforo-thread", url }), ); } test("the forum-thread channels: only xenforo threads, by host; remembered until channels/ changes or fresh", async () => { const paths = getPaths(); await forumChannel("a-thread", "https://forum.example.test/threads/a-thread.1/"); const first = await listForumChannels(paths, { fresh: true }); assert.deepEqual(first, [{ host: "forum.example.test", slug: "a-thread", url: "https://forum.example.test/threads/a-thread.1/" }]); // A new channel dir moves channels/'s mtime: read again at once. await forumChannel("b-thread", "https://other.example.test/threads/b-thread.2/"); const second = await listForumChannels(paths); assert.deepEqual(second.map((c) => c.slug), ["a-thread", "b-thread"]); // An edit inside a channel does not move channels/'s mtime: remembered… await writeFile(path.join(corpus.transcripts, "channels", "b-thread", "config.json"), JSON.stringify({ handling: "youtube", name: "b" })); assert.equal((await listForumChannels(paths)).length, 2); // …until fresh. assert.deepEqual((await listForumChannels(paths, { fresh: true })).map((c) => c.slug), ["a-thread"]); }); test("the controls carry only their slices: X's session, the archive block, yt-dlp's two keys, the forums", async () => { const c = await loadCookieControls(getPaths()); assert.deepEqual(Object.keys(c).sort(), ["archive", "forums", "x", "ytdlp"]); assert.deepEqual(c.ytdlp, { cookiesFromBrowser: "firefox", cookieMode: "always" }); const x = ok(c.x); assert.equal(x.initial.hasProfile, false); assert.equal(typeof x.initialMaxCookieAge, "number"); assert.ok("archiveTodayHost" in ok(c.archive).initial); const forums = ok(c.forums); assert.deepEqual(Object.keys(forums), ["forum.example.test"]); assert.deepEqual(forums["forum.example.test"]?.channels, ["a-thread"]); assert.equal(forums["forum.example.test"]?.status.hasProfile, false); // No other settings block rode along. assert.equal(JSON.stringify(c).includes("autoQueue"), false); }); test("one section's read failing is that section's sentence; the others still read", async () => { const c = await loadCookieControls(getPaths(), { getSettings: () => { throw new Error("settings.json is unreadable"); }, }); for (const k of ["x", "archive", "ytdlp"] as const) { assert.equal(sectionFailed(c[k]), true, k); assert.match((c[k] as { error: string }).error, /^Could not read .*settings\.json is unreadable/, k); } assert.equal(sectionFailed(c.forums), false); }); test("X's browser spec reaches the client through the privacy rules (lib/xSourceDisplay.ts)", async () => { const { xSourceForDisplay } = await import("./xSourceDisplay"); const { resetPrivacyMemo } = await import("yt-dlp-transcript-common/lib/privacy/privacy-server"); const home = (await import("node:os")).homedir(); await corpus.writeSettings({ cookiesFromBrowser: `firefox:${home}/zq-profile`, privacy: { displayPaths: "masked" } }); resetPrivacyMemo(); const v = xSourceForDisplay({ source: "browser", chosen: false, browserSpec: `firefox:${home}/zq-profile`, label: `Browser login (firefox:${home}/zq-profile)`, summary: `No store at ${home}/zq-profile.`, }); assert.equal(v.source, "browser"); for (const s of [v.browserSpec, v.label, v.summary]) assert.equal(String(s).includes(home), false, String(s)); assert.match(String(v.browserSpec), /^firefox:.*zq-profile$/); await corpus.writeSettings({ cookiesFromBrowser: "firefox", cookieMode: "always", minFreeDiskGB: 0 }); resetPrivacyMemo(); }); test("yt-dlp's cookie keys save alone: the two keys written, the rest of settings kept, a bad mode is the default", async () => { const fd = new FormData(); fd.set("cookiesFromBrowser", " firefox:fixture-profile "); fd.set("cookieMode", "defer"); const r = await saveYtdlpCookieSourceAction(undefined, fd); assert.deepEqual(r, { ok: true, cookiesFromBrowser: "firefox:fixture-profile", cookieMode: "defer" }); const saved = JSON.parse(await readFile(getPaths().settingsFile, "utf8")) as Record; assert.equal(saved.cookiesFromBrowser, "firefox:fixture-profile"); assert.equal(saved.cookieMode, "defer"); assert.equal(saved.minFreeDiskGB, 0); const bad = new FormData(); bad.set("cookiesFromBrowser", ""); bad.set("cookieMode", "sometimes"); const r2 = await saveYtdlpCookieSourceAction(undefined, bad); assert.deepEqual(r2, { ok: true, cookiesFromBrowser: "", cookieMode: "when-required" }); }); test.after(async () => { await rm(path.join(corpus.transcripts, "channels", "a-thread"), { recursive: true, force: true }); });