@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 197 lines 7.71 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 // Monitor groups are Enterprise-only. On the testing account creation is
17 // rejected with "monitor groups not enabled; enterprise only" (Cloudflare
18 // code 1002), surfaced as the typed `MonitorGroupsNotEnabled` error. Set
19 // CLOUDFLARE_LB_MONITOR_GROUPS_ENABLED=1 on an Enterprise account (which
20 // also implies the Load Balancing subscription) to run the lifecycle test.
21 const monitorGroupsEnabled = !!process.env.CLOUDFLARE_LB_MONITOR_GROUPS_ENABLED;
22
23 // Deterministic per-test physical names — reused on every run.
24 const NAME_ENTITLEMENT = testName("alchemy-lb-monitorgroup-entitlement-probe");
25 const NAME_LIFECYCLE = testName("alchemy-lb-monitorgroup-lifecycle");
26 const NAME_MONITOR = testName("alchemy-lb-monitorgroup-lifecycle-monitor");
27
28 // Freshly minted scoped tokens propagate eventually-consistently across
29 // Cloudflare's edge — retry the typed `Forbidden` blips on out-of-band calls.
30 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
31
32 const getMonitorGroup = (accountId: string, monitorGroupId: string) =>
33 loadBalancers.getMonitorGroup({ accountId, monitorGroupId }).pipe(
34 Effect.retry({
35 while: (e) => e._tag === "Forbidden",
36 schedule: forbiddenRetrySchedule,
37 times: 8,
38 }),
39 );
40
41 const expectGone = (accountId: string, monitorGroupId: string) =>
42 getMonitorGroup(accountId, monitorGroupId).pipe(
43 Effect.flatMap(() => Effect.fail({ _tag: "MonitorGroupNotDeleted" } as const)),
44 // A missing group surfaces as the typed `MonitorGroupNotFound`
45 // (Cloudflare error code 1001) — that's the success condition here.
46 Effect.catchTag("MonitorGroupNotFound", () => Effect.void),
47 Effect.retry({
48 while: (e) => e._tag === "MonitorGroupNotDeleted",
49 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
50 }),
51 );
52
53 // Unentitlement probe: pins the typed plan-gate rejection, so it must skip
54 // on Enterprise accounts — there the create would succeed instead of failing.
55 test.provider.skipIf(monitorGroupsEnabled)(
56 "surfaces the typed MonitorGroupsNotEnabled error on non-Enterprise accounts",
57 (stack) =>
58 Effect.gen(function* () {
59 const { accountId } = yield* yield* CloudflareEnvironment;
60
61 yield* stack.destroy();
62
63 // The entitlement gate fires before member validation, so a
64 // placeholder monitor id is fine here.
65 const error = yield* loadBalancers
66 .createMonitorGroup({
67 accountId,
68 description: NAME_ENTITLEMENT,
69 members: [
70 {
71 monitorId: "17b5962d775c646f3f9725cbc7a53df4",
72 enabled: true,
73 monitoringOnly: false,
74 mustBeHealthy: true,
75 },
76 ],
77 })
78 .pipe(
79 Effect.retry({
80 while: (e) => e._tag === "Forbidden",
81 schedule: forbiddenRetrySchedule,
82 times: 8,
83 }),
84 Effect.flip,
85 );
86 expect(error._tag).toEqual("MonitorGroupsNotEnabled");
87
88 yield* stack.destroy();
89 }).pipe(logLevel),
90 { tags: ["provider:cloudflare", "provider:cloudflare:loadbalancer", "live"] },
91 );
92
93 // Ungated probe: monitor group enumeration is account-scoped and works
94 // regardless of the Enterprise entitlement — a non-entitled account simply
95 // has no groups, so `list()` returns an array (typically empty here).
96 test.provider(
97 "list returns an array of monitor groups",
98 () =>
99 Effect.gen(function* () {
100 const provider = yield* Provider.findProvider(Cloudflare.LoadBalancer.MonitorGroup);
101 const all = yield* provider.list();
102 expect(Array.isArray(all)).toBe(true);
103 }).pipe(logLevel),
104 { tags: ["provider:cloudflare", "provider:cloudflare:loadbalancer", "live"] },
105 );
106
107 test.provider.skipIf(!monitorGroupsEnabled)(
108 "list enumerates the deployed monitor group",
109 (stack) =>
110 Effect.gen(function* () {
111 yield* stack.destroy();
112
113 const deployed = yield* stack.deploy(
114 Effect.gen(function* () {
115 const monitor = yield* Cloudflare.LoadBalancer.Monitor("Monitor", {
116 description: NAME_MONITOR,
117 type: "https",
118 path: "/health",
119 expectedCodes: "2xx",
120 });
121 const group = yield* Cloudflare.LoadBalancer.MonitorGroup("Group", {
122 description: NAME_LIFECYCLE,
123 members: [{ monitorId: monitor.monitorId }],
124 });
125 return { monitor, group };
126 }),
127 );
128
129 const provider = yield* Provider.findProvider(Cloudflare.LoadBalancer.MonitorGroup);
130 const all = yield* provider.list();
131 expect(all.some((g) => g.monitorGroupId === deployed.group.monitorGroupId)).toBe(true);
132
133 yield* stack.destroy();
134 }).pipe(logLevel),
135 { tags: ["provider:cloudflare", "provider:cloudflare:loadbalancer", "live"], timeout: 120_000 },
136 );
137
138 test.provider.skipIf(!monitorGroupsEnabled)(
139 "create, update in place, and destroy a monitor group",
140 (stack) =>
141 Effect.gen(function* () {
142 const { accountId } = yield* yield* CloudflareEnvironment;
143
144 yield* stack.destroy();
145
146 const initial = yield* stack.deploy(
147 Effect.gen(function* () {
148 const monitor = yield* Cloudflare.LoadBalancer.Monitor("Monitor", {
149 description: NAME_MONITOR,
150 type: "https",
151 path: "/health",
152 expectedCodes: "2xx",
153 });
154 const group = yield* Cloudflare.LoadBalancer.MonitorGroup("Group", {
155 description: NAME_LIFECYCLE,
156 members: [{ monitorId: monitor.monitorId }],
157 });
158 return { monitor, group };
159 }),
160 );
161
162 expect(initial.group.monitorGroupId).toBeTruthy();
163 expect(initial.group.accountId).toEqual(accountId);
164 expect(initial.group.description).toEqual(NAME_LIFECYCLE);
165
166 const live = yield* getMonitorGroup(accountId, initial.group.monitorGroupId);
167 expect(live.description).toEqual(NAME_LIFECYCLE);
168 expect(live.members.map((m) => m.monitorId)).toEqual([initial.monitor.monitorId]);
169
170 // Member flags update in place — same monitorGroupId. Keep the
171 // monitor deployed across every step.
172 const updated = yield* stack.deploy(
173 Effect.gen(function* () {
174 const monitor = yield* Cloudflare.LoadBalancer.Monitor("Monitor", {
175 description: NAME_MONITOR,
176 type: "https",
177 path: "/health",
178 expectedCodes: "2xx",
179 });
180 const group = yield* Cloudflare.LoadBalancer.MonitorGroup("Group", {
181 description: NAME_LIFECYCLE,
182 members: [{ monitorId: monitor.monitorId, monitoringOnly: true }],
183 });
184 return { monitor, group };
185 }),
186 );
187 expect(updated.group.monitorGroupId).toEqual(initial.group.monitorGroupId);
188
189 const synced = yield* getMonitorGroup(accountId, updated.group.monitorGroupId);
190 expect(synced.members[0]?.monitoringOnly).toEqual(true);
191
192 yield* stack.destroy();
193
194 yield* expectGone(accountId, initial.group.monitorGroupId);
195 }).pipe(logLevel),
196 { tags: ["provider:cloudflare", "provider:cloudflare:loadbalancer", "live"], timeout: 120_000 },
197 );