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 * as Test from "@alchemy.run/test/Alchemy"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { expectUrlContains } from "../Utils/Http.ts"; |
| 5 | |
| 6 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 7 | |
| 8 | const script = `export default { |
| 9 | async fetch(request, env) { |
| 10 | await env.KV.put("ref-binding-key", "bound-through-ref"); |
| 11 | const value = await env.KV.get("ref-binding-key"); |
| 12 | return new Response(value ?? "missing"); |
| 13 | }, |
| 14 | };`; |
| 15 | |
| 16 | /** |
| 17 | * A `Resource.ref(...)` in a Worker's `env` must produce the native |
| 18 | * binding, exactly like a locally-declared resource. A ref used to |
| 19 | * silently degrade to a plain JSON env var: the worker deployed |
| 20 | * "successfully" and `env.KV.put` blew up at runtime — so the live KV |
| 21 | * roundtrip through the deployed worker is the airtight assertion. |
| 22 | */ |
| 23 | test.provider( |
| 24 | "a KV namespace ref in worker env produces a working native binding", |
| 25 | (stack) => |
| 26 | Effect.gen(function* () { |
| 27 | yield* stack.destroy(); |
| 28 | |
| 29 | // Phase 1: the target must already be in state for the ref to |
| 30 | // resolve (a ghost ref fails the plan). |
| 31 | yield* stack.deploy( |
| 32 | Effect.gen(function* () { |
| 33 | return yield* Cloudflare.KV.Namespace("RefNamespace"); |
| 34 | }), |
| 35 | ); |
| 36 | |
| 37 | // Phase 2: bind the namespace via `Namespace.ref(...)` and prove |
| 38 | // the binding works end-to-end. |
| 39 | const deployed = yield* stack.deploy( |
| 40 | Effect.gen(function* () { |
| 41 | const namespace = yield* Cloudflare.KV.Namespace("RefNamespace"); |
| 42 | const worker = yield* Cloudflare.Worker("ref-binding-worker", { |
| 43 | script, |
| 44 | workersDev: true, |
| 45 | env: { |
| 46 | KV: yield* Cloudflare.KV.Namespace.ref("RefNamespace"), |
| 47 | }, |
| 48 | }); |
| 49 | return { namespace, worker }; |
| 50 | }), |
| 51 | ); |
| 52 | |
| 53 | yield* expectUrlContains(deployed.worker.url!, "bound-through-ref", { |
| 54 | label: "ref-bound KV worker", |
| 55 | }); |
| 56 | |
| 57 | yield* stack.destroy(); |
| 58 | }), |
| 59 | { |
| 60 | tags: ["provider:cloudflare", "provider:cloudflare:kv", "provider:cloudflare:worker", "live"], |
| 61 | timeout: 240_000, |
| 62 | }, |
| 63 | ); |
| 64 | |
| 65 | const targetScript = `import { WorkerEntrypoint } from "cloudflare:workers"; |
| 66 | export class Api extends WorkerEntrypoint { |
| 67 | async greet(name) { |
| 68 | return "hello " + name + " from ref target"; |
| 69 | } |
| 70 | } |
| 71 | export default { |
| 72 | async fetch() { |
| 73 | return new Response("ref target up"); |
| 74 | }, |
| 75 | };`; |
| 76 | |
| 77 | const callerScript = `export default { |
| 78 | async fetch(request, env) { |
| 79 | try { |
| 80 | return new Response(await env.API.greet("alice")); |
| 81 | } catch (err) { |
| 82 | return new Response("caller failed: " + err.message, { status: 500 }); |
| 83 | } |
| 84 | }, |
| 85 | };`; |
| 86 | |
| 87 | /** |
| 88 | * `Cloudflare.WorkerEntrypoint(Worker.ref(...), "Api")` must target the |
| 89 | * referenced Worker. A ref is a callable Output proxy, so it used to fail |
| 90 | * the "is this a Worker?" check and silently lower to a self binding on |
| 91 | * the caller (#2262) — the caller has no `Api`, so the greeting proves |
| 92 | * the binding reached the ref'd target. |
| 93 | */ |
| 94 | test.provider( |
| 95 | "a Worker ref in WorkerEntrypoint targets the referenced worker", |
| 96 | (stack) => |
| 97 | Effect.gen(function* () { |
| 98 | yield* stack.destroy(); |
| 99 | |
| 100 | const target = () => |
| 101 | Cloudflare.Worker("ref-entrypoint-target", { |
| 102 | script: targetScript, |
| 103 | }); |
| 104 | |
| 105 | yield* stack.deploy(target()); |
| 106 | |
| 107 | const deployed = yield* stack.deploy( |
| 108 | Effect.gen(function* () { |
| 109 | const deployedTarget = yield* target(); |
| 110 | const caller = yield* Cloudflare.Worker("ref-entrypoint-caller", { |
| 111 | script: callerScript, |
| 112 | workersDev: true, |
| 113 | env: { |
| 114 | API: Cloudflare.WorkerEntrypoint( |
| 115 | yield* Cloudflare.Worker.ref("ref-entrypoint-target"), |
| 116 | "Api", |
| 117 | ), |
| 118 | }, |
| 119 | }); |
| 120 | return { target: deployedTarget, caller }; |
| 121 | }), |
| 122 | ); |
| 123 | |
| 124 | yield* expectUrlContains(deployed.caller.url!, "hello alice from ref target", { |
| 125 | label: "ref-bound entrypoint caller", |
| 126 | }); |
| 127 | |
| 128 | yield* stack.destroy(); |
| 129 | }), |
| 130 | { |
| 131 | tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"], |
| 132 | timeout: 240_000, |
| 133 | }, |
| 134 | ); |
| 135 | |
| 136 | /** |
| 137 | * `Cloudflare.Worker.ref(Declaration)` references the Worker by the |
| 138 | * declaration's logical id (#2607). The caller stack only refs the target, |
| 139 | * so the greeting proves the ref resolved to the deployed target. |
| 140 | */ |
| 141 | test.provider( |
| 142 | "a Worker ref taken from a declaration targets the declared worker", |
| 143 | (stack) => |
| 144 | Effect.gen(function* () { |
| 145 | yield* stack.destroy(); |
| 146 | |
| 147 | const Target = Cloudflare.Worker("ref-declaration-target", { |
| 148 | script: targetScript, |
| 149 | }); |
| 150 | |
| 151 | yield* stack.deploy(Target); |
| 152 | |
| 153 | const deployed = yield* stack.deploy( |
| 154 | Effect.gen(function* () { |
| 155 | const target = yield* Target; |
| 156 | const caller = yield* Cloudflare.Worker("ref-declaration-caller", { |
| 157 | script: callerScript, |
| 158 | workersDev: true, |
| 159 | env: { |
| 160 | API: Cloudflare.WorkerEntrypoint(yield* Cloudflare.Worker.ref(Target), "Api"), |
| 161 | }, |
| 162 | }); |
| 163 | return { target, caller }; |
| 164 | }), |
| 165 | ); |
| 166 | |
| 167 | yield* expectUrlContains(deployed.caller.url!, "hello alice from ref target", { |
| 168 | label: "declaration-ref-bound entrypoint caller", |
| 169 | }); |
| 170 | |
| 171 | yield* stack.destroy(); |
| 172 | }), |
| 173 | { |
| 174 | tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"], |
| 175 | timeout: 240_000, |
| 176 | }, |
| 177 | ); |