The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 3 | import { assert, describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as Cause from "effect/Cause"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Exit from "effect/Exit"; |
| 8 | import * as FileSystem from "effect/FileSystem"; |
| 9 | import * as Path from "effect/Path"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as pathe from "pathe"; |
| 12 | import { |
| 13 | foldkitFixture, |
| 14 | foldkitMemo, |
| 15 | verifyFoldkitBrowser, |
| 16 | verifyFoldkitRendering, |
| 17 | } from "../../Website/Foldkit.ts"; |
| 18 | import { cloneFixture } from "../Utils/Fixture.ts"; |
| 19 | import { expectDirectStatus, expectUrlContains } from "../Utils/Http.ts"; |
| 20 | import { expectWorkerExists, waitForWorkerToBeDeleted } from "../Utils/Worker.ts"; |
| 21 | |
| 22 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 23 | |
| 24 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 25 | |
| 26 | // Same rationale as Vite.test.ts: Vite's `vite:build-html` plugin expresses |
| 27 | // emitted asset paths relative to `cwd`, so the temp clone has to live under |
| 28 | // the same workspace root. |
| 29 | const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp"); |
| 30 | |
| 31 | const fixtureDir = pathe.resolve(import.meta.dirname, "foldkit-fixture"); |
| 32 | const workerFixtureDir = pathe.resolve(import.meta.dirname, "foldkit-worker-fixture"); |
| 33 | const ssrFixtureDir = pathe.resolve(import.meta.dirname, "foldkit-ssr-fixture"); |
| 34 | |
| 35 | const fixtureEntries = ["index.html", "package.json", "vite.config.ts", "src"]; |
| 36 | |
| 37 | // Restrict the input memo to fixture sources so the test isn't re-hashing |
| 38 | // the whole monorepo on every deploy. |
| 39 | const memoInclude = ["index.html", "src/**", "package.json", "vite.config.ts"]; |
| 40 | |
| 41 | const foldkitProps = (rootDir: string) => ({ |
| 42 | rootDir, |
| 43 | workersDev: true, |
| 44 | compatibility: { date: "2024-09-23", flags: ["nodejs_compat"] }, |
| 45 | memo: { include: memoInclude }, |
| 46 | }); |
| 47 | |
| 48 | const htmlPage = (marker: string) => `<!doctype html> |
| 49 | <html lang="en"> |
| 50 | <head> |
| 51 | <meta charset="UTF-8" /> |
| 52 | <title>${marker}</title> |
| 53 | </head> |
| 54 | <body> |
| 55 | <div id="root"></div> |
| 56 | <script type="module" src="./src/entry.ts"></script> |
| 57 | </body> |
| 58 | </html> |
| 59 | `; |
| 60 | |
| 61 | describe.concurrent( |
| 62 | "Foldkit", |
| 63 | { |
| 64 | tags: [ |
| 65 | "provider:cloudflare", |
| 66 | "provider:cloudflare:website", |
| 67 | "provider:cloudflare:worker", |
| 68 | "live", |
| 69 | ], |
| 70 | }, |
| 71 | () => { |
| 72 | // Client-only builds have no Foldkit SSR build plugin and receive SPA |
| 73 | // fallback; server-rendered builds expose metadata instead. |
| 74 | test.provider( |
| 75 | "Foldkit: a client-only app gets the single-page-application fallback", |
| 76 | (stack) => |
| 77 | Effect.gen(function* () { |
| 78 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 79 | |
| 80 | yield* stack.destroy(); |
| 81 | |
| 82 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 83 | prefix: "alchemy-foldkit-default-", |
| 84 | tempRoot, |
| 85 | entries: fixtureEntries, |
| 86 | }); |
| 87 | |
| 88 | const site = yield* stack.deploy( |
| 89 | Effect.gen(function* () { |
| 90 | // Deliberately no `assets` — the derived default is what is |
| 91 | // under test. |
| 92 | return yield* Cloudflare.Website.Foldkit("FixFoldkitDefault", foldkitProps(rootDir)); |
| 93 | }), |
| 94 | ); |
| 95 | |
| 96 | expect(site.url).toBeDefined(); |
| 97 | expect(site.hash?.input).toBeDefined(); |
| 98 | yield* expectWorkerExists(site.workerName, accountId); |
| 99 | |
| 100 | yield* expectUrlContains(`${site.url!}/`, "Foldkit Fixture", { |
| 101 | timeout: "30 seconds", |
| 102 | label: "foldkit index", |
| 103 | }); |
| 104 | // The deep link matches no file; the fallback answers it with the |
| 105 | // template and the app boots. A declaration still wins over the |
| 106 | // default; see the next case. |
| 107 | yield* expectUrlContains(`${site.url!}/counter/42`, "Foldkit Fixture", { |
| 108 | timeout: "15 seconds", |
| 109 | label: "foldkit deep link fallback", |
| 110 | }); |
| 111 | |
| 112 | yield* stack.destroy(); |
| 113 | yield* waitForWorkerToBeDeleted(site.workerName, accountId); |
| 114 | }).pipe(logLevel), |
| 115 | { timeout: 120_000 }, |
| 116 | ); |
| 117 | |
| 118 | // An explicit `assets` wins over the derived default. |
| 119 | test.provider( |
| 120 | "Foldkit: an explicit assets config is passed through", |
| 121 | (stack) => |
| 122 | Effect.gen(function* () { |
| 123 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 124 | |
| 125 | yield* stack.destroy(); |
| 126 | |
| 127 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 128 | prefix: "alchemy-foldkit-override-", |
| 129 | tempRoot, |
| 130 | entries: fixtureEntries, |
| 131 | }); |
| 132 | |
| 133 | const site = yield* stack.deploy( |
| 134 | Effect.gen(function* () { |
| 135 | return yield* Cloudflare.Website.Foldkit("FixFoldkitOverride", { |
| 136 | ...foldkitProps(rootDir), |
| 137 | assets: { notFoundHandling: "none" }, |
| 138 | }); |
| 139 | }), |
| 140 | ); |
| 141 | |
| 142 | expect(site.url).toBeDefined(); |
| 143 | yield* expectWorkerExists(site.workerName, accountId); |
| 144 | |
| 145 | // The app itself still serves... |
| 146 | yield* expectUrlContains(`${site.url!}/`, "Foldkit Fixture", { |
| 147 | timeout: "30 seconds", |
| 148 | label: "foldkit override index", |
| 149 | }); |
| 150 | // ...but with `notFoundHandling: "none"` the deep link is a miss |
| 151 | // rather than an index.html fallback. |
| 152 | yield* expectDirectStatus(`${site.url!}/counter/42`, 404, { |
| 153 | timeout: "15 seconds", |
| 154 | label: "foldkit override deep link", |
| 155 | }); |
| 156 | |
| 157 | yield* stack.destroy(); |
| 158 | yield* waitForWorkerToBeDeleted(site.workerName, accountId); |
| 159 | }).pipe(logLevel), |
| 160 | { timeout: 120_000 }, |
| 161 | ); |
| 162 | |
| 163 | // A client-only Foldkit deployment may carry a Worker entry in front of |
| 164 | // the assets (API routes, error reporting, Durable Objects). The client |
| 165 | // build still serves through the ASSETS binding, and the derived |
| 166 | // single-page-application fallback still applies behind it. |
| 167 | test.provider( |
| 168 | "Foldkit: a custom main entry serves API routes alongside the app", |
| 169 | (stack) => |
| 170 | Effect.gen(function* () { |
| 171 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 172 | |
| 173 | yield* stack.destroy(); |
| 174 | |
| 175 | const rootDir = yield* cloneFixture(workerFixtureDir, { |
| 176 | prefix: "alchemy-foldkit-worker-", |
| 177 | tempRoot, |
| 178 | entries: fixtureEntries, |
| 179 | }); |
| 180 | |
| 181 | const site = yield* stack.deploy( |
| 182 | Effect.gen(function* () { |
| 183 | return yield* Cloudflare.Website.Foldkit("FixFoldkitWorker", { |
| 184 | ...foldkitProps(rootDir), |
| 185 | main: "src/worker.ts", |
| 186 | assets: { runWorkerFirst: ["/api/*"] }, |
| 187 | env: { GREETING: "foldkit-worker-fixture" }, |
| 188 | }); |
| 189 | }), |
| 190 | ); |
| 191 | |
| 192 | expect(site.url).toBeDefined(); |
| 193 | yield* expectWorkerExists(site.workerName, accountId); |
| 194 | |
| 195 | // The Worker entry answers its own route from the binding. |
| 196 | yield* expectUrlContains(`${site.url!}/api/hello`, "foldkit-worker-fixture", { |
| 197 | timeout: "30 seconds", |
| 198 | label: "foldkit worker api", |
| 199 | }); |
| 200 | // Everything else passes through to the assets binding. |
| 201 | yield* expectUrlContains(`${site.url!}/`, "Foldkit Fixture", { |
| 202 | timeout: "15 seconds", |
| 203 | label: "foldkit worker index", |
| 204 | }); |
| 205 | // The derived fallback still answers the deep link through |
| 206 | // `env.ASSETS.fetch`. |
| 207 | yield* expectUrlContains(`${site.url!}/counter/42`, "Foldkit Fixture", { |
| 208 | timeout: "15 seconds", |
| 209 | label: "foldkit worker spa fallback", |
| 210 | }); |
| 211 | |
| 212 | yield* stack.destroy(); |
| 213 | yield* waitForWorkerToBeDeleted(site.workerName, accountId); |
| 214 | }).pipe(logLevel), |
| 215 | { timeout: 120_000 }, |
| 216 | ); |
| 217 | |
| 218 | // With `ssr.build` the one `vite build` also emits `dist/server/fetch.js`, |
| 219 | // and that handler is the Worker. The fixture prerenders `/about` and not |
| 220 | // `/`, and the build keeps the unfilled template out of the client |
| 221 | // output, so the front page has to reach the handler. Each page stamps |
| 222 | // the `count` query into its markup, which tells a file served by the |
| 223 | // asset layer (the count it was built with) from a page rendered on |
| 224 | // request (the query's). |
| 225 | test.provider( |
| 226 | "Foldkit: a server-rendered app deploys its fetch handler and serves the front page from it", |
| 227 | (stack) => |
| 228 | Effect.gen(function* () { |
| 229 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 230 | |
| 231 | yield* stack.destroy(); |
| 232 | |
| 233 | const rootDir = yield* cloneFixture(ssrFixtureDir, { |
| 234 | prefix: "alchemy-foldkit-ssr-", |
| 235 | tempRoot, |
| 236 | entries: fixtureEntries.filter((entry) => entry !== "index.html"), |
| 237 | }); |
| 238 | |
| 239 | const site = yield* stack.deploy( |
| 240 | Effect.gen(function* () { |
| 241 | // No `main`, no `assets`: the handler and the routing both come |
| 242 | // from the build. |
| 243 | return yield* Cloudflare.Website.Foldkit("FixFoldkitSsr", foldkitProps(rootDir)); |
| 244 | }), |
| 245 | ); |
| 246 | |
| 247 | expect(site.url).toBeDefined(); |
| 248 | yield* expectWorkerExists(site.workerName, accountId); |
| 249 | |
| 250 | // The front page renders on request — a served template would |
| 251 | // carry an empty `<div id="root">` and no count at all. |
| 252 | yield* expectUrlContains(`${site.url!}/?count=7`, ">7<", { |
| 253 | timeout: "30 seconds", |
| 254 | label: "foldkit ssr front page", |
| 255 | }); |
| 256 | // A prerendered route is a file: the asset layer answers it and the |
| 257 | // query never reaches a render. |
| 258 | yield* expectUrlContains(`${site.url!}/about/?count=7`, ">0<", { |
| 259 | timeout: "15 seconds", |
| 260 | label: "foldkit ssr prerendered page", |
| 261 | }); |
| 262 | // A deep link matches no file and is rendered, not answered with a |
| 263 | // single-page-application fallback. |
| 264 | yield* expectUrlContains(`${site.url!}/counter/42?count=3`, ">3<", { |
| 265 | timeout: "15 seconds", |
| 266 | label: "foldkit ssr deep link", |
| 267 | }); |
| 268 | // The handler classifies an asset-shaped miss itself. |
| 269 | yield* expectDirectStatus(`${site.url!}/assets/missing.js`, 404, { |
| 270 | timeout: "15 seconds", |
| 271 | label: "foldkit ssr asset miss", |
| 272 | }); |
| 273 | |
| 274 | yield* stack.destroy(); |
| 275 | yield* waitForWorkerToBeDeleted(site.workerName, accountId); |
| 276 | }).pipe(logLevel), |
| 277 | { timeout: 120_000 }, |
| 278 | ); |
| 279 | |
| 280 | // `ssr.build` generates the Worker's fetch handler, so a custom `main` |
| 281 | // would silently replace it. The deploy fails instead of picking one. |
| 282 | test.provider( |
| 283 | "Foldkit: a custom main alongside ssr.build fails the deploy", |
| 284 | (stack) => |
| 285 | Effect.gen(function* () { |
| 286 | yield* stack.destroy(); |
| 287 | |
| 288 | const rootDir = yield* cloneFixture(ssrFixtureDir, { |
| 289 | prefix: "alchemy-foldkit-ssr-main-", |
| 290 | tempRoot, |
| 291 | entries: fixtureEntries.filter((entry) => entry !== "index.html"), |
| 292 | }); |
| 293 | |
| 294 | const exit = yield* stack |
| 295 | .deploy( |
| 296 | Cloudflare.Website.Foldkit("FixFoldkitSsrMain", { |
| 297 | ...foldkitProps(rootDir), |
| 298 | main: "src/entry.server.ts", |
| 299 | }), |
| 300 | ) |
| 301 | .pipe(Effect.exit); |
| 302 | |
| 303 | assert(Exit.isFailure(exit)); |
| 304 | expect(Cause.pretty(exit.cause)).toContain("cannot be combined with main"); |
| 305 | |
| 306 | yield* stack.destroy(); |
| 307 | }).pipe(logLevel), |
| 308 | { tags: ["local"], timeout: 120_000 }, |
| 309 | ); |
| 310 | |
| 311 | // Each rendering mode, deployed from its own fixture clone: prerendered |
| 312 | // pages are files, everything else reaches the generated fetch handler. |
| 313 | for (const mode of ["ssg", "ssr", "hybrid"] as const) { |
| 314 | test.provider( |
| 315 | `Foldkit: ${mode} renders every route and cleans up`, |
| 316 | (stack) => |
| 317 | Effect.gen(function* () { |
| 318 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 319 | |
| 320 | yield* stack.destroy(); |
| 321 | |
| 322 | const rootDir = yield* foldkitFixture(mode); |
| 323 | const site = yield* stack.deploy( |
| 324 | Cloudflare.Website.Foldkit(`FixFoldkit${mode}`, { |
| 325 | ...foldkitProps(rootDir), |
| 326 | memo: foldkitMemo, |
| 327 | }), |
| 328 | ); |
| 329 | |
| 330 | expect(site.url).toBeDefined(); |
| 331 | yield* expectWorkerExists(site.workerName, accountId); |
| 332 | // The asset layer answers a POST to a prerendered page itself. |
| 333 | yield* verifyFoldkitRendering(site.url!, mode, { prerenderedPost: 405 }); |
| 334 | yield* verifyFoldkitBrowser(site.url!, mode); |
| 335 | |
| 336 | yield* stack.destroy(); |
| 337 | yield* waitForWorkerToBeDeleted(site.workerName, accountId); |
| 338 | }).pipe(logLevel), |
| 339 | { timeout: 120_000 }, |
| 340 | ); |
| 341 | } |
| 342 | |
| 343 | // Editing a source file must change the input hash so the next deploy |
| 344 | // rebuilds — the memo is keyed on the project tree, not on wall time. |
| 345 | test.provider( |
| 346 | "Foldkit: editing a source file republishes the assets", |
| 347 | (stack) => |
| 348 | Effect.gen(function* () { |
| 349 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 350 | const fs = yield* FileSystem.FileSystem; |
| 351 | const path = yield* Path.Path; |
| 352 | |
| 353 | yield* stack.destroy(); |
| 354 | |
| 355 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 356 | prefix: "alchemy-foldkit-edit-", |
| 357 | tempRoot, |
| 358 | entries: fixtureEntries, |
| 359 | }); |
| 360 | const indexPath = path.join(rootDir, "index.html"); |
| 361 | |
| 362 | const v1Marker = "foldkit-v1-marker"; |
| 363 | yield* fs.writeFileString(indexPath, htmlPage(v1Marker)); |
| 364 | |
| 365 | const site1 = yield* stack.deploy( |
| 366 | Effect.gen(function* () { |
| 367 | return yield* Cloudflare.Website.Foldkit("FixFoldkitEdit", foldkitProps(rootDir)); |
| 368 | }), |
| 369 | ); |
| 370 | |
| 371 | expect(site1.hash?.input).toBeDefined(); |
| 372 | yield* expectUrlContains(`${site1.url!}/`, v1Marker, { |
| 373 | timeout: "30 seconds", |
| 374 | label: "foldkit edit v1", |
| 375 | }); |
| 376 | |
| 377 | const v2Marker = "foldkit-v2-marker"; |
| 378 | yield* fs.writeFileString(indexPath, htmlPage(v2Marker)); |
| 379 | |
| 380 | const site2 = yield* stack.deploy( |
| 381 | Effect.gen(function* () { |
| 382 | return yield* Cloudflare.Website.Foldkit("FixFoldkitEdit", foldkitProps(rootDir)); |
| 383 | }), |
| 384 | ); |
| 385 | |
| 386 | expect(site2.hash?.input).toBeDefined(); |
| 387 | expect(site2.hash?.input).not.toEqual(site1.hash?.input); |
| 388 | yield* expectUrlContains(`${site2.url!}/`, v2Marker, { |
| 389 | timeout: "15 seconds", |
| 390 | label: "foldkit edit v2", |
| 391 | }); |
| 392 | |
| 393 | yield* stack.destroy(); |
| 394 | yield* waitForWorkerToBeDeleted(site1.workerName, accountId); |
| 395 | }).pipe(logLevel), |
| 396 | { timeout: 120_000 }, |
| 397 | ); |
| 398 | }, |
| 399 | ); |