The best way to control Claude Code, Codex and more from any computer or phone

main
ts 95 lines 3.96 KB
Raw
1 import * as Playwright from "@alchemy.run/cloudflare-test-tools/e2e/Playwright";
2 import { expect, test } from "@playwright/test";
3
4 for (const mode of Playwright.SERVER_METHODS) {
5 test.describe(mode, () => {
6 const it = Playwright.make(mode);
7
8 it("server-renders the homepage markup", async ({ server }) => {
9 // Raw fetch, no JS: the hero copy must already be in the HTML the
10 // worker streamed (SSR), not injected after hydration.
11 const response = await server.fetch("/");
12 expect(response.status).toBe(200);
13 const html = await response.text();
14 expect(html).toContain("Start simple, ship quickly.");
15 expect(html).toContain("TanStack Start Starter");
16 });
17
18 it("renders the homepage", async ({ page, server }) => {
19 const response = await page.goto(server.url.toString());
20 expect(response?.status()).toBe(200);
21 await page.evaluate(() => document.fonts.ready);
22
23 await expect(page).toHaveScreenshot("index.png", {
24 animations: "disabled",
25 maxDiffPixelRatio: 0.03,
26 });
27
28 await page.click("a[href='/about']");
29 await page.waitForURL("**/about");
30 await page.evaluate(() => document.fonts.ready);
31
32 await expect(page).toHaveScreenshot("about.png", {
33 animations: "disabled",
34 maxDiffPixelRatio: 0.03,
35 });
36 });
37
38 it("hydrates: the theme toggle mutates the document theme", async ({ page, server }) => {
39 await page.goto(server.url.toString());
40 const toggle = page.getByRole("button", { name: /Theme mode/ });
41 // The client effect ran (SSR renders "Auto" and hydration keeps it
42 // until clicked).
43 await expect(toggle).toHaveText("Auto");
44 // In dev the click can land before hydration attaches the handler —
45 // retry, but only click while the toggle still reads "Auto" so a
46 // registered-but-slow click never advances the cycle twice.
47 await expect(async () => {
48 if ((await toggle.textContent()) === "Auto") {
49 await toggle.click();
50 }
51 await expect(toggle).toHaveText("Light", { timeout: 500 });
52 }).toPass({ timeout: 15_000 });
53 await expect(page.locator("html")).toHaveAttribute("data-theme", "light");
54 });
55
56 it("serves a direct navigation to /about (SSR)", async ({ page, server }) => {
57 // Raw fetch: the about copy is server-rendered.
58 const raw = await server.fetch("/about");
59 expect(raw.status).toBe(200);
60 expect(await raw.text()).toContain("A small starter with room to grow.");
61
62 // Hard browser navigation straight to the route (no client-side
63 // transition from /).
64 const response = await page.goto(new URL("/about", server.url).toString());
65 expect(response?.status()).toBe(200);
66 await expect(page.locator("h1")).toHaveText("A small starter with room to grow.");
67 });
68
69 it("serves static assets", async ({ server }) => {
70 // Assets-first routing (`runWorkerFirst` unset, preview
71 // `invoke_user_worker_ahead_of_assets: false`) — static files are
72 // answered by the asset layer without invoking the worker.
73 const response = await server.fetch("/robots.txt");
74 expect(response.status).toBe(200);
75 expect(await response.text()).toContain("User-agent");
76 });
77
78 it("returns 404 for an unmatched route", async ({ server }) => {
79 const response = await server.fetch("/definitely/not/a/route");
80 expect(response.status).toBe(404);
81 });
82
83 // Needs a Postgres database; skipped unless `TEST_POSTGRES_URL` is set.
84 const skipIfNoDatabase = process.env.TEST_POSTGRES_URL ? it : it.skip;
85 skipIfNoDatabase("fetches database", async ({ server }) => {
86 const response = await server.fetchJson<[{ "?column?": number }]>("/api/db");
87 expect(response).toMatchObject([{ "?column?": 1 }]);
88 });
89
90 it("fetches WASM", async ({ server }) => {
91 const response = await server.fetchJson<{ result: number }>("/api/wasm");
92 expect(response).toMatchObject({ result: 3 });
93 });
94 });
95 }