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
| 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 Effect from "effect/Effect"; |
| 6 | import * as FileSystem from "effect/FileSystem"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as pathe from "pathe"; |
| 9 | import { cloneFixture } from "../Utils/Fixture.ts"; |
| 10 | import { expectResponse } from "../Utils/Http.ts"; |
| 11 | import { expectWorkerExists, waitForWorkerToBeDeleted } from "../Utils/Worker.ts"; |
| 12 | |
| 13 | // The build output of a `Website.Vite` Worker, mirroring |
| 14 | // `@cloudflare/vite-plugin`'s `explicit-resource-management` and |
| 15 | // `prerendering` playgrounds, deployed and under `alchemy dev`. |
| 16 | |
| 17 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 18 | const { test: devTest } = Test.make({ providers: Cloudflare.providers(), dev: true }); |
| 19 | |
| 20 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 21 | const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp"); |
| 22 | const explicitResourceManagementFixtureDir = pathe.resolve( |
| 23 | import.meta.dirname, |
| 24 | "vite-explicit-resource-management-fixture", |
| 25 | ); |
| 26 | const prerenderingFixtureDir = pathe.resolve(import.meta.dirname, "vite-prerendering-fixture"); |
| 27 | |
| 28 | type Mode = "live" | "dev"; |
| 29 | |
| 30 | // The Worker's built chunks, concatenated. |
| 31 | const readWorkerOutput = (rootDir: string) => |
| 32 | Effect.gen(function* () { |
| 33 | const fs = yield* FileSystem.FileSystem; |
| 34 | const directory = pathe.join(rootDir, "dist/ssr"); |
| 35 | const files = (yield* fs.readDirectory(directory, { recursive: true })).filter((file) => |
| 36 | /\.m?js$/.test(file), |
| 37 | ); |
| 38 | const sources = yield* Effect.forEach(files, (file) => |
| 39 | fs.readFileString(pathe.join(directory, file)), |
| 40 | ); |
| 41 | return sources.join("\n"); |
| 42 | }); |
| 43 | |
| 44 | const expectText = (url: string, expected: string, status = 200) => |
| 45 | expectResponse(url, (res) => |
| 46 | res.status === status && res.body === expected |
| 47 | ? undefined |
| 48 | : `${res.status} ${JSON.stringify(res.body.slice(0, 300))}, expected ${JSON.stringify(expected)}`, |
| 49 | ); |
| 50 | |
| 51 | describe("Website.Vite build output", () => { |
| 52 | for (const mode of ["live", "dev"] as const satisfies ReadonlyArray<Mode>) { |
| 53 | // `explicit-resource-management`: workerd runs `using` natively, so the |
| 54 | // Worker build keeps it and the Worker disposes in reverse order. |
| 55 | (mode === "live" ? test : devTest).provider( |
| 56 | `Vite build: keeps using declarations in the Worker${mode === "dev" ? " (dev)" : ""}`, |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | yield* stack.destroy(); |
| 60 | const rootDir = yield* cloneFixture(explicitResourceManagementFixtureDir, { |
| 61 | prefix: "alchemy-vite-explicit-resource-management-", |
| 62 | tempRoot, |
| 63 | entries: ["package.json", "vite.config.ts", "index.html", "src", "worker"], |
| 64 | }); |
| 65 | const site = yield* stack.deploy( |
| 66 | Cloudflare.Website.Vite(`ViteExplicitResourceManagement${mode}`, { |
| 67 | rootDir, |
| 68 | main: "./worker/index.ts", |
| 69 | workersDev: true, |
| 70 | compatibility: { date: "2024-12-30" }, |
| 71 | memo: { include: ["**/*"] }, |
| 72 | ...(mode === "dev" ? { dev: { port: 0 } } : {}), |
| 73 | }), |
| 74 | ); |
| 75 | const url = site.url!.replace(/\/$/, ""); |
| 76 | yield* expectText(`${url}/api`, `["socket","session"]`); |
| 77 | if (mode === "live") { |
| 78 | const output = yield* readWorkerOutput(rootDir); |
| 79 | expect(output).toMatch(/\bawait using [\w$]+ =/); |
| 80 | expect(output).toMatch(/\busing [\w$]+ =/); |
| 81 | expect(output).not.toMatch(/__using|_usingCtx/); |
| 82 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 83 | yield* stack.destroy(); |
| 84 | yield* waitForWorkerToBeDeleted(site.workerName, accountId); |
| 85 | } |
| 86 | }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.ignore)), logLevel), |
| 87 | { |
| 88 | tags: ["provider:cloudflare:worker", mode === "dev" ? "local" : "live"], |
| 89 | timeout: 300_000, |
| 90 | }, |
| 91 | ); |
| 92 | |
| 93 | // `prerendering`: a build plugin renders a page through Vite's preview |
| 94 | // server, where the Worker reaches another Worker over a service |
| 95 | // binding, into the static assets (build only; dev serves the Worker). |
| 96 | (mode === "live" ? test : devTest).provider( |
| 97 | `Vite build: prerenders a page through the preview server${mode === "dev" ? " (dev)" : ""}`, |
| 98 | (stack) => |
| 99 | Effect.gen(function* () { |
| 100 | yield* stack.destroy(); |
| 101 | const rootDir = yield* cloneFixture(prerenderingFixtureDir, { |
| 102 | prefix: "alchemy-vite-prerendering-", |
| 103 | tempRoot, |
| 104 | entries: ["package.json", "vite.config.ts", "src"], |
| 105 | }); |
| 106 | const site = yield* stack.deploy( |
| 107 | Effect.gen(function* () { |
| 108 | const auxiliary = yield* Cloudflare.Worker(`VitePrerenderingAuxiliary${mode}`, { |
| 109 | main: pathe.join(rootDir, "src/auxiliary.ts"), |
| 110 | compatibility: { date: "2024-12-30" }, |
| 111 | ...(mode === "dev" ? { dev: { port: 0 } } : {}), |
| 112 | }); |
| 113 | return yield* Cloudflare.Website.Vite(`VitePrerendering${mode}`, { |
| 114 | rootDir, |
| 115 | main: "./src/index.ts", |
| 116 | workersDev: true, |
| 117 | compatibility: { date: "2024-12-30" }, |
| 118 | memo: { include: ["**/*"] }, |
| 119 | env: { AUXILIARY_WORKER: auxiliary }, |
| 120 | ...(mode === "dev" ? { dev: { port: 0 } } : {}), |
| 121 | }); |
| 122 | }), |
| 123 | ); |
| 124 | const url = site.url!.replace(/\/$/, ""); |
| 125 | yield* expectText(`${url}/hello`, "Hello world"); |
| 126 | if (mode === "live") { |
| 127 | yield* expectResponse(`${url}/prerendered`, (res) => |
| 128 | res.status === 200 && |
| 129 | res.body.includes("<h1>Pre-rendered HTML from auxiliary Worker</h1>") |
| 130 | ? undefined |
| 131 | : `${res.status} ${res.body.slice(0, 300)}`, |
| 132 | ); |
| 133 | const output = yield* readWorkerOutput(rootDir); |
| 134 | expect(output).toMatch(/\busing [\w$]+ =/); |
| 135 | expect(output).not.toMatch(/__using|_usingCtx/); |
| 136 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 137 | yield* expectWorkerExists(site.workerName, accountId); |
| 138 | yield* stack.destroy(); |
| 139 | yield* waitForWorkerToBeDeleted(site.workerName, accountId); |
| 140 | } |
| 141 | }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.ignore)), logLevel), |
| 142 | { |
| 143 | tags: ["provider:cloudflare:worker", mode === "dev" ? "local" : "live"], |
| 144 | timeout: 300_000, |
| 145 | }, |
| 146 | ); |
| 147 | } |
| 148 | }); |