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

main
ts 171 lines 6.82 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 * as Stream from "effect/Stream";
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 // Ride out 403 blips (`Forbidden`) while the harness-minted token
18 // propagates across Cloudflare's edge.
19 const getEndpoint = (accountId: string, proxyEndpointId: string) =>
20 zeroTrust.getGatewayProxyEndpoint({ accountId, proxyEndpointId }).pipe(
21 Effect.retry({
22 while: (e) => e._tag === "Forbidden",
23 schedule: Schedule.exponential("500 millis"),
24 times: 8,
25 }),
26 );
27
28 // A deleted endpoint surfaces as `ProxyEndpointNotFound` (code 2002).
29 const expectGone = (accountId: string, proxyEndpointId: string) =>
30 getEndpoint(accountId, proxyEndpointId).pipe(
31 Effect.flatMap(() => Effect.fail({ _tag: "EndpointNotDeleted" } as const)),
32 Effect.catchTag("ProxyEndpointNotFound", () => Effect.void),
33 Effect.retry({
34 while: (e) => e._tag === "EndpointNotDeleted",
35 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
36 }),
37 );
38
39 test.provider(
40 "create, update, and destroy an identity-kind proxy endpoint",
41 (stack) =>
42 Effect.gen(function* () {
43 const { accountId } = yield* yield* CloudflareEnvironment;
44
45 yield* stack.destroy();
46
47 const endpoint = yield* stack.deploy(
48 Cloudflare.Gateway.ProxyEndpoint("IdentityProxy", {
49 name: testName("alchemy-zt-proxy-identity"),
50 kind: "identity",
51 }),
52 );
53
54 expect(endpoint.proxyEndpointId).toBeTruthy();
55 expect(endpoint.accountId).toEqual(accountId);
56 expect(endpoint.kind).toEqual("identity");
57 // Cloudflare assigns a stable PAC/proxy subdomain.
58 expect(endpoint.subdomain).toBeTruthy();
59
60 const live = yield* getEndpoint(accountId, endpoint.proxyEndpointId);
61 expect(live.name).toEqual(testName("alchemy-zt-proxy-identity"));
62 expect(live.subdomain).toEqual(endpoint.subdomain);
63
64 // Rename in place — same id, same subdomain.
65 const updated = yield* stack.deploy(
66 Cloudflare.Gateway.ProxyEndpoint("IdentityProxy", {
67 name: testName("alchemy-zt-proxy-identity-v2"),
68 kind: "identity",
69 }),
70 );
71 expect(updated.proxyEndpointId).toEqual(endpoint.proxyEndpointId);
72 expect(updated.subdomain).toEqual(endpoint.subdomain);
73 expect(updated.name).toEqual(testName("alchemy-zt-proxy-identity-v2"));
74
75 const liveAfter = yield* getEndpoint(accountId, endpoint.proxyEndpointId);
76 expect(liveAfter.name).toEqual(testName("alchemy-zt-proxy-identity-v2"));
77
78 yield* stack.destroy();
79 yield* expectGone(accountId, endpoint.proxyEndpointId);
80 }).pipe(logLevel),
81 { tags: ["provider:cloudflare", "provider:cloudflare:gateway", "live"] },
82 );
83
84 // IP-based proxy endpoints are an Enterprise entitlement. On the testing
85 // account, creation fails with the typed `IpProxyEndpointsRequireEnterprise`
86 // error (Cloudflare code 2009: "IP based proxy endpoints are limited to
87 // enterprise accounts."). Exercise the typed error surface directly; if the
88 // account ever becomes Enterprise the create succeeds and we clean up.
89 test.provider(
90 "ip-kind endpoints surface the typed entitlement error",
91 () =>
92 Effect.gen(function* () {
93 const { accountId } = yield* yield* CloudflareEnvironment;
94
95 const outcome = yield* zeroTrust
96 .createGatewayProxyEndpoint({
97 accountId,
98 name: "alchemy-zt-proxy-ip",
99 kind: "ip",
100 // RFC 5737 documentation range.
101 ips: ["203.0.113.1/32"],
102 })
103 .pipe(
104 Effect.retry({
105 while: (e) => e._tag === "Forbidden",
106 schedule: Schedule.exponential("500 millis"),
107 times: 8,
108 }),
109 Effect.map((created) => ({ _tag: "Created" as const, created })),
110 Effect.catchTag("IpProxyEndpointsRequireEnterprise", (e) =>
111 Effect.succeed({ _tag: "Entitlement" as const, error: e }),
112 ),
113 );
114
115 if (outcome._tag === "Created") {
116 // Enterprise account — clean up the endpoint we just made.
117 yield* zeroTrust
118 .deleteGatewayProxyEndpoint({ accountId, proxyEndpointId: outcome.created.id ?? "" })
119 .pipe(Effect.catchTag("ProxyEndpointNotFound", () => Effect.void));
120 expect(outcome.created.name).toEqual("alchemy-zt-proxy-ip");
121 } else {
122 expect(outcome.error._tag).toEqual("IpProxyEndpointsRequireEnterprise");
123 }
124 }).pipe(logLevel),
125 { tags: ["provider:cloudflare", "provider:cloudflare:gateway", "live"] },
126 );
127
128 test.provider(
129 "list enumerates the deployed proxy endpoint",
130 (stack) =>
131 Effect.gen(function* () {
132 const { accountId } = yield* yield* CloudflareEnvironment;
133
134 yield* stack.destroy();
135
136 // Self-heal: remove any same-named endpoint left orphaned in the cloud by a
137 // previously-interrupted run so the deploy below doesn't trip the
138 // "OwnedBySomeoneElse" adoption guard.
139 const orphans = yield* zeroTrust.listGatewayProxyEndpoints.items({ accountId }).pipe(
140 Stream.filter((e) => e.name === testName("alchemy-zt-proxy-list")),
141 Stream.runCollect,
142 Effect.map((chunk) => Array.from(chunk)),
143 );
144 yield* Effect.forEach(orphans, (o) =>
145 zeroTrust
146 .deleteGatewayProxyEndpoint({ accountId, proxyEndpointId: o.id ?? "" })
147 .pipe(Effect.catchTag("ProxyEndpointNotFound", () => Effect.void)),
148 );
149
150 // identity-kind endpoints work on all Zero Trust plans, so this deploy is
151 // not entitlement-gated (unlike ip-kind, which needs Enterprise).
152 const endpoint = yield* stack.deploy(
153 Cloudflare.Gateway.ProxyEndpoint("ListProxy", {
154 name: testName("alchemy-zt-proxy-list"),
155 kind: "identity",
156 }),
157 );
158
159 const provider = yield* Provider.findProvider(Cloudflare.Gateway.ProxyEndpoint);
160 const all = yield* provider.list();
161
162 const match = all.find((x) => x.proxyEndpointId === endpoint.proxyEndpointId);
163 expect(match).toBeDefined();
164 expect(match?.accountId).toEqual(accountId);
165 expect(match?.name).toEqual(testName("alchemy-zt-proxy-list"));
166 expect(match?.kind).toEqual("identity");
167
168 yield* stack.destroy();
169 }).pipe(logLevel),
170 { tags: ["provider:cloudflare", "provider:cloudflare:gateway", "live"] },
171 );