Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit 06edae8f3f06a3d2d610b7b8ddd7ba7420cd7c80
parent 6f1a7e1b2dd8ceacc39f6282797915e448a501f0
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Tue, 17 Nov 2026 19:22:20 -0500

x-trending S1: e2e — the form, one read over the fake browser, the library, a stale jar

editor/e2e/x-trending.spec.ts: the "X trending library" platform choice makes a
private library; Read a trending page runs one read-x-trending job over the test
server's fake browser (an invented page), which leaves responses.jsonl, page.png
and read.json and four posts in a topic; the channel page and GET /api/ops/trending
show them; a site cannot take the library; a cookie jar past the limit refuses the
next read before any job. No browser starts and nothing is asked of X.

Co-Authored-By: Claude <noreply@anthropic.com>

Diffstat:
Aeditor/e2e/x-trending.spec.ts | 168+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 168 insertions(+), 0 deletions(-)

diff --git a/editor/e2e/x-trending.spec.ts b/editor/e2e/x-trending.spec.ts @@ -0,0 +1,168 @@ +// AN X TRENDING LIBRARY end to end (lane x-trending, slice 1): the platform +// choice "X trending library" in the New channel form makes a private library +// (no URL, the Sites section replaced by "never on a site"); Read a trending +// page on its channel page runs ONE read-x-trending job on X's queue, which +// saves the page's answers raw and writes the posts it parses from the saved +// file; the page then shows the library by topic; `get trending` answers the +// same; a stale cookie jar refuses a second read before any job. +// +// X is NEVER contacted and no browser starts: the test server's fake browser +// (E2E_FAKE_X_PROFILE_COOKIES_FILE, common/social/fakeXProfileBrowser.ts) +// plays back the INVENTED trending page of +// common/social/__fixtures__/xTrendingCapture.ts. The "connected profile" is +// an empty directory and a cookie jar with a made-up login. + +import { test, expect } from "@playwright/test"; +import { mkdir, readFile, readdir, utimes, writeFile } from "node:fs/promises"; +import { generateReport, pathExists, readJson, resetData, resolvePath, testPaths, writeSettings, writeSite } from "./helpers"; +import { baseUrl } from "./baseUrl"; +// By relative path: a runtime import of the package specifier does not resolve +// under playwright's loader (digest.spec.ts says why). +import { FIXTURE_TREND_URL, fixtureTrendingResponses } from "../../common/social/__fixtures__/xTrendingCapture"; + +const TOKEN = "test-worker-token"; +const AUTH = { authorization: `Bearer ${TOKEN}` }; +const SLUG = "trending"; +const t = (p: string) => resolvePath(`${testPaths().transcriptsDir}/${p}`); +const rel = (p: string) => `${testPaths().transcriptsDir}/${p}`; + +type Config = { platform?: string; sourceKind?: string; url?: string; socialHandle?: string; postFetcher?: string; xTrending?: Record<string, number> }; + +const archiveLines = async (): Promise<number> => { + const file = rel(`channels/${SLUG}/posts-archive`); + return (await pathExists(file)) ? (await readFile(resolvePath(file), "utf8")).split("\n").filter(Boolean).length : 0; +}; + +async function seed() { + await resetData("empty"); + // The cookie-age limit is off on the test server unless a spec sets it (e2e/servers.ts). + await writeSettings({ minFreeDiskGB: 0, social: { x: { cookieSource: "profile", maxCookieAgeMinutes: 60 } } }); + await writeSite("demo-site", { siteTitle: "Demo" }); + // A connected X profile whose jar, written now, holds a (made-up) login. + await mkdir(t(".x-session/profile"), { recursive: true }); + await writeFile( + t(".x-session/cookies.txt"), + "# Netscape HTTP Cookie File\n.x.com\tTRUE\t/\tTRUE\t0\tauth_token\tfixture-not-a-real-token\n", + ); + // The fake browser's page: the invented trending page. + await writeFile( + t(".fake-x-profile-cookies.json"), + JSON.stringify({ + cookies: [{ name: "auth_token", value: "fixture-not-a-real-token", domain: ".x.com" }], + trending: fixtureTrendingResponses(), + }), + ); +} + +test("an X trending library: the form, one read, the library by topic, the listing, a stale jar", async ({ page, request }) => { + test.setTimeout(120_000); + await seed(); + await generateReport(page, "new"); + await page.goto("/channels/new"); + + await page.locator('select[name="platform"]').selectOption("x-trending"); + await expect(page.locator("[data-x-trending-no-site]")).toHaveText("An X trending library is private: it is never put on a site."); + await expect(page.locator("[data-x-trending-fields]")).toBeVisible(); + await expect(page.locator('input[name="socialHandle"]')).toHaveCount(0); + await expect(page.locator('input[name="fetchPostsNow"]')).toHaveCount(0); + await page.locator('input[name="name"]').fill("Trending"); + await page.locator('input[name="slug"]').fill(SLUG); + await page.getByRole("button", { name: /create|add channel|save/i }).first().click(); + + const configPath = rel(`channels/${SLUG}/config.json`); + await expect + .poll(async () => ((await pathExists(configPath)) ? await readJson<Config>(configPath) : null), { timeout: 15_000 }) + .toMatchObject({ platform: "twitter", sourceKind: "social", xTrending: {} }); + const config = await readJson<Config>(configPath); + expect(config.url).toBeUndefined(); + expect(config.socialHandle).toBeUndefined(); + expect(config.postFetcher).toBeUndefined(); + + // The library's page: nothing yet, and it says it is private. + await generateReport(page, SLUG); + await page.goto(`/channels/${SLUG}`); + await expect(page.locator("[data-x-trending-private]")).toHaveText("Private: on no site, in no build, not in search."); + await expect(page.getByLabel("trending topics")).toContainText("No topic yet."); + await expect(page.getByRole("button", { name: /fetch posts|sync new/i })).toHaveCount(0); + + // One read, into a named topic. + await page.getByLabel("trending page URL").fill(`${FIXTURE_TREND_URL}?src=trend_click`); + await page.getByLabel("topic", { exact: true }).fill("Kettle Festival"); + await page.getByRole("button", { name: "Read trending page" }).click(); + // Four posts: the promoted entry, the quoted post and another surface's post are not the page's. + await expect.poll(archiveLines, { timeout: 60_000 }).toBe(4); + + // What the read left: the raw answers, a screenshot, a record with names and sizes only. + const readsDir = t(`channels/${SLUG}/trending/reads`); + await expect.poll(async () => (await readdir(readsDir).catch(() => [])).length, { timeout: 15_000 }).toBe(1); + const [readId] = await readdir(readsDir); + expect(readId).toMatch(/^tr-[0-9A-Z]{26}$/); + await expect + .poll(async () => (await readdir(`${readsDir}/${readId}`)).sort(), { timeout: 15_000 }) + .toEqual(["page.png", "read.json", "responses.jsonl"]); + const record = await readJson<{ outcome: string; stop: string; scrolls: number; posts: { onPage: number; added: number }; operations: { op: string }[]; sentence: string }>( + rel(`channels/${SLUG}/trending/reads/${readId}/read.json`), + ); + expect(record.outcome).toBe("read"); + expect(record.stop).toBe("idle"); + expect(record.scrolls).toBe(3); + expect(record.posts).toEqual({ onPage: 4, added: 4 }); + expect(record.operations.map((o) => o.op)).toEqual(["ExampleTrendPage", "ExampleTrendTimeline", "HomeTimeline"]); + expect(record.sentence).toBe(`Read ${FIXTURE_TREND_URL}: 4 posts on the page, 4 new, in topic "Kettle Festival" (kettle-festival).`); + const recordText = await readFile(t(`channels/${SLUG}/trending/reads/${readId}/read.json`), "utf8"); + expect(recordText).not.toContain("teapot_news"); + expect(recordText).not.toContain("festival opens"); + + // The library by topic. + await page.reload(); + const topic = page.locator('[data-x-trending-topic="kettle-festival"]'); + await expect(topic).toContainText("Kettle Festival · 4 posts"); + await expect(topic).toContainText(FIXTURE_TREND_URL); + await expect(topic.locator("[data-x-trending-post]")).toHaveCount(4); + const first = topic.locator('[data-x-trending-post="1900000000000001001"]'); + await expect(first).toContainText("@teapot_news (Teapot News)"); + await expect(first).toContainText("2 media (image)"); + await expect(first).toContainText("The kettle festival opens today."); + await expect(topic.locator('[data-x-trending-post="1900000000000001004"]')).toContainText("1 media (video)"); + await expect(page.getByLabel("saved read")).toHaveValue(readId); + await expect(page.locator("body")).not.toContainText("Buy our kettles."); + await expect(page.locator("body")).not.toContainText("Not on the trending page."); + + // The same over ops: what an agent reads. Never the raw capture. + const listed = await request.get(`${baseUrl}/api/ops/trending?slug=${SLUG}&topic=kettle-festival`, { headers: AUTH }); + expect(listed.status()).toBe(200); + const body = (await listed.json()) as { + topics: { id: string; title: string; posts: number; sources: { url: string; reads: number }[] }[]; + posts: { id: string; author: string; text: string; media: { count: number; kinds: string[] }; topic: string; reads: { read: string }[] }[]; + total: number; + reads: string[]; + }; + expect(body.topics).toMatchObject([{ id: "kettle-festival", title: "Kettle Festival", posts: 4, sources: [{ url: FIXTURE_TREND_URL, reads: 1 }] }]); + expect(body.posts.map((p) => p.id)).toEqual(["1900000000000001001", "1900000000000001002", "1900000000000001004", "1900000000000001005"]); + expect(body.posts[3]).toMatchObject({ author: "lid_watch", media: { count: 1, kinds: ["gif"] }, topic: "kettle-festival", reads: [{ read: readId }] }); + expect(body.total).toBe(4); + expect(body.reads).toEqual([readId]); + expect(JSON.stringify(body)).not.toContain("fixtureQueryId"); + + // On no site, and a site cannot take it. + const site = await readJson<{ channels: { slug: string }[] }>(rel("sites/demo-site/site.json")); + expect(site.channels).toEqual([]); + const onSite = await request.post(`${baseUrl}/api/ops/channel-config`, { + headers: AUTH, + data: { slug: SLUG, sites: [{ siteId: "demo-site", groupId: "default" }] }, + }); + expect(onSite.status()).toBe(400); + expect(((await onSite.json()) as { error: string }).error).toBe("trending is an X trending library: it is private, and never put on a site."); + + // A cookie jar past the limit: the next read is refused before any job, with the guard's sentence. + const old = new Date(Date.now() - 2 * 60 * 60_000); + await utimes(t(".x-session/cookies.txt"), old, old); + const stale = await request.post(`${baseUrl}/api/ops/read-x-trending`, { + headers: AUTH, + data: { slug: SLUG, url: FIXTURE_TREND_URL }, + }); + expect(stale.status()).toBe(400); + expect(((await stale.json()) as { error: string }).error).toMatch(/^X cookies were last refreshed 2 h ago \(limit 60 min\)/); + expect((await readdir(readsDir)).length).toBe(1); + expect(await archiveLines()).toBe(4); +});