@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 193 lines 6.99 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 addressing from "@distilled.cloud/cloudflare/addressing";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Result from "effect/Result";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 // A freshly minted scoped token propagates eventually-consistently across
17 // Cloudflare's edge — retry the typed `Forbidden` blips on out-of-band calls.
18 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
19
20 const resolveAccountId = Effect.gen(function* () {
21 const { accountId } = yield* yield* CloudflareEnvironment;
22 return accountId;
23 });
24
25 const getMap = (accountId: string, addressMapId: string) =>
26 addressing.getAddressMap({ accountId, addressMapId }).pipe(
27 Effect.retry({
28 while: (e) => e._tag === "Forbidden",
29 schedule: forbiddenRetrySchedule,
30 times: 8,
31 }),
32 Effect.catchTag("AddressMapNotFound", () => Effect.succeed(undefined)),
33 );
34
35 // Address Maps require the BYOIP add-on or Cloudflare-assigned static IPs
36 // (Enterprise). On the standard testing account every mutating call fails
37 // with the typed `FeatureNotEnabled` error
38 // (`address_maps_not_enabled_on_account`). The test probes once: unentitled
39 // accounts assert the typed tag; entitled accounts run the full lifecycle.
40 test.provider(
41 "address map lifecycle (typed FeatureNotEnabled on unentitled accounts)",
42 (stack) =>
43 Effect.gen(function* () {
44 const accountId = yield* resolveAccountId;
45
46 yield* stack.destroy();
47
48 const probe = yield* addressing
49 .createAddressMap({
50 accountId,
51 description: "alchemy-addressmap-probe",
52 enabled: false,
53 })
54 .pipe(
55 Effect.retry({
56 while: (e) => e._tag === "Forbidden",
57 schedule: forbiddenRetrySchedule,
58 times: 8,
59 }),
60 Effect.result,
61 );
62
63 if (Result.isFailure(probe)) {
64 // Unentitled — the distilled call must fail with the typed
65 // entitlement tag, never an untyped catch-all.
66 expect(probe.failure._tag).toEqual("FeatureNotEnabled");
67 yield* stack.destroy();
68 return;
69 }
70
71 // Entitled — clean up the probe map and run the full lifecycle.
72 if (probe.success.id) {
73 yield* addressing
74 .deleteAddressMap({ accountId, addressMapId: probe.success.id })
75 .pipe(Effect.catchTag("AddressMapNotFound", () => Effect.void));
76 }
77
78 // Create a disabled map with a description.
79 const created = yield* stack.deploy(
80 Effect.gen(function* () {
81 return yield* Cloudflare.Addressing.AddressMap("Map", {
82 description: "alchemy-addressmap v1",
83 enabled: false,
84 });
85 }),
86 );
87 expect(created.addressMapId).toBeDefined();
88 expect(created.accountId).toEqual(accountId);
89 expect(created.description).toEqual("alchemy-addressmap v1");
90 expect(created.enabled).toEqual(false);
91
92 // Out-of-band verification via the distilled API.
93 const live = yield* getMap(accountId, created.addressMapId);
94 expect(live?.description).toEqual("alchemy-addressmap v1");
95
96 // Update the description in place — same physical map.
97 const updated = yield* stack.deploy(
98 Effect.gen(function* () {
99 return yield* Cloudflare.Addressing.AddressMap("Map", {
100 description: "alchemy-addressmap v2",
101 enabled: false,
102 });
103 }),
104 );
105 expect(updated.addressMapId).toEqual(created.addressMapId);
106 expect(updated.description).toEqual("alchemy-addressmap v2");
107
108 const patched = yield* getMap(accountId, updated.addressMapId);
109 expect(patched?.description).toEqual("alchemy-addressmap v2");
110
111 // Destroy and verify the map is gone (typed not-found read).
112 yield* stack.destroy();
113 const gone = yield* getMap(accountId, created.addressMapId);
114 expect(gone).toBeUndefined();
115 }).pipe(logLevel),
116 {
117 tags: ["provider:cloudflare", "provider:cloudflare:addressing", "live"],
118 timeout: 120_000,
119 },
120 );
121
122 // `list()` enumerates every Address Map on the account (paginated) and
123 // hydrates each into the full `read` shape. Requires the same BYOIP /
124 // static-IP entitlement as the lifecycle: unentitled accounts can't create
125 // a map to observe, so we probe once and assert the typed `FeatureNotEnabled`
126 // tag (clean skip); entitled accounts run the full deploy + list assertion.
127 test.provider(
128 "list enumerates the deployed address map",
129 (stack) =>
130 Effect.gen(function* () {
131 const accountId = yield* resolveAccountId;
132
133 yield* stack.destroy();
134
135 const probe = yield* addressing
136 .createAddressMap({
137 accountId,
138 description: "alchemy-addressmap-list-probe",
139 enabled: false,
140 })
141 .pipe(
142 Effect.retry({
143 while: (e) => e._tag === "Forbidden",
144 schedule: forbiddenRetrySchedule,
145 times: 8,
146 }),
147 Effect.result,
148 );
149
150 if (Result.isFailure(probe)) {
151 // Unentitled — the distilled call must fail with the typed
152 // entitlement tag, never an untyped catch-all.
153 expect(probe.failure._tag).toEqual("FeatureNotEnabled");
154 yield* stack.destroy();
155 return;
156 }
157
158 // Entitled — clean up the probe map and run the full lifecycle.
159 if (probe.success.id) {
160 yield* addressing
161 .deleteAddressMap({ accountId, addressMapId: probe.success.id })
162 .pipe(Effect.catchTag("AddressMapNotFound", () => Effect.void));
163 }
164
165 const deployed = yield* stack.deploy(
166 Effect.gen(function* () {
167 return yield* Cloudflare.Addressing.AddressMap("ListMap", {
168 description: "alchemy-addressmap-list",
169 enabled: false,
170 });
171 }),
172 );
173 expect(deployed.addressMapId).toBeDefined();
174
175 const provider = yield* Provider.findProvider(Cloudflare.Addressing.AddressMap);
176 const all = yield* provider.list();
177
178 // The deployed map appears in the exhaustively-paginated result, fully
179 // hydrated into the `read` shape (ips/memberships present).
180 const found = all.find((m) => m.addressMapId === deployed.addressMapId);
181 expect(found).toBeDefined();
182 expect(found?.accountId).toEqual(accountId);
183 expect(found?.description).toEqual("alchemy-addressmap-list");
184 expect(found?.ips).toBeDefined();
185 expect(found?.memberships).toBeDefined();
186
187 yield* stack.destroy();
188 }).pipe(logLevel),
189 {
190 tags: ["provider:cloudflare", "provider:cloudflare:addressing", "live"],
191 timeout: 120_000,
192 },
193 );