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

main
ts 320 lines 11.8 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { describe } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7 import * as pathe from "pathe";
8 import { cloneFixture } from "../Utils/Fixture.ts";
9 import { expectResponse } from "../Utils/Http.ts";
10 import { expectWorkerExists, waitForWorkerToBeDeleted } from "../Utils/Worker.ts";
11
12 // The create-cloudflare (C3) templates built on `@cloudflare/vite-plugin`,
13 // as generated, deployed and under `alchemy dev`. Each keeps the official
14 // `cloudflare()` call in its Vite config, which alchemy neutralizes.
15
16 const { test } = Test.make({ providers: Cloudflare.providers() });
17 const { test: devTest } = Test.make({ providers: Cloudflare.providers(), dev: true });
18
19 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
20 const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp");
21
22 interface Template {
23 name: string;
24 fixture: string;
25 entries: Array<string>;
26 props: Omit<Cloudflare.Website.ViteProps, "rootDir">;
27 check: (url: string) => Effect.Effect<void, unknown>;
28 }
29
30 // The SPA templates: the HTML shell and its script, the Worker's `/api/`
31 // route, and the SPA fallback for client-side routes.
32 const checkSpaWithApi = (mountId: string) => (url: string) =>
33 Effect.gen(function* () {
34 let script: string | undefined;
35 yield* expectResponse(url, (res) => {
36 if (res.status !== 200 || !res.body.includes(`<div id="${mountId}">`))
37 return `${res.status} ${res.body.slice(0, 300)}`;
38 script = res.body.match(/<script type="module"[^>]* src="(\/[^"]+)"/)?.[1];
39 return script ? undefined : `no module script in ${res.body.slice(0, 300)}`;
40 });
41 yield* expectResponse(`${url}${script}`, (res) =>
42 res.status === 200 ? undefined : `script ${res.status} ${res.body.slice(0, 200)}`,
43 );
44 yield* expectResponse(`${url}/api/`, (res) =>
45 res.status === 200 && res.body === `{"name":"Cloudflare"}`
46 ? undefined
47 : `api ${res.status} ${res.body.slice(0, 200)}`,
48 );
49 // A browser navigation: other requests for unknown paths reach the Worker.
50 yield* expectResponse(
51 `${url}/client/route`,
52 (res) =>
53 res.status === 200 && res.body.includes(`<div id="${mountId}">`)
54 ? undefined
55 : `fallback ${res.status} ${res.body.slice(0, 200)}`,
56 { headers: { "sec-fetch-mode": "navigate" } },
57 );
58 });
59
60 // A server-rendered page and the same-origin stylesheet it links.
61 const expectSsrPage = (url: string, text: string) =>
62 Effect.gen(function* () {
63 let stylesheet: string | undefined;
64 yield* expectResponse(url, (res) => {
65 if (res.status !== 200 || !res.body.includes(text))
66 return `${res.status} ${res.body.slice(0, 300)}`;
67 stylesheet = res.body.match(/rel="stylesheet" href="(\/[^"]+)"/)?.[1];
68 return stylesheet
69 ? undefined
70 : `no stylesheet link in ${JSON.stringify(res.body.match(/<link[^>]*>/g))}`;
71 });
72 // Tailwind v4's output (minified builds drop its banner).
73 yield* expectResponse(new URL(stylesheet!.replaceAll("&amp;", "&"), url).toString(), (res) =>
74 res.status === 200 && /tailwindcss|@layer properties/.test(res.body)
75 ? undefined
76 : `css ${res.status} ${res.body.slice(0, 200)}`,
77 );
78 });
79
80 const TEMPLATES: ReadonlyArray<Template> = [
81 {
82 // `npm create cloudflare -- --framework=vike`: create-vike's React
83 // starter, whose Worker entry is the virtual module `vike:server`.
84 name: "vike",
85 fixture: "vite-vike-template-fixture",
86 entries: [
87 "package.json",
88 "vite.config.ts",
89 "wrangler.jsonc",
90 "tsconfig.json",
91 "assets",
92 "components",
93 "pages",
94 ],
95 props: {
96 main: "vike:server",
97 compatibility: { date: "2026-03-12" },
98 },
99 check: (url) =>
100 Effect.gen(function* () {
101 let stylesheet: string | undefined;
102 yield* expectResponse(url, (res) => {
103 if (res.status !== 200 || !res.body.includes("<h1>My Vike app</h1>"))
104 return `${res.status} ${res.body.slice(0, 300)}`;
105 stylesheet = res.body.match(/rel="stylesheet"[^>]* href="(\/[^"]+)"/)?.[1];
106 return stylesheet ? undefined : `no stylesheet in ${res.body.slice(0, 300)}`;
107 });
108 yield* expectResponse(`${url}${stylesheet}`, (res) =>
109 res.status === 200 ? undefined : `css ${res.status} ${res.body.slice(0, 200)}`,
110 );
111 // A page whose `+data` hook runs on the server.
112 yield* expectResponse(`${url}/todo`, (res) =>
113 res.status === 200 && res.body.includes("Buy strawberries")
114 ? undefined
115 : `todo ${res.status} ${res.body.slice(0, 200)}`,
116 );
117 }),
118 },
119 {
120 // `npm create cloudflare -- --framework=redwood`: create-rwsdk's
121 // starter, server components rendered in the `worker` environment.
122 name: "redwood",
123 fixture: "vite-redwood-template-fixture",
124 entries: [
125 "package.json",
126 "vite.config.mts",
127 "wrangler.jsonc",
128 "tsconfig.json",
129 "public",
130 "src",
131 "types",
132 ],
133 props: {
134 main: "./src/worker.tsx",
135 compatibility: { date: "2025-08-21", flags: ["nodejs_compat"] },
136 viteEnvironments: { entry: "worker" },
137 },
138 check: (url) =>
139 Effect.gen(function* () {
140 let script: string | undefined;
141 yield* expectResponse(url, (res) => {
142 if (res.status !== 200 || !res.body.includes("Welcome to RedwoodSDK"))
143 return `${res.status} ${res.body.slice(0, 300)}`;
144 // The client entry, imported by an inline script.
145 script = res.body.match(/rel="modulepreload" href="(\/[^"]+)"/)?.[1];
146 return script ? undefined : `no client script in ${res.body.slice(0, 600)}`;
147 });
148 yield* expectResponse(`${url}${script}`, (res) =>
149 res.status === 200 ? undefined : `script ${res.status} ${res.body.slice(0, 200)}`,
150 );
151 yield* expectResponse(`${url}/favicon-light.svg`, (res) =>
152 res.status === 200 ? undefined : `favicon ${res.status}`,
153 );
154 }),
155 },
156 {
157 // `npm create cloudflare -- --framework=tanstack-start`: the
158 // `@tanstack/cli` Cloudflare starter, whose Worker entry is a package
159 // export, with Tailwind CSS and the TanStack devtools plugin.
160 name: "tanstack-start",
161 fixture: "vite-tanstack-start-template-fixture",
162 entries: [
163 "package.json",
164 "vite.config.ts",
165 "wrangler.jsonc",
166 "tsconfig.json",
167 "tsr.config.json",
168 "src",
169 ],
170 props: {
171 main: "@tanstack/react-start/server-entry",
172 compatibility: { date: "2025-09-02", flags: ["nodejs_compat"] },
173 },
174 check: (url) =>
175 Effect.gen(function* () {
176 yield* expectSsrPage(url, "Start simple, ship quickly.");
177 yield* expectResponse(`${url}/about`, (res) =>
178 res.status === 200 && res.body.includes("<title>TanStack Start Starter</title>")
179 ? undefined
180 : `about ${res.status} ${res.body.slice(0, 200)}`,
181 );
182 }),
183 },
184 {
185 // `npm create cloudflare -- --framework=react`: create-vite's
186 // `react-ts` with a Worker for `/api/`.
187 name: "react",
188 fixture: "vite-react-template-fixture",
189 entries: [
190 "package.json",
191 "vite.config.ts",
192 "wrangler.jsonc",
193 "index.html",
194 "public",
195 "src",
196 "worker",
197 ],
198 props: {
199 main: "./worker/index.ts",
200 compatibility: { date: "2025-10-08" },
201 assets: { notFoundHandling: "single-page-application" },
202 },
203 check: checkSpaWithApi("root"),
204 },
205 {
206 // `npm create cloudflare -- --framework=vue`: create-vue with the
207 // router, TypeScript and Vue DevTools, and a Worker for `/api/`.
208 name: "vue",
209 fixture: "vite-vue-template-fixture",
210 entries: [
211 "package.json",
212 "vite.config.ts",
213 "wrangler.jsonc",
214 "env.d.ts",
215 "index.html",
216 "public",
217 "src",
218 "server",
219 ],
220 props: {
221 main: "./server/index.ts",
222 compatibility: { date: "2025-10-08" },
223 assets: { notFoundHandling: "single-page-application" },
224 },
225 check: checkSpaWithApi("app"),
226 },
227 {
228 // `npm create cloudflare -- --framework=react-router`: SSR through
229 // `createRequestHandler`, a loader reading `env` from
230 // `cloudflare:workers`, and Tailwind CSS.
231 name: "react-router",
232 fixture: "vite-react-router-fixture",
233 entries: [
234 "package.json",
235 "vite.config.ts",
236 "wrangler.jsonc",
237 "react-router.config.ts",
238 "app",
239 "public",
240 "workers",
241 ],
242 props: {
243 main: "./workers/app.ts",
244 compatibility: { date: "2025-10-08" },
245 env: { VALUE_FROM_CLOUDFLARE: "Hello from Cloudflare" },
246 },
247 check: (url) =>
248 Effect.gen(function* () {
249 let stylesheet: string | undefined;
250 yield* expectResponse(url, (res) => {
251 if (res.status !== 200) return `status ${res.status} ${res.body.slice(0, 300)}`;
252 if (!res.body.includes("Hello from Cloudflare")) return `body ${res.body.slice(0, 300)}`;
253 // Built: the hashed bundle. Dev: React Router's critical CSS.
254 stylesheet = res.body.match(/rel="stylesheet" href="(\/[^"]+)"/)?.[1];
255 return stylesheet
256 ? undefined
257 : `no stylesheet link in ${JSON.stringify(res.body.match(/<link[^>]*>/g))}`;
258 });
259 yield* expectResponse(
260 new URL(stylesheet!.replaceAll("&amp;", "&"), url).toString(),
261 (res) =>
262 res.status === 200 && res.body.includes("tailwindcss")
263 ? undefined
264 : `css ${res.status} ${res.body.slice(0, 200)}`,
265 );
266 yield* expectResponse(`${url}/favicon.ico`, (res) =>
267 res.status === 200 ? undefined : `favicon ${res.status}`,
268 );
269 yield* expectResponse(`${url}/missing`, (res) =>
270 res.status === 404 && res.body.includes("The requested page could not be found.")
271 ? undefined
272 : `missing ${res.status} ${res.body.slice(0, 200)}`,
273 );
274 }),
275 },
276 ];
277
278 describe("Website.Vite templates", () => {
279 for (const template of TEMPLATES) {
280 for (const mode of ["live", "dev"] as const) {
281 const id = `ViteTemplate${mode}${template.name.replace(/(^|-)(\w)/g, (_, _dash, c: string) => c.toUpperCase())}`;
282 (mode === "live" ? test : devTest).provider(
283 `Vite template: ${template.name}${mode === "dev" ? " (dev)" : ""}`,
284 (stack) =>
285 Effect.gen(function* () {
286 yield* stack.destroy();
287 const rootDir = yield* cloneFixture(
288 pathe.resolve(import.meta.dirname, template.fixture),
289 {
290 prefix: `alchemy-vite-template-${template.name}-`,
291 tempRoot,
292 entries: template.entries,
293 },
294 );
295 const site = yield* stack.deploy(
296 Cloudflare.Website.Vite(id, {
297 ...template.props,
298 rootDir,
299 workersDev: true,
300 memo: { include: ["**/*"] },
301 ...(mode === "dev" ? { dev: { port: 0 } } : {}),
302 }),
303 );
304 const url = site.url!.replace(/\/$/, "");
305 yield* template.check(url);
306 if (mode === "live") {
307 const { accountId } = yield* yield* CloudflareEnvironment;
308 yield* expectWorkerExists(site.workerName, accountId);
309 yield* stack.destroy();
310 yield* waitForWorkerToBeDeleted(site.workerName, accountId);
311 }
312 }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.ignore)), logLevel),
313 {
314 tags: ["provider:cloudflare:worker", mode === "dev" ? "local" : "live"],
315 timeout: 300_000,
316 },
317 );
318 }
319 }
320 });