// THE X RUN CONSOLE (/review, lane x-console), end to end. // // The 8–9 Nov case in miniature: X fetch-posts for several channels, one after // another, until the cookie jar passes social.x.maxCookieAgeMinutes — then the // rest wait for a refresh. One x-run job does it: the console picks and orders // the channels, the run fetches each in turn (the fake gallery-dl: X is NEVER // contacted), the jar is read before each channel, and once it is stale the // remaining channel is deferred. The operator refreshes the jar (written by // hand here — no X, no browser) and Resume runs the deferred channel. // // The fake gallery-dl ages the jar it is handed for the account // `jar_ages_user` (two hours old once its fetch ends): time passing mid-run, // deterministically. The test server turns the built-in gap and age limit off // (E2E_JOB_GAP_SECONDS=0, E2E_X_MAX_COOKIE_AGE_MINUTES=0); this spec sets the // age limit itself. import { mkdir, utimes, writeFile } from "node:fs/promises"; import path from "node:path"; import { test, expect, type Page } from "@playwright/test"; import { pathExists, resetData, resolvePath, testPaths, writeSettings } from "./helpers"; import { baseUrl } from "./baseUrl"; const AUTH = { authorization: "Bearer test-worker-token" }; async function xChannel(slug: string, handle: string): Promise { const dir = resolvePath(`${testPaths().transcriptsDir}/channels/${slug}`); await mkdir(dir, { recursive: true }); await writeFile( `${dir}/config.json`, JSON.stringify({ name: `${handle} (X)`, handling: "transcribe", sourceKind: "social", postFetcher: "x-gallery-dl", platform: "twitter", socialHandle: handle, url: `https://x.com/${handle}`, }), ); } async function xJar(ageMs: number): Promise { const dir = resolvePath(`${testPaths().transcriptsDir}/.x-session`); await mkdir(`${dir}/profile`, { recursive: true }); await writeFile(`${dir}/cookies.txt`, "# Netscape HTTP Cookie File\n.x.com\tTRUE\t/\tTRUE\t0\tauth_token\tfake\n"); const t = new Date(Date.now() - ageMs); await utimes(`${dir}/cookies.txt`, t, t); } const SHOTS = process.env.E2E_UI_OPS_SHOTS ?? ""; async function shot(page: Page, name: string) { if (!SHOTS) return; await mkdir(SHOTS, { recursive: true }); await page.screenshot({ path: path.join(SHOTS, `${name}.png`), fullPage: false }); } const posts = (slug: string) => `${testPaths().transcriptsDir}/channels/${slug}/posts-archive`; test.beforeEach(async () => { await resetData("empty"); await writeSettings({ minFreeDiskGB: 0, social: { x: { cookieSource: "profile", maxCookieAgeMinutes: 60 } }, channelPriority: { focus: { kind: "none" }, channels: { "x-low": { tier: "low" }, "x-paused": { tier: "paused" } } }, }); await xChannel("x-alpha", "alpha_user"); await xChannel("x-beta", "jar_ages_user"); await xChannel("x-gamma", "gamma_user"); await xChannel("x-low", "low_user"); await xChannel("x-paused", "paused_user"); }); test("an X run: picked and ordered, the jar goes stale mid-run, the rest deferred; a refreshed jar and Resume finish it", async ({ page, request, }) => { test.setTimeout(120_000); await page.setViewportSize({ width: 1366, height: 768 }); await xJar(60_000); await page.goto("/review"); const consoleEl = page.getByRole("region", { name: "x run console" }); await expect(consoleEl.getByRole("heading", { name: "X run" })).toBeVisible(); // The default order is the priority tiers': normal (by slug), low, paused; // a paused channel starts unpicked. const picker = consoleEl.getByRole("list", { name: "x run channels" }); await expect(picker.getByRole("listitem")).toHaveText([/x-alpha/, /x-beta/, /x-gamma/, /x-low/, /x-paused/]); await expect(picker.getByRole("checkbox", { name: "run x-paused" })).not.toBeChecked(); await expect(picker.getByRole("checkbox", { name: "run x-alpha" })).toBeChecked(); // Reorder: x-low up above x-gamma, then back down. await picker.getByRole("button", { name: "move x-low up" }).click(); await expect(picker.getByRole("listitem")).toHaveText([/x-alpha/, /x-beta/, /x-low/, /x-gamma/, /x-paused/]); await picker.getByRole("button", { name: "move x-low down" }).click(); await expect(picker.getByRole("listitem")).toHaveText([/x-alpha/, /x-beta/, /x-gamma/, /x-low/, /x-paused/]); await picker.getByRole("checkbox", { name: "run x-low" }).uncheck(); // The jar, counted down against the limit. const jar = consoleEl.getByRole("status", { name: "x cookie jar" }); await expect(jar).toHaveAttribute("data-jar", "fresh"); await expect(jar).toContainText(/Cookie jar 1 min old · 5\d:\d\d left of 60 min/); await shot(page, "x-run-console-picker"); await consoleEl.getByRole("button", { name: "Start X run (3)" }).click(); const run = consoleEl.getByLabel("x run", { exact: true }); await expect(run).toHaveAttribute("data-run-status", "deferred", { timeout: 60_000 }); await expect(consoleEl.getByLabel("x run summary")).toHaveText("2 done, 1 deferred until the next refresh"); await expect(run.getByLabel("x run channel x-alpha")).toHaveAttribute("data-status", "done"); await expect(run.getByLabel("x run channel x-alpha")).toContainText("done · 3 posts"); await expect(run.getByLabel("x run channel x-beta")).toHaveAttribute("data-status", "done"); await expect(run.getByLabel("x run channel x-gamma")).toHaveAttribute("data-status", "deferred"); await expect(consoleEl.getByLabel("x run stopped")).toContainText( /X cookies were last refreshed (120 min|2 h) ago \(limit 60 min\)/, ); // The jar the fetch aged is stale now: the countdown says so. await expect(jar).toHaveAttribute("data-jar", "stale"); expect(await pathExists(posts("x-alpha"))).toBe(true); expect(await pathExists(posts("x-gamma"))).toBe(false); await shot(page, "x-run-console-deferred"); // The ops twin reads the same run. const got = await request.get(`${baseUrl}/api/ops/x-run`, { headers: AUTH }); const body = (await got.json()) as { run: { id: string; summary: string; resumable: number; jobIds: string[] } }; expect(body.run.summary).toBe("2 done, 1 deferred until the next refresh"); expect(body.run.resumable).toBe(1); expect(body.run.jobIds).toHaveLength(1); // The operator refreshes the jar; Resume runs the deferred channel. await xJar(0); await expect(jar).toHaveAttribute("data-jar", "fresh", { timeout: 20_000 }); await consoleEl.getByRole("button", { name: "Resume (1 deferred)" }).click(); await expect(run).toHaveAttribute("data-run-status", "done", { timeout: 60_000 }); await expect(consoleEl.getByLabel("x run summary")).toHaveText("3 done"); await expect(run.getByLabel("x run channel x-gamma")).toHaveAttribute("data-status", "done"); expect(await pathExists(posts("x-gamma"))).toBe(true); // x-low was not picked: never fetched. expect(await pathExists(posts("x-low"))).toBe(false); const after = (await (await request.get(`${baseUrl}/api/ops/x-run?run=${body.run.id}`, { headers: AUTH })).json()) as { run: { jobIds: string[] }; }; expect(after.run.jobIds).toHaveLength(2); await shot(page, "x-run-console-done"); }); test("the ops twin: a stale jar refuses a new run before any job, and the console offers its Resume", async ({ page, request, }) => { await xJar(16 * 24 * 60 * 60_000); const res = await request.post(`${baseUrl}/api/ops/x-run`, { headers: AUTH, data: { channels: ["x-alpha", "x-gamma"] }, }); expect(res.status()).toBe(400); const body = (await res.json()) as { error: string; run: string }; expect(body.error).toMatch(/^X cookies were last refreshed 16 days ago \(limit 60 min\)/); expect(body.run).toMatch(/^xr-/); await page.goto("/review"); const consoleEl = page.getByRole("region", { name: "x run console" }); await expect(consoleEl.getByLabel("x run summary")).toHaveText("0 done, 2 deferred until the next refresh"); await expect(consoleEl.getByRole("status", { name: "x cookie jar" })).toHaveAttribute("data-jar", "stale"); // Resume on the same stale jar is refused with the guard's sentence. await consoleEl.getByRole("button", { name: "Resume (2 deferred)" }).click(); await expect(consoleEl.getByRole("alert", { name: "x run error" })).toContainText("X cookies were last refreshed 16 days ago"); });