The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { readPrebuiltWorkerBundle } from "@alchemy.run/cloudflare/Workers/Sources/Prebuilt"; |
| 2 | import { expect, layer } from "@alchemy.run/test"; |
| 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 | |
| 8 | const decode = (content: string | Uint8Array<ArrayBufferLike>) => |
| 9 | typeof content === "string" ? content : new TextDecoder().decode(content as Uint8Array); |
| 10 | |
| 11 | /** |
| 12 | * Write `files` (paths relative to a fresh temp directory) and return |
| 13 | * the temp directory's absolute path. |
| 14 | */ |
| 15 | const writeFixture = Effect.fn(function* (files: Record<string, string | Uint8Array>) { |
| 16 | const fs = yield* FileSystem.FileSystem; |
| 17 | const path = yield* Path.Path; |
| 18 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prebuilt-" }); |
| 19 | for (const [name, content] of Object.entries(files)) { |
| 20 | const file = path.join(root, name); |
| 21 | yield* fs.makeDirectory(path.dirname(file), { recursive: true }); |
| 22 | if (typeof content === "string") { |
| 23 | yield* fs.writeFileString(file, content); |
| 24 | } else { |
| 25 | yield* fs.writeFile(file, content); |
| 26 | } |
| 27 | } |
| 28 | return root; |
| 29 | }); |
| 30 | |
| 31 | layer(NodeServices.layer)("readPrebuiltWorkerBundle", (it) => { |
| 32 | it.effect( |
| 33 | "collects modules matching the default rules, entry first, names POSIX-relative to the entry's directory", |
| 34 | () => |
| 35 | Effect.gen(function* () { |
| 36 | const fs = yield* FileSystem.FileSystem; |
| 37 | const path = yield* Path.Path; |
| 38 | const root = yield* writeFixture({ |
| 39 | "index.mjs": `import { greeting } from "./lib/greeting.mjs";\nexport default { fetch: () => new Response(greeting) };\n`, |
| 40 | "lib/greeting.mjs": `export const greeting = "hello";\n`, |
| 41 | "lib/deep/notice.txt": "NOTICE", |
| 42 | "add.wasm": new Uint8Array([0x00, 0x61, 0x73, 0x6d, 1, 0, 0, 0]), |
| 43 | "schema.sql": "select 1;", |
| 44 | "page.html": "<html></html>", |
| 45 | "blob.bin": new Uint8Array([1, 2, 3]), |
| 46 | // hidden directories are walked (wrangler glob parity) |
| 47 | ".chunks/extra.mjs": `export const extra = 1;\n`, |
| 48 | // not matched by any default rule |
| 49 | "README.md": "# not a module", |
| 50 | // source maps are never uploaded as modules |
| 51 | "index.mjs.map": "{}", |
| 52 | "lib/greeting.mjs.map": "{}", |
| 53 | }); |
| 54 | |
| 55 | const bundle = yield* readPrebuiltWorkerBundle({ main: path.join(root, "index.mjs") }); |
| 56 | |
| 57 | expect(bundle.files[0].path).toEqual("index.mjs"); |
| 58 | expect(bundle.files.slice(1).map((file) => file.path)).toEqual([ |
| 59 | ".chunks/extra.mjs", |
| 60 | "add.wasm", |
| 61 | "blob.bin", |
| 62 | "lib/deep/notice.txt", |
| 63 | "lib/greeting.mjs", |
| 64 | "page.html", |
| 65 | "schema.sql", |
| 66 | ]); |
| 67 | |
| 68 | yield* fs.remove(root, { recursive: true }); |
| 69 | }), |
| 70 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] }, |
| 71 | ); |
| 72 | |
| 73 | it.effect( |
| 74 | "uploads file contents byte-for-byte", |
| 75 | () => |
| 76 | Effect.gen(function* () { |
| 77 | const fs = yield* FileSystem.FileSystem; |
| 78 | const path = yield* Path.Path; |
| 79 | // Comments and formatting that any bundler/minifier would strip. |
| 80 | const entrySource = `// SENTINEL: must survive byte-for-byte 7f1c\nconst kSentinel = "sentinel/7f1c";\nexport default { fetch: () => new Response(kSentinel) };\n`; |
| 81 | const wasmBytes = new Uint8Array([0x00, 0x61, 0x73, 0x6d, 1, 0, 0, 0, 42]); |
| 82 | const root = yield* writeFixture({ "index.mjs": entrySource, "add.wasm": wasmBytes }); |
| 83 | |
| 84 | const bundle = yield* readPrebuiltWorkerBundle({ main: path.join(root, "index.mjs") }); |
| 85 | |
| 86 | expect(decode(bundle.files[0].content)).toEqual(entrySource); |
| 87 | const wasm = bundle.files.find((file) => file.path === "add.wasm")!; |
| 88 | expect(new Uint8Array(wasm.content as Uint8Array)).toEqual(wasmBytes); |
| 89 | |
| 90 | yield* fs.remove(root, { recursive: true }); |
| 91 | }), |
| 92 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] }, |
| 93 | ); |
| 94 | |
| 95 | it.effect( |
| 96 | "does not walk outside the entry's directory", |
| 97 | () => |
| 98 | Effect.gen(function* () { |
| 99 | const fs = yield* FileSystem.FileSystem; |
| 100 | const path = yield* Path.Path; |
| 101 | const root = yield* writeFixture({ |
| 102 | "outside.mjs": `export const outside = true;\n`, |
| 103 | "dist/index.mjs": `export default { fetch: () => new Response("ok") };\n`, |
| 104 | "dist/chunk.mjs": `export const chunk = 1;\n`, |
| 105 | }); |
| 106 | |
| 107 | const bundle = yield* readPrebuiltWorkerBundle({ main: path.join(root, "dist/index.mjs") }); |
| 108 | |
| 109 | expect(bundle.files.map((file) => file.path)).toEqual(["index.mjs", "chunk.mjs"]); |
| 110 | |
| 111 | yield* fs.remove(root, { recursive: true }); |
| 112 | }), |
| 113 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] }, |
| 114 | ); |
| 115 | |
| 116 | it.effect( |
| 117 | "custom rules replace the defaults", |
| 118 | () => |
| 119 | Effect.gen(function* () { |
| 120 | const fs = yield* FileSystem.FileSystem; |
| 121 | const path = yield* Path.Path; |
| 122 | const root = yield* writeFixture({ |
| 123 | "index.mjs": `export default { fetch: () => new Response("ok") };\n`, |
| 124 | "data/weights.dat": "0101", |
| 125 | "data/skipped.txt": "skipped", |
| 126 | "skipped.mjs": `export const skipped = true;\n`, |
| 127 | }); |
| 128 | |
| 129 | const bundle = yield* readPrebuiltWorkerBundle({ |
| 130 | main: path.join(root, "index.mjs"), |
| 131 | rules: [{ globs: ["**/*.dat"] }], |
| 132 | }); |
| 133 | |
| 134 | // The entry is always included, even when it matches no rule. |
| 135 | expect(bundle.files.map((file) => file.path)).toEqual(["index.mjs", "data/weights.dat"]); |
| 136 | |
| 137 | yield* fs.remove(root, { recursive: true }); |
| 138 | }), |
| 139 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] }, |
| 140 | ); |
| 141 | |
| 142 | it.effect( |
| 143 | "types matching files by their rule's type", |
| 144 | () => |
| 145 | Effect.gen(function* () { |
| 146 | const fs = yield* FileSystem.FileSystem; |
| 147 | const path = yield* Path.Path; |
| 148 | const root = yield* writeFixture({ |
| 149 | "index.mjs": `export default { fetch: () => new Response("ok") };\n`, |
| 150 | "notes.md": "# notes", |
| 151 | "font.ttf": "FONT", |
| 152 | }); |
| 153 | const read = (type?: "Text") => |
| 154 | readPrebuiltWorkerBundle({ |
| 155 | main: path.join(root, "index.mjs"), |
| 156 | rules: [{ globs: ["**/*.mjs"] }, { type, globs: ["**/*.md"] }, { globs: ["**/*.ttf"] }], |
| 157 | }); |
| 158 | |
| 159 | const bundle = yield* read("Text"); |
| 160 | expect(bundle.files.map((file) => [file.path, file.type])).toEqual([ |
| 161 | ["index.mjs", undefined], |
| 162 | ["font.ttf", undefined], |
| 163 | ["notes.md", "Text"], |
| 164 | ]); |
| 165 | // The type is part of the hash, so changing it redeploys. |
| 166 | expect((yield* read()).hash).not.toEqual(bundle.hash); |
| 167 | |
| 168 | // The first matching rule decides, typed or not, as when bundling. |
| 169 | const first = yield* readPrebuiltWorkerBundle({ |
| 170 | main: path.join(root, "index.mjs"), |
| 171 | rules: [{ globs: ["**/*.mjs", "**/*.md"] }, { type: "Text", globs: ["**/*.md"] }], |
| 172 | }); |
| 173 | expect(first.files.find((file) => file.path === "notes.md")?.type).toBeUndefined(); |
| 174 | |
| 175 | yield* fs.remove(root, { recursive: true }); |
| 176 | }), |
| 177 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] }, |
| 178 | ); |
| 179 | |
| 180 | it.effect( |
| 181 | "hash is stable across reads and sensitive to module changes", |
| 182 | () => |
| 183 | Effect.gen(function* () { |
| 184 | const fs = yield* FileSystem.FileSystem; |
| 185 | const path = yield* Path.Path; |
| 186 | const root = yield* writeFixture({ |
| 187 | "index.mjs": `import { x } from "./lib/x.mjs";\nexport default { fetch: () => new Response(String(x)) };\n`, |
| 188 | "lib/x.mjs": `export const x = 1;\n`, |
| 189 | }); |
| 190 | const main = path.join(root, "index.mjs"); |
| 191 | |
| 192 | const first = yield* readPrebuiltWorkerBundle({ main }); |
| 193 | const second = yield* readPrebuiltWorkerBundle({ main }); |
| 194 | expect(second.hash).toEqual(first.hash); |
| 195 | |
| 196 | yield* fs.writeFileString(path.join(root, "lib/x.mjs"), `export const x = 2;\n`); |
| 197 | const changed = yield* readPrebuiltWorkerBundle({ main }); |
| 198 | expect(changed.hash).not.toEqual(first.hash); |
| 199 | |
| 200 | yield* fs.remove(root, { recursive: true }); |
| 201 | }), |
| 202 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] }, |
| 203 | ); |
| 204 | |
| 205 | it.effect( |
| 206 | "fails with BundleError when the entry does not exist", |
| 207 | () => |
| 208 | Effect.gen(function* () { |
| 209 | const fs = yield* FileSystem.FileSystem; |
| 210 | const path = yield* Path.Path; |
| 211 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prebuilt-" }); |
| 212 | |
| 213 | const error = yield* readPrebuiltWorkerBundle({ |
| 214 | main: path.join(root, "missing.mjs"), |
| 215 | }).pipe(Effect.flip); |
| 216 | |
| 217 | expect(error._tag).toEqual("BundleError"); |
| 218 | expect(error.message).toContain("missing.mjs"); |
| 219 | |
| 220 | yield* fs.remove(root, { recursive: true }); |
| 221 | }), |
| 222 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] }, |
| 223 | ); |
| 224 | }); |