The best way to control Claude Code, Codex and more from any computer or phone

main
ts 97 lines 3.6 KB
Raw
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 // R2 is eventually consistent — a freshly written object can read back `null`
14 // for a moment. Bounded spaced retry so a real failure surfaces fast.
15 const readBack = Schedule.max([Schedule.spaced("1 second"), Schedule.recurs(15)]);
16
17 // Binding an R2 bucket inside an Action via `ReadWriteBucketLocal` — the local
18 // (current-credentials) implementation of the `ReadWriteBucket` binding.
19 // Exercises the client (put/get/list/delete) and the accessor mechanism
20 // (`yield* bucket.bucketName` resolved at apply time).
21 test.provider(
22 "ReadWriteBucketLocal: seed and read a bucket from an Action",
23 (stack) =>
24 Effect.gen(function* () {
25 yield* stack.destroy();
26
27 const out = yield* stack.deploy(
28 Effect.gen(function* () {
29 const bucket = yield* Cloudflare.R2.Bucket("SeedBucket", {
30 forceDestroy: true,
31 });
32
33 const Seed = Action(
34 "Seed",
35 Effect.gen(function* () {
36 const r2 = yield* Cloudflare.R2.ReadWriteBucket(bucket);
37 // Accessor — resolved at apply time against the tracker.
38 const bucketName = yield* bucket.bucketName;
39
40 return Effect.fn(function* () {
41 yield* r2.put("hello.txt", "Hello, World!").pipe(Effect.orDie);
42 yield* r2.put("greeting/hi.txt", "hi").pipe(Effect.orDie);
43
44 // Read back with bounded retry (eventual consistency).
45 const value = yield* r2.get("hello.txt").pipe(
46 Effect.flatMap((o) => (o ? o.text() : Effect.succeed<string | null>(null))),
47 Effect.orDie,
48 Effect.repeat({
49 schedule: readBack,
50 until: (v) => v !== null,
51 }),
52 );
53
54 const listed = yield* r2.list().pipe(Effect.orDie);
55 const keys = listed.objects.map((o) => o.key).sort();
56
57 const head = yield* r2.head("hello.txt").pipe(Effect.orDie);
58
59 yield* r2.delete("hello.txt").pipe(Effect.orDie);
60 yield* r2.delete("greeting/hi.txt").pipe(Effect.orDie);
61
62 const afterDelete = yield* r2.head("hello.txt").pipe(
63 Effect.orDie,
64 Effect.repeat({
65 schedule: readBack,
66 until: (o) => o === null,
67 }),
68 );
69
70 return {
71 bucketName: yield* bucketName,
72 value,
73 keys,
74 headExists: head !== null,
75 deleted: afterDelete === null,
76 };
77 });
78 }).pipe(Effect.provide(Cloudflare.R2.ReadWriteBucketLocal)),
79 );
80
81 return yield* Seed({});
82 }),
83 );
84
85 expect(out.bucketName).toBeTruthy();
86 expect(out.value).toBe("Hello, World!");
87 expect(out.keys).toEqual(["greeting/hi.txt", "hello.txt"]);
88 expect(out.headExists).toBe(true);
89 expect(out.deleted).toBe(true);
90
91 yield* stack.destroy();
92 }).pipe(logLevel),
93 {
94 tags: ["provider:cloudflare", "provider:cloudflare:r2", "live"],
95 timeout: 120_000,
96 },
97 );