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

main
ts 225 lines 8.13 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as waitingRooms from "@distilled.cloud/cloudflare/waiting-rooms";
7 import * as Provider from "alchemy/Provider";
8 import * as Effect from "effect/Effect";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11 import * as Stream from "effect/Stream";
12 import { testName } from "../../names.ts";
13
14 const { test } = Test.make({ providers: Cloudflare.providers() });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
19
20 // Waiting Rooms require a Business or Enterprise zone plan. On the testing
21 // account's zone every write fails with "Zone not entitled to this
22 // functionality" (Cloudflare code 1034), surfaced as the typed
23 // `ZoneNotEntitled` error. The full lifecycle test below is gated behind an
24 // entitled zone id supplied via env.
25 const entitledZoneId = process.env.CLOUDFLARE_TEST_WAITING_ROOM_ZONE_ID;
26 const entitledZoneHost = process.env.CLOUDFLARE_TEST_WAITING_ROOM_HOST ?? zoneName;
27
28 // Deterministic per-test room names — reused on every run.
29 const NAME_LIFECYCLE = testName("alchemy-waitingroom-lifecycle");
30
31 const resolveZoneId = Effect.gen(function* () {
32 const { accountId } = yield* yield* CloudflareEnvironment;
33 const zone = yield* findZoneByName({ accountId, name: zoneName });
34 if (!zone) {
35 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
36 }
37 return zone.id;
38 });
39
40 // Freshly minted scoped tokens propagate eventually-consistently across
41 // Cloudflare's edge — retry the typed `Forbidden` blips on out-of-band calls.
42 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
43
44 const getRoom = (zoneId: string, waitingRoomId: string) =>
45 waitingRooms.getWaitingRoom({ zoneId, waitingRoomId }).pipe(
46 Effect.retry({
47 while: (e) => e._tag === "Forbidden",
48 schedule: forbiddenRetrySchedule,
49 times: 8,
50 }),
51 );
52
53 const findRoomByName = (zoneId: string, name: string) =>
54 waitingRooms.listWaitingRoomsForZone.items({ zoneId }).pipe(
55 Stream.filter((room) => room.name === name),
56 Stream.runCollect,
57 Effect.map((chunk) => Array.from(chunk)[0]),
58 Effect.retry({
59 while: (e) => e._tag === "Forbidden",
60 schedule: forbiddenRetrySchedule,
61 times: 8,
62 }),
63 );
64
65 test.provider(
66 "surfaces the typed ZoneNotEntitled error on unentitled zones",
67 (stack) =>
68 Effect.gen(function* () {
69 const zoneId = yield* resolveZoneId;
70
71 yield* stack.destroy();
72
73 // The standard testing zone lacks the Waiting Rooms entitlement —
74 // the distilled create must fail with the typed plan-gate tag.
75 const error = yield* waitingRooms
76 .createWaitingRoom({
77 zoneId,
78 name: "alchemy-waitingroom-entitlement-probe",
79 host: zoneName,
80 totalActiveUsers: 200,
81 newUsersPerMinute: 200,
82 })
83 .pipe(
84 Effect.retry({
85 while: (e) => e._tag === "Forbidden",
86 schedule: forbiddenRetrySchedule,
87 times: 8,
88 }),
89 Effect.flip,
90 );
91 expect(error._tag).toEqual("ZoneNotEntitled");
92
93 yield* stack.destroy();
94 }).pipe(logLevel),
95 {
96 tags: [
97 "provider:cloudflare",
98 "provider:cloudflare:waitingroom",
99 "provider:cloudflare:zone",
100 "live",
101 ],
102 },
103 );
104
105 test.provider.skipIf(!entitledZoneId)(
106 "create, update in place, and destroy a waiting room",
107 (stack) =>
108 Effect.gen(function* () {
109 const zoneId = entitledZoneId!;
110
111 yield* stack.destroy();
112
113 const initial = yield* stack.deploy(
114 Effect.gen(function* () {
115 return yield* Cloudflare.WaitingRoom.WaitingRoom("Room", {
116 zoneId,
117 name: NAME_LIFECYCLE,
118 host: entitledZoneHost,
119 path: "/alchemy-test",
120 totalActiveUsers: 200,
121 newUsersPerMinute: 200,
122 description: "v1",
123 });
124 }),
125 );
126
127 expect(initial.waitingRoomId).toBeDefined();
128 expect(initial.zoneId).toEqual(zoneId);
129 expect(initial.name).toEqual(NAME_LIFECYCLE);
130 expect(initial.host).toEqual(entitledZoneHost);
131 expect(initial.path).toEqual("/alchemy-test");
132 expect(initial.totalActiveUsers).toEqual(200);
133 expect(initial.newUsersPerMinute).toEqual(200);
134 expect(initial.description).toEqual("v1");
135 // Cloudflare defaults.
136 expect(initial.sessionDuration).toEqual(5);
137 expect(initial.queueAll).toEqual(false);
138 expect(initial.queueingMethod).toEqual("fifo");
139 expect(initial.suspended).toEqual(false);
140
141 // Out-of-band verification via the distilled API.
142 const live = yield* getRoom(zoneId, initial.waitingRoomId);
143 expect(live.id).toEqual(initial.waitingRoomId);
144 expect(live.name).toEqual(NAME_LIFECYCLE);
145 expect(live.host).toEqual(entitledZoneHost);
146
147 // Update mutable fields in place — same physical room.
148 const updated = yield* stack.deploy(
149 Effect.gen(function* () {
150 return yield* Cloudflare.WaitingRoom.WaitingRoom("Room", {
151 zoneId,
152 name: NAME_LIFECYCLE,
153 host: entitledZoneHost,
154 path: "/alchemy-test",
155 totalActiveUsers: 250,
156 newUsersPerMinute: 200,
157 description: "v2",
158 sessionDuration: 10,
159 queueAll: true,
160 });
161 }),
162 );
163
164 expect(updated.waitingRoomId).toEqual(initial.waitingRoomId);
165 expect(updated.totalActiveUsers).toEqual(250);
166 expect(updated.description).toEqual("v2");
167 expect(updated.sessionDuration).toEqual(10);
168 expect(updated.queueAll).toEqual(true);
169
170 const liveUpdated = yield* getRoom(zoneId, updated.waitingRoomId);
171 expect(liveUpdated.totalActiveUsers).toEqual(250);
172
173 yield* stack.destroy();
174
175 const gone = yield* findRoomByName(zoneId, NAME_LIFECYCLE);
176 expect(gone).toBeUndefined();
177
178 // Destroy again — deletion is idempotent.
179 yield* stack.destroy();
180 }).pipe(logLevel),
181 { tags: ["provider:cloudflare", "provider:cloudflare:waitingroom", "live"], timeout: 120_000 },
182 );
183
184 // Canonical `list()` test (zone-scoped collection): waiting rooms have no
185 // account-wide enumeration API, so `list()` fans out across every zone via
186 // `listAllZones`, exhaustively paginates `listWaitingRoomsForZone` per zone,
187 // and hydrates each room into the `read` Attributes shape. Plan-gated /
188 // unentitled zones reject the route (typed `Forbidden`) and are skipped to
189 // `[]`. The standing test zone lacks the Waiting Rooms entitlement, so the
190 // read-only assertion only requires a well-typed array; the deployed-presence
191 // assertion is gated behind an entitled zone id supplied via env.
192 test.provider(
193 "list enumerates waiting rooms across zones",
194 (stack) =>
195 Effect.gen(function* () {
196 yield* stack.destroy();
197
198 const provider = yield* Provider.findProvider(Cloudflare.WaitingRoom.WaitingRoom);
199
200 if (entitledZoneId) {
201 const deployed = yield* stack.deploy(
202 Effect.gen(function* () {
203 return yield* Cloudflare.WaitingRoom.WaitingRoom("ListRoom", {
204 zoneId: entitledZoneId,
205 host: entitledZoneHost,
206 path: "/alchemy-list",
207 totalActiveUsers: 200,
208 newUsersPerMinute: 200,
209 });
210 }),
211 );
212
213 const all = yield* provider.list();
214 expect(all.some((r) => r.waitingRoomId === deployed.waitingRoomId)).toBe(true);
215 } else {
216 // Unentitled standing zone: no rooms exist, but `list()` must still
217 // return a well-typed array (unentitled zones skip to `[]`).
218 const all = yield* provider.list();
219 expect(Array.isArray(all)).toBe(true);
220 }
221
222 yield* stack.destroy();
223 }).pipe(logLevel),
224 { tags: ["provider:cloudflare", "provider:cloudflare:waitingroom", "live"], timeout: 120_000 },
225 );