import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdirSync, mkdtempSync, utimesSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { browserUserAgentOr, defaultProfileFromIni, deriveFirefoxUserAgent, FALLBACK_FIREFOX_VERSION, firefoxOsToken, firefoxRoots, firefoxUserAgent, formatFirefoxUserAgent, parseCompatibilityIniVersion, parseCookieBrowserSpec, parseFirefoxVersionOutput, PROFILE_TTL_MS, resolveFirefoxProfile, setFirefoxUserAgentForTests, } from "./browserUserAgent-server"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/browserUserAgent-server.test.ts const compat = (v: string) => `[Compatibility]\nLastVersion=${v}_20261015234007/20261015234007\nLastOSABI=Linux_x86_64-gcc3\nLastPlatformDir=/usr/lib/firefox\n`; // A temp HOME with ~/.mozilla/firefox holding `profiles` (name → version, or // null for no compatibility.ini), each with a cookies.sqlite of the given age. function fakeHome( profiles: Record, ini?: { installs?: string; profiles?: string }, ): { home: string; root: string } { const home = mkdtempSync(path.join(os.tmpdir(), "ff-ua-")); const root = path.join(home, ".mozilla", "firefox"); mkdirSync(root, { recursive: true }); for (const [name, p] of Object.entries(profiles)) { const dir = path.join(root, name); mkdirSync(dir); if (p.version) writeFileSync(path.join(dir, "compatibility.ini"), compat(p.version)); if (p.cookiesAgeS !== undefined) { const f = path.join(dir, "cookies.sqlite"); writeFileSync(f, ""); const t = Date.now() / 1000 - p.cookiesAgeS; utimesSync(f, t, t); } } if (ini?.installs) writeFileSync(path.join(root, "installs.ini"), ini.installs); if (ini?.profiles) writeFileSync(path.join(root, "profiles.ini"), ini.profiles); return { home, root }; } const LINUX = { platform: "linux" as const, env: {} as NodeJS.ProcessEnv }; const noBinary = () => null; test("compatibility.ini's LastVersion, and `firefox --version`, parse to the version", () => { assert.equal(parseCompatibilityIniVersion(compat("158.0")), "158.0"); assert.equal(parseCompatibilityIniVersion(compat("149.0.2")), "149.0.2"); assert.equal(parseCompatibilityIniVersion("[Compatibility]\nLastOSABI=x\n"), null); assert.equal(parseCompatibilityIniVersion(""), null); assert.equal(parseFirefoxVersionOutput("Mozilla Firefox 158.0\n"), "158.0"); assert.equal(parseFirefoxVersionOutput("Mozilla Firefox 140.4.0esr"), "140.4.0"); assert.equal(parseFirefoxVersionOutput("command not found"), null); }); test("the UA is Firefox's own format, major.0, with this platform's token", () => { assert.equal( formatFirefoxUserAgent("158.0", "linux", "x64"), "Mozilla/5.0 (X11; Linux x86_64; rv:158.0) Gecko/20100101 Firefox/158.0", ); assert.equal( formatFirefoxUserAgent("149.0.2", "linux", "x64"), "Mozilla/5.0 (X11; Linux x86_64; rv:149.0) Gecko/20100101 Firefox/149.0", ); assert.equal( formatFirefoxUserAgent("158.0", "darwin", "arm64"), "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:158.0) Gecko/20100101 Firefox/158.0", ); assert.equal( formatFirefoxUserAgent("158.0", "win32", "x64"), "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:158.0) Gecko/20100101 Firefox/158.0", ); assert.equal(firefoxOsToken("linux", "arm64"), "X11; Linux aarch64"); assert.equal(formatFirefoxUserAgent("nightly", "linux", "x64"), null); }); test("cookie specs parse as yt-dlp's BROWSER[+KEYRING][:PROFILE][::CONTAINER]", () => { assert.deepEqual(parseCookieBrowserSpec("firefox"), { browser: "firefox" }); assert.deepEqual(parseCookieBrowserSpec("Firefox:/data/ff"), { browser: "firefox", profile: "/data/ff" }); assert.deepEqual(parseCookieBrowserSpec("firefox:work::Personal"), { browser: "firefox", profile: "work", container: "Personal", }); assert.deepEqual(parseCookieBrowserSpec("firefox::Personal"), { browser: "firefox", container: "Personal" }); assert.deepEqual(parseCookieBrowserSpec("chrome+gnomekeyring:Default"), { browser: "chrome", keyring: "gnomekeyring", profile: "Default", }); assert.equal(parseCookieBrowserSpec("file:/x/cookies.txt"), null); assert.equal(parseCookieBrowserSpec(""), null); assert.equal(parseCookieBrowserSpec(undefined), null); }); test("roots per platform, in yt-dlp's order", () => { assert.deepEqual(firefoxRoots("/h", "linux", {}), [ "/h/.config/mozilla/firefox", "/h/.mozilla/firefox", "/h/snap/firefox/common/.mozilla/firefox", "/h/.var/app/org.mozilla.firefox/.mozilla/firefox", ]); assert.deepEqual(firefoxRoots("/h", "darwin", {}), ["/h/Library/Application Support/Firefox"]); }); test("the default profile: installs.ini, else profiles.ini's Install, Default=1, or only profile", () => { const files: Record = {}; const read = (p: string) => files[p] ?? null; files["/r/profiles.ini"] = "[Profile0]\nName=a\nIsRelative=1\nPath=a.default\n\n[Profile1]\nName=b\nIsRelative=1\nPath=b.x\nDefault=1\n"; assert.equal(defaultProfileFromIni("/r", read), "/r/b.x"); files["/r/profiles.ini"] += "\n[Install4F96]\nDefault=a.default\nLocked=1\n"; assert.equal(defaultProfileFromIni("/r", read), "/r/a.default"); files["/r/installs.ini"] = "[4F96D1932A9F858E]\nDefault=c.release\n"; assert.equal(defaultProfileFromIni("/r", read), "/r/c.release"); assert.equal(defaultProfileFromIni("/r", () => "[Profile0]\nIsRelative=0\nPath=/abs/p\n"), "/abs/p"); assert.equal(defaultProfileFromIni("/r", () => null), null); }); test("yt-dlp's profile: the newest cookies.sqlite; a named one; else the ini's default", () => { const { home, root } = fakeHome( { "old.default": { version: "107.0.1", cookiesAgeS: 86400 * 400 }, "cur.default-release": { version: "158.0", cookiesAgeS: 60 }, "rec.Recording": { version: "149.0.2", cookiesAgeS: 86400 * 30 }, }, ); assert.equal(resolveFirefoxProfile(undefined, { home, ...LINUX }), path.join(root, "cur.default-release")); assert.equal(resolveFirefoxProfile("rec.Recording", { home, ...LINUX }), path.join(root, "rec.Recording")); assert.equal(resolveFirefoxProfile(path.join(root, "old.default"), { home, ...LINUX }), path.join(root, "old.default")); assert.equal(resolveFirefoxProfile("missing", { home, ...LINUX }), null); const noCookies = fakeHome( { "a.default": { version: "150.0" }, "b.main": { version: "158.0" } }, { installs: "[ABC]\nDefault=b.main\n" }, ); assert.equal(resolveFirefoxProfile(undefined, { home: noCookies.home, ...LINUX }), path.join(noCookies.root, "b.main")); }); test("derived from the profile; re-read when compatibility.ini changes", () => { const { home, root } = fakeHome({ "cur.default": { version: "158.0", cookiesAgeS: 10 } }); let now = 1_000_000; const deps = { home, ...LINUX, arch: "x64", now: () => now, firefoxVersion: noBinary }; const a = deriveFirefoxUserAgent(undefined, deps); assert.equal(a?.source, "profile"); assert.equal(a?.userAgent, "Mozilla/5.0 (X11; Linux x86_64; rv:158.0) Gecko/20100101 Firefox/158.0"); // Firefox updated: it rewrote compatibility.ini (a new mtime) — picked up within the TTL. const f = path.join(root, "cur.default", "compatibility.ini"); writeFileSync(f, compat("159.0")); const t = Date.now() / 1000 + 5; utimesSync(f, t, t); now += 1_000; assert.equal(deriveFirefoxUserAgent(undefined, deps)?.version, "159.0"); // After the TTL the profile is resolved again (a new profile's cookies are newer). mkdirSync(path.join(root, "new.default")); writeFileSync(path.join(root, "new.default", "compatibility.ini"), compat("160.0")); writeFileSync(path.join(root, "new.default", "cookies.sqlite"), ""); utimesSync(path.join(root, "new.default", "cookies.sqlite"), t + 10, t + 10); assert.equal(deriveFirefoxUserAgent(undefined, deps)?.version, "159.0", "within the TTL: the same profile"); now += PROFILE_TTL_MS; assert.equal(deriveFirefoxUserAgent(undefined, deps)?.version, "160.0"); }); test("no version in the profile: `firefox --version`, else the logged fallback", () => { const { home } = fakeHome({ "x.default": { version: null, cookiesAgeS: 10 } }); const bin = deriveFirefoxUserAgent(undefined, { home, ...LINUX, arch: "x64", firefoxVersion: () => "157.0" }); assert.equal(bin?.source, "binary"); assert.equal(bin?.version, "157.0"); const other = fakeHome({ "y.default": { version: null, cookiesAgeS: 10 } }); const logs: string[] = []; setFirefoxUserAgentForTests(undefined); // clears the once-only warning const fb = deriveFirefoxUserAgent(undefined, { home: other.home, ...LINUX, arch: "x64", firefoxVersion: noBinary, log: (m) => logs.push(m), }); assert.equal(fb?.source, "fallback"); assert.equal(fb?.version, FALLBACK_FIREFOX_VERSION); assert.equal(logs.length, 1); assert.match(logs[0], /FALLBACK_FIREFOX_VERSION/); }); test("no Firefox at all (a container): null, and callers that need a browser get their fallback", () => { const home = mkdtempSync(path.join(os.tmpdir(), "ff-none-")); assert.equal(deriveFirefoxUserAgent(undefined, { home, ...LINUX, firefoxVersion: noBinary }), null); // The binary alone is enough (a Firefox that has never been opened). const home2 = mkdtempSync(path.join(os.tmpdir(), "ff-bin-")); assert.equal( deriveFirefoxUserAgent(undefined, { home: home2, ...LINUX, arch: "x64", firefoxVersion: () => "158.0" })?.source, "binary", ); setFirefoxUserAgentForTests(null); try { assert.equal(firefoxUserAgent("firefox"), null); assert.equal(browserUserAgentOr("UA-FALLBACK"), "UA-FALLBACK"); } finally { setFirefoxUserAgentForTests(undefined); } setFirefoxUserAgentForTests("UA-X"); try { assert.equal(firefoxUserAgent(), "UA-X"); assert.equal(browserUserAgentOr("UA-FALLBACK"), "UA-X"); } finally { setFirefoxUserAgentForTests(undefined); } });