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

main
ts 193 lines 6.56 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 magicTransit from "@distilled.cloud/cloudflare/magic-transit";
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 import { testName } from "../../names.ts";
11
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 // Magic WAN custom apps are entitlement-gated. On the standard testing
17 // account every app call fails with the typed `MagicWanUnauthorized`
18 // error (Cloudflare code 1025) or `MagicWanNotEnabled`
19 // (code 1101, "forbidden: feature not enabled"). The lifecycle test below is gated behind an explicit opt-in env
20 // flag for entitled accounts; the probe test always runs and pins the
21 // typed tag.
22 const entitled = !!process.env.CLOUDFLARE_TEST_MAGIC_WAN;
23
24 // There is no getApp endpoint — scan the list for the app id.
25 const findApp = (accountId: string, appId: string) =>
26 magicTransit
27 .listApps({ accountId })
28 .pipe(
29 Effect.map((r) =>
30 r.result.find((app) => "accountAppId" in app && app.accountAppId === appId),
31 ),
32 );
33
34 // Poll until the app is gone from the list after destroy.
35 const expectGone = (accountId: string, appId: string) =>
36 findApp(accountId, appId).pipe(
37 Effect.flatMap((found) =>
38 found ? Effect.fail({ _tag: "AppNotDeleted" } as const) : Effect.void,
39 ),
40 Effect.retry({
41 while: (e) => e._tag === "AppNotDeleted",
42 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
43 }),
44 );
45
46 test.provider(
47 "unentitled accounts surface the typed Magic WAN entitlement error",
48 (stack) =>
49 Effect.gen(function* () {
50 const { accountId } = yield* yield* CloudflareEnvironment;
51
52 yield* stack.destroy();
53
54 const canList = yield* magicTransit.listApps({ accountId }).pipe(
55 Effect.as(true),
56 Effect.catchTag(["MagicWanUnauthorized", "MagicWanNotEnabled"], () =>
57 Effect.succeed(false),
58 ),
59 );
60 if (canList) {
61 // Entitled account — the gated lifecycle test covers real behavior.
62 yield* Effect.logInfo("account is Magic WAN-entitled; probe test is a no-op");
63 return;
64 }
65
66 // The typed tag — not UnknownCloudflareError, not a status check.
67 const error = yield* magicTransit.listApps({ accountId }).pipe(Effect.flip);
68 expect(["MagicWanUnauthorized", "MagicWanNotEnabled"]).toContain(error._tag);
69
70 const createError = yield* magicTransit
71 .createApp({
72 accountId,
73 name: "alchemy-app-probe",
74 type: "Collaboration",
75 hostnames: ["probe.alchemy.test"],
76 })
77 .pipe(Effect.flip);
78 expect(["MagicWanUnauthorized", "MagicWanNotEnabled"]).toContain(createError._tag);
79
80 yield* stack.destroy();
81 }).pipe(logLevel),
82 {
83 tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"],
84 timeout: 120_000,
85 },
86 );
87
88 test.provider.skipIf(!entitled)(
89 "creates a custom app, updates mutable props in place, and destroys it",
90 (stack) =>
91 Effect.gen(function* () {
92 const { accountId } = yield* yield* CloudflareEnvironment;
93
94 yield* stack.destroy();
95
96 const app = yield* stack.deploy(
97 Cloudflare.MagicTransit.MagicApp("App", {
98 name: testName("alchemy-magic-app"),
99 type: "Collaboration",
100 hostnames: ["crm.alchemy.test"],
101 }),
102 );
103
104 expect(app.appId).toBeTruthy();
105 expect(app.accountId).toEqual(accountId);
106 expect(app.name).toEqual(testName("alchemy-magic-app"));
107 expect(app.type).toEqual("Collaboration");
108 expect(app.hostnames).toEqual(["crm.alchemy.test"]);
109
110 // Out-of-band verification via the distilled API.
111 const live = yield* findApp(accountId, app.appId);
112 expect(live).toBeDefined();
113
114 // Update mutable props in place — same appId.
115 const updated = yield* stack.deploy(
116 Cloudflare.MagicTransit.MagicApp("App", {
117 name: testName("alchemy-magic-app-v2"),
118 type: "Collaboration",
119 hostnames: ["crm.alchemy.test", "erp.alchemy.test"],
120 }),
121 );
122
123 expect(updated.appId).toEqual(app.appId);
124 expect(updated.name).toEqual(testName("alchemy-magic-app-v2"));
125 expect([...(updated.hostnames ?? [])].sort()).toEqual([
126 "crm.alchemy.test",
127 "erp.alchemy.test",
128 ]);
129
130 yield* stack.destroy();
131
132 yield* expectGone(accountId, app.appId);
133 }).pipe(logLevel),
134 {
135 tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"],
136 timeout: 120_000,
137 },
138 );
139
140 // Read-only list assertion. Always safe: on an unentitled account the
141 // account-scoped apps list rejects with the typed `MagicWanUnauthorized` /
142 // `MagicWanNotEnabled`, which `list()` maps to a well-typed empty array.
143 test.provider(
144 "list enumerates account apps (well-typed [] when unentitled)",
145 (stack) =>
146 Effect.gen(function* () {
147 yield* stack.destroy();
148
149 const provider = yield* Provider.findProvider(Cloudflare.MagicTransit.MagicApp);
150 const all = yield* provider.list();
151
152 expect(Array.isArray(all)).toBe(true);
153 for (const app of all) {
154 expect(typeof app.appId).toBe("string");
155 expect(typeof app.accountId).toBe("string");
156 }
157
158 yield* stack.destroy();
159 }).pipe(logLevel),
160 {
161 tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"],
162 timeout: 120_000,
163 },
164 );
165
166 // Entitled-account variant: deploy a real app and assert it appears in the
167 // exhaustively-paginated list result.
168 test.provider.skipIf(!entitled)(
169 "list includes a deployed custom app",
170 (stack) =>
171 Effect.gen(function* () {
172 yield* stack.destroy();
173
174 const deployed = yield* stack.deploy(
175 Cloudflare.MagicTransit.MagicApp("ListApp", {
176 name: testName("alchemy-magic-list-app"),
177 type: "Collaboration",
178 hostnames: ["list.alchemy.test"],
179 }),
180 );
181
182 const provider = yield* Provider.findProvider(Cloudflare.MagicTransit.MagicApp);
183 const all = yield* provider.list();
184
185 expect(all.some((app) => app.appId === deployed.appId)).toBe(true);
186
187 yield* stack.destroy();
188 }).pipe(logLevel),
189 {
190 tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"],
191 timeout: 120_000,
192 },
193 );