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

main
ts 168 lines 7.19 KB
Raw
1 import * as Playwright from "@alchemy.run/cloudflare-test-tools/e2e/Playwright";
2 import { expect, test } from "@playwright/test";
3
4 const FIXTURE_VALUE = "hello-from-astro-ssr-binding";
5
6 // This suite exercises the user-config wave's behavior
7 // (packages/frontend-frameworks/fixtures/astro-ssr/astro.config.mjs honored by the integration).
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 per request: binding + fresh request id", async ({ page, server }) => {
13 const response = await page.goto(server.url.toString());
14 expect(response?.status()).toBe(200);
15 await expect(page.locator("#mode")).toHaveText("on-demand");
16 await expect(page.locator("#binding")).toHaveText(FIXTURE_VALUE);
17
18 // Per-request rendering: two requests must carry different
19 // middleware-stamped request ids.
20 const firstId = await page.locator("#request-id").textContent();
21 expect(firstId).toBeTruthy();
22 expect(firstId).not.toBe("missing");
23 await page.reload();
24 const secondId = await page.locator("#request-id").textContent();
25 expect(secondId).toBeTruthy();
26 expect(secondId).not.toBe(firstId);
27 });
28
29 it("renders dynamic param routes on demand", async ({ server }) => {
30 const first = await server.fetch("/greet/world");
31 expect(first.status).toBe(200);
32 expect(await first.text()).toContain("Hello, world!");
33
34 // No getStaticPaths: any param renders — the defining SSR behavior.
35 const second = await server.fetch("/greet/another-visitor");
36 expect(second.status).toBe(200);
37 expect(await second.text()).toContain("Hello, another-visitor!");
38 });
39
40 it("handles a form POST server-side (browser submission)", async ({ page, server }) => {
41 await page.goto(new URL("/feedback", server.url).toString());
42 await expect(page.locator("#feedback-result")).toHaveCount(0);
43 await page.locator("#message").fill("hello from playwright");
44 await page.locator("#submit").click();
45 await expect(page.locator("#feedback-result")).toHaveText("received: hello from playwright");
46 });
47
48 it("handles a direct form POST (checkOrigin: false from the user config)", async ({
49 server,
50 }) => {
51 // Plain HTTP against the listening URL in both modes — the live
52 // server's dispatchFetch helper does not forward request init. No
53 // Origin header is sent: this passing proves the user config's
54 // `security.checkOrigin: false` is honored (the default would 403).
55 const response = await fetch(new URL("/feedback", server.url), {
56 method: "POST",
57 headers: { "content-type": "application/x-www-form-urlencoded" },
58 body: new URLSearchParams({ message: "direct-post" }).toString(),
59 });
60 expect(response.status).toBe(200);
61 expect(await response.text()).toContain("received: direct-post");
62 });
63
64 it("round-trips Astro.session across requests (zero-config sessions)", async ({ server }) => {
65 const first = await fetch(new URL("/session", server.url));
66 expect(first.status).toBe(200);
67 expect(await first.text()).toContain('<p id="count">1</p>');
68 const setCookie = first.headers.get("set-cookie");
69 expect(setCookie).toBeTruthy();
70 const cookie = setCookie!.split(";")[0]!;
71 const second = await fetch(new URL("/session", server.url), {
72 headers: { cookie },
73 });
74 expect(second.status).toBe(200);
75 expect(await second.text()).toContain('<p id="count">2</p>');
76 });
77
78 it("renders the streaming page (early + late chunks)", async ({ server }) => {
79 const response = await server.fetch("/stream");
80 expect(response.status).toBe(200);
81 const html = await response.text();
82 const early = html.indexOf('id="early"');
83 const late = html.indexOf('id="late"');
84 expect(early).toBeGreaterThan(-1);
85 expect(late).toBeGreaterThan(early);
86 });
87
88 it("serves the single prerendered exception (hybrid)", async ({ server }) => {
89 const response = await server.fetch("/about/");
90 expect(response.status).toBe(200);
91 const html = await response.text();
92 expect(html).toContain('id="mode"');
93 expect(html).toContain("prerendered");
94 });
95
96 it("reads the binding from the JSON API route", async ({ server }) => {
97 const json = await server.fetchJson<{
98 value: string | null;
99 hasAssetsBinding: boolean;
100 }>("/api/hello");
101 expect(json).toMatchObject({
102 value: FIXTURE_VALUE,
103 hasAssetsBinding: true,
104 });
105 });
106
107 it("echoes a JSON POST body through the API route", async ({ server }) => {
108 const response = await fetch(new URL("/api/hello", server.url), {
109 method: "POST",
110 headers: { "content-type": "application/json" },
111 body: JSON.stringify({ ping: "pong" }),
112 });
113 expect(response.status).toBe(200);
114 expect(await response.json()).toEqual({
115 echoed: { ping: "pong" },
116 method: "POST",
117 });
118 });
119
120 it("returns binary data from an endpoint", async ({ server }) => {
121 const response = await server.fetch("/api/binary");
122 expect(response.status).toBe(200);
123 expect(response.headers.get("content-type")).toBe("application/octet-stream");
124 const bytes = new Uint8Array(await response.arrayBuffer());
125 expect([...bytes]).toEqual([
126 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x04, 0x05, 0x06, 0x07,
127 ]);
128 expect(response.headers.get("x-binary-length")).toBe(String(bytes.length));
129 });
130
131 it("runs middleware on on-demand routes: locals + response headers", async ({ server }) => {
132 const response = await server.fetch("/greet/middleware-check");
133 expect(response.status).toBe(200);
134 expect(response.headers.get("x-middleware")).toBe("hit");
135 const requestId = response.headers.get("x-request-id");
136 expect(requestId).toBeTruthy();
137 // The header mirrors the locals value rendered into the page body.
138 expect(await response.text()).toContain(requestId!);
139 });
140
141 it("serves the redirect declared in astro.config.mjs", async ({ server }) => {
142 // The redirect target (/greet/astro) is an on-demand route, so the
143 // redirect must be handled dynamically by the worker in BOTH modes —
144 // there is no prerendered target for the asset layer to serve.
145 const response = await fetch(new URL("/legacy-greeting", server.url), {
146 redirect: "manual",
147 });
148 expect([301, 302, 308]).toContain(response.status);
149 expect(response.headers.get("location")).toContain("/greet/astro");
150
151 const followed = await fetch(new URL("/legacy-greeting", server.url));
152 expect(followed.status).toBe(200);
153 expect(await followed.text()).toContain("Hello, astro!");
154 });
155
156 it("serves public assets", async ({ server }) => {
157 const response = await server.fetch("/robots.txt");
158 expect(response.status).toBe(200);
159 expect(await response.text()).toContain("User-agent");
160 });
161
162 it("returns 404 for unmatched routes", async ({ server }) => {
163 const response = await server.fetch("/definitely-not-a-route");
164 expect(response.status).toBe(404);
165 await response.arrayBuffer();
166 });
167 });
168 }