import { test, expect, type APIRequestContext, type Page } from "@playwright/test"; import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { portFor } from "yt-dlp-transcript-common/lib/ports.mjs"; import { LIB, makeLibraryFixture, NOT_A_LIB, P, removeLibraryFixture, VIDEO_BYTES } from "./fixtures/library-fixture.mjs"; import * as urls from "./urls"; import { warm, WARM_TIMEOUT } from "./warm"; // --------------------------------------------------------------------------- // THE POSTS LIBRARY (/tools/library; plans/x-trending.md slice 3, lane // posts-library): a private X trending library browsed by topic and judged. // // The library is the STUB EDITOR's (e2e/fixtures/editor-stub-trending.mjs: an // invented library in memory, every press recorded with the `by` it carried); // its captured media are files in the fixture's corpus, served by umtool's own // route. Nothing here reaches X, the live editor, or the operator's reports. // WRITES the fixture's REPORTS_ROOT/.umtool/posts-library.json (Mark seen) and // channels/trending-lib/ (made before, removed after). // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const CHANNELS = path.join(FIXTURE, "channels"); const SEEN = path.join(FIXTURE, ".umtool", "posts-library.json"); const STUB = `http://127.0.0.1:${portFor("EDITOR_STUB_PORT")}`; const KETTLES = "kettle-festival"; const CUPS = "cup-shortage"; type Recorded = { action: string; body: Record; authorization: string }; type StubState = { requests: Recorded[]; reads: number; judgements: { posts: Record; threads: Record }; captured: string[]; watches: Record }; const stub = async (request: APIRequestContext): Promise => (await request.get(`${STUB}/__stub/trending`)).json(); const reset = async (request: APIRequestContext, mode: "ok" | "unreachable" | "none" = "ok") => { expect((await request.post(`${STUB}/__stub/trending`, { data: { reset: true, mode } })).status()).toBe(200); rmSync(SEEN, { force: true }); }; const last = async (request: APIRequestContext) => (await stub(request)).requests.at(-1)!; // What a browser puts on the page's own fetch, and a page cannot forge: the press routes take a press with them only. const asPage = (baseURL: string | undefined) => ({ origin: new URL(baseURL!).origin, "sec-fetch-site": "same-origin" }); const lib = (view: Record = {}) => urls.postsLibraryHref({ lib: LIB, ...view }); const post = (page: Page, id: string) => page.locator(`[data-library-post="${id}"]`); const rail = (page: Page) => page.locator('[data-library-topics="rail"]'); const status = (page: Page) => page.locator("[data-library-status]"); const idle = (page: Page) => expect(page.locator("[data-library]")).toHaveAttribute("data-library-busy", "false"); const shown = (page: Page) => page.locator("[data-library-post]").evaluateAll((els) => els.map((e) => e.getAttribute("data-library-post"))); async function open(page: Page, view: Record = {}) { await page.goto(lib(view)); await expect(page.locator('[data-stream-filled="library"]')).toBeVisible({ timeout: 30_000 }); // The presses are live once the page is (a button is disabled until then). await expect(page.locator("[data-library-scope]")).toBeEnabled(); } test.describe.configure({ mode: "serial" }); test.beforeAll(async ({ playwright }) => { test.setTimeout(WARM_TIMEOUT); makeLibraryFixture({ channels: CHANNELS }); await warm(playwright, [urls.postsLibraryHref(), urls.toolsHref()]); }); test.afterAll(async ({ request }) => { await reset(request); removeLibraryFixture({ channels: CHANNELS }); }); test.beforeEach(async ({ request, page }) => { await reset(request); await page.setViewportSize({ width: 1366, height: 900 }); }); test("the head is sent before the library, behind a placeholder of its shape", async ({ baseURL }) => { const res = await fetch(baseURL + lib(), { headers: { "Accept-Encoding": "identity", Cookie: "e2e-stream-delay=1500" } }); expect(res.status).toBe(200); const html = await res.text(); const filledAt = html.indexOf("data-stream-filled"); expect(filledAt, "the library never streamed in").toBeGreaterThan(0); const shell = html.slice(0, filledAt); expect(shell).toContain("data-app-rail"); expect(shell).toContain("Posts library"); expect(shell).toContain('data-stream-skeleton="library"'); expect(shell).not.toContain("data-library-post"); expect(html.slice(filledAt)).toContain(`data-library-post="${P.opening}"`); }); test("found from Tools; topics on the left with their counts and what each watch is doing", async ({ page }) => { await page.goto(urls.toolsHref()); await expect(page.locator("[data-tool-entry=library] a")).toHaveAttribute("href", urls.postsLibraryHref()); await open(page); const kettles = rail(page).locator(`[data-library-topic="${KETTLES}"]`); await expect(kettles).toContainText("Kettle festival"); await expect(kettles.locator("[data-library-topic-posts]")).toHaveText("6"); await expect(kettles.locator("[data-library-topic-unjudged]")).toHaveText("4"); // A paused watch says why; a failing one is told apart. await expect(kettles).toHaveAttribute("data-library-watch", "paused"); await expect(kettles.locator("[data-library-topic-watch]")).toContainText("Watch paused · Watched every 30 min"); await expect(kettles.locator("[data-library-topic-watch]")).toContainText("X's cookie jar is 75 minutes old"); const cups = rail(page).locator(`[data-library-topic="${CUPS}"]`); await expect(cups).toHaveAttribute("data-library-watch", "failing"); await expect(cups.locator("[data-library-topic-watch]")).toContainText("Watch failing"); await expect(rail(page).locator('[data-library-topic="lids"]')).toHaveAttribute("data-library-watch", "none"); // Every topic's posts, 9 in all. await expect(page.locator("[data-library-count]")).toHaveAttribute("data-library-count", "9"); await kettles.click(); await expect(page).toHaveURL(new RegExp(`topic=${KETTLES}`)); await expect(page.locator("[data-library-count]")).toHaveAttribute("data-library-count", "6"); await expect(page.locator(`[data-library-panel="${KETTLES}"]`)).toContainText("6 posts · 4 unjudged · 0 kept · 1 dropped · 1 later"); // It fits a desk without scrolling sideways. expect(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth + 1)).toBe(true); // Nothing here offers a post to a project, a report or a site. THE ONE WAY OUT (lane x-trending-publish) is the // topic's own Publish, the operator's, into a channel on no site: one button, and it names no site. await expect(page.locator("[data-posts-library]").getByRole("button", { name: /add to|cut|project|report|site/i })).toHaveCount(0); await expect(page.locator("[data-posts-library]").getByRole("button", { name: /publish/i })).toHaveText(["Publish kept posts…"]); await expect(page.locator("[data-posts-library]").getByRole("link", { name: /add to|publish/i })).toHaveCount(0); }); test("a post is drawn with its captured media, its thread context, and the words for gone and for a capture's state", async ({ page }) => { await open(page, { topic: KETTLES }); const media = (id: string, name: string) => `/api/library/media/${LIB}/${id}/${name}`; // The opening post: the archive viewer's body, with the screenshot, a picture and a video from umtool's media route. const opening = post(page, P.opening); await expect(opening.locator(`article[data-post-id="${P.opening}"]`)).toContainText("The kettle festival opens today."); await expect(opening.locator("[data-post-capture-shot]")).toHaveAttribute("src", media(P.opening, "shot.png")); await expect(opening.locator('[data-post-capture-media="image"] img')).toHaveAttribute("src", media(P.opening, `${P.opening}_1.jpg`)); await expect(opening.locator('[data-post-capture-media="video"] video')).toHaveAttribute("src", media(P.opening, `${P.opening}_2.mp4`)); // The picture really loads. await expect.poll(() => opening.locator("[data-post-capture-shot]").evaluate((img: HTMLImageElement) => img.complete && img.naturalWidth > 0)).toBe(true); await expect(opening.locator("[data-library-capture]")).toHaveText("capture: captured at 17 Nov 2026, 10:05 UTC"); // The reply: what it answers, as its capture read it, and the thread shot. const reply = post(page, P.reply); await expect(reply.locator('[data-library-context="parent"]')).toHaveText("In answer to @teapot_news: The kettle festival opens today."); await expect(reply.locator("[data-library-thread]")).toHaveText("in a thread"); await expect(reply.locator("[data-library-thread-shot] img")).toHaveAttribute("src", media(P.reply, "shot-thread.png")); // Gone from the page, in those words; a capture's own state verbatim. await expect(post(page, P.gone).locator("[data-library-gone]")).toHaveText("Gone from the page at 17 Nov 2026, 11:00 UTC"); // PER TAB: a post one tab stopped showing while the other still shows it says which tab, and is not "gone from the page". await expect(post(page, P.reply).locator('[data-library-gone-tab="Latest"]')).toHaveText("Gone from the “Latest” tab at 17 Nov 2026, 11:00 UTC; another tab still shows it."); await expect(post(page, P.reply).locator("[data-library-gone]")).toHaveCount(0); await expect(post(page, P.fresh).locator("[data-library-capture]")).toHaveText("capture: deleted at 17 Nov 2026, 12:05 UTC"); // No post is called deleted: the only place the word may stand is a capture's recorded state. const text = await page.locator("[data-library-posts]").innerText(); expect(text.match(/deleted/gi) ?? []).toHaveLength(1); expect(await page.locator("[data-library-gone]").allInnerTexts()).toEqual(["Gone from the page at 17 Nov 2026, 11:00 UTC"]); // Judgements already there: whose, and the note. await expect(post(page, P.dropped)).toHaveAttribute("data-library-judgement", "drop"); await expect(post(page, P.dropped).locator("[data-library-by]")).toContainText("by the operator"); await expect(post(page, P.later).locator("[data-library-by]")).toContainText("by the agent"); await expect(post(page, P.later).locator("[data-library-note]")).toHaveText("Note: check the source"); }); test("keep, drop, later and clear are the editor's judge-trending, as the operator; a thread, a note, and undo of the last press", async ({ page, request }) => { await open(page, { topic: KETTLES }); await post(page, P.opening).locator('[data-library-press="keep"]').click(); await expect(post(page, P.opening)).toHaveAttribute("data-library-judgement", "keep"); await expect(status(page)).toHaveText("1 judgement kept. 1 post queued for archiving (the editor's capture job stub-capture-1)."); let r = await last(request); expect(r.action).toBe("judge-trending"); expect(r.body).toEqual({ slug: LIB, by: "operator", set: [{ id: P.opening, state: "keep" }] }); expect(r.authorization).toBe("Bearer umtool-e2e-token"); await expect(post(page, P.opening).locator("[data-library-by]")).toContainText("by the operator"); await idle(page); // Undo: its own entry removed, nothing archived, and the bar says what undo does not do. await expect(page.locator("[data-library-undo-says]")).toHaveText("Undo puts the judgements back, written as the operator's whoever wrote them before. It does not cancel a capture already queued."); await page.locator("[data-library-undo]").click(); await expect(post(page, P.opening)).toHaveAttribute("data-library-judgement", "unjudged"); r = await last(request); expect(r.body).toEqual({ slug: LIB, by: "operator", set: [{ id: P.opening, state: "undecided" }], archive: false }); await expect(status(page)).toContainText("Undone: 1 judgement put back."); await expect(page.locator("[data-library-undo]")).toBeDisabled(); await idle(page); // The whole thread: the reply's Drop judges the thread's root, and both posts read dropped. await page.locator("[data-library-scope]").click(); await expect(page.locator("[data-library-scope]")).toHaveAttribute("data-library-scope", "thread"); await post(page, P.reply).locator('[data-library-press="drop"]').click(); await expect(post(page, P.reply)).toHaveAttribute("data-library-judgement", "drop"); await expect(post(page, P.opening)).toHaveAttribute("data-library-judgement", "drop"); await expect(post(page, P.reply).locator("[data-library-by]")).toContainText("from its thread"); r = await last(request); expect(r.body).toEqual({ slug: LIB, by: "operator", set: [{ thread: P.opening, state: "drop" }] }); await idle(page); // Clear the thread's. await post(page, P.reply).locator('[data-library-press="clear"]').click(); await expect(post(page, P.opening)).toHaveAttribute("data-library-judgement", "unjudged"); expect((await last(request)).body.set).toEqual([{ thread: P.opening, state: "undecided" }]); await idle(page); await page.locator("[data-library-scope]").click(); // A note goes with a judgement. await post(page, P.gone).locator('[data-library-press="note"]').click(); await post(page, P.gone).locator("[data-library-note-editor] textarea").fill("left the page within the hour"); await post(page, P.gone).locator('[data-library-note-save="later"]').click(); await expect(post(page, P.gone).locator("[data-library-note]")).toHaveText("Note: left the page within the hour"); expect((await last(request)).body).toEqual({ slug: LIB, by: "operator", set: [{ id: P.gone, state: "later", note: "left the page within the hour" }] }); await idle(page); // Undo puts back what was there: nothing. await page.locator("[data-library-undo]").click(); await expect(post(page, P.gone)).toHaveAttribute("data-library-judgement", "unjudged"); // Every press of this test was the operator's. expect((await stub(request)).requests.every((x) => x.body.by === "operator")).toBe(true); }); test("the keys: j and k move, a d l c judge, x selects, u undoes, ? lists them, Shift asks before a bulk press", async ({ page, request }) => { await open(page, { topic: KETTLES }); const focused = () => page.locator('[data-library-focus="true"]').getAttribute("data-library-post"); const order = await shown(page); expect(await focused()).toBe(order[0]); await page.keyboard.press("j"); expect(await focused()).toBe(order[1]); await page.keyboard.press("j"); await page.keyboard.press("k"); expect(await focused()).toBe(order[1]); await page.keyboard.press("a"); await expect(post(page, order[1]!)).toHaveAttribute("data-library-judgement", "keep"); expect((await last(request)).body.set).toEqual([{ id: order[1], state: "keep" }]); await idle(page); await page.keyboard.press("d"); await expect(post(page, order[1]!)).toHaveAttribute("data-library-judgement", "drop"); await idle(page); await page.keyboard.press("l"); await expect(post(page, order[1]!)).toHaveAttribute("data-library-judgement", "later"); await idle(page); // u: back to what it was before the last press (drop). await page.keyboard.press("u"); await expect(post(page, order[1]!)).toHaveAttribute("data-library-judgement", "drop"); expect((await last(request)).body).toMatchObject({ archive: false, by: "operator", set: [{ id: order[1], state: "drop" }] }); await idle(page); await page.keyboard.press("c"); await expect(post(page, order[1]!)).toHaveAttribute("data-library-judgement", "unjudged"); await idle(page); // x selects; with a selection a press judges the selected posts. await page.keyboard.press("x"); await page.keyboard.press("j"); await page.keyboard.press("x"); await expect(page.locator("[data-library-selected]")).toHaveAttribute("data-library-selected", "2"); await page.keyboard.press("l"); await expect(post(page, order[2]!)).toHaveAttribute("data-library-judgement", "later"); expect((await last(request)).body.set).toEqual([{ id: order[1], state: "later" }, { id: order[2], state: "later" }]); await idle(page); // t: the scope; ?: the list; typing in the search box presses nothing. await page.keyboard.press("t"); await expect(page.locator("[data-library-scope]")).toHaveAttribute("data-library-scope", "thread"); await page.keyboard.press("t"); await page.keyboard.press("?"); await expect(page.locator("[data-library-help]")).toContainText("keep everything in the filter"); await expect(page.locator("[data-library-help] kbd").first()).toHaveText("j"); const before = (await stub(request)).requests.length; await page.keyboard.press("/"); await expect(page.locator("[data-library-search]")).toBeFocused(); await page.keyboard.type("ad"); expect((await stub(request)).requests.length).toBe(before); await page.locator("[data-library-search]").fill(""); await page.locator("[data-library-search]").blur(); // Shift+D asks first, with the count; Escape leaves it. await page.keyboard.press("Shift+D"); await expect(page.locator('[data-library-confirm="drop"] [data-library-confirm-count]')).toHaveText("6"); await page.keyboard.press("Escape"); await expect(page.locator("[data-library-confirm]")).toHaveCount(0); expect((await stub(request)).requests.length).toBe(before); }); test("the filters: unjudged, kept, dropped, later, gone, has media, by author, search, and new since Mark seen", async ({ page, request }) => { await open(page, { topic: KETTLES }); const only = async (expected: string[]) => { await expect(page.locator("[data-library-count]")).toHaveAttribute("data-library-count", String(expected.length)); expect((await shown(page)).sort()).toEqual([...expected].sort()); }; await page.locator('[data-library-state="unjudged"]').click(); await only([P.opening, P.reply, P.gone, P.fresh]); await page.locator('[data-library-state="drop"]').click(); await only([P.dropped]); await page.locator('[data-library-state="later"]').click(); await only([P.later]); await page.locator('[data-library-state="keep"]').click(); await only([]); await expect(page.locator("[data-library-count]")).toHaveText("No post kept in “Kettle festival”."); await page.locator('[data-library-state="all"]').click(); await only([P.opening, P.reply, P.gone, P.dropped, P.later, P.fresh]); await page.locator('[data-library-toggle="gone"]').click(); await only([P.gone]); await page.locator('[data-library-toggle="gone"]').click(); await page.locator('[data-library-toggle="media"]').click(); await only([P.opening, P.dropped]); await page.locator('[data-library-toggle="media"]').click(); await only([P.opening, P.reply, P.gone, P.dropped, P.later, P.fresh]); await page.locator("[data-library-author]").selectOption("teapot_news"); await only([P.opening, P.later]); await page.locator("[data-library-author]").selectOption(""); await page.locator("[data-library-search]").fill("LID kettle"); await page.locator("[data-library-search]").press("Enter"); await only([P.gone]); await expect(page).toHaveURL(/q=LID\+kettle/); await expect(page).toHaveURL(new RegExp(`topic=${KETTLES}`)); // New: never marked, every post is new. Marked seen before the last read: the one post that read first showed. await open(page, { topic: KETTLES }); await expect(rail(page).locator(`[data-library-topic="${KETTLES}"] [data-library-topic-new]`)).toHaveText("6"); mkdirSync(path.dirname(SEEN), { recursive: true }); writeFileSync(SEEN, JSON.stringify({ format: "umtool-posts-library", version: 1, libraries: { [LIB]: { topics: { [KETTLES]: { seenAt: "2026-11-17T11:30:00.000Z" } } } } })); await open(page, { topic: KETTLES }); await expect(rail(page).locator(`[data-library-topic="${KETTLES}"] [data-library-topic-new]`)).toHaveText("1"); await page.locator('[data-library-toggle="new"]').click(); await only([P.fresh]); await expect(page.locator("[data-library-seen-at]")).toHaveText("marked seen 17 Nov 2026, 11:30 UTC"); // Mark seen is the only thing that moves it: after the press nothing is new. await page.locator("[data-library-seen]").click(); await expect(status(page)).toContainText("“Kettle festival” marked seen"); await only([]); const doc = JSON.parse(readFileSync(SEEN, "utf8")); expect(doc.libraries[LIB].topics[KETTLES].previous).toBe("2026-11-17T11:30:00.000Z"); expect(Date.parse(doc.libraries[LIB].topics[KETTLES].seenAt)).toBeGreaterThan(Date.parse("2026-11-18T00:00:00.000Z")); // Marking seen asked the editor for nothing but reads. expect((await stub(request)).requests).toEqual([]); }); test("bulk over the filter: the confirm states the count and that keeps archive through the paced capture job; one ask, one job; undo puts all back", async ({ page, request }) => { await open(page, { topic: KETTLES, state: "unjudged" }); await page.locator('[data-library-bulk="keep"]').click(); const confirm = page.locator('[data-library-confirm="keep"]'); await expect(confirm.locator("[data-library-confirm-count]")).toHaveText("4"); await expect(confirm).toContainText("Keep all 4 posts (unjudged) in “Kettle festival”?"); await expect(confirm).toContainText("Each keep archives the post's screenshot and media through the editor's paced capture job: one job per 500 posts, never one per post."); expect((await stub(request)).requests).toEqual([]); await confirm.locator("[data-library-confirm-yes]").click(); await expect(status(page)).toHaveText("4 judgements kept. 4 posts queued for archiving (the editor's capture job stub-capture-1)."); const all = (await stub(request)).requests; // ONE judge ask for the four posts: one capture job, not four. expect(all).toHaveLength(1); expect(all[0].body.by).toBe("operator"); expect(all[0].body.set.map((s: { id: string }) => s.id).sort()).toEqual([P.opening, P.reply, P.gone, P.fresh].sort()); expect(all[0].body.set.every((s: { state: string }) => s.state === "keep")).toBe(true); // The unjudged filter is now empty. await expect(page.locator("[data-library-count]")).toHaveAttribute("data-library-count", "0"); await idle(page); await page.locator("[data-library-undo]").click(); await expect(page.locator("[data-library-count]")).toHaveAttribute("data-library-count", "4"); const undo = await last(request); expect(undo.body.archive).toBe(false); expect(undo.body.set).toHaveLength(4); expect(undo.body.set.every((s: { state: string }) => s.state === "undecided")).toBe(true); expect((await stub(request)).judgements.posts[P.opening]).toBeUndefined(); }); test("the watch: why it is paused, that only the operator restarts a failing one, and the switch with its terms", async ({ page, request }) => { await open(page, { topic: KETTLES }); const panel = page.locator("[data-library-watch-panel]"); await expect(panel).toHaveAttribute("data-library-watch-panel", "paused"); await expect(panel.locator("[data-library-watch-why]")).toContainText("Paused: X's cookie jar is 75 minutes old, past the watch's 60."); await expect(panel.locator("[data-library-watch-why]")).toContainText("Refresh cookies in the editor, on /cookies"); await open(page, { topic: CUPS }); await expect(panel).toHaveAttribute("data-library-watch-panel", "failing"); await expect(panel.locator("[data-library-watch-why]")).toContainText("Only the operator restarts a failing watch: press Watch again."); await panel.locator("[data-library-watch-interval]").fill("45"); await panel.locator("[data-library-watch-hours]").fill("24"); await panel.locator("[data-library-watch-on]").click(); await expect(panel).toHaveAttribute("data-library-watch-panel", "on"); let r = await last(request); expect(r.action).toBe("watch-trending"); expect(r.body).toEqual({ slug: LIB, topic: CUPS, on: true, intervalMinutes: 45, hours: 24, shots: true, by: "operator" }); await expect(panel.locator("[data-library-watch-sentence]")).toContainText("Watched every 45 min"); await expect(rail(page).locator(`[data-library-topic="${CUPS}"]`)).toHaveAttribute("data-library-watch", "on"); await idle(page); await panel.locator("[data-library-watch-off]").click(); await expect(panel).toHaveAttribute("data-library-watch-panel", "ended"); r = await last(request); expect(r.body).toEqual({ slug: LIB, topic: CUPS, on: false, by: "operator" }); // A topic never watched: Watch, with "screenshot everything new". await open(page, { topic: "lids" }); await expect(panel).toHaveAttribute("data-library-watch-panel", "none"); await expect(panel.locator("[data-library-watch-sentence]")).toHaveText("Not watched."); // X's own title and summary of the page read into the topic, beside the operator's name for it, and the tab the last read saw. const source = page.locator('[data-library-panel="lids"] [data-library-source]'); await expect(source.locator("[data-library-x-title]")).toContainText("X’s title: Lid makers meet"); await expect(source.locator("[data-library-x-summary]")).toHaveText("X’s summary: Lid makers met to agree on one thread size."); await expect(source.locator("[data-library-tab]")).toHaveText("The last read saw the page’s “Top” tab (the page has “Top”, “Latest”)."); // A topic with no words from X shows none. await expect(page.locator('[data-library-panel="lids"] [data-library-source]')).toHaveCount(1); await panel.locator("[data-library-watch-shots]").check(); await panel.locator("[data-library-watch-on]").click(); await expect(panel).toHaveAttribute("data-library-watch-panel", "on"); expect((await last(request)).body).toEqual({ slug: LIB, topic: "lids", on: true, shots: true, by: "operator" }); }); test("purge of the operator's dropped posts' captures: two presses, the first saying what would go and what is true of it; the undo memory goes with it", async ({ page, request }) => { await open(page, { topic: KETTLES }); // A keep first: there is something to undo. await post(page, P.opening).locator('[data-library-press="keep"]').click(); await expect(post(page, P.opening)).toHaveAttribute("data-library-judgement", "keep"); await idle(page); await expect(page.locator("[data-library-undo]")).toBeEnabled(); await page.locator("[data-library-purge]").click(); const plan = page.locator("[data-library-purge-plan]"); await expect(plan.locator("[data-library-purge-sentence]")).toHaveText("Would remove the capture of 1 dropped post: 3 files, 204 B. The judgements stay. Keeping it again captures it again."); await expect(plan.locator("[data-library-purge-numbers]")).toHaveText("1 to remove · 0 of them gone from the page · 0 dropped by an agent, left alone"); let r = await last(request); expect(r.action).toBe("purge-trending"); expect(r.body).toEqual({ slug: LIB, topic: KETTLES, dryRun: true, by: "operator" }); // Nothing removed yet. expect((await stub(request)).captured).toContain(P.dropped); await idle(page); await plan.locator("[data-library-purge-yes]").click(); await expect(status(page)).toHaveText("Removed the capture of 1 dropped post: 3 files, 204 B. The judgements stay."); r = await last(request); expect(r.body.by).toBe("operator"); expect(r.body.topic).toBe(KETTLES); expect(r.body.confirm).toMatch(/^[0-9a-f]{16}$/); expect(r.body.dryRun).toBeUndefined(); const after = await stub(request); expect(after.captured).not.toContain(P.dropped); expect(after.judgements.posts[P.dropped].state).toBe("drop"); await expect(post(page, P.dropped)).toHaveAttribute("data-library-judgement", "drop"); // S2: a purge takes the undo memory: the keep before it can no longer be undone from here. await expect(page.locator("[data-library-undo]")).toBeDisabled(); const asked = (await stub(request)).requests.length; await page.keyboard.press("u"); await expect(status(page)).toHaveText("Nothing to undo."); expect((await stub(request)).requests.length).toBe(asked); await expect(post(page, P.opening)).toHaveAttribute("data-library-judgement", "keep"); await idle(page); // Again: nothing to remove, and no removal is offered. await page.locator("[data-library-purge]").click(); await expect(plan.locator("[data-library-purge-sentence]")).toHaveText("No post the operator dropped has a capture: nothing to remove."); await expect(plan.locator("[data-library-purge-yes]")).toHaveCount(0); await plan.locator("[data-library-purge-no]").click(); // S1: an AGENT's drop is never purged: it is counted and left, and nothing is offered. await open(page, { topic: CUPS }); await page.locator("[data-library-purge]").click(); await expect(plan.locator("[data-library-purge-sentence]")).toHaveText("No post the operator dropped has a capture: nothing to remove. 1 post an agent dropped is left alone: a purge takes the operator's drops only."); await expect(plan.locator("[data-library-purge-yes]")).toHaveCount(0); await plan.locator("[data-library-purge-no]").click(); expect((await stub(request)).captured).toContain(P.cupDropped); // S1: a dropped post gone from the page: the capture is the only copy, and nothing promises it back. await open(page, { topic: "lids" }); await page.locator("[data-library-purge]").click(); await expect(plan.locator("[data-library-purge-sentence]")).toHaveText( "Would remove the capture of 1 dropped post: 2 files, 136 B. The judgements stay. It is gone from the page: this is the only copy: X no longer showed this post at 2026-11-17 11:00 UTC; removing it cannot be undone.", ); await expect(plan.locator("[data-library-purge-numbers]")).toHaveText("1 to remove · 1 of them gone from the page · 0 dropped by an agent, left alone"); await expect(plan).not.toContainText("captures it again"); // Cancel after a dry run removes nothing. await plan.locator("[data-library-purge-no]").click(); expect((await stub(request)).captured).toContain(P.goneDropped); }); test("the media route serves a library's captured pictures and videos, and nothing else", async ({ request }) => { const at = (l: string, id: string, name: string) => `/api/library/media/${l}/${id}/${name}`; const shot = await request.get(at(LIB, P.opening, "shot.png")); expect(shot.status()).toBe(200); expect(shot.headers()["content-type"]).toBe("image/png"); expect(shot.headers()["cache-control"]).toBe("private, no-store"); expect(shot.headers()["cross-origin-resource-policy"]).toBe("same-origin"); // A video, by byte range. const part = await request.get(at(LIB, P.opening, `${P.opening}_2.mp4`), { headers: { range: "bytes=0-9" } }); expect(part.status()).toBe(206); expect(part.headers()["content-range"]).toBe(`bytes 0-9/${VIDEO_BYTES.length}`); expect((await part.body()).toString()).toBe(VIDEO_BYTES.subarray(0, 10).toString()); const head = await request.head(at(LIB, P.opening, "shot.png")); expect(head.status()).toBe(200); // The record, a name whose real path is the raw read, a traversal, an id that is no id: never. for (const [l, id, name] of [ [LIB, P.opening, "capture.json"], [LIB, P.opening, "planted.png"], [LIB, P.opening, "missing.png"], [LIB, "..", "judgements.json"], [LIB, "reads", "page.png"], [LIB, P.opening, "..%2F..%2Fjudgements.json"], [LIB, P.opening, "%2E%2E"], // A channel that is no library, with a file of the same shape on disk. [NOT_A_LIB, P.opening, "shot.png"], ["nope", P.opening, "shot.png"], ]) { const res = await request.get(at(l, id, name)); expect([400, 404], `${l}/${id}/${name} answered ${res.status()}`).toContain(res.status()); } expect(existsSync(path.join(CHANNELS, NOT_A_LIB, "trending", "posts-media", P.opening, "shot.png"))).toBe(true); // It lists nothing: no directory, no index. for (const dir of [`/api/library/media/${LIB}/${P.opening}`, `/api/library/media/${LIB}`, "/api/library/media"]) { expect((await request.get(dir)).status(), dir).toBe(404); } // Another site's page cannot fetch it. expect((await request.get(at(LIB, P.opening, "shot.png"), { headers: { "sec-fetch-site": "cross-site" } })).status()).toBe(403); expect((await request.get(at(LIB, P.opening, "shot.png"), { headers: { "sec-fetch-site": "same-origin" } })).status()).toBe(200); // A range that cannot be met keeps the route's private headers. const beyond = await request.get(at(LIB, P.opening, `${P.opening}_2.mp4`), { headers: { range: "bytes=999999-" } }); expect(beyond.status()).toBe(416); expect(beyond.headers()["cache-control"]).toBe("private, no-store"); expect(beyond.headers()["cross-origin-resource-policy"]).toBe("same-origin"); expect(beyond.headers()["content-range"]).toBe(`bytes */${VIDEO_BYTES.length}`); // Read only. expect([404, 405]).toContain((await request.post(at(LIB, P.opening, "shot.png"), { data: {} })).status()); }); test("the press routes take a press from umtool's own page only: stamped the operator's whatever the body says; a request no browser sent is not the operator", async ({ request, baseURL }) => { const page = asPage(baseURL); // The page's fetch, with a body that claims to be an agent: not read. const judged = await request.post("/api/library/judge", { headers: page, data: { lib: LIB, by: "agent", set: [{ id: P.cup, state: "later", by: "agent" }] } }); expect(judged.status()).toBe(200); expect((await last(request)).body).toEqual({ slug: LIB, by: "operator", set: [{ id: P.cup, state: "later" }] }); expect((await request.post("/api/library/watch", { headers: page, data: { lib: LIB, topic: "lids", on: true, by: "agent" } })).status()).toBe(200); expect((await last(request)).body.by).toBe("operator"); expect((await request.post("/api/library/purge", { headers: page, data: { lib: LIB, dryRun: true, by: "agent" } })).status()).toBe(200); expect((await last(request)).body.by).toBe("operator"); // M1: A HEADERLESS POST (curl, a script, an agent with no bearer) IS NOT THE OPERATOR: refused on all four, // the dry run and its confirm included, and nothing reaches the editor or umtool's state. const before = (await stub(request)).requests.length; const bodies: Record = { judge: { lib: LIB, set: [{ id: P.cup, state: "keep" }] }, watch: { lib: LIB, topic: "lids", on: false }, purge: { lib: LIB, dryRun: true }, seen: { lib: LIB, topics: ["lids"] }, }; for (const [route, data] of Object.entries(bodies)) { for (const headers of [{}, { authorization: "Bearer umtool-e2e-token" }, { ...page, authorization: "Bearer umtool-e2e-token" }, { origin: "https://example.org", "sec-fetch-site": "cross-site" }] as Record[]) { const res = await request.post(`/api/library/${route}`, { headers, data }); expect(res.status(), `${route} ${JSON.stringify(headers)}`).toBe(403); if (!headers.origin || headers.authorization) expect((await res.json()).error).toContain("pnpm ops judge-trending"); } } const confirm = await request.post("/api/library/purge", { data: { lib: LIB, confirm: "FAKE-TEST-VALUE" } }); expect(confirm.status()).toBe(403); expect((await stub(request)).requests.length).toBe(before); expect(existsSync(SEEN)).toBe(false); // Mark seen, from the page: the library and the topic are checked through the editor first. const ghost = await request.post("/api/library/seen", { headers: page, data: { lib: LIB, topics: ["lids", "spoons"] } }); expect(ghost.status()).toBe(404); expect((await ghost.json()).error).toContain('has no topic "spoons": nothing was marked'); expect((await request.post("/api/library/seen", { headers: page, data: { lib: "nope", topics: ["lids"] } })).status()).toBe(404); expect(existsSync(SEEN)).toBe(false); expect((await request.post("/api/library/seen", { headers: page, data: { lib: LIB, topics: ["lids"] } })).status()).toBe(200); expect(Object.keys(JSON.parse(readFileSync(SEEN, "utf8")).libraries[LIB].topics)).toEqual(["lids"]); // The page's own read, asked again after a press: the view's rows, to umtool's own pages only. const read = await request.get(`/api/library?lib=${LIB}&topic=${KETTLES}&state=drop`); expect(read.status()).toBe(200); expect(read.headers()["cache-control"]).toBe("private, no-store"); const rows = await read.json(); expect(rows.ok).toBe(true); expect(rows.posts.map((p: { id: string }) => p.id)).toEqual([P.dropped]); expect((await request.get(`/api/library?lib=${LIB}`, { headers: { "sec-fetch-site": "cross-site" } })).status()).toBe(403); // Ill-shaped: refused here with a sentence, the editor not asked. const bad = await request.post("/api/library/judge", { headers: page, data: { lib: LIB, set: [{ id: "../x", state: "keep" }] } }); expect(bad.status()).toBe(400); expect((await bad.json()).error).toContain("not a post id"); // A bulk press whose count is stale judges nothing. const stale = await request.post("/api/library/judge", { headers: page, data: { lib: LIB, bulk: { view: { topic: KETTLES, state: "unjudged" }, state: "drop" }, expect: 99 } }); expect(stale.status()).toBe(409); expect((await stale.json()).error).toContain("nothing was judged"); expect((await stub(request)).requests.filter((x) => x.action === "judge-trending").length).toBe(1); }); test("S5: one view is two reads of the editor, whatever the number of topics and marks; so is the read after a press", async ({ page, request }) => { // Every topic marked seen: the new counts still come in the one listing read. mkdirSync(path.dirname(SEEN), { recursive: true }); const seenAt = "2026-11-17T11:30:00.000Z"; writeFileSync(SEEN, JSON.stringify({ format: "umtool-posts-library", version: 1, libraries: { [LIB]: { topics: { [KETTLES]: { seenAt }, [CUPS]: { seenAt }, lids: { seenAt } } } } })); await request.post(`${STUB}/__stub/trending`, { data: { mode: "ok" } }); const before = (await stub(request)).reads; await open(page, { topic: KETTLES }); await expect(rail(page).locator(`[data-library-topic="${KETTLES}"] [data-library-topic-new]`)).toHaveText("1"); await expect(rail(page).locator(`[data-library-topic="${CUPS}"] [data-library-topic-new]`)).toHaveCount(0); await idle(page); expect((await stub(request)).reads - before).toBe(2); // A press: the press itself, then the view read again: two reads more. await post(page, P.gone).locator('[data-library-press="later"]').click(); await expect(post(page, P.gone)).toHaveAttribute("data-library-judgement", "later"); await idle(page); await expect(page.locator(`[data-library-panel="${KETTLES}"]`)).toContainText("3 unjudged"); expect((await stub(request)).reads - before).toBe(4); }); test("on a phone (360 px): nothing pushes the page sideways, the topics fold, and a press works", async ({ page, request }) => { await page.setViewportSize({ width: 360, height: 740 }); await open(page, { topic: KETTLES }); expect(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth + 1)).toBe(true); await expect(rail(page)).toBeHidden(); const fold = page.locator('[data-library-topics="fold"]'); await expect(fold.locator("summary")).toHaveText("Topics · Kettle festival"); await fold.locator("summary").click(); await expect(fold.locator(`[data-library-topic="${CUPS}"]`)).toBeVisible(); await fold.locator("summary").click(); await post(page, P.opening).locator('[data-library-press="keep"]').click(); await expect(post(page, P.opening)).toHaveAttribute("data-library-judgement", "keep"); expect((await last(request)).body).toEqual({ slug: LIB, by: "operator", set: [{ id: P.opening, state: "keep" }] }); // The bar and the watch panel fit too. await expect(page.locator("[data-library-bar]")).toBeVisible(); for (const sel of ["[data-library-bar]", "[data-library-panel]", "[data-library-filters]", `[data-library-post="${P.opening}"]`]) { const box = (await page.locator(sel).first().boundingBox())!; expect(box.x, sel).toBeGreaterThanOrEqual(-1); expect(box.x + box.width, sel).toBeLessThanOrEqual(361); } expect(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth + 1)).toBe(true); await page.screenshot({ path: test.info().outputPath("library-360.png"), fullPage: true }); }); // ── lane x-trending-publish: organise, publish, the accounts ─────────────── type Slice4 = StubState & { topics: { id: string; title: string; aliases: string[]; posts: string[] }[]; published: Record; archived: Record }; const stub4 = async (request: APIRequestContext): Promise => (await request.get(`${STUB}/__stub/trending`)).json(); test("organise: rename a topic, move the selection to another, merge behind a confirm that says it cannot be undone and which watch stands", async ({ page, request }) => { await open(page, { topic: KETTLES }); const organise = page.locator(`[data-library-organise="${KETTLES}"]`); // RENAME: the id stays (the address does not change), the rail says the new name. await expect(organise.locator("[data-library-rename]")).toBeDisabled(); await organise.locator("[data-library-rename-input]").fill("Kettle festival 2026"); // S9: while a read of the library is queued or running the editor refuses it, and the page says why. expect((await request.post(`${STUB}/__stub/trending`, { data: { reading: true } })).status()).toBe(200); await organise.locator("[data-library-rename]").click(); await expect(status(page)).toHaveAttribute("data-library-status", "error"); await expect(status(page)).toContainText("A read of this library is queued or running: rename its topic once it has finished"); await expect(rail(page).locator(`[data-library-topic="${KETTLES}"]`)).not.toContainText("2026"); await idle(page); expect((await request.post(`${STUB}/__stub/trending`, { data: { reading: false } })).status()).toBe(200); await organise.locator("[data-library-rename]").click(); await expect(status(page)).toHaveText('Topic "Kettle festival" is now "Kettle festival 2026". Its id stays kettle-festival: its watch, its posts and their judgements are untouched.'); expect((await last(request)).action).toBe("organise-trending"); expect((await last(request)).body).toEqual({ slug: LIB, op: "rename", topic: KETTLES, title: "Kettle festival 2026", by: "operator" }); await expect(rail(page).locator(`[data-library-topic="${KETTLES}"]`)).toContainText("Kettle festival 2026"); await idle(page); // MOVE: what a press would judge (here the selection) goes to the topic chosen. await expect(page.locator("[data-library-move]")).toBeDisabled(); await page.getByLabel("Select the post by @kettle_fan").check(); await page.locator("[data-library-move-to]").selectOption(CUPS); await page.locator("[data-library-move]").click(); await expect(status(page)).toHaveText('1 post moved to "Cup shortage". Their judgements and captures are untouched.'); expect((await last(request)).body).toEqual({ slug: LIB, op: "move", to: CUPS, ids: [P.fresh], by: "operator" }); await expect(post(page, P.fresh)).toHaveCount(0); expect((await stub4(request)).topics.find((t) => t.id === CUPS)!.posts).toContain(P.fresh); await idle(page); // Mark seen first: umtool's own mark must follow the merge. await page.locator("[data-library-seen]").click(); await expect(status(page)).toContainText("marked seen"); await idle(page); expect(Object.keys(JSON.parse(readFileSync(SEEN, "utf8")).libraries[LIB].topics)).toEqual([KETTLES]); // MERGE: asked twice. The first press writes nothing and says what is true of it. await expect(organise.locator("[data-library-merge]")).toBeDisabled(); await organise.locator("[data-library-merge-into]").selectOption(CUPS); await organise.locator("[data-library-merge]").click(); const plan = page.locator("[data-library-merge-plan]"); await expect(plan.locator("[data-library-merge-sentence]")).toContainText("Merge “Kettle festival 2026” into “Cup shortage”?"); await expect(plan.locator("[data-library-merge-sentence]")).toContainText("This cannot be undone: there is no unmerge."); await expect(plan.locator("[data-library-merge-sentence]")).toContainText("Both are watched: the watch of “Cup shortage” stands with its own terms, and this topic's watch is gone."); expect((await stub4(request)).requests.filter((r) => r.body.op === "merge")).toHaveLength(0); await plan.locator("[data-library-merge-no]").click(); await expect(plan).toHaveCount(0); expect((await stub4(request)).topics.map((t) => t.id)).toContain(KETTLES); await organise.locator("[data-library-merge]").click(); await plan.locator("[data-library-merge-yes]").click(); await expect(status(page)).toContainText('"Kettle festival 2026" is now part of "Cup shortage"'); await expect(status(page)).toContainText("This cannot be undone."); await expect(status(page)).toContainText('The watch of "Cup shortage" stands with its own terms.'); const merge = (await stub4(request)).requests.filter((r) => r.body.op === "merge"); expect(merge.map((r) => r.body)).toEqual([{ slug: LIB, op: "merge", into: CUPS, from: [KETTLES], by: "operator" }]); // The page is on the topic that stays, which now holds the merged posts. await expect(page).toHaveURL(new RegExp(`topic=${CUPS}`)); await expect(page.locator(`[data-library-panel="${CUPS}"]`)).toBeVisible(); await expect(post(page, P.opening)).toBeVisible(); await expect(rail(page).locator(`[data-library-topic="${KETTLES}"]`)).toHaveCount(0); const after = await stub4(request); expect(after.topics.find((t) => t.id === CUPS)!.aliases).toEqual([KETTLES]); // umtool's mark followed: the topic that stays was never marked, so it stays unmarked and the merged one's mark is gone. expect(JSON.parse(readFileSync(SEEN, "utf8")).libraries[LIB].topics).toEqual({}); }); test("publish: the first press says exactly what would be written and writes nothing; the slug can be changed before a first publish; publishing again adds and removes nothing", async ({ page, request }) => { // No post kept by the operator: nothing to publish (an agent's judgement is not one). await open(page, { topic: CUPS }); await expect(page.locator("[data-library-publish]")).toBeDisabled(); await expect(page.locator("[data-library-published]")).toHaveText("Not published."); await open(page, { topic: KETTLES }); await post(page, P.opening).locator('[data-library-press="keep"]').click(); await expect(post(page, P.opening)).toHaveAttribute("data-library-judgement", "keep"); await idle(page); await page.locator("[data-library-publish]").click(); const plan = page.locator("[data-library-publish-plan]"); await expect(plan.locator("[data-library-publish-sentence]")).toHaveText('Publishing "Kettle festival" would make the channel "kettle-festival", on no site, writing 1 post and 3 files (204 B); 0 of the 1 kept post are there already.'); await expect(plan.locator("[data-library-publish-numbers]")).toHaveText("channel kettle-festival (new, on no site) · 1 post to add · 3 files · 204 B · 0 there already"); let r = await last(request); expect(r.action).toBe("publish-trending"); expect(r.body).toEqual({ slug: LIB, topic: KETTLES, dryRun: true, by: "operator" }); expect((await stub4(request)).published).toEqual({}); await idle(page); // The slug, changed before the first publish: counted again under it, and the publish waits for that. await plan.locator("[data-library-publish-channel]").fill("Kettles-2026"); await expect(plan.locator("[data-library-publish-yes]")).toBeDisabled(); await plan.locator("[data-library-publish-recount]").click(); await expect(plan.locator("[data-library-publish-numbers]")).toContainText("channel Kettles-2026 (new, on no site)"); expect((await last(request)).body).toEqual({ slug: LIB, topic: KETTLES, channel: "Kettles-2026", dryRun: true, by: "operator" }); await idle(page); await expect(plan.locator("[data-library-publish-yes]")).toHaveText("Publish 1 post into “Kettles-2026”"); await plan.locator("[data-library-publish-yes]").click(); await expect(status(page)).toContainText('Publishing "Kettle festival" made the channel "Kettles-2026", on no site, and wrote 1 post and 3 files'); r = await last(request); expect(r.body.by).toBe("operator"); expect(r.body.channel).toBe("Kettles-2026"); expect(r.body.confirm).toMatch(/^[0-9a-f]{24}$/); expect(r.body.dryRun).toBeUndefined(); expect((await stub4(request)).published).toEqual({ [KETTLES]: { channel: "Kettles-2026", posts: [P.opening] } }); await expect(page.locator("[data-library-published]")).toHaveAttribute("data-library-published", "Kettles-2026"); await expect(page.locator("[data-library-published]")).toContainText("Published into the channel “Kettles-2026”: 1 post, last"); await idle(page); // AGAIN: the opening post is dropped, the reply kept. One is added; the dropped one is reported and stays. await post(page, P.opening).locator('[data-library-press="drop"]').click(); await expect(post(page, P.opening)).toHaveAttribute("data-library-judgement", "drop"); await idle(page); await post(page, P.reply).locator('[data-library-press="keep"]').click(); await expect(post(page, P.reply)).toHaveAttribute("data-library-judgement", "keep"); await idle(page); // S4: the operator has put the channel on a site meanwhile: the plan says where the post goes public. await expect(plan.locator("[data-library-publish-sites]")).toHaveCount(0); expect((await request.post(`${STUB}/__stub/trending`, { data: { sites: { "Kettles-2026": ["kettle-site"] } } })).status()).toBe(200); await page.locator("[data-library-publish]").click(); await expect(plan.locator("[data-library-publish-sites]")).toHaveText("On the site kettle-site: public at the next build."); await expect(plan.locator("[data-library-publish-sentence]")).toContainText("THIS CHANNEL IS ON THE SITE kettle-site: these posts go public at its next build."); await expect(plan.locator("[data-library-publish-numbers]")).toHaveText("channel Kettles-2026 · 1 post to add · 1 file · 68 B · 0 there already · 1 no longer kept, left"); await expect(plan.locator("[data-library-publish-sentence]")).toContainText("1 post published before is no longer kept: it stays in the channel (a publish removes nothing)."); // The slug is fixed now: no field for it. await expect(plan.locator("[data-library-publish-channel]")).toHaveCount(0); await idle(page); await plan.locator("[data-library-publish-yes]").click(); await expect(status(page)).toContainText('added to the channel "Kettles-2026"'); expect((await last(request)).body.channel).toBeUndefined(); expect((await stub4(request)).published[KETTLES].posts).toEqual([P.opening, P.reply]); await idle(page); // On a phone the panel and an open plan fit. await page.setViewportSize({ width: 360, height: 740 }); await page.locator("[data-library-publish]").click(); await expect(plan.locator("[data-library-publish-sentence]")).toContainText("Nothing to add"); await expect(plan.locator("[data-library-publish-yes]")).toHaveCount(0); for (const sel of ["[data-library-organise]", "[data-library-publish-plan]", "[data-library-move-control]"]) { const box = (await page.locator(sel).first().boundingBox())!; expect(box.x, sel).toBeGreaterThanOrEqual(-1); expect(box.x + box.width, sel).toBeLessThanOrEqual(361); } expect(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth + 1)).toBe(true); await plan.locator("[data-library-publish-no]").click(); }); test("the accounts: who wrote the posts; Archive this account makes -x and asks X nothing", async ({ page, request }) => { await open(page); const accounts = page.locator("[data-library-accounts]"); await expect(accounts).toHaveAttribute("data-library-accounts", "closed"); await accounts.locator("summary").click(); await expect(accounts.locator("[data-library-accounts-count]")).toContainText("accounts. Archiving one makes its own X channel, on no site, with sync off: nothing is asked of X until you turn its sync on or fetch it in the editor."); const row = accounts.locator('[data-library-account="saucer_fan"]'); await expect(row).toContainText("@saucer_fan"); await expect(row).toContainText("1 post · 0 kept · Kettle festival"); await expect(accounts.locator('[data-library-account="teapot_news"]')).toContainText("2 posts"); // Asked twice; the first press asks the editor nothing. await row.locator("[data-library-archive]").click(); await expect(row.locator("[data-library-archive-plan]")).toContainText("Makes the channel “saucer_fan-x”, on no site, with sync off. Nothing is asked of X."); expect((await stub4(request)).requests.filter((r) => r.action === "x-search-lead")).toHaveLength(0); await row.locator("[data-library-archive-no]").click(); await row.locator("[data-library-archive]").click(); await row.locator("[data-library-archive-yes]").click(); // The editor's sentence, whole: sync off, nothing asked of X, how to turn sync on and run the first fetch. await expect(status(page)).toHaveText( `Made the channel "saucer_fan-x" for @saucer_fan, on no site, with sync off (its priority is paused): nothing was asked of X, and nothing will be until you say so. To turn sync on, set its priority to normal on /channels (pnpm ops channel-priority --json '{"slugs":["saucer_fan-x"],"tier":"normal"}'). Its first fetch is Fetch posts on its page (pnpm ops fetch-posts --json '{"slug":"saucer_fan-x"}').`, ); // The lead action, "archive", and NO fetch. const r = await last(request); expect(r.action).toBe("x-search-lead"); expect(r.body).toEqual({ slug: LIB, handle: "saucer_fan", action: "archive", name: "Saucer Fan" }); expect((await stub4(request)).archived).toEqual({ saucer_fan: "saucer_fan-x" }); await expect(row.locator("[data-library-account-archived]")).toHaveText("archived as saucer_fan-x"); await expect(row.locator("[data-library-archive]")).toHaveCount(0); // Of one topic. await open(page, { topic: CUPS }); await page.locator("[data-library-accounts] summary").click(); await expect(page.locator("[data-library-account]")).toHaveCount(1); await expect(page.locator('[data-library-account="cup_other"]')).toContainText("2 posts · 0 kept"); }); test("the organise, publish and archive routes take a press from umtool's own page only, and stamp it the operator's", async ({ request, baseURL }) => { const page = asPage(baseURL); const bodies: Record> = { organise: { lib: LIB, op: "rename", topic: "lids", title: "Lids and tops", by: "agent" }, publish: { lib: LIB, topic: KETTLES, dryRun: true, by: "agent" }, archive: { lib: LIB, handle: "lid_watch", dryRun: true, fetch: true }, }; // A request no browser sent — curl, a script, an agent, with a bearer or with nothing — is refused before its body is read. const before = (await stub(request)).requests.length; for (const [route, data] of Object.entries(bodies)) { for (const headers of [{}, { authorization: "Bearer umtool-e2e-token" }, { ...page, authorization: "Bearer umtool-e2e-token" }, { origin: "https://example.org", "sec-fetch-site": "cross-site" }] as Record[]) { const res = await request.post(`/api/library/${route}`, { headers, data }); expect(res.status(), `${route} ${JSON.stringify(headers)}`).toBe(403); if (!headers.origin || headers.authorization) expect((await res.json()).error).toContain("`organise-trending` / `publish-trending` or `umtool library`"); } } for (const data of [{ lib: LIB, op: "merge", into: KETTLES, from: [CUPS] }, { lib: LIB, topic: KETTLES, confirm: "FAKE-TEST-VALUE01234567" }]) { expect((await request.post(`/api/library/${"op" in data ? "organise" : "publish"}`, { data })).status()).toBe(403); } expect((await stub(request)).requests.length).toBe(before); // The page's own press, whatever its body says about who. expect((await request.post("/api/library/organise", { headers: page, data: bodies.organise })).status()).toBe(200); expect((await last(request)).body).toEqual({ slug: LIB, op: "rename", topic: "lids", title: "Lids and tops", by: "operator" }); // No post kept by the operator: the editor's refusal, in its words. const none = await request.post("/api/library/publish", { headers: page, data: bodies.publish }); expect(none.status()).toBe(400); expect((await none.json()).error).toContain('The operator has kept no post in "Kettle festival"'); expect((await last(request)).body).toEqual({ slug: LIB, topic: KETTLES, dryRun: true, by: "operator" }); // A publish with no dry run's confirm asks the editor nothing. const asked = (await stub(request)).requests.length; const noConfirm = await request.post("/api/library/publish", { headers: page, data: { lib: LIB, topic: KETTLES } }); expect(noConfirm.status()).toBe(400); expect((await noConfirm.json()).error).toContain('carries the "confirm" its dry run answered'); expect((await stub(request)).requests.length).toBe(asked); // Archive never sends "fetch", even when the body asks for it. const dry = await request.post("/api/library/archive", { headers: page, data: bodies.archive }); expect(dry.status()).toBe(200); expect((await last(request)).body).toEqual({ slug: LIB, handle: "lid_watch", action: "archive", dryRun: true }); expect((await dry.json()).sentence).toBe('Would make the channel "lid_watch-x" for @lid_watch, on no site, with sync off. Nothing is asked of X.'); // An agent's merge cannot come through the page's route dressed as the operator's without a browser; from the page it is the operator's. expect((await request.post("/api/library/organise", { headers: page, data: { lib: LIB, op: "merge", into: KETTLES, from: ["lids"], by: "agent" } })).status()).toBe(200); expect((await last(request)).body).toEqual({ slug: LIB, op: "merge", into: KETTLES, from: ["lids"], by: "operator" }); // The accounts read: umtool's own pages only, never cached. const read = await request.get(`/api/library/accounts?lib=${LIB}`); expect(read.status()).toBe(200); expect(read.headers()["cache-control"]).toBe("private, no-store"); expect((await read.json()).accounts.map((a: { handle: string }) => a.handle)).toContain("teapot_news"); expect((await request.get(`/api/library/accounts?lib=${LIB}`, { headers: { "sec-fetch-site": "cross-site" } })).status()).toBe(403); expect((await request.get("/api/library/accounts?lib=../x")).status()).toBe(400); }); test("with the editor down, or holding no library, the page says so in a sentence", async ({ page, request }) => { await reset(request, "unreachable"); await page.goto(lib()); const gone = page.locator("[data-library-unavailable]"); await expect(gone).toHaveAttribute("data-library-unavailable", "unreachable", { timeout: 30_000 }); await expect(gone).toContainText("The editor is not answering at"); await expect(gone).toContainText("The library is read and judged through the editor: start it, then reload."); await expect(page.locator("[data-library-post]")).toHaveCount(0); // The shell is whole. await expect(page.locator("[data-app-rail]")).toBeVisible(); await reset(request, "none"); await page.goto(lib()); await expect(gone).toHaveAttribute("data-library-unavailable", "no-library"); await expect(gone).toContainText("The editor has no X trending library."); });