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

main
ts 203 lines 7.73 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 diagnostics from "@distilled.cloud/cloudflare/diagnostics";
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 const getHealthcheck = (accountId: string, id: string) =>
17 diagnostics.getEndpointHealthcheck({ accountId, id });
18
19 // A GET issued immediately after a create/update can transiently 404
20 // (`EndpointHealthcheckNotFound`, code 1022) while the new state
21 // propagates across Cloudflare's edge — bounded-retry the out-of-band
22 // verification read until it resolves.
23 const getHealthcheckLive = (accountId: string, id: string) =>
24 getHealthcheck(accountId, id).pipe(
25 Effect.retry({
26 while: (e) => e._tag === "EndpointHealthcheckNotFound",
27 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
28 }),
29 );
30
31 // Poll until the healthcheck is gone after destroy. Cloudflare answers
32 // GET for a missing healthcheck with the typed
33 // `EndpointHealthcheckNotFound` (code 1022, 404).
34 const expectGone = (accountId: string, id: string) =>
35 getHealthcheck(accountId, id).pipe(
36 Effect.flatMap(() => Effect.fail({ _tag: "HealthcheckNotDeleted" } as const)),
37 Effect.catchTag("EndpointHealthcheckNotFound", () => Effect.void),
38 Effect.retry({
39 while: (e) => e._tag === "HealthcheckNotDeleted",
40 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
41 }),
42 );
43
44 test.provider(
45 "rejects public IPs with the typed InvalidHealthcheckEndpoint error",
46 (stack) =>
47 Effect.gen(function* () {
48 const { accountId } = yield* yield* CloudflareEnvironment;
49
50 yield* stack.destroy();
51
52 // Cloudflare only accepts on-net (private) IPs; a public IP fails
53 // with code 1002 — pinned here as the typed tag.
54 const error = yield* diagnostics
55 .createEndpointHealthcheck({
56 accountId,
57 checkType: "icmp",
58 endpoint: "1.1.1.1",
59 name: "alchemy-diag-ehc-invalid-probe",
60 })
61 .pipe(Effect.flip);
62 expect(error._tag).toEqual("InvalidHealthcheckEndpoint");
63
64 yield* stack.destroy();
65 }).pipe(logLevel),
66 {
67 tags: ["provider:cloudflare", "provider:cloudflare:diagnostics", "live"],
68 timeout: 120_000,
69 },
70 );
71
72 // Out-of-band cleanup of healthchecks leaked by previous failed runs —
73 // the test state store does not persist across runs, so a mid-test
74 // failure leaves the cloud resource behind and the cold read would
75 // (correctly) gate it behind the adopt policy.
76 const cleanLeftovers = (accountId: string) =>
77 diagnostics.listEndpointHealthchecks({ accountId }).pipe(
78 Effect.flatMap(
79 Effect.fn(function* (list) {
80 for (const hc of list) {
81 if (hc.name?.startsWith(testName("alchemy-diag-ehc")) && hc.id) {
82 yield* diagnostics
83 .deleteEndpointHealthcheck({ accountId, id: hc.id })
84 .pipe(Effect.catchTag("EndpointHealthcheckNotFound", () => Effect.void));
85 }
86 }
87 }),
88 ),
89 );
90
91 test.provider(
92 "creates an endpoint healthcheck, updates in place, and destroys",
93 (stack) =>
94 Effect.gen(function* () {
95 const { accountId } = yield* yield* CloudflareEnvironment;
96
97 yield* stack.destroy();
98 yield* cleanLeftovers(accountId);
99
100 const check = yield* stack.deploy(
101 Cloudflare.Diagnostics.EndpointHealthcheck("Check", {
102 endpoint: "10.77.0.1",
103 name: testName("alchemy-diag-ehc"),
104 }),
105 );
106
107 expect(check.healthcheckId).toBeTruthy();
108 expect(check.accountId).toEqual(accountId);
109 expect(check.checkType).toEqual("icmp");
110 expect(check.endpoint).toEqual("10.77.0.1");
111 expect(check.name).toEqual(testName("alchemy-diag-ehc"));
112
113 // Out-of-band verification via the distilled API.
114 const live = yield* getHealthcheckLive(accountId, check.healthcheckId);
115 expect(live.endpoint).toEqual("10.77.0.1");
116 expect(live.name).toEqual(testName("alchemy-diag-ehc"));
117
118 // Update the probed endpoint in place — same UUID.
119 const updated = yield* stack.deploy(
120 Cloudflare.Diagnostics.EndpointHealthcheck("Check", {
121 endpoint: "10.77.0.2",
122 name: testName("alchemy-diag-ehc"),
123 }),
124 );
125 expect(updated.healthcheckId).toEqual(check.healthcheckId);
126 expect(updated.endpoint).toEqual("10.77.0.2");
127 expect(updated.name).toEqual(testName("alchemy-diag-ehc"));
128
129 const liveUpdated = yield* getHealthcheckLive(accountId, check.healthcheckId);
130 expect(liveUpdated.endpoint).toEqual("10.77.0.2");
131 expect(liveUpdated.name).toEqual(testName("alchemy-diag-ehc"));
132
133 // No-op deploy — converges without churn, same UUID.
134 const noop = yield* stack.deploy(
135 Cloudflare.Diagnostics.EndpointHealthcheck("Check", {
136 endpoint: "10.77.0.2",
137 name: testName("alchemy-diag-ehc"),
138 }),
139 );
140 expect(noop.healthcheckId).toEqual(check.healthcheckId);
141
142 // Changing the name is create-only on Cloudflare's side — the
143 // resource is replaced with a fresh UUID and the old one removed.
144 // The endpoint changes too: endpoints are unique per account and
145 // replacement creates the new healthcheck before deleting the old.
146 const replaced = yield* stack.deploy(
147 Cloudflare.Diagnostics.EndpointHealthcheck("Check", {
148 endpoint: "10.77.0.3",
149 name: testName("alchemy-diag-ehc-v2"),
150 }),
151 );
152 expect(replaced.healthcheckId).not.toEqual(check.healthcheckId);
153 expect(replaced.name).toEqual(testName("alchemy-diag-ehc-v2"));
154 yield* expectGone(accountId, check.healthcheckId);
155
156 const liveReplaced = yield* getHealthcheckLive(accountId, replaced.healthcheckId);
157 expect(liveReplaced.name).toEqual(testName("alchemy-diag-ehc-v2"));
158
159 yield* stack.destroy();
160
161 yield* expectGone(accountId, replaced.healthcheckId);
162 }).pipe(logLevel),
163 {
164 tags: ["provider:cloudflare", "provider:cloudflare:diagnostics", "live"],
165 timeout: 180_000,
166 },
167 );
168
169 // Canonical `list()` test (account collection): deploy a real healthcheck,
170 // then assert its UUID appears in the exhaustively-enumerated result.
171 test.provider(
172 "list enumerates the deployed endpoint healthcheck",
173 (stack) =>
174 Effect.gen(function* () {
175 const { accountId } = yield* yield* CloudflareEnvironment;
176
177 yield* stack.destroy();
178 yield* cleanLeftovers(accountId);
179
180 const deployed = yield* stack.deploy(
181 Cloudflare.Diagnostics.EndpointHealthcheck("ListCheck", {
182 endpoint: "10.78.0.1",
183 }),
184 );
185
186 const provider = yield* Provider.findProvider(Cloudflare.Diagnostics.EndpointHealthcheck);
187 const all = yield* provider.list();
188
189 expect(all.some((hc) => hc.healthcheckId === deployed.healthcheckId)).toBe(true);
190 // Each listed item is the full `read` Attributes shape.
191 const found = all.find((hc) => hc.healthcheckId === deployed.healthcheckId);
192 expect(found?.accountId).toEqual(accountId);
193 expect(found?.endpoint).toEqual("10.78.0.1");
194 expect(found?.checkType).toEqual("icmp");
195
196 yield* stack.destroy();
197 yield* expectGone(accountId, deployed.healthcheckId);
198 }).pipe(logLevel),
199 {
200 tags: ["provider:cloudflare", "provider:cloudflare:diagnostics", "live"],
201 timeout: 180_000,
202 },
203 );