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 { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as Alchemy from "alchemy"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as pathe from "pathe"; |
| 9 | import { cloneFixture } from "../Utils/Fixture.ts"; |
| 10 | import { expectUrlContains } from "../Utils/Http.ts"; |
| 11 | import { expectWorkerExists, waitForWorkerToBeDeleted } from "../Utils/Worker.ts"; |
| 12 | |
| 13 | // `dev: true` runs local providers behind the RPC sidecar proxy by default, |
| 14 | // matching the process topology of the real `alchemy dev` command. |
| 15 | const { test } = Test.make({ providers: Cloudflare.providers(), dev: true }); |
| 16 | |
| 17 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 18 | |
| 19 | // Same rationale as Vite.test.ts: Vite's `vite:build-html` plugin expresses |
| 20 | // emitted asset paths relative to `cwd`, so the temp clone has to live under |
| 21 | // the same workspace root. |
| 22 | const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp"); |
| 23 | |
| 24 | const ssrFixtureDir = pathe.resolve(import.meta.dirname, "foldkit-ssr-fixture"); |
| 25 | |
| 26 | const fixtureEntries = ["package.json", "vite.config.ts", "src"]; |
| 27 | |
| 28 | // Restrict the input memo to fixture sources so the test isn't re-hashing |
| 29 | // the whole monorepo on every deploy. |
| 30 | const memoInclude = ["index.html", "src/**", "package.json", "vite.config.ts"]; |
| 31 | |
| 32 | // Tests are independent (per-test scratch stacks, private fixture clones), |
| 33 | // so run them concurrently; suites are sequential by default. |
| 34 | describe.concurrent( |
| 35 | "Foldkit dev", |
| 36 | { |
| 37 | tags: ["provider:cloudflare", "provider:cloudflare:website", "provider:cloudflare:worker"], |
| 38 | }, |
| 39 | () => { |
| 40 | // A server-rendered app names no Worker entry of its own, so workerd |
| 41 | // serves assets alone in dev and Vite's `ssr` environment stays runnable: |
| 42 | // the Foldkit plugin renders through it exactly as under the app's own |
| 43 | // `vite dev`. Each page stamps the `count` query into its markup, which |
| 44 | // is what tells a render from a served template. |
| 45 | test.provider( |
| 46 | "Foldkit dev: a server-rendered app renders through the local dev server", |
| 47 | (stack) => |
| 48 | Effect.gen(function* () { |
| 49 | yield* stack.destroy(); |
| 50 | |
| 51 | const rootDir = yield* cloneFixture(ssrFixtureDir, { |
| 52 | prefix: "alchemy-foldkit-dev-ssr-", |
| 53 | tempRoot, |
| 54 | entries: fixtureEntries, |
| 55 | }); |
| 56 | |
| 57 | const site = yield* stack.deploy( |
| 58 | Cloudflare.Website.Foldkit("FoldkitSsrLocal", { |
| 59 | rootDir, |
| 60 | dev: { port: 0 }, |
| 61 | memo: { include: memoInclude }, |
| 62 | }), |
| 63 | ); |
| 64 | |
| 65 | // Local identity: the url points at the alchemy dev proxy — no |
| 66 | // cloud Worker exists. |
| 67 | expect(site.url).toBeDefined(); |
| 68 | expect(site.url).toMatch(/^http:\/\/localhost:\d+/); |
| 69 | |
| 70 | // The front page renders on request — a served template would |
| 71 | // carry an empty `<div id="root">` and no count at all. |
| 72 | yield* expectUrlContains(`${site.url!}/?count=7`, ">7<", { |
| 73 | timeout: "30 seconds", |
| 74 | label: "foldkit dev ssr front page", |
| 75 | }); |
| 76 | // A deep link is rendered too; nothing is prerendered in dev. |
| 77 | yield* expectUrlContains(`${site.url!}/counter/42?count=3`, ">3<", { |
| 78 | timeout: "15 seconds", |
| 79 | label: "foldkit dev ssr deep link", |
| 80 | }); |
| 81 | |
| 82 | yield* stack.destroy(); |
| 83 | }).pipe(logLevel), |
| 84 | { tags: ["local"], timeout: 120_000 }, |
| 85 | ); |
| 86 | |
| 87 | /** |
| 88 | * `Alchemy.remote()` opts the whole site OUT of local emulation: even under |
| 89 | * `alchemy dev` the build runs, the fetch handler it emits deploys to real |
| 90 | * Cloudflare, and destroy deletes the cloud Worker (the state row is |
| 91 | * stamped live). |
| 92 | */ |
| 93 | test.provider( |
| 94 | "Foldkit dev: Alchemy.remote() deploys the real Worker and destroy removes it", |
| 95 | (stack) => |
| 96 | Effect.gen(function* () { |
| 97 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 98 | |
| 99 | yield* stack.destroy(); |
| 100 | |
| 101 | const rootDir = yield* cloneFixture(ssrFixtureDir, { |
| 102 | prefix: "alchemy-foldkit-dev-remote-", |
| 103 | tempRoot, |
| 104 | entries: fixtureEntries, |
| 105 | }); |
| 106 | |
| 107 | const site = yield* stack.deploy( |
| 108 | Cloudflare.Website.Foldkit("FoldkitRemoteSite", { |
| 109 | rootDir, |
| 110 | workersDev: true, |
| 111 | compatibility: { |
| 112 | date: "2024-09-23", |
| 113 | flags: ["nodejs_compat"], |
| 114 | }, |
| 115 | memo: { include: memoInclude }, |
| 116 | }).pipe(Alchemy.remote()), |
| 117 | ); |
| 118 | |
| 119 | // Real identity: a non-local URL and a Worker that exists on |
| 120 | // Cloudflare. |
| 121 | expect(site.url).toBeDefined(); |
| 122 | expect(site.url).not.toMatch(/^http:\/\/localhost/); |
| 123 | yield* expectWorkerExists(site.workerName, accountId); |
| 124 | |
| 125 | // The deployed handler renders the front page on request. |
| 126 | yield* expectUrlContains(`${site.url!}/?count=7`, ">7<", { |
| 127 | timeout: "30 seconds", |
| 128 | label: "remote() foldkit ssr front page in dev mode", |
| 129 | }); |
| 130 | // The route the fixture prerenders is a file: the query never |
| 131 | // reaches a render. |
| 132 | yield* expectUrlContains(`${site.url!}/about/?count=7`, ">0<", { |
| 133 | timeout: "15 seconds", |
| 134 | label: "remote() foldkit prerendered page in dev mode", |
| 135 | }); |
| 136 | |
| 137 | yield* stack.destroy(); |
| 138 | |
| 139 | // The stamped-live row deletes the real Worker even in a dev run. |
| 140 | yield* waitForWorkerToBeDeleted(site.workerName, accountId); |
| 141 | }).pipe(logLevel), |
| 142 | { tags: ["live"], timeout: 120_000 }, |
| 143 | ); |
| 144 | }, |
| 145 | ); |