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

main
ts 114 lines 4.49 KB
Raw
1 import * as NodeChildProcess from "node:child_process";
2 import * as NodeFs from "node:fs/promises";
3 import * as NodePath from "node:path";
4 import { fileURLToPath } from "node:url";
5 /**
6 * Dev v2 ("hmr" mode) spec: spawns `scripts/hmr-server.mjs` — the Framework
7 * service driving the real `next dev` (Turbopack) with proxied Cloudflare
8 * bindings — in a CHILD Node process (next's require hooks collide with
9 * playwright's in-worker transform, and a separate process is how the dev
10 * server runs in real life), then exercises over HTTP:
11 *
12 * - the page serves through the real `next dev` (Turbopack) server
13 * - `getCloudflareContext().env` sees a binding value in SSR output —
14 * without `initOpenNextCloudflareForDev()` and without wrangler
15 * - the same binding read from an API route
16 * - an edit to a page file is reflected on a subsequent request (bounded
17 * retry; the file is restored in a finally)
18 */
19 import { expect, test } from "@playwright/test";
20
21 const root = fileURLToPath(new URL("..", import.meta.url));
22 const probePage = NodePath.join(root, "app", "hmr-probe", "page.jsx");
23
24 let child: NodeChildProcess.ChildProcess;
25 let url: URL;
26
27 /** GET `path` until `predicate(body)` holds — bounded polling. */
28 const pollText = async (
29 path: string,
30 predicate: (body: string) => boolean,
31 { times = 60 }: { times?: number } = {},
32 ): Promise<string> => {
33 let last = "";
34 for (let i = 0; i < times; i++) {
35 try {
36 const response = await fetch(new URL(path, url));
37 const body = await response.text();
38 last = `status ${response.status}: ${body.slice(0, 500).replace(/\n/g, " ")}`;
39 if (response.status === 200 && predicate(body)) return body;
40 } catch (error) {
41 last = String(error);
42 }
43 await new Promise((resolve) => setTimeout(resolve, 500));
44 }
45 throw new Error(`not ready after ${times} attempts: ${last}`);
46 };
47
48 test.beforeAll(async () => {
49 // Cold `next dev` prepare + first Turbopack compile can be slow.
50 test.setTimeout(300_000);
51 child = NodeChildProcess.spawn(
52 process.execPath,
53 [NodePath.join(root, "scripts", "hmr-server.mjs")],
54 { cwd: root, stdio: ["ignore", "pipe", "inherit"] },
55 );
56 url = await new Promise<URL>((resolve, reject) => {
57 let buffer = "";
58 child.stdout!.on("data", (chunk: Buffer) => {
59 buffer += chunk.toString();
60 const match = buffer.match(/DEV_URL (\S+)/);
61 if (match !== null) resolve(new URL(match[1]!));
62 });
63 child.once("exit", (code) =>
64 reject(new Error(`hmr-server exited before reporting a URL (code ${code}): ${buffer}`)),
65 );
66 });
67 });
68
69 test.afterAll(async () => {
70 if (child !== undefined && child.exitCode === null) {
71 const exited = new Promise<void>((resolve) => child.once("exit", () => resolve()));
72 child.kill("SIGTERM");
73 // The driver closes the Next app + binding proxy on SIGTERM; escalate
74 // if it wedges so the suite never hangs on teardown.
75 const timeout = setTimeout(() => child.kill("SIGKILL"), 15_000);
76 await exited;
77 clearTimeout(timeout);
78 }
79 });
80
81 test.describe("hmr dev mode", () => {
82 test("serves the SSR page through next dev", async () => {
83 const body = await pollText("/", (b) => b.includes("fixtures-nextjs SSR page"));
84 expect(body).toContain("rendered-at:");
85 });
86
87 test("getCloudflareContext().env binding is visible in SSR output", async () => {
88 const body = await pollText("/hmr-probe", (b) => b.includes("hmr-binding:"));
89 // React may render an empty comment between adjacent text nodes.
90 expect(body).toMatch(/hmr-binding:(?:<!-- -->)?hello-from-binding/);
91 expect(body).toContain("marker:v1");
92 });
93
94 test("reads a Text binding through getCloudflareContext in an API route", async () => {
95 const response = await fetch(new URL("/api/binding", url));
96 expect(response.status).toBe(200);
97 const json = (await response.json()) as { value: string | null };
98 expect(json.value).toBe("hello-from-binding");
99 });
100
101 test("reflects a page edit on a subsequent request", async () => {
102 test.setTimeout(180_000);
103 // Warm the page first so the edit is an incremental recompile.
104 await pollText("/hmr-probe", (b) => b.includes("marker:v1"));
105 const original = await NodeFs.readFile(probePage, "utf8");
106 try {
107 await NodeFs.writeFile(probePage, original.replace("marker:v1", "marker:v2"), "utf8");
108 const body = await pollText("/hmr-probe", (b) => b.includes("marker:v2"), { times: 120 });
109 expect(body).toContain("marker:v2");
110 } finally {
111 await NodeFs.writeFile(probePage, original, "utf8");
112 }
113 });
114 });