import { test } from "node:test"; import assert from "node:assert/strict"; import { connectWith, fakeEditor, isError, textOf } from "./editorOps.testkit"; // ─── x_trending_topics / x_trending_posts (lane x-trending), through tools/call ─── // // One GET on the fake editor (/api/ops/trending?slug=…); what is pinned is the // request sent and the words the agent reads. Invented ids, handles and words. const EDITOR = "http://editor.test"; const READ = "tr-01JA0000000000000000000000"; const TREND = "https://x.com/i/trending/1900000000000000001"; const LISTING = { ok: true, slug: "trending", name: "Trending", topics: [ { id: "kettle-festival", title: "Kettle Festival", posts: 2, sources: [ { trendId: "1900000000000000001", url: TREND, title: "Kettle festival opens", summary: "The annual kettle festival opened this morning.", lastUpdatedAt: "2026-11-16T11:30:00.000Z", tabs: ["Top", "Latest"], lastReadTab: "Top", reads: 2, lastReadAt: "2026-11-17T12:00:00.000Z", }, ], }, ], posts: [ { id: "1900000000000001001", author: "teapot_news", authorName: "Teapot News", createdAt: "2026-11-16T12:00:00.000Z", url: "https://x.com/teapot_news/status/1900000000000001001", text: "The kettle festival\nopens today.", media: { count: 2, kinds: ["image"] }, topic: "kettle-festival", reads: [{ read: READ, trendId: "1900000000000000001", at: "2026-11-17T10:00:00.000Z", tab: "Top" }], }, { id: "1900000000000001002", author: "saucer_fan", createdAt: "2026-11-16T13:00:00.000Z", url: "https://x.com/saucer_fan/status/1900000000000001002", text: "Second.", media: { count: 0, kinds: [], thumbnail: "trending/posts-media/1900000000000001002/shot.png" }, topic: "kettle-festival", reads: [{ read: READ, trendId: "1900000000000000001", at: "2026-11-17T10:00:00.000Z" }], judgement: { state: "keep", note: "the reply everyone quotes", by: "agent", at: "2026-11-17T11:00:00.000Z" }, }, ], total: 5, reads: [READ], }; const env = { ARCHILYZER_EDITOR_URL: EDITOR, WORKER_TOKEN: "t" }; // The tool's own words: every answer ends with the server's corpus footer. const body = (res: unknown) => textOf(res as never).split("\n\n(corpus:")[0]; test("x_trending_topics: the library's topics, their pages and reads", async () => { const { deps, sent } = fakeEditor(() => ({ status: 200, body: LISTING }), env); const client = await connectWith(deps); const res = await client.callTool({ name: "x_trending_topics", arguments: { channel: "trending" } }); const url = new URL(sent[0].url); assert.equal(sent[0].method, "GET"); assert.equal(url.pathname, "/api/ops/trending"); assert.equal(url.searchParams.get("slug"), "trending"); assert.equal(url.searchParams.get("limit"), "1"); assert.equal( body(res), [ "X trending library trending: 1 topic, 5 posts (private: on no site, in no index)", '- kettle-festival — "Kettle Festival": 2 posts', ` ${TREND} · read 2×, last 2026-11-17 12:00 UTC · the last read saw the "Top" tab of "Top", "Latest"`, " X's title: Kettle festival opens (X updated it 2026-11-16 11:30 UTC)", " X's summary: The annual kettle festival opened this morning.", `Saved reads (newest first): ${READ}`, ].join("\n"), ); }); test("x_trending_posts: each post's id, author, words, media, reads and topic", async () => { const { deps, sent } = fakeEditor(() => ({ status: 200, body: LISTING }), env); const client = await connectWith(deps); const res = await client.callTool({ name: "x_trending_posts", arguments: { channel: "trending", topic: "kettle-festival", limit: 2 } }); const url = new URL(sent[0].url); assert.equal(url.searchParams.get("slug"), "trending"); assert.equal(url.searchParams.get("topic"), "kettle-festival"); assert.equal(url.searchParams.get("limit"), "2"); assert.equal( body(res), [ "X trending library trending, topic kettle-festival: 2 of 5 posts", "1. 1900000000000001001 @teapot_news (Teapot News) · 2026-11-16 12:00 UTC · 2 media (image) · topic kettle-festival", " https://x.com/teapot_news/status/1900000000000001001", " The kettle festival / opens today.", ` shown by: ${READ} (2026-11-17 10:00 UTC, the "Top" tab)`, "2. 1900000000000001002 @saucer_fan · 2026-11-16 13:00 UTC · no media · topic kettle-festival", " https://x.com/saucer_fan/status/1900000000000001002", " Second.", ` shown by: ${READ} (2026-11-17 10:00 UTC)`, " captured: trending/posts-media/1900000000000001002/shot.png", " judged keep by agent (2026-11-17 11:00 UTC): the reply everyone quotes", "… 3 more (raise limit, or narrow by topic).", ].join("\n"), ); }); test("the two tools refuse a missing channel, a bad topic or limit, and say the editor's own sentence", async () => { const { deps, sent } = fakeEditor(() => ({ status: 400, body: { ok: false, error: 'demo is not an X trending library (create one with platform "x-trending").' } }), env); const client = await connectWith(deps); for (const name of ["x_trending_topics", "x_trending_posts"]) { const none = await client.callTool({ name, arguments: { channel: "" } }); assert.ok(isError(none)); assert.match(textOf(none), /channel is required \(the X trending library's slug\)/); const traversing = await client.callTool({ name, arguments: { channel: "../x" } }); assert.ok(isError(traversing)); } assert.match(textOf(await client.callTool({ name: "x_trending_posts", arguments: { channel: "demo", limit: 501 } })), /limit is a whole number, 1–500/); assert.match(textOf(await client.callTool({ name: "x_trending_posts", arguments: { channel: "demo", topic: " " } })), /topic is a topic's id/); assert.equal(sent.length, 0, "a refused ask sends nothing"); const refused = await client.callTool({ name: "x_trending_posts", arguments: { channel: "demo" } }); assert.ok(isError(refused)); assert.match(textOf(refused), /the editor refused \(HTTP 400\): demo is not an X trending library/); }); test("with no editor configured, the tools say what is missing and ask nothing", async () => { const { deps, sent } = fakeEditor(() => ({ status: 200, body: LISTING }), {}); const client = await connectWith(deps); const res = await client.callTool({ name: "x_trending_topics", arguments: { channel: "trending" } }); assert.ok(isError(res)); assert.equal(sent.length, 0); }); // Lane x-trending-2: what a watch, a capture, a thread's judgement and a page // that stopped showing a post look like to the agent. test("the tools say a topic's watch, and a post's thread, capture, gone-from-the-page mark and thread judgement", async () => { const listing = { ...LISTING, topics: [ { ...LISTING.topics[0], watch: { on: true, paused: { reason: "jar" }, sentence: "Watched every 30 min until 2026-11-20 10:00 UTC — PAUSED since 2026-11-18 11:00 UTC: X cookies were last refreshed 61 min ago (limit 60 min)." }, }, ], posts: [ { ...LISTING.posts[0], thread: "1900000000000000999", gone: { at: "2026-11-18T10:30:00.000Z", read: READ, trendId: "1900000000000000001" }, capture: { at: "2026-11-18T10:05:00.000Z", state: "captured", shot: false, files: 1, mediaState: "error", mediaOwed: "kept" }, judgement: { state: "keep", by: "operator", at: "2026-11-18T09:00:00.000Z", from: "thread" }, }, ], total: 1, }; const { deps } = fakeEditor(() => ({ status: 200, body: listing }), env); const client = await connectWith(deps); const topics = body(await client.callTool({ name: "x_trending_topics", arguments: { channel: "trending" } })); assert.match(topics, /^ {4}watch: Watched every 30 min until 2026-11-20 10:00 UTC — PAUSED since 2026-11-18 11:00 UTC: X cookies were last refreshed 61 min ago/m); const posts = body(await client.callTool({ name: "x_trending_posts", arguments: { channel: "trending" } })); assert.deepEqual(posts.split("\n").slice(4), [ ` shown by: ${READ} (2026-11-17 10:00 UTC, the "Top" tab)`, " in thread 1900000000000000999", ` gone from the page at 2026-11-18 10:30 UTC (read ${READ}; the page stopped showing it, which is not "deleted")`, " captured 2026-11-18 10:05 UTC: captured, 1 media file; more media owed once it is kept", " judged keep by operator (its thread) (2026-11-18 09:00 UTC)", ]); }); // Lane x-trending-publish: the library's accounts. test("x_trending_accounts: one GET with accounts=1; handle, posts, kept, topics and who archives it", async () => { const ACCOUNTS = { ok: true, slug: "trending", accounts: [ { handle: "teapot_news", key: "teapot_news", name: "Teapot News", posts: 3, kept: 2, keptByOperator: 1, topics: ["kettle-festival", "cups"], last: "2026-11-16T12:00:00.000Z" }, { handle: "saucer_fan", key: "saucer_fan", posts: 1, kept: 0, keptByOperator: 0, topics: ["cups"], last: "2026-11-16T13:00:00.000Z", archivedAs: ["saucer_fan-x"] }, ], total: 4, posts: 6, more: 2, }; const { deps, sent } = fakeEditor(() => ({ status: 200, body: ACCOUNTS }), env); const client = await connectWith(deps); const res = await client.callTool({ name: "x_trending_accounts", arguments: { channel: "trending", topic: "cups", limit: 2 } }); assert.ok(!isError(res)); assert.equal(sent.length, 1); assert.equal(sent[0].method, "GET"); assert.equal(new URL(sent[0].url).pathname, "/api/ops/trending"); assert.deepEqual([...new URL(sent[0].url).searchParams.entries()], [["slug", "trending"], ["accounts", "1"], ["topic", "cups"], ["limit", "2"]]); const text = body(res); assert.match(text, /^X trending library trending, topic cups: 4 accounts over 6 posts \(2 listed, 2 more\)\./); assert.match(text, /- @teapot_news \(Teapot News\): 3 posts, 2 kept \(1 by the operator\); topics: kettle-festival, cups; newest 2026-11-16 12:00/); assert.match(text, /- @saucer_fan: 1 post; topics: cups; newest 2026-11-16 13:00; already archived as saucer_fan-x/); assert.match(text, /From a library it asks X nothing/); assert.match(textOf(await client.callTool({ name: "x_trending_accounts", arguments: { channel: "" } })), /channel is required/); assert.match(textOf(await client.callTool({ name: "x_trending_accounts", arguments: { channel: "trending", limit: 501 } })), /limit is a whole number, 1–500/); assert.match(textOf(await client.callTool({ name: "x_trending_accounts", arguments: { channel: "trending", topic: " " } })), /topic is a topic's id/); assert.equal(sent.length, 1, "a refused ask sends nothing"); }); test("the MCP has a tool to read a library's accounts and none to organise or publish", async () => { const { deps } = fakeEditor(() => ({ status: 200, body: {} }), env); const client = await connectWith(deps); const names = (await client.listTools()).tools.map((t) => t.name).filter((n) => /trending/.test(n)); assert.deepEqual(names.sort(), ["x_trending_accounts", "x_trending_posts", "x_trending_topics"]); }); // Review 1: X's summary is listed at most 600 characters long. test("x_trending_topics cuts a long summary of X's at 600 characters, at a word", async () => { const summary = Array.from({ length: 150 }, (_, i) => `word${i}`).join(" "); const listing = { ...LISTING, topics: [{ ...LISTING.topics[0], sources: [{ ...LISTING.topics[0].sources[0], summary }] }] }; const { deps } = fakeEditor(() => ({ status: 200, body: listing }), env); const client = await connectWith(deps); const line = body(await client.callTool({ name: "x_trending_topics", arguments: { channel: "trending" } })).split("\n").find((l) => l.includes("X's summary: "))!; const said = line.replace(" X's summary: ", ""); assert.ok(said.length <= 600 && said.endsWith("…"), String(said.length)); assert.ok(summary.startsWith(said.slice(0, -1))); }); // Added scope R3: a read covers both tabs; a post may be gone from one tab only. test("the tools say both tabs of a two-tab read, and a post gone from one tab while another still shows it", async () => { const listing = { ...LISTING, topics: [{ ...LISTING.topics[0], sources: [{ trendId: "1900000000000000001", url: TREND, tabs: ["Top", "Latest"], lastReadTabs: ["Top", "Latest"], reads: 1, lastReadAt: "2026-11-17T12:00:00.000Z" }] }], posts: [{ ...LISTING.posts[0], goneTabs: [{ tab: "Latest", at: "2026-11-18T10:30:00.000Z", read: READ }] }], total: 1, }; const { deps } = fakeEditor(() => ({ status: 200, body: listing }), env); const client = await connectWith(deps); const topics = body(await client.callTool({ name: "x_trending_topics", arguments: { channel: "trending" } })); assert.match(topics, / · read 1×, last 2026-11-17 12:00 UTC · the last read saw the "Top" and "Latest" tabs$/m); const posts = body(await client.callTool({ name: "x_trending_posts", arguments: { channel: "trending" } })); assert.match(posts, new RegExp(`^ gone from the "Latest" tab at 2026-11-18 10:30 UTC \\(read ${READ}\\); another tab still shows it$`, "m")); assert.doesNotMatch(posts, /gone from the page/); });