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

main
ts 201 lines 7.29 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 getPreset = (accountId: string, appId: string, presetId: string) =>
29 realtimeKit.getPresetByIdPreset({ accountId, appId, presetId }).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 preset is gone after destroy — Cloudflare answers GET for
39 // a missing preset with the typed `RealtimeKitPresetNotFound` (404).
40 const expectGone = (accountId: string, appId: string, presetId: string) =>
41 getPreset(accountId, appId, presetId).pipe(
42 Effect.asSome,
43 Effect.catchTag("RealtimeKitPresetNotFound", () => Effect.succeedNone),
44 Effect.repeat({
45 schedule: Schedule.exponential("500 millis"),
46 until: Option.isNone,
47 times: 8,
48 }),
49 Effect.map((preset) => expect(Option.isNone(preset)).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 preset
54 // name: the suites run concurrently (`sequence.concurrent`) and a shared
55 // preset name would make two stacks create the same preset in the same app
56 // and race to a 409.
57 const APP_NAME = "alchemy-rtk-test-app";
58 const LIFECYCLE_PRESET_NAME = testName("alchemy-rtk-test-preset-lifecycle");
59 const LIST_PRESET_NAME = testName("alchemy-rtk-test-preset-list");
60
61 test.provider(
62 "create with defaults, verify out-of-band, update in place, destroy",
63 (stack) =>
64 Effect.gen(function* () {
65 const entitled = yield* probeEntitlement;
66 if (!entitled) {
67 yield* Effect.logInfo("account is not RealtimeKit-entitled; skipping lifecycle");
68 return;
69 }
70
71 const { accountId } = yield* yield* CloudflareEnvironment;
72
73 yield* stack.destroy();
74
75 // Create — preset with all-default config / ui / permissions.
76 const v1 = yield* stack.deploy(
77 Effect.gen(function* () {
78 const app = yield* Cloudflare.RealtimeKit.App("App", {
79 name: APP_NAME,
80 });
81 return yield* Cloudflare.RealtimeKit.Preset("Preset", {
82 appId: app.appId,
83 name: LIFECYCLE_PRESET_NAME,
84 });
85 }),
86 );
87
88 expect(v1.presetId).toBeTruthy();
89 expect(v1.accountId).toEqual(accountId);
90 expect(v1.name).toEqual(LIFECYCLE_PRESET_NAME);
91 expect(v1.config.viewType).toEqual("GROUP_CALL");
92 expect(v1.permissions?.canRecord).toBe(false);
93
94 // Out-of-band verification via the distilled API.
95 const live = yield* getPreset(accountId, v1.appId, v1.presetId);
96 expect(live.name).toEqual(LIFECYCLE_PRESET_NAME);
97 expect(live.config.viewType).toEqual("GROUP_CALL");
98
99 // In-place update — grant recording + moderation. Same preset (no
100 // replacement).
101 const v2 = yield* stack.deploy(
102 Effect.gen(function* () {
103 const app = yield* Cloudflare.RealtimeKit.App("App", {
104 name: APP_NAME,
105 });
106 return yield* Cloudflare.RealtimeKit.Preset("Preset", {
107 appId: app.appId,
108 name: LIFECYCLE_PRESET_NAME,
109 permissions: {
110 ...Cloudflare.RealtimeKit.defaultRealtimeKitPresetPermissions(),
111 canRecord: true,
112 kickParticipant: true,
113 pinParticipant: true,
114 },
115 });
116 }),
117 );
118
119 expect(v2.presetId).toEqual(v1.presetId);
120 expect(v2.permissions?.canRecord).toBe(true);
121 expect(v2.permissions?.kickParticipant).toBe(true);
122
123 const updated = yield* getPreset(accountId, v2.appId, v2.presetId);
124 expect(updated.permissions?.canRecord).toBe(true);
125
126 // Idempotent re-deploy — reconcile must detect the no-op.
127 const v3 = yield* stack.deploy(
128 Effect.gen(function* () {
129 const app = yield* Cloudflare.RealtimeKit.App("App", {
130 name: APP_NAME,
131 });
132 return yield* Cloudflare.RealtimeKit.Preset("Preset", {
133 appId: app.appId,
134 name: LIFECYCLE_PRESET_NAME,
135 permissions: {
136 ...Cloudflare.RealtimeKit.defaultRealtimeKitPresetPermissions(),
137 canRecord: true,
138 kickParticipant: true,
139 pinParticipant: true,
140 },
141 });
142 }),
143 );
144 expect(v3.presetId).toEqual(v1.presetId);
145
146 // Destroy — the preset must be deleted (the app remains; it has no
147 // delete API).
148 yield* stack.destroy();
149 yield* expectGone(accountId, v1.appId, v1.presetId);
150 }).pipe(logLevel),
151 {
152 tags: ["provider:cloudflare", "provider:cloudflare:realtimekit", "live"],
153 timeout: 120_000,
154 },
155 );
156
157 test.provider(
158 "list enumerates the deployed preset",
159 (stack) =>
160 Effect.gen(function* () {
161 const entitled = yield* probeEntitlement;
162 if (!entitled) {
163 // RealtimeKit beta is entitlement-gated: an unentitled account gets the
164 // typed `Forbidden` (403) on `getApp`, which `list()` propagates. Skip
165 // the live assertion; the App suite pins the typed tag.
166 yield* Effect.logInfo("account is not RealtimeKit-entitled; skipping list");
167 return;
168 }
169
170 yield* stack.destroy();
171
172 const deployed = yield* stack.deploy(
173 Effect.gen(function* () {
174 const app = yield* Cloudflare.RealtimeKit.App("App", {
175 name: APP_NAME,
176 });
177 return yield* Cloudflare.RealtimeKit.Preset("Preset", {
178 appId: app.appId,
179 name: LIST_PRESET_NAME,
180 });
181 }),
182 );
183
184 const provider = yield* Provider.findProvider(Cloudflare.RealtimeKit.Preset);
185 const all = yield* provider.list();
186
187 expect(all.some((p) => p.presetId === deployed.presetId && p.appId === deployed.appId)).toBe(
188 true,
189 );
190 const found = all.find((p) => p.presetId === deployed.presetId);
191 expect(found?.name).toEqual(LIST_PRESET_NAME);
192 expect(found?.config.viewType).toEqual("GROUP_CALL");
193
194 yield* stack.destroy();
195 yield* expectGone(deployed.accountId, deployed.appId, deployed.presetId);
196 }).pipe(logLevel),
197 {
198 tags: ["provider:cloudflare", "provider:cloudflare:realtimekit", "live"],
199 timeout: 120_000,
200 },
201 );