// `umtool library` (lane posts-library): the page's calls from a shell. A fake // editor (no socket), a temp reports root. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { libraryCommand } from "./cli.mjs"; function harness(answers = {}) { const asked = []; const lines = []; const errors = []; const ask = async (route, init = {}) => { asked.push({ route, ...init }); if (route === "/api/ops/trending") return { ok: true, libraries: [{ slug: "lib", name: "Lib", topics: 1, posts: 3, unjudged: 2 }] }; if (route.startsWith("/api/ops/trending?")) { const sp = new URL(route, "http://x").searchParams; if (sp.get("ids") === "1") return { ok: true, ids: answers.ids ?? ["11", "12"], posts: [], total: 2, topics: [] }; return { ok: true, total: 1, topics: [{ id: "kettles", title: "Kettles", posts: 3, counts: { unjudged: 2, keep: 0, drop: 1, later: 0, gone: 1 }, ...(sp.get("seen") ? { fresh: Object.keys(JSON.parse(sp.get("seen"))).length ? 2 : 3 } : {}), watch: { on: true, sentence: "Watched every 30 min." }, sources: answers.sources ?? [] }], posts: [{ id: "11", author: "teapot_news", text: "The kettle\nfestival opens.", thread: "11", gone: { at: "2026-11-17T11:00:00.000Z" }, capture: { state: "captured", at: "2026-11-17T10:05:00.000Z" } }], }; } if (route === "/api/ops/judge-trending") return { ok: true, written: init.body.set.map((s) => ({ ...s })), skipped: answers.skipped ?? [], kept: init.body.set.filter((s) => s.state === "keep").map((s) => s.id), ...(init.body.archive === false ? {} : { archive: { jobId: "cap-1", ids: ["11"] } }) }; if (route === "/api/ops/watch-trending") { if (answers.watchRefused) throw new Error(answers.watchRefused); return { ok: true, sentence: "Watched every 45 min." }; } if (route === "/api/ops/purge-trending") return init.body.dryRun ? { ok: true, dryRun: true, posts: 1, gone: [{ id: "11", at: "2026-11-17T11:00:00.000Z" }], agentDropped: 2, confirm: "FAKE-TEST-VALUE", sentence: "Would remove the capture of 1 dropped post: 3 files, 204 B. The judgements stay." } : { ok: true, dryRun: false, posts: 1, sentence: "Removed the capture of 1 dropped post: 3 files, 204 B. The judgements stay." }; throw new Error(`unexpected ${route}`); }; return { asked, lines, errors, deps: { ask, out: (s) => lines.push(s), err: (s) => errors.push(s) }, posts: () => asked.filter((a) => a.method === "POST") }; } async function withRoot(fn) { const root = await mkdtemp(path.join(tmpdir(), "umtool-library-cli-")); try { return await fn(root); } finally { await rm(root, { recursive: true, force: true }); } } test("ls, topics and posts read through the editor; --json prints the answer", async () => { await withRoot(async (root) => { const h = harness(); assert.equal(await libraryCommand([], { ...h.deps, reportsRoot: root }), 0); assert.equal(h.lines[0], "lib Lib 1 topics · 3 posts · 2 unjudged"); assert.equal(await libraryCommand(["topics", "lib"], { ...h.deps, reportsRoot: root }), 0); assert.equal(h.lines[1], "kettles Kettles 3 posts · 2 unjudged · 3 new\n Watched every 30 min."); assert.equal(await libraryCommand(["posts", "lib", "--topic", "kettles", "--state", "unjudged", "--gone", "--media", "--author", "teapot_news", "--q", "kettle lid", "--limit", "5", "--offset", "10"], { ...h.deps, reportsRoot: root }), 0); assert.equal(h.asked.at(-1).route, "/api/ops/trending?slug=lib&topic=kettles&limit=5&offset=10&state=unjudged&gone=1&media=1&author=teapot_news&q=kettle+lid&detail=1"); // Gone from the page, in those words; the capture's state verbatim. assert.match(h.lines[2], /^1 post\n11 {2}@teapot_news {2}unjudged · gone from the page at 2026-11-17T11:00:00\.000Z · capture: captured at 2026-11-17T10:05:00\.000Z\n {4}The kettle festival opens\.$/); assert.equal(await libraryCommand(["posts", "lib", "--json"], { ...h.deps, reportsRoot: root }), 0); assert.equal(JSON.parse(h.lines[3]).posts[0].id, "11"); }); }); test("judge: an agent by default, the operator when said; ids, threads, and a filter that asks first", async () => { await withRoot(async (root) => { const h = harness(); const deps = { ...h.deps, reportsRoot: root }; assert.equal(await libraryCommand(["judge", "lib", "--state", "keep", "--id", "11,12", "--note", "the opening"], deps), 0); assert.deepEqual(h.posts()[0].body, { slug: "lib", by: "agent", set: [{ id: "11", state: "keep", note: "the opening" }, { id: "12", state: "keep", note: "the opening" }] }); assert.match(h.lines[0], /^2 judgements written as the agent's\.\n1 post queued for archiving \(the editor's capture job cap-1\)\.$/); assert.equal(await libraryCommand(["judge", "lib", "--state", "drop", "--thread", "11", "--by", "operator", "--no-archive"], deps), 0); assert.deepEqual(h.posts()[1].body, { slug: "lib", by: "operator", set: [{ thread: "11", state: "drop" }], archive: false }); // --filter: a count first (exit 1 without --yes, 0 with --dry-run), then one judge ask. assert.equal(await libraryCommand(["judge", "lib", "--state", "later", "--filter", "--topic", "kettles", "--media"], deps), 1); assert.equal(h.lines.at(-1), "2 posts would be judged later (--yes judges them)."); assert.equal(await libraryCommand(["judge", "lib", "--state", "later", "--filter", "--topic", "kettles", "--dry-run", "--json"], deps), 0); assert.deepEqual(JSON.parse(h.lines.at(-1)), { slug: "lib", dryRun: true, state: "later", posts: 2, ids: ["11", "12"] }); assert.equal(h.posts().length, 2); assert.equal(await libraryCommand(["judge", "lib", "--state", "later", "--filter", "--where", "unjudged", "--topic", "kettles", "--media", "--yes"], deps), 0); assert.equal(h.asked.at(-2).route, "/api/ops/trending?slug=lib&topic=kettles&state=unjudged&media=1&ids=1"); assert.deepEqual(h.posts()[2].body.set, [{ id: "11", state: "later" }, { id: "12", state: "later" }]); for (const [args, re] of [ [["judge", "lib", "--state", "deleted", "--id", "11"], /--state is one of keep, drop, later, undecided/], [["judge", "lib", "--state", "keep"], /--id, --thread or --filter, one of them/], [["judge", "lib", "--state", "keep", "--id", "11", "--thread", "11"], /one of them/], [["judge", "lib", "--state", "keep", "--id", "11", "--by", "me"], /--by is "agent" or "operator"/], [["judge", "--state", "keep", "--id", "11"], /name the library/], [["posts", "lib", "--new"], /--new goes with --topic/], [["posts", "lib", "--nope"], /unknown flag --nope/], [["frobnicate", "lib"], /unknown verb "frobnicate"/], ]) { const before = h.posts().length; assert.equal(await libraryCommand(args, deps), 1); assert.match(h.errors.at(-1), re); assert.equal(h.posts().length, before); } }); }); test("watch and purge: the switch with its terms; the editor's refusal is exit 1 in its words; the purge is a dry run, then --confirm", async () => { await withRoot(async (root) => { const h = harness(); const deps = { ...h.deps, reportsRoot: root }; assert.equal(await libraryCommand(["watch", "lib", "kettles", "on", "--interval", "45", "--hours=24", "--shots"], deps), 0); assert.deepEqual(h.posts()[0].body, { slug: "lib", topic: "kettles", on: true, intervalMinutes: 45, hours: 24, shots: true, by: "agent" }); assert.equal(h.lines[0], "Watched every 45 min."); assert.equal(await libraryCommand(["watch", "lib", "kettles", "off", "--interval", "45", "--by", "operator"], deps), 0); assert.deepEqual(h.posts()[1].body, { slug: "lib", topic: "kettles", on: false, by: "operator" }); assert.equal(await libraryCommand(["watch", "lib", "kettles"], deps), 1); const refused = harness({ watchRefused: "This watch is paused as failing: only the operator switches it on again." }); assert.equal(await libraryCommand(["watch", "lib", "kettles", "on"], { ...refused.deps, reportsRoot: root }), 1); assert.equal(refused.errors[0], "This watch is paused as failing: only the operator switches it on again."); assert.equal(await libraryCommand(["purge", "lib", "--topic", "kettles", "--dry-run"], deps), 0); assert.deepEqual(h.posts().at(-1).body, { slug: "lib", topic: "kettles", dryRun: true, by: "agent" }); // The same numbers the page and `pnpm ops` give: to remove, gone from the page, an agent's left alone. assert.match(h.lines.at(-1), /^Would remove the capture of 1 dropped post: 3 files, 204 B\. The judgements stay\.\n1 to remove · 1 of them gone from the page · 2 dropped by an agent, left alone\nconfirm: FAKE-TEST-VALUE {3}\(umtool library purge lib --topic kettles --confirm FAKE-TEST-VALUE --by operator\)$/); assert.equal(await libraryCommand(["purge", "lib"], deps), 1); assert.match(h.errors.at(-1), /--dry-run says what would be removed/); assert.equal(await libraryCommand(["purge", "lib", "--topic", "kettles", "--confirm", "FAKE-TEST-VALUE", "--by", "operator"], deps), 0); assert.deepEqual(h.posts().at(-1).body, { slug: "lib", topic: "kettles", confirm: "FAKE-TEST-VALUE", by: "operator" }); }); }); test("topics says X's own title and summary of each page, and the tab the last read saw", async () => { await withRoot(async (root) => { const h = harness({ sources: [ { trendId: "1", url: "https://x.com/i/trending/1", title: "Kettle festival opens", summary: "The festival opened this morning.", tabs: ["Top", "Latest"], lastReadTab: "Top", reads: 1, lastReadAt: "2026-11-17T10:00:00.000Z" }, { trendId: "2", url: "https://x.com/i/trending/2", reads: 1, lastReadAt: "2026-11-17T10:00:00.000Z" }, ], }); assert.equal(await libraryCommand(["topics", "lib"], { ...h.deps, reportsRoot: root }), 0); assert.equal( h.lines[0], [ "kettles Kettles 3 posts · 2 unjudged · 3 new", " X's title: Kettle festival opens", " X's summary: The festival opened this morning.", ' The last read saw the "Top" tab of "Top", "Latest".', " Watched every 30 min.", ].join("\n"), ); }); }); test("topics cuts a long summary of X's at 600 characters", async () => { await withRoot(async (root) => { const h = harness({ sources: [{ trendId: "1", url: "https://x.com/i/trending/1", summary: "s".repeat(900), reads: 1, lastReadAt: null }] }); assert.equal(await libraryCommand(["topics", "lib"], { ...h.deps, reportsRoot: root }), 0); const said = h.lines[0].split("\n").find((l) => l.includes("X's summary: ")).replace(" X's summary: ", ""); assert.equal(said, `${"s".repeat(599)}…`); }); }); test("seen marks topics in umtool's own state, and --new counts from it", async () => { await withRoot(async (root) => { const h = harness(); const deps = { ...h.deps, reportsRoot: root }; // Mark seen is the operator's: an agent (the default) is refused, and nothing is written. assert.equal(await libraryCommand(["seen", "lib", "--all"], deps), 1); assert.match(h.errors.at(-1), /Mark seen is the operator's .*say --by operator\. An agent does not mark topics seen\./); assert.equal(await libraryCommand(["seen", "lib", "kettles", "--by", "agent"], deps), 1); assert.equal(await libraryCommand(["posts", "lib", "--topic", "kettles", "--new"], deps), 0); assert.doesNotMatch(h.asked.at(-1).route, /since=/); // A topic the library does not hold: refused, nothing marked. assert.equal(await libraryCommand(["seen", "lib", "spoons", "--by", "operator"], deps), 1); assert.match(h.errors.at(-1), /lib has no topic "spoons": nothing was marked/); assert.equal(await libraryCommand(["seen", "lib", "--by", "operator"], deps), 1); assert.equal(await libraryCommand(["seen", "lib", "--all", "--by", "operator", "--json"], deps), 0); const marked = JSON.parse(h.lines.at(-1)); assert.ok(Date.parse(marked.topics.kettles.seenAt) > 0); assert.equal(h.posts().length, 0); assert.equal(await libraryCommand(["posts", "lib", "--topic", "kettles", "--new"], deps), 0); assert.match(h.asked.at(-1).route, new RegExp(`since=${encodeURIComponent(marked.topics.kettles.seenAt).replace(/\./g, "\\.")}`)); // topics: ONE read, the marks riding it. const before = h.asked.length; assert.equal(await libraryCommand(["topics", "lib"], deps), 0); assert.match(h.lines.at(-1), /2 unjudged · 2 new/); assert.equal(h.asked.length, before + 1); assert.match(h.asked.at(-1).route, /detail=1/); assert.match(h.asked.at(-1).route, /seen=%7B%22kettles%22/); }); });