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 { Action } from "alchemy/Action"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 10 | |
| 11 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 12 | |
| 13 | // Binding a KV namespace inside an Action via `ReadWriteNamespaceLocal` — the |
| 14 | // local (current-credentials) implementation of the `ReadWriteNamespace` |
| 15 | // binding. Exercises the client (put/get/list/delete) and the accessor |
| 16 | // mechanism (`yield* namespace.namespaceId` resolved at apply time). |
| 17 | test.provider( |
| 18 | "ReadWriteNamespaceLocal: seed and read a namespace from an Action", |
| 19 | (stack) => |
| 20 | Effect.gen(function* () { |
| 21 | yield* stack.destroy(); |
| 22 | |
| 23 | const out = yield* stack.deploy( |
| 24 | Effect.gen(function* () { |
| 25 | const namespace = yield* Cloudflare.KV.Namespace("SeedNamespace"); |
| 26 | |
| 27 | const Seed = Action( |
| 28 | "Seed", |
| 29 | Effect.gen(function* () { |
| 30 | const kv = yield* Cloudflare.KV.ReadWriteNamespace(namespace); |
| 31 | // Accessor — resolved at apply time against the tracker. |
| 32 | const namespaceId = yield* namespace.namespaceId; |
| 33 | |
| 34 | return Effect.fn(function* () { |
| 35 | // The namespace was created moments ago; the KV value API can |
| 36 | // briefly answer `namespace not found` before it sees it. |
| 37 | // Retry the first write, bounded, until it lands. |
| 38 | yield* kv.put("greeting", "hello world").pipe( |
| 39 | Effect.retry({ |
| 40 | schedule: Schedule.spaced("1 second"), |
| 41 | times: 10, |
| 42 | }), |
| 43 | ); |
| 44 | |
| 45 | // KV is eventually consistent — retry the read-back until the |
| 46 | // value propagates (bounded so the test fails fast). |
| 47 | const value = yield* kv.get("greeting").pipe( |
| 48 | Effect.flatMap((v) => |
| 49 | v === "hello world" |
| 50 | ? Effect.succeed(v) |
| 51 | : Effect.fail("not yet propagated" as const), |
| 52 | ), |
| 53 | Effect.retry({ |
| 54 | schedule: Schedule.spaced("1 second"), |
| 55 | times: 10, |
| 56 | }), |
| 57 | Effect.orElseSucceed(() => null), |
| 58 | ); |
| 59 | |
| 60 | const listed = yield* kv.list(); |
| 61 | const names = listed.keys.map((k) => k.name); |
| 62 | |
| 63 | yield* kv.delete("greeting"); |
| 64 | |
| 65 | const afterDelete = yield* kv.get("greeting").pipe( |
| 66 | Effect.flatMap((v) => |
| 67 | v === null ? Effect.succeed(v) : Effect.fail("not yet deleted" as const), |
| 68 | ), |
| 69 | Effect.retry({ |
| 70 | schedule: Schedule.spaced("1 second"), |
| 71 | times: 10, |
| 72 | }), |
| 73 | Effect.orElseSucceed(() => "still present" as string | null), |
| 74 | ); |
| 75 | |
| 76 | return { |
| 77 | namespaceId: yield* namespaceId, |
| 78 | value, |
| 79 | names, |
| 80 | afterDelete, |
| 81 | }; |
| 82 | }); |
| 83 | }).pipe(Effect.provide(Cloudflare.KV.ReadWriteNamespaceLocal)), |
| 84 | ); |
| 85 | |
| 86 | return yield* Seed({}); |
| 87 | }), |
| 88 | ); |
| 89 | |
| 90 | expect(out.namespaceId).toBeTruthy(); |
| 91 | expect(out.value).toBe("hello world"); |
| 92 | expect(out.names).toContain("greeting"); |
| 93 | expect(out.afterDelete).toBeNull(); |
| 94 | |
| 95 | yield* stack.destroy(); |
| 96 | }).pipe(logLevel), |
| 97 | { |
| 98 | tags: ["provider:cloudflare", "provider:cloudflare:kv", "live"], |
| 99 | timeout: 120_000, |
| 100 | }, |
| 101 | ); |