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

main
ts 206 lines 7.81 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as realtimeKit from "@distilled.cloud/cloudflare/realtime-kit";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import * as Option from "effect/Option";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11 import { testName } from "../../names.ts";
12
13 const { test } = Test.make({ providers: Cloudflare.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 // RealtimeKit is beta / entitlement-gated — unentitled accounts get the
18 // typed `Forbidden` (403) on every call. Probe and no-op when unentitled;
19 // the App suite pins the typed tag.
20 const probeEntitlement = Effect.gen(function* () {
21 const { accountId } = yield* yield* CloudflareEnvironment;
22 return yield* realtimeKit.getApp({ accountId }).pipe(
23 Effect.as(true),
24 Effect.catchTag("Forbidden", () => Effect.succeed(false)),
25 );
26 });
27
28 const getWebhook = (accountId: string, appId: string, webhookId: string) =>
29 realtimeKit.getWebhookByIdWebhook({ accountId, appId, webhookId }).pipe(
30 Effect.retry({
31 while: (e) => e._tag === "Forbidden",
32 schedule: Schedule.exponential("500 millis"),
33 times: 8,
34 }),
35 Effect.map((res) => res.data),
36 );
37
38 // Poll until the webhook is gone after destroy — Cloudflare answers GET for
39 // a missing webhook with the typed `RealtimeKitWebhookNotFound` (404).
40 const expectGone = (accountId: string, appId: string, webhookId: string) =>
41 getWebhook(accountId, appId, webhookId).pipe(
42 Effect.asSome,
43 Effect.catchTag("RealtimeKitWebhookNotFound", () => Effect.succeedNone),
44 Effect.repeat({
45 schedule: Schedule.exponential("500 millis"),
46 until: Option.isNone,
47 times: 8,
48 }),
49 Effect.map((webhook) => expect(Option.isNone(webhook)).toBe(true)),
50 );
51
52 // Deterministic names — apps cannot be deleted, so every run adopts the
53 // same app instead of leaking a new one. Each test gets its own webhook
54 // name AND url: the suites run concurrently (`sequence.concurrent`) and both
55 // adopt the same singleton app, so they'd otherwise create webhooks in the
56 // same app. RealtimeKit enforces a unique webhook url per app, so two suites
57 // sharing a url race to a 409 (a shared name alone is not enough to collide).
58 const APP_NAME = "alchemy-rtk-test-app";
59 const LIFECYCLE_WEBHOOK_NAME = testName("alchemy-rtk-test-webhook-lifecycle");
60 const LIST_WEBHOOK_NAME = testName("alchemy-rtk-test-webhook-list");
61 const LIFECYCLE_WEBHOOK_URL = "https://example.com/alchemy-rtk-webhook-lifecycle";
62 const LIST_WEBHOOK_URL = "https://example.com/alchemy-rtk-webhook-list";
63
64 test.provider(
65 "create, verify out-of-band, update in place, destroy",
66 (stack) =>
67 Effect.gen(function* () {
68 const entitled = yield* probeEntitlement;
69 if (!entitled) {
70 yield* Effect.logInfo("account is not RealtimeKit-entitled; skipping lifecycle");
71 return;
72 }
73
74 const { accountId } = yield* yield* CloudflareEnvironment;
75
76 yield* stack.destroy();
77
78 // Create — meeting lifecycle events to a fixed URL.
79 const v1 = yield* stack.deploy(
80 Effect.gen(function* () {
81 const app = yield* Cloudflare.RealtimeKit.App("App", {
82 name: APP_NAME,
83 });
84 return yield* Cloudflare.RealtimeKit.Webhook("Webhook", {
85 appId: app.appId,
86 name: LIFECYCLE_WEBHOOK_NAME,
87 url: LIFECYCLE_WEBHOOK_URL,
88 events: ["meeting.started", "meeting.ended"],
89 });
90 }),
91 );
92
93 expect(v1.webhookId).toBeTruthy();
94 expect(v1.accountId).toEqual(accountId);
95 expect(v1.name).toEqual(LIFECYCLE_WEBHOOK_NAME);
96 expect(v1.url).toEqual(LIFECYCLE_WEBHOOK_URL);
97 expect([...v1.events].sort()).toEqual(["meeting.ended", "meeting.started"]);
98 expect(v1.enabled).toBe(true);
99
100 // Out-of-band verification via the distilled API.
101 const live = yield* getWebhook(accountId, v1.appId, v1.webhookId);
102 expect(live.name).toEqual(LIFECYCLE_WEBHOOK_NAME);
103 expect(live.url).toEqual(LIFECYCLE_WEBHOOK_URL);
104
105 // In-place update — pause delivery and change the event set. Same
106 // webhook (no replacement).
107 const v2 = yield* stack.deploy(
108 Effect.gen(function* () {
109 const app = yield* Cloudflare.RealtimeKit.App("App", {
110 name: APP_NAME,
111 });
112 return yield* Cloudflare.RealtimeKit.Webhook("Webhook", {
113 appId: app.appId,
114 name: LIFECYCLE_WEBHOOK_NAME,
115 url: LIFECYCLE_WEBHOOK_URL,
116 events: ["recording.statusUpdate"],
117 enabled: false,
118 });
119 }),
120 );
121
122 expect(v2.webhookId).toEqual(v1.webhookId);
123 expect(v2.enabled).toBe(false);
124 expect(v2.events).toEqual(["recording.statusUpdate"]);
125
126 const updated = yield* getWebhook(accountId, v2.appId, v2.webhookId);
127 expect(updated.enabled).toBe(false);
128 expect([...updated.events]).toEqual(["recording.statusUpdate"]);
129
130 // Idempotent re-deploy — reconcile must detect the no-op.
131 const v3 = yield* stack.deploy(
132 Effect.gen(function* () {
133 const app = yield* Cloudflare.RealtimeKit.App("App", {
134 name: APP_NAME,
135 });
136 return yield* Cloudflare.RealtimeKit.Webhook("Webhook", {
137 appId: app.appId,
138 name: LIFECYCLE_WEBHOOK_NAME,
139 url: LIFECYCLE_WEBHOOK_URL,
140 events: ["recording.statusUpdate"],
141 enabled: false,
142 });
143 }),
144 );
145 expect(v3.webhookId).toEqual(v1.webhookId);
146
147 // Destroy — the webhook must be deleted (the app remains; it has no
148 // delete API).
149 yield* stack.destroy();
150 yield* expectGone(accountId, v1.appId, v1.webhookId);
151 }).pipe(logLevel),
152 {
153 tags: ["provider:cloudflare", "provider:cloudflare:realtimekit", "live"],
154 timeout: 120_000,
155 },
156 );
157
158 // `list()` fans out over every RealtimeKit app in the account and flattens
159 // each app's webhooks. When the account is RealtimeKit-entitled, deploy a
160 // webhook and assert it appears; when unentitled the apps endpoint 403s
161 // (typed `Forbidden`) and `list()` returns a well-typed empty array.
162 test.provider(
163 "list enumerates webhooks across the account's apps",
164 (stack) =>
165 Effect.gen(function* () {
166 yield* stack.destroy();
167
168 const provider = yield* Provider.findProvider(Cloudflare.RealtimeKit.Webhook);
169
170 const entitled = yield* probeEntitlement;
171 if (!entitled) {
172 yield* Effect.logInfo("account is not RealtimeKit-entitled; asserting empty list");
173 const empty = yield* provider.list();
174 expect(empty).toEqual([]);
175 yield* stack.destroy();
176 return;
177 }
178
179 const deployed = yield* stack.deploy(
180 Effect.gen(function* () {
181 const app = yield* Cloudflare.RealtimeKit.App("App", {
182 name: APP_NAME,
183 });
184 return yield* Cloudflare.RealtimeKit.Webhook("Webhook", {
185 appId: app.appId,
186 name: LIST_WEBHOOK_NAME,
187 url: LIST_WEBHOOK_URL,
188 events: ["meeting.started", "meeting.ended"],
189 });
190 }),
191 );
192
193 const all = yield* provider.list();
194 expect(all.some((w) => w.webhookId === deployed.webhookId)).toBe(true);
195 const found = all.find((w) => w.webhookId === deployed.webhookId);
196 expect(found?.name).toEqual(LIST_WEBHOOK_NAME);
197 expect(found?.url).toEqual(LIST_WEBHOOK_URL);
198 expect(found?.appId).toEqual(deployed.appId);
199
200 yield* stack.destroy();
201 }).pipe(logLevel),
202 {
203 tags: ["provider:cloudflare", "provider:cloudflare:realtimekit", "live"],
204 timeout: 120_000,
205 },
206 );