The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { createHash } from "node:crypto"; |
| 2 | import { pathToFileURL } from "node:url"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Path from "effect/Path"; |
| 7 | import type * as Scope from "effect/Scope"; |
| 8 | import { describe, expect, it } from "vitest"; |
| 9 | import { runBuildChild } from "../../core/BuildChild.ts"; |
| 10 | import { buildCloudflare, clientPlugins } from "../CloudflareBuild.ts"; |
| 11 | |
| 12 | const run = <A, E>(effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path | Scope.Scope>) => |
| 13 | Effect.runPromise(Effect.scoped(effect).pipe(Effect.provide(NodeServices.layer))); |
| 14 | |
| 15 | const fixture = Effect.gen(function* () { |
| 16 | const fs = yield* FileSystem.FileSystem; |
| 17 | const path = yield* Path.Path; |
| 18 | const packageDir = yield* path.fromFileUrl(new URL("../../../", import.meta.url)); |
| 19 | const source = yield* path.fromFileUrl(new URL("./fixtures/cloudflare", import.meta.url)); |
| 20 | const root = yield* fs.makeTempDirectoryScoped({ |
| 21 | directory: packageDir, |
| 22 | prefix: ".octane-test-", |
| 23 | }); |
| 24 | yield* fs.copy(source, root, { overwrite: true }); |
| 25 | return root; |
| 26 | }); |
| 27 | |
| 28 | const build = (root: string) => |
| 29 | runBuildChild({ |
| 30 | module: new URL("../source.ts", import.meta.url).href, |
| 31 | rootDir: root, |
| 32 | framework: "octane", |
| 33 | config: { |
| 34 | rootDir: root, |
| 35 | compatibilityDate: "2026-08-31", |
| 36 | compatibilityFlags: [], |
| 37 | }, |
| 38 | }); |
| 39 | |
| 40 | describe("Octane Cloudflare build", () => { |
| 41 | it("preserves native plugins without mutating their server hooks", async () => { |
| 42 | const closeBundle = () => undefined; |
| 43 | const native = { name: "@octanejs/vite-plugin", closeBundle }; |
| 44 | const custom = { name: "custom" }; |
| 45 | const plugins = await run(clientPlugins([Promise.resolve([native, false]), custom])); |
| 46 | expect(plugins.map((plugin) => plugin.name)).toEqual([native.name, custom.name]); |
| 47 | expect(plugins[0]?.closeBundle).toBeUndefined(); |
| 48 | expect(native.closeBundle).toBe(closeBundle); |
| 49 | expect(plugins[1]).toBe(custom); |
| 50 | }); |
| 51 | |
| 52 | it("rejects a missing native plugin instead of silently dropping SSR", async () => { |
| 53 | const error = await run(clientPlugins([{ name: "unrelated" }]).pipe(Effect.flip)); |
| 54 | expect(error.message).toContain("native octane()"); |
| 55 | }); |
| 56 | |
| 57 | it("builds and serves an adapter-free app with native plugins, CSS, routes, and bindings", async () => { |
| 58 | await run( |
| 59 | Effect.gen(function* () { |
| 60 | const fs = yield* FileSystem.FileSystem; |
| 61 | const path = yield* Path.Path; |
| 62 | const root = yield* fixture; |
| 63 | const configPath = path.join(root, "octane.config.ts"); |
| 64 | const vitePath = path.join(root, "vite.config.ts"); |
| 65 | const config = yield* fs.readFileString(configPath); |
| 66 | const vite = yield* fs.readFileString(vitePath); |
| 67 | const output = yield* build(`${root}/`); |
| 68 | expect(output.serverModules?.[0]?.name).toBe("server/worker.js"); |
| 69 | expect(output.clientDirectory).toBe(path.join(root, "output/client")); |
| 70 | expect(yield* fs.exists(path.join(root, "output/client/index.html"))).toBe(false); |
| 71 | expect(yield* fs.exists(path.join(root, "output/client/.vite"))).toBe(false); |
| 72 | expect(yield* fs.readFileString(configPath)).toBe(config); |
| 73 | expect(yield* fs.readFileString(vitePath)).toBe(vite); |
| 74 | expect( |
| 75 | (yield* fs.readDirectory(root)).filter((entry) => |
| 76 | entry.startsWith(".alchemy-octane-worker-"), |
| 77 | ), |
| 78 | ).toEqual([]); |
| 79 | const modules = output.serverModules |
| 80 | ?.map((module) => Buffer.from(module.content).toString()) |
| 81 | .join("\n"); |
| 82 | expect(modules).not.toContain("node:fs"); |
| 83 | expect(modules).not.toContain("node:http"); |
| 84 | const worker = yield* Effect.tryPromise( |
| 85 | () => |
| 86 | import( |
| 87 | /* @vite-ignore */ pathToFileURL(path.join(root, "output/server/worker.js")).href |
| 88 | ), |
| 89 | ); |
| 90 | const fetch = (pathname: string, init?: RequestInit) => |
| 91 | Effect.tryPromise( |
| 92 | () => |
| 93 | worker.default.fetch( |
| 94 | new Request(`https://octane.example${pathname}`, init), |
| 95 | { MESSAGE: "hello" }, |
| 96 | { waitUntil() {} }, |
| 97 | ) as Promise<Response>, |
| 98 | ); |
| 99 | const response = yield* fetch("/api/hello"); |
| 100 | expect(yield* Effect.tryPromise(() => response.json())).toEqual({ |
| 101 | message: "native-config:hello", |
| 102 | hasWaitUntil: true, |
| 103 | }); |
| 104 | for (const route of ["/", "/other"]) { |
| 105 | const page = yield* fetch(route); |
| 106 | expect(page.status).toBe(200); |
| 107 | const html = yield* Effect.tryPromise(() => page.text()); |
| 108 | expect(html).toContain("ALCHEMY_OCTANE_WORKER"); |
| 109 | expect(html).toContain("native-plugin-preserved"); |
| 110 | expect(html).toMatch(/stylesheet[^>]+\.css/); |
| 111 | } |
| 112 | const rpcId = yield* Effect.sync(() => |
| 113 | createHash("sha256").update("/src/rpc.tsx#greet").digest("hex").slice(0, 8), |
| 114 | ); |
| 115 | const rpc = yield* fetch(`/_$_ripple_rpc_$_/${rpcId}`, { |
| 116 | method: "POST", |
| 117 | headers: { |
| 118 | "Content-Type": "application/json", |
| 119 | Origin: "https://octane.example", |
| 120 | }, |
| 121 | body: '[[1],"Worker"]', |
| 122 | }); |
| 123 | expect(rpc.status).toBe(200); |
| 124 | expect(yield* Effect.tryPromise(() => rpc.text())).toContain('"hello Worker"'); |
| 125 | expect((yield* fetch("/missing")).status).toBe(404); |
| 126 | }), |
| 127 | ); |
| 128 | }, 120_000); |
| 129 | |
| 130 | it("preserves Cloudflare module imports in the Worker bundle", async () => { |
| 131 | await run( |
| 132 | Effect.gen(function* () { |
| 133 | const fs = yield* FileSystem.FileSystem; |
| 134 | const path = yield* Path.Path; |
| 135 | const root = yield* fixture; |
| 136 | const file = path.join(root, "octane.config.ts"); |
| 137 | const config = yield* fs.readFileString(file); |
| 138 | yield* fs.writeFileString( |
| 139 | file, |
| 140 | config |
| 141 | .replace( |
| 142 | "handler: (context) => {", |
| 143 | 'handler: async (context) => { const { env } = await import("cloudflare:workers");', |
| 144 | ) |
| 145 | .replace( |
| 146 | "message: `${prefix}:${platform.env.MESSAGE}`", |
| 147 | "message: `${prefix}:${env.MESSAGE}`", |
| 148 | ), |
| 149 | ); |
| 150 | const output = yield* build(root); |
| 151 | expect( |
| 152 | output.serverModules?.map((module) => Buffer.from(module.content).toString()).join("\n"), |
| 153 | ).toContain("cloudflare:workers"); |
| 154 | }), |
| 155 | ); |
| 156 | }, 120_000); |
| 157 | |
| 158 | it("cleans generated files after a server-build failure without rewriting config", async () => { |
| 159 | await run( |
| 160 | Effect.gen(function* () { |
| 161 | const fs = yield* FileSystem.FileSystem; |
| 162 | const path = yield* Path.Path; |
| 163 | const root = yield* fixture; |
| 164 | const file = path.join(root, "vite.config.ts"); |
| 165 | const original = yield* fs.readFileString(file); |
| 166 | const failing = original.replace( |
| 167 | 'name: "native-app-plugin",', |
| 168 | 'name: "native-app-plugin", configResolved(config) { if (config.build.ssr) throw new Error("intentional server-build failure"); },', |
| 169 | ); |
| 170 | yield* fs.writeFileString(file, failing); |
| 171 | yield* build(root).pipe(Effect.flip); |
| 172 | expect(yield* fs.readFileString(file)).toBe(failing); |
| 173 | expect(yield* fs.exists(path.join(root, "output/client/index.html"))).toBe(true); |
| 174 | expect( |
| 175 | (yield* fs.readDirectory(root)).filter((entry) => |
| 176 | entry.startsWith(".alchemy-octane-worker-"), |
| 177 | ), |
| 178 | ).toEqual([]); |
| 179 | }), |
| 180 | ); |
| 181 | }, 120_000); |
| 182 | |
| 183 | it("rejects incompatible adapter declarations", async () => { |
| 184 | await run( |
| 185 | Effect.gen(function* () { |
| 186 | const fs = yield* FileSystem.FileSystem; |
| 187 | const path = yield* Path.Path; |
| 188 | const root = yield* fixture; |
| 189 | const configPath = path.join(root, "octane.config.ts"); |
| 190 | const config = yield* fs.readFileString(configPath); |
| 191 | yield* fs.writeFileString( |
| 192 | configPath, |
| 193 | config.replace("defineConfig({", 'defineConfig({ adapter: { name: "vercel" },'), |
| 194 | ); |
| 195 | const error = yield* buildCloudflare(root).pipe(Effect.flip); |
| 196 | expect(error.message).toContain('"vercel" is incompatible with Cloudflare'); |
| 197 | }), |
| 198 | ); |
| 199 | }, 30_000); |
| 200 | }); |