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

main
ts 180 lines 6.95 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 { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: Cloudflare.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // RealtimeKit (the acquired Dyte platform) is in beta and must be enabled
16 // per account. On unentitled accounts every call fails with the typed
17 // `Forbidden` error (HTTP 403). Probe once per test: either the account can
18 // list apps (entitled) or we pin the typed tag and no-op the lifecycle.
19 const probeEntitlement = Effect.gen(function* () {
20 const { accountId } = yield* yield* CloudflareEnvironment;
21 return yield* realtimeKit.getApp({ accountId }).pipe(
22 Effect.as(true),
23 Effect.catchTag("Forbidden", () => Effect.succeed(false)),
24 );
25 });
26
27 // The scoped API token the test harness mints propagates eventually-
28 // consistently — ride out 403 blips on out-of-band verification calls.
29 const listApps = (accountId: string) =>
30 realtimeKit.getApp({ accountId }).pipe(
31 Effect.retry({
32 while: (e) => e._tag === "Forbidden",
33 schedule: Schedule.exponential("500 millis"),
34 times: 8,
35 }),
36 Effect.map((res) => res.data ?? []),
37 );
38
39 // Deterministic name — RealtimeKit apps cannot be deleted, so every run of
40 // this suite adopts the same app instead of leaking a new one, so the name is
41 // shared by every user rather than derived from the stage. The Preset
42 // and Webhook suites share this name and adopt the same app.
43 const APP_NAME = "alchemy-rtk-test-app";
44
45 // Adoption is deterministic: among same-named apps (duplicates can exist on
46 // Cloudflare's side), the provider picks the oldest by createdAt.
47 const oldestNamed = (
48 apps: readonly ({
49 id?: string | null;
50 name?: string | null;
51 createdAt?: string | null;
52 } | null)[],
53 ) =>
54 apps
55 .filter((a) => a?.name === APP_NAME)
56 .sort((a, b) => (a?.createdAt ?? "").localeCompare(b?.createdAt ?? ""))
57 .at(0);
58
59 test.provider(
60 "unentitled accounts surface the typed Forbidden error",
61 (stack) =>
62 Effect.gen(function* () {
63 yield* stack.destroy();
64
65 const entitled = yield* probeEntitlement;
66 if (entitled) {
67 yield* Effect.logInfo("account is RealtimeKit-entitled; probe test is a no-op");
68 return;
69 }
70
71 const { accountId } = yield* yield* CloudflareEnvironment;
72 // The typed tag — not UnknownCloudflareError, not a status check.
73 const error = yield* realtimeKit.getApp({ accountId }).pipe(Effect.flip);
74 expect(error._tag).toEqual("Forbidden");
75
76 yield* stack.destroy();
77 }).pipe(logLevel),
78 {
79 tags: ["provider:cloudflare", "provider:cloudflare:realtimekit", "live"],
80 timeout: 120_000,
81 },
82 );
83
84 test.provider(
85 "create (or adopt), verify out-of-band, destroy forgets but keeps the app",
86 (stack) =>
87 Effect.gen(function* () {
88 const entitled = yield* probeEntitlement;
89 if (!entitled) {
90 yield* Effect.logInfo("account is not RealtimeKit-entitled; skipping lifecycle");
91 return;
92 }
93
94 const { accountId } = yield* yield* CloudflareEnvironment;
95
96 yield* stack.destroy();
97
98 // If an app with the deterministic name already exists (from any
99 // previous run), the deploy must adopt the oldest one instead of
100 // creating a duplicate.
101 const preexisting = oldestNamed(yield* listApps(accountId));
102
103 // Create — or adopt the app left over from a previous run (apps can
104 // never be deleted, so adoption-by-name is the designed behavior).
105 const app = yield* stack.deploy(Cloudflare.RealtimeKit.App("App", { name: APP_NAME }));
106
107 expect(app.appId).toBeTruthy();
108 expect(app.accountId).toEqual(accountId);
109 expect(app.name).toEqual(APP_NAME);
110 if (preexisting) {
111 expect(app.appId).toEqual(preexisting.id);
112 }
113
114 // Out-of-band verification via the distilled API.
115 const live = (yield* listApps(accountId)).find((a) => a?.id === app.appId);
116 expect(live?.name).toEqual(APP_NAME);
117
118 // Re-deploy is a no-op update — same app, same id.
119 const again = yield* stack.deploy(Cloudflare.RealtimeKit.App("App", { name: APP_NAME }));
120 expect(again.appId).toEqual(app.appId);
121
122 // Destroy only forgets the app from state (no delete API) — the app
123 // must still exist on the account afterwards.
124 yield* stack.destroy();
125 const after = (yield* listApps(accountId)).find((a) => a?.id === app.appId);
126 expect(after?.id).toEqual(app.appId);
127
128 // A fresh deploy after destroy adopts by name instead of creating a
129 // duplicate — deterministically the oldest same-named app (which may
130 // predate the one this run touched if duplicates leaked before
131 // adoption-by-name existed).
132 const expectedAdoptee = oldestNamed(yield* listApps(accountId));
133 const adopted = yield* stack.deploy(Cloudflare.RealtimeKit.App("App", { name: APP_NAME }));
134 expect(adopted.appId).toEqual(expectedAdoptee?.id);
135
136 yield* stack.destroy();
137 }).pipe(logLevel),
138 {
139 tags: ["provider:cloudflare", "provider:cloudflare:realtimekit", "live"],
140 timeout: 120_000,
141 },
142 );
143
144 test.provider(
145 "list enumerates the deployed RealtimeKit app",
146 (stack) =>
147 Effect.gen(function* () {
148 const entitled = yield* probeEntitlement;
149 if (!entitled) {
150 // RealtimeKit beta is entitlement-gated: an unentitled account gets the
151 // typed `Forbidden` (403) on `getApp`, which `list()` propagates. Skip
152 // the live assertion; the probe test above pins the typed tag.
153 yield* Effect.logInfo("account is not RealtimeKit-entitled; skipping list");
154 return;
155 }
156
157 yield* stack.destroy();
158
159 // Create — or adopt the same-named app left over from a previous run
160 // (apps can never be deleted, so adoption-by-name is the designed
161 // behavior).
162 const deployed = yield* stack.deploy(Cloudflare.RealtimeKit.App("App", { name: APP_NAME }));
163
164 const provider = yield* Provider.findProvider(Cloudflare.RealtimeKit.App);
165 const all = yield* provider.list();
166
167 expect(all.some((a) => a.appId === deployed.appId)).toBe(true);
168 const found = all.find((a) => a.appId === deployed.appId);
169 expect(found?.name).toEqual(APP_NAME);
170 expect(found?.accountId).toEqual(deployed.accountId);
171
172 // Destroy only forgets the app (no delete API) — it still exists, so a
173 // subsequent list() would still observe it. That's the expected residue.
174 yield* stack.destroy();
175 }).pipe(logLevel),
176 {
177 tags: ["provider:cloudflare", "provider:cloudflare:realtimekit", "live"],
178 timeout: 120_000,
179 },
180 );