// What library.spec.ts judges (lane posts-library; the page /tools/library): // an invented X trending library — two topics with watches, a third with none, // a thread, a post gone from the page, dropped posts with captures — as the // stub editor answers it (editor-stub-trending.mjs), and the few media files // umtool's own media route serves from the fixture's corpus. // // The channel directory is made by the spec and removed after it // (makeLibraryFixture / removeLibraryFixture), not by make-fixture.mjs: no // other spec sees one more channel. Invented ids, handles and words; no real // trending id, and nothing here is ever asked of X. import { linkSync, mkdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import path from "node:path"; export const LIB = "trending-lib"; export const LIB_NAME = "Trending fixture"; export const NOT_A_LIB = "testchan"; const id = (n) => `19000000000000${String(n).padStart(5, "0")}`; export const P = { opening: id(7001), reply: id(7002), gone: id(7003), dropped: id(7004), later: id(7005), fresh: id(7006), goneDropped: id(7007), cup: id(7101), cupDropped: id(7102), }; const READ_1 = "2026-11-17T10:00:00.000Z"; const READ_2 = "2026-11-17T11:00:00.000Z"; const READ_3 = "2026-11-17T12:00:00.000Z"; // A 1×1 PNG and a few bytes that stand for a video (the route serves bytes; nothing decodes them). const PNG = Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==", "base64"); export const VIDEO_BYTES = Buffer.from("FAKE-TEST-VALUEghijklmnopqrstuvwxyz-library-fixture-video"); /** The captured files of each post: name → bytes. */ export const CAPTURES = { [P.opening]: { "shot.png": PNG, [`${P.opening}_1.jpg`]: PNG, [`${P.opening}_2.mp4`]: VIDEO_BYTES }, [P.reply]: { "shot.png": PNG, "shot-thread.png": PNG }, [P.dropped]: { "shot.png": PNG, [`${P.dropped}_1.jpg`]: PNG }, [P.cupDropped]: { "shot.png": PNG }, [P.goneDropped]: { "shot.png": PNG }, }; /** The library's state as the stub editor starts with it (a fresh copy per call). */ export function libraryState() { const post = (pid, author, authorName, text, topic, firstSeenAt, extra = {}) => ({ id: pid, author, authorName, createdAt: "2026-11-16T12:00:00.000Z", text, url: `https://x.com/${author}/status/${pid}`, topic, thread: pid, firstSeenAt, mediaCount: 0, kinds: [], ...extra }); return { slug: LIB, name: LIB_NAME, topics: [ { id: "kettle-festival", title: "Kettle festival", watch: { on: true, intervalMinutes: 30, startedAt: READ_1, until: "2026-11-19T10:00:00.000Z", shots: false, by: "operator", ticks: 2, failures: 0, nextSource: 0, goneSources: [], paused: { reason: "jar", since: READ_3, sentence: "X's cookie jar is 75 minutes old, past the watch's 60." } }, }, { id: "cup-shortage", title: "Cup shortage", watch: { on: true, intervalMinutes: 60, startedAt: READ_1, until: "2026-11-19T10:00:00.000Z", shots: true, by: "agent", ticks: 5, failures: 3, nextSource: 0, goneSources: [], paused: { reason: "failing", since: READ_3, sentence: "The last read ended rate-limited." } }, }, // A topic whose page X described: its own title and summary, and the tab the last read saw (as the editor's listing gives them). { id: "lids", title: "Lids", sources: [{ trendId: "1900000000000000009", url: "https://x.com/i/trending/1900000000000000009", title: "Lid makers meet", summary: "Lid makers met to agree on one thread size.", lastUpdatedAt: "2026-11-17T09:30:00.000Z", tabs: ["Top", "Latest"], lastReadTab: "Top", reads: 1, lastReadAt: READ_1 }], }, ], posts: [ post(P.opening, "teapot_news", "Teapot News", "The kettle festival opens today.", "kettle-festival", READ_1, { mediaCount: 2, kinds: ["image", "video"], capture: { at: "2026-11-17T10:05:00.000Z", state: "captured", mediaState: "ok" } }), post(P.reply, "saucer_fan", "Saucer Fan", "A reply about spouts.", "kettle-festival", READ_1, { // Gone from one tab of the page while the other still shows it (a two-tab read). goneTabs: [{ tab: "Latest", at: READ_2, read: "tr-01JB0000000000000000000000" }], thread: P.opening, isReply: true, replyTo: { platform: "twitter", id: P.opening, author: "teapot_news" }, capture: { at: "2026-11-17T10:06:00.000Z", state: "captured", mediaState: "none" }, context: { parent: { handle: "teapot_news", text: "The kettle festival opens today." } }, }), post(P.gone, "lid_watch", "Lid Watch", "A lid fell off a kettle.", "kettle-festival", READ_1, { gone: { at: READ_2, read: "tr-01JB0000000000000000000000", trendId: "1900000000000000001" } }), post(P.dropped, "spout_co", "Spout Co", "Spouts are back in stock.", "kettle-festival", READ_1, { mediaCount: 1, kinds: ["image"], capture: { at: "2026-11-17T10:07:00.000Z", state: "captured", mediaState: "ok" } }), post(P.later, "teapot_news", "Teapot News", "More on the kettle parade later.", "kettle-festival", READ_2), post(P.fresh, "kettle_fan", "Kettle Fan", "Just arrived at the festival.", "kettle-festival", READ_3, { capture: { at: "2026-11-17T12:05:00.000Z", state: "deleted", mediaState: "skipped" } }), post(P.cup, "cup_other", "Cup Other", "Cups are short this week.", "cup-shortage", READ_1), // The operator dropped it, and the page no longer shows it: its capture is the only copy. post(P.goneDropped, "lid_watch", "Lid Watch", "Lids, half price.", "lids", READ_1, { gone: { at: READ_2, read: "tr-01JB0000000000000000000000", trendId: "1900000000000000003" }, capture: { at: "2026-11-17T10:09:00.000Z", state: "captured", mediaState: "none" } }), post(P.cupDropped, "cup_other", "Cup Other", "Buy my cups.", "cup-shortage", READ_1, { capture: { at: "2026-11-17T10:08:00.000Z", state: "captured", mediaState: "none" } }), ], judgements: { posts: { [P.dropped]: { state: "drop", by: "operator", at: "2026-11-17T12:30:00.000Z" }, [P.later]: { state: "later", note: "check the source", by: "agent", at: "2026-11-17T12:31:00.000Z" }, [P.cupDropped]: { state: "drop", by: "agent", at: "2026-11-17T12:32:00.000Z" }, [P.goneDropped]: { state: "drop", by: "operator", at: "2026-11-17T12:33:00.000Z" }, }, threads: {}, }, // Posts whose capture is still on disk (a purge takes the dropped ones). captured: Object.keys(CAPTURES), }; } const libDir = (channels) => path.join(channels, LIB); /** The library's channel in the fixture corpus: its marker and its captured files (a video as a hardlink to its blob). */ export function makeLibraryFixture({ channels }) { removeLibraryFixture({ channels }); const dir = libDir(channels); mkdirSync(path.join(dir, "trending", "media-store", "ab"), { recursive: true }); mkdirSync(path.join(dir, "trending", "reads", "tr-01JA0000000000000000000000"), { recursive: true }); writeFileSync(path.join(dir, "config.json"), JSON.stringify({ name: LIB_NAME, handling: "transcribe", sourceKind: "social", platform: "twitter", xTrending: {} })); writeFileSync(path.join(dir, "trending", "reads", "tr-01JA0000000000000000000000", "page.png"), PNG); writeFileSync(path.join(dir, "trending", "judgements.json"), JSON.stringify({ version: 1, posts: {}, threads: {} })); for (const [pid, files] of Object.entries(CAPTURES)) { const cap = path.join(dir, "trending", "posts-media", pid); mkdirSync(cap, { recursive: true }); writeFileSync(path.join(cap, "capture.json"), JSON.stringify({ version: 1, id: pid, state: "captured" })); for (const [name, bytes] of Object.entries(files)) { if (name.endsWith(".mp4")) { const blob = path.join(dir, "trending", "media-store", "ab", `${pid}.mp4`); writeFileSync(blob, bytes); linkSync(blob, path.join(cap, name)); } else writeFileSync(path.join(cap, name), bytes); } } // A name planted in a capture whose real path is the raw read's page: never served. symlinkSync(path.join(dir, "trending", "reads", "tr-01JA0000000000000000000000", "page.png"), path.join(dir, "trending", "posts-media", P.opening, "planted.png")); // A channel that is no library, with a file of the same shape. const other = path.join(channels, NOT_A_LIB, "trending", "posts-media", P.opening); mkdirSync(other, { recursive: true }); writeFileSync(path.join(other, "shot.png"), PNG); } export function removeLibraryFixture({ channels }) { rmSync(libDir(channels), { recursive: true, force: true }); rmSync(path.join(channels, NOT_A_LIB, "trending"), { recursive: true, force: true }); }