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

main
ts 192 lines 8.2 KB
Raw
1 import { readdir, readFile } from "node:fs/promises";
2 import * as path from "node:path";
3 import * as Playwright from "@alchemy.run/cloudflare-test-tools/e2e/Playwright";
4 import { expect, test } from "@playwright/test";
5
6 const FIXTURE_VALUE = "hello-from-astro-binding";
7
8 for (const mode of Playwright.SERVER_METHODS) {
9 test.describe(mode, () => {
10 const it = Playwright.make(mode);
11
12 it("renders the SSR page with the binding and hydrates the client script", async ({
13 page,
14 server,
15 }) => {
16 const response = await page.goto(server.url.toString());
17 expect(response?.status()).toBe(200);
18 await expect(page.locator("#mode")).toHaveText("on-demand");
19 await expect(page.locator("#binding")).toHaveText(FIXTURE_VALUE);
20 await page.evaluate(() => document.fonts.ready);
21
22 await expect(page).toHaveScreenshot("index.png", {
23 animations: "disabled",
24 maxDiffPixelRatio: 0.03,
25 });
26
27 const button = page.locator("#counter");
28 await expect(button).toHaveText("count is 0");
29 await button.click();
30 await expect(button).toHaveText("count is 1");
31 });
32
33 it("serves the prerendered page", async ({ server }) => {
34 const response = await server.fetch("/about/");
35 expect(response.status).toBe(200);
36 const html = await response.text();
37 expect(html).toContain('id="mode"');
38 expect(html).toContain("prerendered");
39 });
40
41 it("prerenders inside workerd: top-level cloudflare:workers import + binding", async ({
42 server,
43 }) => {
44 // The page is impossible under node prerendering (top-level
45 // `cloudflare:workers` import); the baked binding value proves the
46 // workerd prerenderer ran with the worker's bindings attached.
47 const response = await server.fetch("/prerendered-env/");
48 expect(response.status).toBe(200);
49 const html = await response.text();
50 expect(html).toContain("prerendered-workerd");
51 expect(html).toContain(FIXTURE_VALUE);
52 });
53
54 it("serves an astro:assets image from a prerendered page", async ({ server }) => {
55 const response = await server.fetch("/prerendered-image/");
56 expect(response.status).toBe(200);
57 const img = (await response.text()).match(/<img[^>]*id="swatch"[^>]*>/)?.[0];
58 const src = img?.match(/src="([^"]+)"/)?.[1]?.replaceAll("&amp;", "&");
59 // The passthrough image service never emits static variants, so even a
60 // prerendered page points at the image endpoint.
61 expect(src).toMatch(/^\/_image\?href=/);
62
63 const image = await server.fetch(src!);
64 expect(image.status).toBe(200);
65 const bytes = new Uint8Array(await image.arrayBuffer());
66 expect([...bytes.subarray(0, 4)]).toEqual([0x89, 0x50, 0x4e, 0x47]);
67 });
68
69 it("reads the binding from the API route", async ({ server }) => {
70 const json = await server.fetchJson<{
71 value: string | null;
72 hasAssetsBinding: boolean;
73 }>("/api/hello");
74 expect(json).toMatchObject({
75 value: FIXTURE_VALUE,
76 hasAssetsBinding: true,
77 });
78 });
79
80 it("honors astro.config.mjs: user integration route + user vite define", async ({ server }) => {
81 // The /user-integration route only exists because the user's own
82 // astro.config.mjs was loaded natively: a user integration injects it,
83 // and the page renders a value produced by the file's `vite.define`.
84 const response = await server.fetch("/user-integration");
85 expect(response.status).toBe(200);
86 const html = await response.text();
87 expect(html).toContain("injected-by-user-integration");
88 expect(html).toContain("hello-from-user-vite-define");
89 });
90
91 it("runs middleware: locals + response-header mutation", async ({ server }) => {
92 const response = await server.fetch("/locals");
93 expect(response.status).toBe(200);
94 expect(response.headers.get("x-middleware")).toBe("hit");
95 expect(await response.text()).toContain("set-by-middleware");
96 });
97
98 it("serves the configured redirect", async ({ server }) => {
99 if (mode === "dev") {
100 // Dev fetch forwards the init: observe the raw 3xx + location.
101 const response = await server.fetch("/old-about", {
102 redirect: "manual",
103 });
104 expect([301, 302, 308]).toContain(response.status);
105 expect(response.headers.get("location")).toContain("/about");
106 } else {
107 // The live server's fetch (miniflare dispatchFetch) does not forward
108 // the init, so assert the redirect end-to-end: following /old-about
109 // lands on the prerendered about page.
110 const response = await server.fetch("/old-about");
111 expect(response.status).toBe(200);
112 const html = await response.text();
113 expect(html).toContain('id="mode"');
114 expect(html).toContain("prerendered");
115 }
116 });
117
118 it("returns binary data from an endpoint", async ({ server }) => {
119 const response = await server.fetch("/api/binary");
120 expect(response.status).toBe(200);
121 expect(response.headers.get("content-type")).toBe("application/octet-stream");
122 const bytes = new Uint8Array(await response.arrayBuffer());
123 expect([...bytes]).toEqual([
124 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x01, 0x02, 0x03,
125 ]);
126 expect(response.headers.get("x-binary-length")).toBe(String(bytes.length));
127 });
128
129 it("serves the content-collection-driven prerendered page", async ({ server }) => {
130 const response = await server.fetch("/posts/hello-world/");
131 expect(response.status).toBe(200);
132 const html = await response.text();
133 expect(html).toContain('id="post-title"');
134 expect(html).toContain("Hello World");
135 expect(html).toContain("content-collection body");
136 });
137
138 it("round-trips Astro.session across requests (zero-config sessions)", async ({ server }) => {
139 // Plain HTTP against the listening URL in both modes — the live
140 // server's dispatchFetch helper does not forward request init.
141 const first = await fetch(new URL("/session", server.url));
142 expect(first.status).toBe(200);
143 expect(await first.text()).toContain('<p id="count">1</p>');
144 const setCookie = first.headers.get("set-cookie");
145 expect(setCookie).toBeTruthy();
146 const cookie = setCookie!.split(";")[0]!;
147 const second = await fetch(new URL("/session", server.url), {
148 headers: { cookie },
149 });
150 expect(second.status).toBe(200);
151 expect(await second.text()).toContain('<p id="count">2</p>');
152 });
153
154 if (mode === "live") {
155 it("emits _redirects and serves the redirect from the asset layer", async ({ server }) => {
156 const content = await readFile(path.join(process.cwd(), "dist/client/_redirects"), "utf-8");
157 expect(content).toMatch(/\/old-about\s+\/about\/\s+301/);
158 const response = await fetch(new URL("/old-about", server.url), {
159 redirect: "manual",
160 });
161 expect([301, 302, 308]).toContain(response.status);
162 expect(response.headers.get("location")).toContain("/about");
163 });
164
165 it("emits _headers and serves hashed assets with immutable Cache-Control", async ({
166 server,
167 }) => {
168 const content = await readFile(path.join(process.cwd(), "dist/client/_headers"), "utf-8");
169 expect(content).toContain("/_astro/*");
170 expect(content).toContain("Cache-Control: public, max-age=31536000, immutable");
171 const assets = await readdir(path.join(process.cwd(), "dist/client/_astro"));
172 expect(assets.length).toBeGreaterThan(0);
173 const response = await fetch(new URL(`/_astro/${assets[0]}`, server.url));
174 expect(response.status).toBe(200);
175 expect(response.headers.get("cache-control")).toBe("public, max-age=31536000, immutable");
176 await response.arrayBuffer();
177 });
178 }
179
180 it("serves public assets", async ({ server }) => {
181 const response = await server.fetch("/robots.txt");
182 expect(response.status).toBe(200);
183 expect(await response.text()).toContain("User-agent");
184 });
185
186 it("falls back through ASSETS to the 404 page", async ({ server }) => {
187 const response = await server.fetch("/definitely-not-a-route");
188 expect(response.status).toBe(404);
189 await response.arrayBuffer();
190 });
191 });
192 }