// The e2e suite's fake "Refresh cookies" browser (fakeXProfileBrowser.ts) is // inert unless the process is the test server AND the file is inside its // transcriptsDir AND exists; when live, refreshXCookies runs on it end to end // with no browser and no request. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test social/fakeXProfileBrowser.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, mkdir, readFile, rm, symlink, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { fakeXProfileChromium, fakeXProfileCookiesFile } from "./fakeXProfileBrowser"; import { clearXSession, connectXAccount, readXSessionStatus, refreshXCookies } from "./xSessionBroker"; test("the gate: test server, inside transcriptsDir, the file exists", async () => { const root = await mkdtemp(path.join(os.tmpdir(), "fake-x-")); try { const t = path.join(root, "transcripts"); await mkdir(t); const inside = path.join(t, ".fake-x-profile-cookies.json"); const outside = path.join(root, "elsewhere.json"); await writeFile(outside, "{}"); const env = { E2E_TEST_ROUTES: "1", E2E_FAKE_X_PROFILE_COOKIES_FILE: inside }; // Not written yet. assert.equal(fakeXProfileCookiesFile(t, env), null); await writeFile(inside, "{}"); assert.equal(fakeXProfileCookiesFile(t, env), inside); // Not the test server: inert, even with the file there. assert.equal(fakeXProfileCookiesFile(t, { E2E_FAKE_X_PROFILE_COOKIES_FILE: inside }), null); assert.equal(fakeXProfileCookiesFile(t, { ...env, E2E_TEST_ROUTES: "0" }), null); assert.equal(fakeXProfileChromium(t, { E2E_FAKE_X_PROFILE_COOKIES_FILE: inside }), null); // A file outside the test data: inert. assert.equal(fakeXProfileCookiesFile(t, { ...env, E2E_FAKE_X_PROFILE_COOKIES_FILE: outside }), null); assert.equal(fakeXProfileCookiesFile(t, { ...env, E2E_FAKE_X_PROFILE_COOKIES_FILE: path.join(t, "..", "elsewhere.json") }), null); assert.equal(fakeXProfileCookiesFile(t, { E2E_TEST_ROUTES: "1" }), null); // A symlink inside the test data pointing out of it: inert. const link = path.join(t, "link.json"); await symlink(outside, link); assert.equal(fakeXProfileCookiesFile(t, { ...env, E2E_FAKE_X_PROFILE_COOKIES_FILE: link }), null); } finally { await rm(root, { recursive: true, force: true }); } }); test("refreshXCookies on the fake writes the jar from the fixture's cookies", async () => { const root = await mkdtemp(path.join(os.tmpdir(), "fake-x-")); try { const paths = { transcriptsDir: root, autoQueueStateFile: path.join(root, ".auto-queue", "state.json") } as Parameters[0]; await mkdir(path.join(root, ".x-session", "profile"), { recursive: true }); const file = path.join(root, ".fake-x-profile-cookies.json"); await writeFile( file, JSON.stringify({ cookies: [{ name: "auth_token", value: "fixture-not-a-token", domain: ".x.com" }] }), ); const chromium = fakeXProfileChromium(root, { E2E_TEST_ROUTES: "1", E2E_FAKE_X_PROFILE_COOKIES_FILE: file }); assert.ok(chromium); const status = await refreshXCookies(paths, { chromium }); assert.equal(status.looksAuthenticated, true); assert.equal((await readXSessionStatus(paths)).hasCookies, true); assert.match(await readFile(path.join(root, ".x-session", "cookies.txt"), "utf8"), /\.x\.com\tTRUE\t\/\tTRUE\t0\tauth_token/); // `fail` makes the launch throw, as a profile the browser cannot open. await writeFile(file, JSON.stringify({ fail: "profile locked" })); await assert.rejects(refreshXCookies(paths, { chromium }), /profile locked/); } finally { await rm(root, { recursive: true, force: true }); } }); test("connectXAccount on the fake: the window closes by itself, the login is exported; clearXSession forgets it", async () => { const root = await mkdtemp(path.join(os.tmpdir(), "fake-x-")); try { const paths = { transcriptsDir: root, autoQueueStateFile: path.join(root, ".auto-queue", "state.json") } as Parameters[0]; const file = path.join(root, ".fake-x-profile-cookies.json"); await writeFile( file, JSON.stringify({ closeAfterMs: 50, cookies: [{ name: "auth_token", value: "fixture-not-a-token", domain: ".x.com" }] }), ); const chromium = fakeXProfileChromium(root, { E2E_TEST_ROUTES: "1", E2E_FAKE_X_PROFILE_COOKIES_FILE: file }); assert.ok(chromium); // No system browser looked up: an empty PATH is the bundled build. const status = await connectXAccount(paths, { chromium, env: { PATH: "" } }); assert.equal(status.looksAuthenticated, true); assert.equal(status.hasProfile, true); await clearXSession(paths); const after = await readXSessionStatus(paths); assert.equal(after.hasProfile, false); assert.equal(after.hasCookies, false); } finally { await rm(root, { recursive: true, force: true }); } });