import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, 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/watch-trending/route.test.ts" // // watch-trending over ops (lane x-trending-2), against a temp corpus with no // connected X profile: the switch, the library's settings, the listing's // watch, and ONE SCAN that — there being no profile — pauses the watch // instead of starting a read. No browser opens and nothing is asked of X. const ROOT = await mkdtemp(path.join(os.tmpdir(), "x-trending-watch-route-")); process.env.WORKER_TOKEN = "test-token"; process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); const READ = "tr-01JA0000000000000000000000"; const TREND = "https://x.com/i/trending/1900000000000000001"; async function channel(slug: string, config: Record): Promise { const dir = path.join(ROOT, "channels", slug); await mkdir(path.join(dir, "trending", "posts"), { recursive: true }); await writeFile(path.join(dir, "config.json"), JSON.stringify({ handling: "transcribe", name: slug, ...config })); return dir; } const lib = await channel("trending", { sourceKind: "social", platform: "twitter", xTrending: { maxPosts: 30 } }); await channel("account-x", { sourceKind: "social", platform: "twitter", postFetcher: "x-gallery-dl", socialHandle: "example", url: "https://x.com/example" }); await writeFile( path.join(lib, "trending", "topics.json"), JSON.stringify({ version: 1, topics: [ { id: "kettles", title: "Kettles", createdAt: "2026-11-17T10:00:00.000Z", sources: [{ trendId: "1900000000000000001", url: TREND, reads: [{ id: READ, at: "2026-11-17T10:00:00.000Z", posts: 2, added: 2 }] }] }, { id: "empty", title: "Empty", createdAt: "2026-11-17T10:00:00.000Z", sources: [] }, ], }), ); const watch = (await import("./route")).POST; const listing = (await import("../trending/route")).GET; test.after(() => rm(ROOT, { recursive: true, force: true })); async function call(body: Record) { const res = await watch( new Request("http://localhost/api/ops/watch-trending", { method: "POST", headers: { authorization: "Bearer test-token", "content-type": "application/json" }, body: JSON.stringify(body), }), ); return { status: res.status, json: (await res.json()) as Record }; } const topics = async () => { const res = await listing(new Request("http://localhost/api/ops/trending?slug=trending", { headers: { authorization: "Bearer test-token" } })); return ((await res.json()) as { topics: Array<{ id: string; watch?: Record }> }).topics; }; test("watch-trending refuses with a sentence", async () => { for (const [body, re] of [ [{ slug: "nope", topic: "kettles", on: true }, /No such channel: nope/], [{ slug: "account-x", topic: "kettles", on: true }, /not an X trending library/], [{ slug: "trending" }, /Give "topic" with "on".*one of them/], [{ slug: "trending", topic: "kettles", on: true, tick: true }, /one of them/], [{ slug: "trending", topic: "kettles" }, /"on" is true \(watch the topic\) or false/], [{ slug: "trending", topic: "spouts", on: true }, /no topic "spouts" \(its topics: kettles, empty\)/], [{ slug: "trending", topic: "empty", on: true }, /holds no trending page to watch/], [{ slug: "trending", topic: "kettles", on: true, intervalMinutes: 5 }, /"intervalMinutes" is a whole number, 15–1440/], [{ slug: "trending", topic: "kettles", on: true, hours: 1000 }, /"hours" is a whole number, 1–168/], [{ slug: "trending", topic: "kettles", on: false }, /is not being watched/], [{ slug: "trending", settings: { maxVideoMb: 0 } }, /"maxVideoMb" is a whole number, 1–2000/], [{ slug: "trending", settings: { colour: 1 } }, /unknown setting\(s\): colour/], [{ slug: "trending", settings: {} }, /names nothing to change/], [{ slug: "trending", topic: "kettles", on: true, force: true }, /unknown key\(s\): force/], ] as const) { const r = await call(body); assert.equal(r.status, 400, JSON.stringify(body)); assert.match(String(r.json.error), re, JSON.stringify(body)); } }); test("the library's settings are written into config.xTrending, the marker and its other keys kept", async () => { const r = await call({ slug: "trending", settings: { maxVideoMb: 50, intervalMinutes: 20, hours: 6, maxTopics: 1 } }); assert.equal(r.status, 200); const config = JSON.parse(await readFile(path.join(lib, "config.json"), "utf8")); assert.deepEqual(config.xTrending, { maxPosts: 30, maxVideoMb: 50, watchIntervalMinutes: 20, watchHours: 6, watchMaxTopics: 1 }); assert.equal(config.platform, "twitter"); }); test("a topic is watched on the library's terms; the listing shows it; a scan with no connected profile pauses it and starts nothing", async () => { const on = await call({ slug: "trending", topic: "Kettles", on: true, shots: true }); assert.equal(on.status, 200, JSON.stringify(on.json)); assert.equal(on.json.topic, "kettles"); const w = on.json.watch as { on: boolean; intervalMinutes: number; shots: boolean; until: string; startedAt: string }; assert.deepEqual([w.on, w.intervalMinutes, w.shots], [true, 20, true]); assert.equal(Date.parse(w.until) - Date.parse(w.startedAt), 6 * 3_600_000); assert.match(String(on.json.sentence), /^Watched every 20 min until .* UTC, screenshotting every new post; 0 ticks so far/); assert.equal((await topics()).find((t) => t.id === "kettles")?.watch?.on, true); assert.equal((await topics()).find((t) => t.id === "empty")?.watch, undefined); const tick = await call({ slug: "trending", tick: true }); assert.equal(tick.status, 200, JSON.stringify(tick.json)); const scan = tick.json.scan as { live: number; enqueued?: unknown; paused: Array<{ topic: string; reason: string }> }; assert.equal(scan.enqueued, undefined); assert.deepEqual(scan.paused, [{ slug: "trending", topic: "kettles", reason: "no-profile" }]); const paused = (await topics()).find((t) => t.id === "kettles")?.watch as { paused?: { reason: string }; sentence: string }; assert.equal(paused.paused?.reason, "no-profile"); assert.match(paused.sentence, /PAUSED since .* UTC: There is no connected X profile/); const off = await call({ slug: "trending", topic: "kettles", on: false, by: "operator" }); assert.equal(off.status, 200); assert.equal(off.json.sentence, 'The watch of "Kettles" was switched off.'); }); // Review 1 S6. test("S6: a scan asked for by hand is refused on an idle boot; a settings change and the switch are not", async () => { process.env.ARCHILYZER_IDLE_BOOT = "1"; try { const tick = await call({ slug: "trending", tick: true }); assert.equal(tick.status, 400); assert.match(String(tick.json.error), /booted idle \(ARCHILYZER_IDLE_BOOT\): it starts no work, so no watch scan runs/); assert.equal((await call({ slug: "trending", settings: { maxTopics: 2 } })).status, 200); } finally { delete process.env.ARCHILYZER_IDLE_BOOT; } }); // Review 1 M3. test("M3: the scan counts a trending job of ANY library as waiting — and no other channel's capture", async () => { const { xTrendingJobWaiting } = await import("../_xTrendingWatch"); const isLibrary = (slug: string) => slug === "trending" || slug === "other-library"; const job = (kind: string, status: string, channelSlug?: string) => ({ kind, status, ...(channelSlug ? { channelSlug } : {}) }); assert.equal(xTrendingJobWaiting([], isLibrary), false); assert.equal(xTrendingJobWaiting([job("read-x-trending", "queued", "other-library")], isLibrary), true); assert.equal(xTrendingJobWaiting([job("read-x-trending", "running", "trending")], isLibrary), true); assert.equal(xTrendingJobWaiting([job("reparse-x-trending", "queued", "other-library")], isLibrary), true); assert.equal(xTrendingJobWaiting([job("capture-posts", "queued", "other-library")], isLibrary), true); // An ordinary X channel's capture is not the watch's business; nor is a finished job. assert.equal(xTrendingJobWaiting([job("capture-posts", "running", "account-x")], isLibrary), false); assert.equal(xTrendingJobWaiting([job("read-x-trending", "done", "trending"), job("capture-posts", "failed", "trending")], isLibrary), false); });