@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 212 lines 7.5 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 loadBalancers from "@distilled.cloud/cloudflare/load-balancers";
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 // Load Balancing is a paid add-on subscription. The testing account does
17 // not have it: monitor creation is rejected with the degenerate plan limit
18 // "interval is not in range [1, 1]" (Cloudflare code 1002), surfaced as the
19 // typed `MonitorIntervalOutOfRange` error. Set CLOUDFLARE_LB_ENABLED=1 once
20 // the subscription is provisioned to run the full lifecycle tests.
21 const lbEnabled = !!process.env.CLOUDFLARE_LB_ENABLED;
22
23 // Deterministic per-test physical names — reused on every run.
24 const NAME_ENTITLEMENT = testName("alchemy-lb-monitor-entitlement-probe");
25 const NAME_LIFECYCLE = testName("alchemy-lb-monitor-lifecycle");
26
27 // Freshly minted scoped tokens propagate eventually-consistently across
28 // Cloudflare's edge — retry the typed `Forbidden` blips on out-of-band calls.
29 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
30
31 const getMonitor = (accountId: string, monitorId: string) =>
32 loadBalancers.getMonitor({ accountId, monitorId }).pipe(
33 Effect.retry({
34 while: (e) => e._tag === "Forbidden",
35 schedule: forbiddenRetrySchedule,
36 times: 8,
37 }),
38 );
39
40 const expectGone = (accountId: string, monitorId: string) =>
41 getMonitor(accountId, monitorId).pipe(
42 Effect.flatMap(() => Effect.fail({ _tag: "MonitorNotDeleted" } as const)),
43 // A missing monitor surfaces as the typed `MonitorNotFound` (Cloudflare
44 // error code 1001) — that's the success condition here.
45 Effect.catchTag("MonitorNotFound", () => Effect.void),
46 Effect.retry({
47 while: (e) => e._tag === "MonitorNotDeleted",
48 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
49 }),
50 );
51
52 // Unentitlement probe: pins the typed plan-gate rejection, so it must skip
53 // on entitled accounts — there the create would succeed instead of failing.
54 test.provider.skipIf(lbEnabled)(
55 "surfaces the typed MonitorIntervalOutOfRange error without the LB subscription",
56 (stack) =>
57 Effect.gen(function* () {
58 const { accountId } = yield* yield* CloudflareEnvironment;
59
60 yield* stack.destroy();
61
62 // Without the Load Balancing subscription the account's allowed
63 // monitor interval range collapses to [1, 1], so any realistic
64 // interval is rejected with the typed plan-gate tag.
65 const error = yield* loadBalancers
66 .createMonitor({
67 accountId,
68 type: "http",
69 description: NAME_ENTITLEMENT,
70 interval: 60,
71 })
72 .pipe(
73 Effect.retry({
74 while: (e) => e._tag === "Forbidden",
75 schedule: forbiddenRetrySchedule,
76 times: 8,
77 }),
78 Effect.flip,
79 );
80 expect(error._tag).toEqual("MonitorIntervalOutOfRange");
81
82 yield* stack.destroy();
83 }).pipe(logLevel),
84 { tags: ["provider:cloudflare", "provider:cloudflare:loadbalancer", "live"] },
85 );
86
87 test.provider.skipIf(!lbEnabled)(
88 "create, update in place, and destroy a monitor",
89 (stack) =>
90 Effect.gen(function* () {
91 const { accountId } = yield* yield* CloudflareEnvironment;
92
93 yield* stack.destroy();
94
95 const initial = yield* stack.deploy(
96 Cloudflare.LoadBalancer.Monitor("Monitor", {
97 description: NAME_LIFECYCLE,
98 type: "https",
99 path: "/health",
100 expectedCodes: "2xx",
101 allowInsecure: true,
102 }),
103 );
104
105 expect(initial.monitorId).toBeTruthy();
106 expect(initial.accountId).toEqual(accountId);
107 expect(initial.description).toEqual(NAME_LIFECYCLE);
108 expect(initial.type).toEqual("https");
109
110 const live = yield* getMonitor(accountId, initial.monitorId);
111 expect(live.description).toEqual(NAME_LIFECYCLE);
112 expect(live.path).toEqual("/health");
113 expect(live.expectedCodes).toEqual("2xx");
114
115 // Mutable props update in place — same monitorId.
116 const updated = yield* stack.deploy(
117 Cloudflare.LoadBalancer.Monitor("Monitor", {
118 description: NAME_LIFECYCLE,
119 type: "https",
120 path: "/healthz",
121 expectedCodes: "200",
122 allowInsecure: true,
123 retries: 1,
124 }),
125 );
126 expect(updated.monitorId).toEqual(initial.monitorId);
127
128 const synced = yield* getMonitor(accountId, updated.monitorId);
129 expect(synced.path).toEqual("/healthz");
130 expect(synced.expectedCodes).toEqual("200");
131 expect(synced.retries).toEqual(1);
132
133 // Redeploying identical props is a no-op (still the same monitor).
134 const noop = yield* stack.deploy(
135 Cloudflare.LoadBalancer.Monitor("Monitor", {
136 description: NAME_LIFECYCLE,
137 type: "https",
138 path: "/healthz",
139 expectedCodes: "200",
140 allowInsecure: true,
141 retries: 1,
142 }),
143 );
144 expect(noop.monitorId).toEqual(initial.monitorId);
145
146 yield* stack.destroy();
147
148 yield* expectGone(accountId, initial.monitorId);
149 }).pipe(logLevel),
150 {
151 tags: ["provider:cloudflare", "provider:cloudflare:loadbalancer", "live"],
152 timeout: 120_000,
153 },
154 );
155
156 // Ungated: the account-scoped listMonitors enumeration works regardless of
157 // the Load Balancing subscription (it just returns an empty array on an
158 // unentitled account), so this proves the list() op end-to-end live.
159 test.provider(
160 "list returns an array of monitor attributes",
161 (stack) =>
162 Effect.gen(function* () {
163 yield* stack.destroy();
164
165 const provider = yield* Provider.findProvider(Cloudflare.LoadBalancer.Monitor);
166 const all = yield* provider.list();
167 expect(Array.isArray(all)).toBe(true);
168 for (const monitor of all) {
169 expect(typeof monitor.monitorId).toBe("string");
170 expect(typeof monitor.accountId).toBe("string");
171 }
172
173 yield* stack.destroy();
174 }).pipe(logLevel),
175 {
176 tags: ["provider:cloudflare", "provider:cloudflare:loadbalancer", "live"],
177 timeout: 60_000,
178 },
179 );
180
181 // Full presence check — requires the LB subscription to deploy a real
182 // monitor. Skips on unentitled accounts (create fails with the typed
183 // MonitorIntervalOutOfRange plan-gate error). Set CLOUDFLARE_LB_ENABLED=1
184 // once the subscription is provisioned.
185 test.provider.skipIf(!lbEnabled)(
186 "list enumerates the deployed monitor",
187 (stack) =>
188 Effect.gen(function* () {
189 yield* stack.destroy();
190
191 const deployed = yield* stack.deploy(
192 Cloudflare.LoadBalancer.Monitor("ListMonitor", {
193 description: NAME_LIFECYCLE,
194 type: "https",
195 path: "/health",
196 expectedCodes: "2xx",
197 allowInsecure: true,
198 }),
199 );
200
201 const provider = yield* Provider.findProvider(Cloudflare.LoadBalancer.Monitor);
202 const all = yield* provider.list();
203
204 expect(all.some((m) => m.monitorId === deployed.monitorId)).toBe(true);
205
206 yield* stack.destroy();
207 }).pipe(logLevel),
208 {
209 tags: ["provider:cloudflare", "provider:cloudflare:loadbalancer", "live"],
210 timeout: 120_000,
211 },
212 );