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

main
ts 188 lines 8.68 KB
Raw
1 import * as NodeFs from "node:fs/promises";
2 import * as NodePath from "node:path";
3 import * as Playwright from "@alchemy.run/cloudflare-test-tools/e2e/Playwright";
4 import { expect, test } from "@playwright/test";
5
6 for (const mode of Playwright.SERVER_METHODS) {
7 test.describe(mode, () => {
8 const it = Playwright.make(mode);
9
10 it("SSR renders the dynamic page with the Text binding", async ({ page, server }) => {
11 const response = await page.goto(server.url.toString());
12 expect(response?.status()).toBe(200);
13 await expect(page.getByTestId("page-marker")).toHaveText("PAGE_MARKER");
14 await expect(page.getByTestId("layout-marker")).toHaveText("LAYOUT_MARKER");
15 // Read at request time from `cloudflare:workers` env — proves the rsc
16 // environment runs against the worker runtime in both modes.
17 await expect(page.getByTestId("env-message")).toHaveText("MESSAGE=hello-from-binding");
18 });
19
20 it("hydrates the client component", async ({ page, server }) => {
21 await page.goto(server.url.toString());
22 const counter = page.getByTestId("counter");
23 await expect(counter).toHaveText("count: 0");
24 // The first click can land before hydration attaches the listener;
25 // retry until the click observably increments.
26 await expect(async () => {
27 await counter.click();
28 await expect(counter).toHaveText(/count: [1-9]\d*/, { timeout: 500 });
29 }).toPass();
30 });
31
32 it("client-navigates to the static page", async ({ page, server }) => {
33 await page.goto(server.url.toString());
34 await page.click("a[href='/about']");
35 await page.waitForURL("**/about");
36 await expect(page.getByTestId("about-marker")).toHaveText("ABOUT_STATIC_MARKER");
37 });
38
39 it("serves the static asset", async ({ server }) => {
40 const response = await server.fetch("/hello.txt");
41 expect(response.status).toBe(200);
42 expect(await response.text()).toContain("hello from public/");
43 });
44
45 it("round-trips a server action (form mutation) through the worker", async ({
46 page,
47 server,
48 }) => {
49 await page.goto(server.url.toString());
50 await page.fill("[data-testid=greet-name]", "Waku");
51 // The submit can land before hydration attaches the action; retry
52 // until the round-trip observably completes. The MESSAGE suffix proves
53 // the action executed inside the worker runtime with bindings.
54 await expect(async () => {
55 await page.click("[data-testid=greet-submit]");
56 await expect(page.getByTestId("greet-output")).toHaveText(
57 "Hello, Waku! MESSAGE=hello-from-binding",
58 { timeout: 1_000 },
59 );
60 }).toPass();
61 });
62
63 it("SSRs the dynamic route with a path param", async ({ page, server }) => {
64 const response = await page.goto(new URL("/items/42", server.url).toString());
65 expect(response?.status()).toBe(200);
66 await expect(page.getByTestId("item-marker")).toHaveText("ITEM_MARKER id=42");
67 await expect(page.getByTestId("item-env")).toHaveText("MESSAGE=hello-from-binding");
68 });
69
70 it("serves the API route (GET) from the worker", async ({ server }) => {
71 const response = await server.fetch("/echo");
72 expect(response.status).toBe(200);
73 expect(await response.json()).toEqual({ message: "hello-from-binding" });
74 });
75
76 it("round-trips a POST through the API route", async ({ server }) => {
77 // Plain HTTP (not `server.fetch`): the live-mode dispatchFetch wrapper
78 // in the harness currently drops the RequestInit, losing POST bodies
79 // (packages/cloudflare-test-tools/src/miniflare/miniflare.ts `fetch(path)` has no init
80 // parameter). Both modes listen on a real socket, so this exercises
81 // the same worker path.
82 const response = await fetch(new URL("/echo", server.url), {
83 method: "POST",
84 headers: { "content-type": "application/json" },
85 body: JSON.stringify({ ping: "pong" }),
86 });
87 expect(response.status).toBe(200);
88 expect(await response.json()).toEqual({
89 echoed: { ping: "pong" },
90 message: "hello-from-binding",
91 });
92 });
93
94 it("runs the src/middleware header middleware on worker responses", async ({ server }) => {
95 // "/" is dynamic, so it always goes through the worker (in live mode
96 // static assets bypass the middleware by design).
97 const response = await server.fetch("/");
98 expect(response.status).toBe(200);
99 expect(response.headers.get("x-waku-middleware")).toBe("fixtures-waku");
100 });
101
102 // Asserts on the served HTML, not the hydrated DOM: this proves the page
103 // rendered inside workerd with bindings. Hydrating this route in a
104 // browser intermittently failed in CI with React #412 ("Connection
105 // closed") on Windows and macOS for reasons not yet understood, so the
106 // browser check was dropped; other routes cover hydration.
107 it("serves the page with a top-level cloudflare:workers import", async ({ server }) => {
108 const response = await server.fetch("/ssg-env");
109 expect(response.status).toBe(200);
110 const html = (await response.text()).replaceAll("<!-- -->", "");
111 expect(html).toContain(">SSG_ENV_MARKER<");
112 expect(html).toContain(">MESSAGE=hello-from-binding<");
113 if (mode === "live") {
114 // Prerendered at build time: the RSC payload is a static file.
115 const payload = await server.fetch("/custom-rsc/R/ssg-env.txt");
116 expect(payload.status).toBe(200);
117 expect(await payload.text()).toContain("hello-from-binding");
118 }
119 });
120
121 it("honors waku.config.ts (user vite plugin's virtual module)", async ({ page, server }) => {
122 // The page imports `virtual:fixtures-waku/user-config-marker`, served
123 // by a USER vite plugin declared in waku.config.ts — it renders only
124 // when the integration loads the user's config file natively and
125 // merges its own plugins over it instead of replacing the file.
126 const response = await page.goto(new URL("/config-marker", server.url).toString());
127 expect(response?.status()).toBe(200);
128 await expect(page.getByTestId("config-marker")).toHaveText("hello-from-waku-config");
129 });
130
131 it("keeps layout client state across client navigation", async ({ page, server }) => {
132 await page.goto(server.url.toString());
133 const counter = page.getByTestId("nav-counter");
134 await expect(counter).toHaveText("nav-count: 0");
135 await expect(async () => {
136 await counter.click();
137 await expect(counter).toHaveText(/nav-count: [1-9]\d*/, {
138 timeout: 500,
139 });
140 }).toPass();
141 const count = await counter.textContent();
142 await page.click("a[href='/about']");
143 await page.waitForURL("**/about");
144 await expect(page.getByTestId("about-marker")).toHaveText("ABOUT_STATIC_MARKER");
145 // The layout stays mounted through waku's client navigation, so the
146 // client component's state survives the page swap.
147 await expect(counter).toHaveText(count!);
148 });
149 });
150 }
151
152 // SSG output only exists in the production build: `dist/public` contains the
153 // prerendered HTML and RSC payload, served by the assets layer in live mode.
154 test.describe("live ssg", () => {
155 const it = Playwright.make("live");
156
157 it("serves the SSG page from assets", async ({ server }) => {
158 const response = await server.fetch("/about");
159 expect(response.status).toBe(200);
160 expect(await response.text()).toContain("ABOUT_STATIC_MARKER");
161 });
162
163 it("serves the SSG RSC payload from assets under the user-configured rscBase", async ({
164 server,
165 }) => {
166 // waku.config.ts sets `rscBase: "custom-rsc"` (default: "RSC") — the
167 // payload only lives here when the user's config file is honored.
168 const response = await server.fetch("/custom-rsc/R/about.txt");
169 expect(response.status).toBe(200);
170 const missing = await server.fetch("/RSC/R/about.txt");
171 expect(missing.status).toBe(404);
172 });
173
174 it("prerendered the cloudflare:workers page inside workerd with real bindings", async () => {
175 // The strongest SSG-in-workerd proof: the emitted static HTML for the
176 // page with a TOP-LEVEL `cloudflare:workers` import contains the binding
177 // value — the prerender ran inside workerd with the worker's env (in
178 // Node it would have failed with ERR_UNSUPPORTED_ESM_URL_SCHEME).
179 const html = await NodeFs.readFile(
180 NodePath.join(import.meta.dirname, "..", "dist", "public", "ssg-env", "index.html"),
181 "utf8",
182 );
183 expect(html).toContain("SSG_ENV_MARKER");
184 // React separates adjacent text nodes with `<!-- -->` in SSR output, so
185 // match with the comment stripped: `MESSAGE=<!-- -->hello-from-binding`.
186 expect(html.replaceAll("<!-- -->", "")).toContain("MESSAGE=hello-from-binding");
187 });
188 });