The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Alchemy from "alchemy"; |
| 5 | import { Action } from "alchemy/Action"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as pathe from "pathe"; |
| 12 | |
| 13 | // `dev: true` runs local providers behind the RPC sidecar proxy by default, |
| 14 | // matching the process topology of the real `alchemy dev` command (see |
| 15 | // MakeOptions.sidecar in Test/Core.ts). |
| 16 | const { test } = Test.make({ |
| 17 | providers: Cloudflare.providers(), |
| 18 | dev: true, |
| 19 | }); |
| 20 | |
| 21 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 22 | |
| 23 | class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{ |
| 24 | status: number; |
| 25 | }> {} |
| 26 | |
| 27 | const getJsonReady = (url: string) => |
| 28 | Effect.gen(function* () { |
| 29 | const client = yield* HttpClient.HttpClient; |
| 30 | const res = yield* client.get(url).pipe( |
| 31 | Effect.flatMap((res) => |
| 32 | res.status === 200 |
| 33 | ? Effect.succeed(res) |
| 34 | : Effect.fail(new WorkerNotReady({ status: res.status })), |
| 35 | ), |
| 36 | Effect.retry({ |
| 37 | while: (e): e is WorkerNotReady => e instanceof WorkerNotReady, |
| 38 | // Cap the backoff: an uncapped exponential over 10 recurs sums to |
| 39 | // ~8.5 minutes and turns a persistent non-200 into an apparent hang. |
| 40 | schedule: Schedule.max([ |
| 41 | Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]), |
| 42 | Schedule.recurs(10), |
| 43 | ]), |
| 44 | }), |
| 45 | ); |
| 46 | return yield* res.json; |
| 47 | }).pipe(Effect.orDie); |
| 48 | |
| 49 | test.provider( |
| 50 | "Instant local binding supports CRUD, rejects metadata, and lists beyond 1,000 keys", |
| 51 | (stack) => |
| 52 | Effect.gen(function* () { |
| 53 | yield* stack.destroy(); |
| 54 | const deploy = (title: string) => |
| 55 | stack.deploy( |
| 56 | Effect.gen(function* () { |
| 57 | const ns = yield* Cloudflare.KV.InstantNamespace("Config", { title }); |
| 58 | const worker = yield* Cloudflare.Worker("instant-local", { |
| 59 | main: pathe.resolve(import.meta.dirname, "fixtures/kv-instant-local-worker.ts"), |
| 60 | env: { KV: ns }, |
| 61 | }); |
| 62 | return { ns, worker }; |
| 63 | }), |
| 64 | ); |
| 65 | const initial = yield* deploy("initial"); |
| 66 | expect(initial.ns.namespaceId).toMatch(/^dev:kv-instant:/); |
| 67 | const result = yield* getJsonReady(`${initial.worker.url}/roundtrip`); |
| 68 | expect(result).toEqual({ |
| 69 | value: "hello", |
| 70 | metadata: null, |
| 71 | metadataRejected: true, |
| 72 | longKeyRejected: true, |
| 73 | count: 1001, |
| 74 | complete: true, |
| 75 | afterDelete: null, |
| 76 | }); |
| 77 | const updated = yield* deploy("renamed"); |
| 78 | expect(updated.ns.namespaceId).toBe(initial.ns.namespaceId); |
| 79 | expect(updated.ns.title).toBe("renamed"); |
| 80 | expect(yield* getJsonReady(`${updated.worker.url}/get?key=config:0`)).toEqual({ value: "1" }); |
| 81 | yield* stack.destroy(); |
| 82 | }).pipe(logLevel), |
| 83 | { timeout: 120_000, tags: ["provider:cloudflare", "provider:cloudflare:kv", "local"] }, |
| 84 | ); |
| 85 | |
| 86 | test.provider( |
| 87 | "Instant ReadWriteNamespaceLocal Action enforces key and metadata rules and single-page listing", |
| 88 | (stack) => |
| 89 | Effect.gen(function* () { |
| 90 | yield* stack.destroy(); |
| 91 | |
| 92 | const deployed = yield* stack.deploy( |
| 93 | Effect.gen(function* () { |
| 94 | const ns = yield* Cloudflare.KV.InstantNamespace("ActionSeededKV"); |
| 95 | |
| 96 | const Seed = Action( |
| 97 | "Seed", |
| 98 | Effect.gen(function* () { |
| 99 | const client = yield* Cloudflare.KV.ReadWriteNamespace(ns); |
| 100 | return Effect.fn(function* () { |
| 101 | yield* client.put("seeded", "from-action"); |
| 102 | yield* client.put("other", "value"); |
| 103 | const value = yield* client.get("seeded"); |
| 104 | const list = yield* client.list(); |
| 105 | yield* client.delete("other"); |
| 106 | const afterDelete = yield* client.get("other"); |
| 107 | const keyError = yield* client.put("é".repeat(151), "value").pipe(Effect.flip); |
| 108 | const metadataError = yield* client |
| 109 | .put("metadata", "value", { |
| 110 | // @ts-expect-error Also verify rejection for untyped JavaScript callers. |
| 111 | metadata: { unsupported: true }, |
| 112 | }) |
| 113 | .pipe(Effect.flip); |
| 114 | yield* client.put("page:one", "value"); |
| 115 | yield* client.put("page:two", "value"); |
| 116 | const all = yield* client.list({ |
| 117 | prefix: "page:", |
| 118 | // @ts-expect-error Native callers may still send a page size; Instant ignores it. |
| 119 | limit: 1, |
| 120 | }); |
| 121 | return { |
| 122 | value, |
| 123 | keys: list.keys.map((k) => k.name), |
| 124 | afterDelete, |
| 125 | keyError: keyError.message, |
| 126 | metadataError: metadataError.message, |
| 127 | count: all.keys.length, |
| 128 | complete: all.list_complete, |
| 129 | }; |
| 130 | }); |
| 131 | }).pipe(Effect.provide(Cloudflare.KV.ReadWriteNamespaceLocal)), |
| 132 | ); |
| 133 | const seeded = yield* Seed({}); |
| 134 | |
| 135 | const worker = yield* Cloudflare.Worker("kv-action-worker", { |
| 136 | main: pathe.resolve(import.meta.dirname, "fixtures/kv-instant-local-worker.ts"), |
| 137 | env: { KV: ns }, |
| 138 | }); |
| 139 | return { ns, worker, seeded }; |
| 140 | }), |
| 141 | ); |
| 142 | |
| 143 | expect(deployed.ns.namespaceId).toMatch(/^dev:kv-instant:/); |
| 144 | expect(deployed.seeded.value).toBe("from-action"); |
| 145 | expect(deployed.seeded.keys.sort()).toEqual(["other", "seeded"]); |
| 146 | expect(deployed.seeded.afterDelete).toBeNull(); |
| 147 | expect(deployed.seeded.keyError).toContain("300 bytes"); |
| 148 | expect(deployed.seeded.metadataError).toContain("does not support metadata"); |
| 149 | expect(deployed.seeded.count).toBe(2); |
| 150 | expect(deployed.seeded.complete).toBe(true); |
| 151 | |
| 152 | // The worker's native binding reads the same simulator storage the |
| 153 | // Action's gateway wrote to. |
| 154 | const body = (yield* getJsonReady(`${deployed.worker.url}/get?key=seeded`)) as { |
| 155 | value: string | null; |
| 156 | }; |
| 157 | expect(body.value).toBe("from-action"); |
| 158 | |
| 159 | yield* stack.destroy(); |
| 160 | }).pipe(logLevel), |
| 161 | { |
| 162 | tags: ["provider:cloudflare", "provider:cloudflare:kv", "provider:cloudflare:worker", "local"], |
| 163 | timeout: 120_000, |
| 164 | }, |
| 165 | ); |
| 166 | |
| 167 | // Requires private beta access and the cost opt-in even when the stack is local. |
| 168 | test.provider( |
| 169 | "Alchemy.remote opts InstantNamespace out of local emulation", |
| 170 | (stack) => |
| 171 | Effect.gen(function* () { |
| 172 | yield* stack.destroy(); |
| 173 | const ns = yield* stack.deploy( |
| 174 | Cloudflare.KV.InstantNamespace("Remote").pipe(Alchemy.remote()), |
| 175 | ); |
| 176 | expect(ns.namespaceId.startsWith("dev:")).toBe(false); |
| 177 | yield* stack.destroy(); |
| 178 | }), |
| 179 | { retry: 0, timeout: 120_000, tags: ["provider:cloudflare", "provider:cloudflare:kv", "live"] }, |
| 180 | ); |
| 181 | |
| 182 | test.provider( |
| 183 | "switching between classic and Instant replaces local storage in both directions", |
| 184 | (stack) => |
| 185 | Effect.gen(function* () { |
| 186 | yield* stack.destroy(); |
| 187 | const classic = yield* stack.deploy(Cloudflare.KV.Namespace("Config", { title: "config" })); |
| 188 | const instant = yield* stack.deploy( |
| 189 | Cloudflare.KV.InstantNamespace("Config", { title: "config" }), |
| 190 | ); |
| 191 | expect(instant.namespaceId).not.toBe(classic.namespaceId); |
| 192 | expect(instant.namespaceId).toMatch(/^dev:kv-instant:/); |
| 193 | expect(instant.mode).toBe("instant"); |
| 194 | const unchanged = yield* stack.deploy( |
| 195 | Cloudflare.KV.InstantNamespace("Config", { title: "config" }), |
| 196 | ); |
| 197 | expect(unchanged.namespaceId).toBe(instant.namespaceId); |
| 198 | const restored = yield* stack.deploy(Cloudflare.KV.Namespace("Config", { title: "config" })); |
| 199 | expect(restored.namespaceId).not.toBe(instant.namespaceId); |
| 200 | expect(restored.namespaceId.startsWith("dev:kv-instant:")).toBe(false); |
| 201 | const stable = yield* stack.deploy(Cloudflare.KV.Namespace("Config", { title: "config" })); |
| 202 | expect(stable.namespaceId).toBe(restored.namespaceId); |
| 203 | yield* stack.destroy(); |
| 204 | }).pipe(logLevel), |
| 205 | { timeout: 120_000, tags: ["provider:cloudflare", "provider:cloudflare:kv", "local"] }, |
| 206 | ); |