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

main
ts 196 lines 7.22 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 zeroTrust from "@distilled.cloud/cloudflare/zero-trust";
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 // Skipped: the testing account isn't entitled to Gateway Locations; every
15 // create fails with `Unauthorized` ("This account does not have access to this
16 // feature"). Re-enable on an account with the Zero Trust Gateway entitlement.
17
18 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
19
20 // Ride out 403 blips (`Forbidden`) while the harness-minted token
21 // propagates across Cloudflare's edge.
22 const getLocation = (accountId: string, locationId: string) =>
23 zeroTrust.getGatewayLocation({ accountId, locationId }).pipe(
24 Effect.retry({
25 while: (e) => e._tag === "Forbidden",
26 schedule: Schedule.exponential("500 millis"),
27 times: 8,
28 }),
29 );
30
31 // A deleted location surfaces as `GatewayLocationNotFound` (code 1103).
32 const expectGone = (accountId: string, locationId: string) =>
33 getLocation(accountId, locationId).pipe(
34 Effect.flatMap(() => Effect.fail({ _tag: "LocationNotDeleted" } as const)),
35 Effect.catchTag("GatewayLocationNotFound", () => Effect.void),
36 Effect.retry({
37 while: (e) => e._tag === "LocationNotDeleted",
38 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
39 }),
40 );
41
42 test.provider.skip(
43 "create, verify, and destroy a location",
44 (stack) =>
45 Effect.gen(function* () {
46 const { accountId } = yield* yield* CloudflareEnvironment;
47
48 yield* stack.destroy();
49
50 const location = yield* stack.deploy(
51 Cloudflare.Gateway.Location("BasicLocation", {
52 name: testName("alchemy-zt-location-basic"),
53 ecsSupport: false,
54 }),
55 );
56
57 expect(location.locationId).toBeTruthy();
58 expect(location.accountId).toEqual(accountId);
59 expect(location.name).toEqual(testName("alchemy-zt-location-basic"));
60 expect(location.clientDefault).toBe(false);
61 // Cloudflare assigns a stable DoH subdomain to every location.
62 expect(location.dohSubdomain).toBeTruthy();
63
64 const live = yield* getLocation(accountId, location.locationId);
65 expect(live.name).toEqual(testName("alchemy-zt-location-basic"));
66 expect(live.dohSubdomain).toEqual(location.dohSubdomain);
67
68 yield* stack.destroy();
69 yield* expectGone(accountId, location.locationId);
70 }).pipe(logLevel),
71 { tags: ["provider:cloudflare", "provider:cloudflare:gateway", "live"] },
72 );
73
74 test.provider.skip(
75 "update name and ecsSupport in place (same id)",
76 (stack) =>
77 Effect.gen(function* () {
78 const { accountId } = yield* yield* CloudflareEnvironment;
79
80 yield* stack.destroy();
81
82 const initial = yield* stack.deploy(
83 Cloudflare.Gateway.Location("UpdateLocation", {
84 name: testName("alchemy-zt-location-update"),
85 ecsSupport: false,
86 }),
87 );
88 expect(initial.ecsSupport).toBe(false);
89
90 const updated = yield* stack.deploy(
91 Cloudflare.Gateway.Location("UpdateLocation", {
92 name: testName("alchemy-zt-location-update-v2"),
93 ecsSupport: true,
94 }),
95 );
96
97 // Same location mutated in place — not a replacement; the
98 // server-assigned DoH subdomain is stable across updates.
99 expect(updated.locationId).toEqual(initial.locationId);
100 expect(updated.dohSubdomain).toEqual(initial.dohSubdomain);
101 expect(updated.name).toEqual(testName("alchemy-zt-location-update-v2"));
102 expect(updated.ecsSupport).toBe(true);
103
104 const live = yield* getLocation(accountId, updated.locationId);
105 expect(live.name).toEqual(testName("alchemy-zt-location-update-v2"));
106 expect(live.ecsSupport).toBe(true);
107
108 // Redeploying identical props is a no-op (still the same location).
109 const noop = yield* stack.deploy(
110 Cloudflare.Gateway.Location("UpdateLocation", {
111 name: testName("alchemy-zt-location-update-v2"),
112 ecsSupport: true,
113 }),
114 );
115 expect(noop.locationId).toEqual(initial.locationId);
116
117 yield* stack.destroy();
118 yield* expectGone(accountId, initial.locationId);
119 }).pipe(logLevel),
120 { tags: ["provider:cloudflare", "provider:cloudflare:gateway", "live"] },
121 );
122
123 test.provider.skip(
124 "recreates after out-of-band delete",
125 (stack) =>
126 Effect.gen(function* () {
127 const { accountId } = yield* yield* CloudflareEnvironment;
128
129 yield* stack.destroy();
130
131 const location = yield* stack.deploy(
132 Cloudflare.Gateway.Location("HealLocation", {
133 name: testName("alchemy-zt-location-heal"),
134 ecsSupport: false,
135 }),
136 );
137
138 yield* zeroTrust.deleteGatewayLocation({ accountId, locationId: location.locationId }).pipe(
139 Effect.retry({
140 while: (e) => e._tag === "Forbidden",
141 schedule: Schedule.exponential("500 millis"),
142 times: 8,
143 }),
144 );
145
146 // Change a prop to force reconcile — it must observe the location as
147 // missing and recreate it instead of failing on a 404.
148 const healed = yield* stack.deploy(
149 Cloudflare.Gateway.Location("HealLocation", {
150 name: testName("alchemy-zt-location-heal"),
151 ecsSupport: true,
152 }),
153 );
154
155 expect(healed.locationId).not.toEqual(location.locationId);
156 expect(healed.ecsSupport).toBe(true);
157 const live = yield* getLocation(accountId, healed.locationId);
158 expect(live.ecsSupport).toBe(true);
159
160 yield* stack.destroy();
161 yield* expectGone(accountId, healed.locationId);
162 }).pipe(logLevel),
163 { tags: ["provider:cloudflare", "provider:cloudflare:gateway", "live"] },
164 );
165
166 // Canonical `list()` test (account-scoped collection): deploy a location,
167 // then enumerate every Gateway location in the account via the typed
168 // provider and assert the deployed one is present in the exhaustively
169 // paginated result.
170 test.provider.skip(
171 "list enumerates deployed gateway locations",
172 (stack) =>
173 Effect.gen(function* () {
174 yield* stack.destroy();
175
176 const location = yield* stack.deploy(
177 Cloudflare.Gateway.Location("ListLocation", {
178 name: testName("alchemy-zt-location-list"),
179 ecsSupport: false,
180 }),
181 );
182
183 const provider = yield* Provider.findProvider(Cloudflare.Gateway.Location);
184 const all = yield* provider.list();
185
186 // The deployed location appears with the exact `read` Attributes shape.
187 const found = all.find((l) => l.locationId === location.locationId);
188 expect(found).toBeDefined();
189 expect(found?.name).toEqual(testName("alchemy-zt-location-list"));
190 expect(found?.accountId).toEqual(location.accountId);
191
192 yield* stack.destroy();
193 yield* expectGone(location.accountId, location.locationId);
194 }).pipe(logLevel),
195 { tags: ["provider:cloudflare", "provider:cloudflare:gateway", "live"] },
196 );