import { test, expect, type Page } from "@playwright/test"; import * as urls from "./urls"; import { warm, WARM_TIMEOUT } from "./warm"; // --------------------------------------------------------------------------- // THE HOME PAGE STREAMS. Its shell — the rail, the header, Quick clip, what is // running, the recent cuts — is sent at once, and the Inbox (every project's // open decisions: the slow read) when it is done, behind a placeholder. // // Against the fixture that read is over in milliseconds, so the app server // honours a cookie (lib/streamDelay.ts; E2E_UMTOOL_STREAM_DELAY, set by // playwright.config.ts) that holds the Inbox back. With it: // 1. the shell is in the document before the Inbox is (its ORDER in the // response, not a clock); // 2. the Inbox starts where its placeholder started, at its width (nothing // sits below it in its column, so its height is open); // 3. a refresh never brings the placeholder back. // WRITES the fixture's REPORTS_ROOT/.umtool/lifecycle.json (zeta parked, then // active again), as inbox.spec does. // --------------------------------------------------------------------------- // A piece with open calls in the fixture (inbox.spec parks the same one). const ZETA = "a:articles/zeta"; const DELAY_MS = 1500; const TOLERANCE_PX = 4; async function holdInbox(page: Page, baseURL: string) { await page.context().addCookies([{ name: "e2e-stream-delay", value: String(DELAY_MS), url: baseURL }]); } function watchConsole(page: Page): string[] { const problems: string[] = []; page.on("console", (m) => { if (m.type() === "error") problems.push(m.text()); }); page.on("pageerror", (e) => problems.push(String(e))); return problems; } test.beforeAll(async ({ playwright }) => { test.setTimeout(WARM_TIMEOUT); await warm(playwright, ["/"]); }); test("the shell is sent before the Inbox", async ({ baseURL }) => { const res = await fetch(baseURL + urls.homeHref(), { headers: { "Accept-Encoding": "identity", Cookie: `e2e-stream-delay=${DELAY_MS}` }, }); expect(res.status).toBe(200); const html = await res.text(); // THE SHELL is everything the server sent before the Inbox: the document is // written in the order it was produced. An assertion about order, not about // a clock. // (The first mention of the filled block in ANY form: its markup, or the // component payload that describes it, whichever the server wrote first.) const filledAt = html.indexOf("data-stream-filled"); expect(filledAt, "the Inbox never streamed in").toBeGreaterThan(0); const shell = html.slice(0, filledAt); // The rail, the aside and the placeholder are in it; the count is not. expect(shell).toContain("data-app-rail"); expect(shell).toContain("data-home-aside"); expect(shell).toContain('data-stream-skeleton="inbox"'); expect(shell).not.toContain("data-inbox-count"); expect(html.slice(filledAt)).toContain("data-inbox-count"); expect(html.slice(filledAt)).toContain('data-stream-filled="inbox"'); }); test("the Inbox starts where its placeholder did, and a refresh never brings the placeholder back", async ({ page, request, baseURL }) => { const problems = watchConsole(page); await holdInbox(page, baseURL!); await page.goto(urls.homeHref(), { waitUntil: "commit" }); const placeholder = page.locator('[data-stream-skeleton="inbox"]'); await expect(placeholder).toBeVisible(); // The shell is usable while the Inbox is still being read. await expect(page.locator("[data-app-rail] [data-place=inbox]")).toBeVisible(); await expect(page.locator("[data-home-aside]")).toBeVisible(); const before = (await placeholder.boundingBox())!; const filled = page.locator('[data-stream-filled="inbox"]'); await expect(filled).toBeVisible({ timeout: 20_000 }); await expect(page.locator("[data-stream-skeleton]")).toHaveCount(0); await expect(page.locator("[data-inbox-count]")).toBeVisible(); const after = (await filled.boundingBox())!; const say = `placeholder ${JSON.stringify(before)} filled ${JSON.stringify(after)}`; expect(Math.abs(after.x - before.x), say).toBeLessThanOrEqual(TOLERANCE_PX); expect(Math.abs(after.y - before.y), say).toBeLessThanOrEqual(TOLERANCE_PX); expect(Math.abs(after.width - before.width), say).toBeLessThanOrEqual(TOLERANCE_PX); // The count's line is the placeholder's first line, to the pixel row. const line = (await page.locator("[data-inbox-count]").boundingBox())!; expect(Math.abs(line.y - before.y), say).toBeLessThanOrEqual(TOLERANCE_PX); // A refresh, with the Inbox held back again: the list on screen stays until // the new one is ready. A piece is parked out of band so the refresh has // something to bring in — the count moving is the proof that it landed. const count = page.locator("[data-inbox-count]"); const was = await count.getAttribute("data-inbox-count"); await page.evaluate(() => { const w = window as unknown as { __skeletons: number }; w.__skeletons = 0; new MutationObserver(() => { w.__skeletons += document.querySelectorAll("[data-stream-skeleton]").length; }).observe(document.body, { childList: true, subtree: true }); }); try { expect((await request.post("/api/pieces/lifecycle", { data: { piece: ZETA, state: "parked" } })).status()).toBe(200); await page.evaluate(() => (window as unknown as { next: { router: { refresh(): void } } }).next.router.refresh()); await expect(count).not.toHaveAttribute("data-inbox-count", was!, { timeout: 20_000 }); expect(await page.evaluate(() => (window as unknown as { __skeletons: number }).__skeletons)).toBe(0); } finally { await request.post("/api/pieces/lifecycle", { data: { piece: ZETA, state: "active" } }); } expect(problems, problems.join("\n")).toEqual([]); });