@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 204 lines 8.66 KB
Raw
1 import * as Playwright from "@alchemy.run/cloudflare-test-tools/e2e/Playwright";
2 import { expect, test } from "@playwright/test";
3
4 /**
5 * Requests target the server's real socket URL (not miniflare's
6 * `dispatchFetch("http://localhost/…")` helper): production requests always
7 * carry a real host:port, and OpenNext's middleware-rewrite handling
8 * re-fetches the rewritten absolute URL — a port-less `localhost` host would
9 * send that loopback fetch to port 80.
10 */
11 const get = (server: { url: URL }, path: string) => fetch(new URL(path, server.url));
12
13 for (const mode of Playwright.SERVER_METHODS) {
14 test.describe(mode, () => {
15 const it = Playwright.make(mode);
16
17 it("renders the SSR page", async ({ server }) => {
18 const response = await get(server, "/");
19 expect(response.status).toBe(200);
20 const body = await response.text();
21 expect(body).toContain("fixtures-nextjs SSR page");
22 expect(body).toContain("rendered-at:");
23 });
24
25 it("serves the API route with the middleware header", async ({ server }) => {
26 const response = await get(server, "/api/hello");
27 expect(response.status).toBe(200);
28 expect(response.headers.get("x-fixture-middleware")).toBe("passed");
29 const json = (await response.json()) as { hello: string };
30 expect(json.hello).toBe("world");
31 });
32
33 it("rewrites via middleware", async ({ server }) => {
34 const response = await get(server, "/mw-rewrite");
35 expect(response.status).toBe(200);
36 const json = (await response.json()) as { hello: string; url: string };
37 expect(json.hello).toBe("world");
38 expect(json.url).toContain("/api/hello");
39 });
40
41 it("reads a Text binding through getCloudflareContext", async ({ server }) => {
42 const response = await get(server, "/api/binding");
43 expect(response.status).toBe(200);
44 const json = (await response.json()) as { value: string | null };
45 expect(json.value).toBe("hello-from-binding");
46 });
47
48 it("serves the ISR page from the prerendered cache", async ({ server }) => {
49 const first = await get(server, "/isr");
50 expect(first.status).toBe(200);
51 const firstBody = await first.text();
52 const firstStamp = firstBody.match(/isr-rendered-at:(?:<!-- -->)?(\d+)/)?.[1];
53 expect(firstStamp).toBeDefined();
54
55 // The prerendered payload serves as-is: repeated hits inside the
56 // revalidate window return the same build-time stamp.
57 const second = await get(server, "/isr");
58 expect(second.status).toBe(200);
59 const secondBody = await second.text();
60 const secondStamp = secondBody.match(/isr-rendered-at:(?:<!-- -->)?(\d+)/)?.[1];
61 expect(secondStamp).toBe(firstStamp);
62 });
63
64 it("serves public/ static assets", async ({ server }) => {
65 const response = await get(server, "/static.txt");
66 expect(response.status).toBe(200);
67 expect(await response.text()).toContain("hello from a static asset");
68 });
69
70 it("serves _next/static client chunks", async ({ server }) => {
71 const html = await (await get(server, "/")).text();
72 const chunk = html.match(/\/_next\/static\/[^"'\s\\]+\.js/)?.[0];
73 expect(chunk).toBeDefined();
74 const response = await get(server, chunk!);
75 expect(response.status).toBe(200);
76 });
77
78 it("hydrates the client-interactive page", async ({ page, server }) => {
79 await page.goto(new URL("/counter", server.url.toString()).toString());
80 await expect(page.getByTestId("count")).toHaveText("count:0");
81 await page.getByTestId("increment").click();
82 await expect(page.getByTestId("count")).toHaveText("count:1");
83 await page.getByTestId("increment").click();
84 await expect(page.getByTestId("count")).toHaveText("count:2");
85 });
86
87 it("serves a prerendered dynamic segment (generateStaticParams)", async ({ server }) => {
88 const response = await get(server, "/products/alpha");
89 expect(response.status).toBe(200);
90 const body = await response.text();
91 expect(body).toContain("PRODUCT_PAGE_MARKER");
92 expect(body).toMatch(/product-slug:(?:<!-- -->)?alpha/);
93 });
94
95 it("renders a non-prerendered dynamic segment on demand", async ({ server }) => {
96 const response = await get(server, "/products/gamma");
97 expect(response.status).toBe(200);
98 expect(await response.text()).toMatch(/product-slug:(?:<!-- -->)?gamma/);
99 });
100
101 it("renders a catch-all dynamic segment", async ({ server }) => {
102 const response = await get(server, "/docs/guides/deploy/workers");
103 expect(response.status).toBe(200);
104 const body = await response.text();
105 expect(body).toContain("DOCS_CATCHALL_MARKER");
106 expect(body).toMatch(/docs-path:(?:<!-- -->)?guides\/deploy\/workers/);
107 });
108
109 it("mutates a KV binding through a server action form", async ({ page, server }) => {
110 const name = `visitor-${Date.now()}`;
111 await page.goto(new URL("/guestbook", server.url.toString()).toString());
112 await expect(page.locator("h1")).toHaveText("GUESTBOOK_MARKER");
113 await page.locator('input[name="name"]').fill(name);
114 await page.locator('button[type="submit"]').click();
115 // The action writes to FIXTURE_KV and revalidates; the page re-renders
116 // with the latest entry read back from KV.
117 await expect(page.locator("main")).toContainText(`guestbook-latest:${name}`);
118 });
119
120 it("streams the Suspense shell before the resolved content", async ({ server }) => {
121 // `accept-encoding: identity`: the local live (miniflare) serving path
122 // buffers gzip bodies to completion, which would hide the progressive
123 // flush this test asserts. Production Cloudflare streams gzip fine.
124 const response = await fetch(new URL("/streaming", server.url), {
125 headers: { "accept-encoding": "identity" },
126 });
127 expect(response.status).toBe(200);
128 const reader = response.body!.getReader();
129 const decoder = new TextDecoder();
130 let sawShellFirst = false;
131 let buffer = "";
132 for (;;) {
133 const { done, value } = await reader.read();
134 if (done) break;
135 buffer += decoder.decode(value, { stream: true });
136 if (
137 !sawShellFirst &&
138 buffer.includes("STREAMING_SUSPENSE_MARKER") &&
139 !buffer.includes("STREAMING_RESOLVED_MARKER")
140 ) {
141 // The shell (with the fallback) flushed before the slow segment.
142 sawShellFirst = true;
143 }
144 }
145 expect(sawShellFirst).toBe(true);
146 expect(buffer).toContain("STREAMING_RESOLVED_MARKER");
147 });
148
149 it("serves the custom not-found boundary for unmatched routes", async ({ server }) => {
150 const response = await get(server, "/definitely/not/a/route");
151 expect(response.status).toBe(404);
152 expect(await response.text()).toContain("CUSTOM_NOT_FOUND_MARKER");
153 });
154
155 it("serves the custom not-found boundary from notFound()", async ({ server }) => {
156 const response = await get(server, "/gone");
157 expect(response.status).toBe(404);
158 expect(await response.text()).toContain("CUSTOM_NOT_FOUND_MARKER");
159 });
160
161 it("renders the segment error boundary after a server render throws", async ({
162 page,
163 server,
164 }) => {
165 await page.goto(new URL("/boom", server.url.toString()).toString());
166 await expect(page.locator("h1")).toHaveText("ERROR_BOUNDARY_MARKER");
167 });
168
169 it("applies next.config redirects", async ({ server }) => {
170 const response = await fetch(new URL("/old-home", server.url), {
171 redirect: "manual",
172 });
173 expect(response.status).toBe(308);
174 expect(response.headers.get("location")).toContain("/");
175 });
176
177 it("applies next.config rewrites", async ({ server }) => {
178 const response = await get(server, "/rewritten-hello");
179 expect(response.status).toBe(200);
180 const json = (await response.json()) as { hello: string };
181 expect(json.hello).toBe("world");
182 });
183
184 it("applies next.config headers", async ({ server }) => {
185 const response = await get(server, "/api/hello");
186 expect(response.headers.get("x-fixture-config-header")).toBe("from-next-config");
187 });
188
189 it("serves a Pages Router page with getServerSideProps", async ({ server }) => {
190 const response = await get(server, "/legacy");
191 expect(response.status).toBe(200);
192 const body = await response.text();
193 expect(body).toContain("PAGES_ROUTER_MARKER");
194 expect(body).toMatch(/legacy-stamp:(?:<!-- -->)?pages-router-ssr/);
195 });
196
197 it("serves a Pages Router API route", async ({ server }) => {
198 const response = await get(server, "/api/legacy");
199 expect(response.status).toBe(200);
200 const json = (await response.json()) as { legacy: string };
201 expect(json.legacy).toBe("pages-api");
202 });
203 });
204 }