The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { expect, layer } from "@alchemy.run/test"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Option from "effect/Option"; |
| 6 | import * as Path from "effect/Path"; |
| 7 | |
| 8 | const sourceEntry = "src/Workers/RpcWebSocketClient.ts"; |
| 9 | const compiledEntry = "lib/Workers/RpcWebSocketClient.js"; |
| 10 | const declarationEntry = "lib/Workers/RpcWebSocketClient.d.ts"; |
| 11 | const fixtureDirectory = new URL("./fixtures/rpc-websocket-client/", import.meta.url); |
| 12 | |
| 13 | interface Manifest { |
| 14 | readonly name: string; |
| 15 | readonly version: string; |
| 16 | readonly type: string; |
| 17 | readonly sideEffects: boolean; |
| 18 | readonly files: ReadonlyArray<string>; |
| 19 | readonly exports: Record<string, unknown>; |
| 20 | readonly publishConfig: { readonly exports: Record<string, unknown> }; |
| 21 | } |
| 22 | |
| 23 | const packageInfo = Effect.gen(function* () { |
| 24 | const fs = yield* FileSystem.FileSystem; |
| 25 | const path = yield* Path.Path; |
| 26 | const root = yield* path.fromFileUrl(new URL("../../../../cloudflare/", import.meta.url)); |
| 27 | const json = yield* fs.readFileString(path.join(root, "package.json")); |
| 28 | const manifest = yield* Effect.try(() => JSON.parse(json) as Manifest); |
| 29 | return { root, manifest }; |
| 30 | }); |
| 31 | |
| 32 | const bundleBrowser = Effect.fn(function* ( |
| 33 | root: string, |
| 34 | entry: string, |
| 35 | conditions: string[] = ["browser", "import", "default"], |
| 36 | ) { |
| 37 | const { rolldown } = yield* Effect.promise(() => import("rolldown")); |
| 38 | const modules = new Set<string>(); |
| 39 | return yield* Effect.acquireUseRelease( |
| 40 | Effect.promise(() => |
| 41 | rolldown({ |
| 42 | cwd: root, |
| 43 | input: entry, |
| 44 | platform: "browser", |
| 45 | resolve: { conditionNames: conditions }, |
| 46 | treeshake: false, |
| 47 | plugins: [ |
| 48 | { |
| 49 | name: "browser-dependency-graph", |
| 50 | moduleParsed(info) { |
| 51 | modules.add(info.id); |
| 52 | for (const id of [...info.importedIds, ...info.dynamicallyImportedIds]) { |
| 53 | modules.add(id); |
| 54 | } |
| 55 | }, |
| 56 | }, |
| 57 | ], |
| 58 | }), |
| 59 | ), |
| 60 | (bundle) => |
| 61 | Effect.gen(function* () { |
| 62 | const { output } = yield* Effect.promise(() => bundle.generate({ format: "esm" })); |
| 63 | const chunks = output.filter((item) => item.type === "chunk"); |
| 64 | const entryChunk = chunks.find((chunk) => chunk.isEntry)!; |
| 65 | expect(entryChunk.exports).toEqual( |
| 66 | expect.arrayContaining(["BrowserClient", "clientLayer", "echo"]), |
| 67 | ); |
| 68 | expect(entryChunk.code.length).toBeGreaterThan(0); |
| 69 | const emitted = new Set(chunks.map((chunk) => chunk.fileName)); |
| 70 | expect( |
| 71 | chunks.flatMap((chunk) => |
| 72 | [...chunk.imports, ...chunk.dynamicImports].filter((id) => !emitted.has(id)), |
| 73 | ), |
| 74 | ).toEqual([]); |
| 75 | return [...modules].map((id) => id.replaceAll("\\", "/")); |
| 76 | }), |
| 77 | (bundle) => Effect.promise(() => bundle.close()), |
| 78 | ); |
| 79 | }); |
| 80 | |
| 81 | const assertBrowserGraph = Effect.fn(function* ( |
| 82 | root: string, |
| 83 | modules: string[], |
| 84 | expectedEntry: string, |
| 85 | ) { |
| 86 | const path = yield* Path.Path; |
| 87 | const sourceRoot = `${path.join(root, "src").replaceAll("\\", "/")}/`; |
| 88 | const libRoot = `${path.join(root, "lib").replaceAll("\\", "/")}/`; |
| 89 | const alchemyModules = modules.filter( |
| 90 | (id) => id.startsWith(sourceRoot) || id.startsWith(libRoot), |
| 91 | ); |
| 92 | expect(alchemyModules).toEqual([path.join(root, expectedEntry).replaceAll("\\", "/")]); |
| 93 | expect(modules.some((id) => id.includes("/effect/"))).toBe(true); |
| 94 | expect( |
| 95 | modules.filter( |
| 96 | (id) => |
| 97 | /^(?:node|cloudflare|bun):/.test(id) || |
| 98 | id.includes("/submodules/distilled/") || |
| 99 | id.includes("/packages/cloudflare-runtime/") || |
| 100 | /\/node_modules\/(?:@distilled\.cloud\/|@cloudflare\/|@alchemy\.run\/cloudflare-runtime\/|@effect\/platform-node(?:-shared)?\/|(?:cloudflare|workerd|wrangler|unenv)\/)/.test( |
| 101 | id, |
| 102 | ), |
| 103 | ), |
| 104 | ).toEqual([]); |
| 105 | }); |
| 106 | |
| 107 | const stagePublishedPackage = Effect.gen(function* () { |
| 108 | const fs = yield* FileSystem.FileSystem; |
| 109 | const path = yield* Path.Path; |
| 110 | const { root, manifest } = yield* packageInfo; |
| 111 | const directory = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-rpc-websocket-client-" }); |
| 112 | const packageDirectory = path.join(directory, "node_modules/@alchemy.run/cloudflare"); |
| 113 | yield* fs.makeDirectory(packageDirectory, { recursive: true }); |
| 114 | yield* fs.writeFileString( |
| 115 | path.join(packageDirectory, "package.json"), |
| 116 | JSON.stringify({ |
| 117 | name: manifest.name, |
| 118 | version: manifest.version, |
| 119 | type: manifest.type, |
| 120 | sideEffects: manifest.sideEffects, |
| 121 | exports: manifest.publishConfig.exports, |
| 122 | }), |
| 123 | ); |
| 124 | for (const entry of ["src", "lib"]) { |
| 125 | yield* fs.symlink(path.join(root, entry), path.join(packageDirectory, entry)); |
| 126 | } |
| 127 | yield* fs.symlink( |
| 128 | path.join(root, "node_modules/effect"), |
| 129 | path.join(directory, "node_modules/effect"), |
| 130 | ); |
| 131 | for (const file of ["browser.ts", "rpcs.ts"]) { |
| 132 | yield* fs.copyFile( |
| 133 | path.join(yield* path.fromFileUrl(fixtureDirectory), file), |
| 134 | path.join(directory, file), |
| 135 | ); |
| 136 | } |
| 137 | return { root, directory, entry: path.join(directory, "browser.ts") }; |
| 138 | }); |
| 139 | |
| 140 | layer(NodeServices.layer)("@alchemy.run/cloudflare/Workers/RpcWebSocketClient", (it) => { |
| 141 | it.effect( |
| 142 | "maps the public source and published browser entry points", |
| 143 | () => |
| 144 | Effect.gen(function* () { |
| 145 | const { manifest } = yield* packageInfo; |
| 146 | // `./Workers/RpcWebSocketClient` resolves through the `./*` file pattern. |
| 147 | const at = (pattern: string) => pattern.replaceAll("*", "Workers/RpcWebSocketClient"); |
| 148 | expect(at(manifest.exports["./*"] as string)).toBe(`./${sourceEntry}`); |
| 149 | const published = manifest.publishConfig.exports["./*"] as Record<string, string>; |
| 150 | expect(Object.fromEntries(Object.entries(published).map(([k, v]) => [k, at(v)]))).toEqual({ |
| 151 | types: `./${declarationEntry}`, |
| 152 | bun: `./${sourceEntry}`, |
| 153 | worker: `./${sourceEntry}`, |
| 154 | default: `./${compiledEntry}`, |
| 155 | }); |
| 156 | expect(manifest.files).toEqual(expect.arrayContaining(["src", "lib"])); |
| 157 | }), |
| 158 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] }, |
| 159 | ); |
| 160 | |
| 161 | it.effect( |
| 162 | "bundles the actual public subpath for a browser without server modules", |
| 163 | () => |
| 164 | Effect.gen(function* () { |
| 165 | const path = yield* Path.Path; |
| 166 | const { root } = yield* packageInfo; |
| 167 | const modules = yield* bundleBrowser( |
| 168 | root, |
| 169 | path.join(yield* path.fromFileUrl(fixtureDirectory), "browser.ts"), |
| 170 | ); |
| 171 | yield* assertBrowserGraph(root, modules, sourceEntry); |
| 172 | }), |
| 173 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] }, |
| 174 | ); |
| 175 | |
| 176 | it.effect( |
| 177 | "resolves the published source condition through the public subpath", |
| 178 | () => |
| 179 | Effect.gen(function* () { |
| 180 | const { root, directory, entry } = yield* stagePublishedPackage; |
| 181 | const modules = yield* bundleBrowser(directory, entry, [ |
| 182 | "bun", |
| 183 | "browser", |
| 184 | "import", |
| 185 | "default", |
| 186 | ]); |
| 187 | yield* assertBrowserGraph(root, modules, sourceEntry); |
| 188 | }), |
| 189 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] }, |
| 190 | ); |
| 191 | |
| 192 | // Compiled packaging checks require a prior workspace build. |
| 193 | it.effect.skipIf(!process.env.ALCHEMY_TEST_RPC_CLIENT_LIB)( |
| 194 | "bundles the fresh published JavaScript entry for browsers", |
| 195 | () => |
| 196 | Effect.gen(function* () { |
| 197 | const fs = yield* FileSystem.FileSystem; |
| 198 | const path = yield* Path.Path; |
| 199 | const { root, directory, entry } = yield* stagePublishedPackage; |
| 200 | const source = yield* fs.stat(path.join(root, sourceEntry)); |
| 201 | for (const file of [compiledEntry, declarationEntry]) { |
| 202 | const info = yield* fs.stat(path.join(root, file)); |
| 203 | expect(Option.getOrThrow(info.mtime).getTime()).toBeGreaterThanOrEqual( |
| 204 | Option.getOrThrow(source.mtime).getTime(), |
| 205 | ); |
| 206 | } |
| 207 | const modules = yield* bundleBrowser(directory, entry); |
| 208 | yield* assertBrowserGraph(root, modules, compiledEntry); |
| 209 | }), |
| 210 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] }, |
| 211 | ); |
| 212 | }); |