import test from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/x-session/route.test.ts" // // Lane x-sittings: the operator marks the X account suspended (or ok) through // POST /api/ops/x-session, and GET reports it with X's sitting. Temp dirs // only; nothing reaches X. const ROOT = await mkdtemp(path.join(os.tmpdir(), "x-session-route-")); process.env.WORKER_TOKEN = "test-token"; process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); await writeFile(process.env.SETTINGS_FILE, JSON.stringify({ social: { x: { cookieSource: "profile" } } })); const { POST, GET } = await import("./route"); test.after(() => rm(ROOT, { recursive: true, force: true })); const AUTH = { authorization: "Bearer test-token" }; async function post(body: Record) { const res = await POST( new Request("http://localhost/api/ops/x-session", { method: "POST", headers: { ...AUTH, "content-type": "application/json" }, body: JSON.stringify(body), }), ); return { status: res.status, body: (await res.json()) as { ok: boolean; error?: string; account?: { state: string; note?: string } } }; } async function get() { const res = await GET(new Request("http://localhost/api/ops/x-session", { headers: AUTH })); return (await res.json()) as { account: { state: string; alert?: string }; sitting: { used: number; limit: number; left: number } }; } test("POST marks the account suspended with a note, GET shows it as the account's alert, and ok lifts it", async () => { assert.equal((await get()).account.state, "ok"); const marked = await post({ accountState: "suspended", note: "read-only since the 21st" }); assert.equal(marked.status, 200, marked.body.error); assert.deepEqual([marked.body.account?.state, marked.body.account?.note], ["suspended", "read-only since the 21st"]); const file = JSON.parse(await readFile(path.join(ROOT, ".x-session", "account.json"), "utf8")); assert.equal(file.state, "suspended"); const after = await get(); assert.equal(after.account.state, "suspended"); assert.match(after.account.alert ?? "", /^The X account is marked suspended .*read-only since the 21st/); // The sitting is reported beside it: the default four, none used. assert.deepEqual([after.sitting.used, after.sitting.limit, after.sitting.left], [0, 4, 4]); assert.equal((await post({ accountState: "ok" })).status, 200); assert.equal((await get()).account.state, "ok"); }); test("POST refuses a state it does not know, a stray key and a note that is not a string", async () => { for (const body of [{ accountState: "banned" }, {}, { accountState: "ok", when: "now" }, { accountState: "ok", note: 3 }]) { const r = await post(body); assert.equal(r.status, 400, JSON.stringify(body)); assert.equal(r.body.ok, false); } }); test("POST clearStop: unconfirmed answers 409 with what it risks; confirmed clears it", async () => { const { recordXStop, xStopRemainingMs } = await import("yt-dlp-transcript-common/jobs/downloadBackoff"); const { getPaths } = await import("yt-dlp-transcript-common/lib/paths"); await recordXStop("a rate limit", getPaths()); const asked = await post({ clearStop: true }); assert.equal(asked.status, 409); assert.match(asked.body.error ?? "", /^Clearing the X stop lets the next X job ask X at once/); assert.ok((await xStopRemainingMs(getPaths())) > 0); const done = await post({ clearStop: true, confirm: true }); assert.equal(done.status, 200); assert.equal(await xStopRemainingMs(getPaths()), 0); });