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

main
ts 148 lines 5.51 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as cache from "@distilled.cloud/cloudflare/cache";
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 const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
16
17 const resolveZoneId = Effect.gen(function* () {
18 const { accountId } = yield* yield* CloudflareEnvironment;
19 const zone = yield* findZoneByName({ accountId, name: zoneName });
20 if (!zone) {
21 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
22 }
23 return zone.id;
24 });
25
26 // The scoped API token the test harness mints propagates eventually-
27 // consistently across Cloudflare's edge — a fresh token intermittently 403s.
28 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
29
30 /**
31 * Probe the origin_cloud_regions API. As of 2026-06 the endpoint exists in
32 * the Cloudflare TypeScript SDK spec but is not yet routed on the public
33 * API — every call answers HTTP 400 with codes 7003/7000 ("No route for
34 * that URI"), surfaced as the typed `InvalidRoute` error. The probe maps
35 * that to "unreleased" so the lifecycle test self-enables the moment
36 * Cloudflare ships the route.
37 */
38 const probeAvailability = (zoneId: string) =>
39 cache.listOriginCloudRegions({ zoneId }).pipe(
40 Effect.map(() => "available" as const),
41 Effect.catchTag("InvalidRoute", () => Effect.succeed("unreleased" as const)),
42 Effect.retry({
43 while: (e) => e._tag === "Forbidden",
44 schedule: forbiddenRetrySchedule,
45 times: 8,
46 }),
47 );
48
49 /** Observe a single mapping out-of-band, `undefined` when gone (typed). */
50 const getMapping = (zoneId: string, ip: string) =>
51 cache.getOriginCloudRegion({ zoneId, originIP: ip }).pipe(
52 Effect.catchTag("OriginCloudRegionNotFound", () => Effect.succeed(undefined)),
53 Effect.retry({
54 while: (e) => e._tag === "Forbidden",
55 schedule: forbiddenRetrySchedule,
56 times: 8,
57 }),
58 );
59
60 test.provider(
61 "creates, updates in place, replaces on ip change, and deletes the mapping",
62 (stack) =>
63 Effect.gen(function* () {
64 const zoneId = yield* resolveZoneId;
65
66 // The endpoint is in the vendor spec but not yet deployed on the
67 // public API. When unreleased, the typed `InvalidRoute` is the whole
68 // observable behavior — assert it and stop. The lifecycle below runs
69 // automatically once Cloudflare routes the endpoint.
70 const availability = yield* probeAvailability(zoneId);
71 if (availability === "unreleased") {
72 yield* Effect.logInfo(
73 "origin_cloud_regions API not yet routed (typed InvalidRoute) — skipping lifecycle",
74 );
75 return;
76 }
77
78 yield* stack.destroy();
79
80 const created = yield* stack.deploy(
81 Effect.gen(function* () {
82 return yield* Cloudflare.Cache.OriginCloudRegion("ApiOrigin", {
83 zoneId,
84 ip: "192.0.2.10",
85 vendor: "aws",
86 region: "us-east-1",
87 });
88 }),
89 );
90 expect(created.zoneId).toEqual(zoneId);
91 expect(created.originIp).toEqual("192.0.2.10");
92 expect(created.vendor).toEqual("aws");
93 expect(created.region).toEqual("us-east-1");
94
95 // Out-of-band verification via the distilled API.
96 const live = yield* getMapping(zoneId, "192.0.2.10");
97 expect(live).toBeDefined();
98 expect(live!.vendor).toEqual("aws");
99 expect(live!.region).toEqual("us-east-1");
100
101 // Update in place — vendor/region are mutable; the IP identity holds.
102 const updated = yield* stack.deploy(
103 Effect.gen(function* () {
104 return yield* Cloudflare.Cache.OriginCloudRegion("ApiOrigin", {
105 zoneId,
106 ip: "192.0.2.10",
107 vendor: "aws",
108 region: "eu-west-1",
109 });
110 }),
111 );
112 expect(updated.originIp).toEqual("192.0.2.10");
113 expect(updated.region).toEqual("eu-west-1");
114
115 const liveUpdated = yield* getMapping(zoneId, "192.0.2.10");
116 expect(liveUpdated!.region).toEqual("eu-west-1");
117
118 // Replacement — the IP is the mapping's identity; the old mapping
119 // must be gone and the new one present.
120 const replaced = yield* stack.deploy(
121 Effect.gen(function* () {
122 return yield* Cloudflare.Cache.OriginCloudRegion("ApiOrigin", {
123 zoneId,
124 ip: "192.0.2.20",
125 vendor: "aws",
126 region: "eu-west-1",
127 });
128 }),
129 );
130 expect(replaced.originIp).toEqual("192.0.2.20");
131
132 const oldGone = yield* getMapping(zoneId, "192.0.2.10");
133 expect(oldGone).toBeUndefined();
134 const newLive = yield* getMapping(zoneId, "192.0.2.20");
135 expect(newLive).toBeDefined();
136
137 yield* stack.destroy();
138
139 // Destroy removed the mapping — observed via the typed
140 // `OriginCloudRegionNotFound` error (mapped to `undefined`).
141 const gone = yield* getMapping(zoneId, "192.0.2.20");
142 expect(gone).toBeUndefined();
143 }).pipe(logLevel),
144 {
145 tags: ["provider:cloudflare", "provider:cloudflare:cache", "provider:cloudflare:zone", "live"],
146 timeout: 120_000,
147 },
148 );