@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 292 lines 10.4 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 rdc from "@distilled.cloud/cloudflare/r2-data-catalog";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import * as Redacted from "effect/Redacted";
9 import { MinimumLogLevel } from "effect/References";
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 interface CatalogOpts {
17 compaction?: Cloudflare.R2.Compaction;
18 snapshotExpiration?: Cloudflare.R2.SnapshotExpiration;
19 token?: Redacted.Redacted<string>;
20 }
21
22 // One program deploying both the R2 bucket and the catalog enabled on it.
23 // `bucket` references the bucket resource, so the engine orders
24 // catalog-after-bucket on deploy (and the reverse on destroy).
25 const program = (opts: CatalogOpts = {}) =>
26 Effect.gen(function* () {
27 const bucket = yield* Cloudflare.R2.Bucket("CatalogBucket", {
28 forceDestroy: true,
29 });
30 const catalog = yield* Cloudflare.R2.DataCatalog("Catalog", {
31 bucket,
32 ...opts,
33 });
34 return { bucket, catalog };
35 });
36
37 // The scoped API token the test harness mints propagates eventually-
38 // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`,
39 // declared in the distilled error union) on out-of-band verification calls.
40 const getCatalog = (accountId: string, bucketName: string) =>
41 rdc.getR2DataCatalog({ accountId, bucketName }).pipe(
42 Effect.retry({
43 while: (e) => e._tag === "Forbidden",
44 schedule: Schedule.exponential("500 millis"),
45 times: 8,
46 }),
47 );
48
49 // After destroy the bucket is deleted along with the catalog, so a missing
50 // warehouse surfaces as `WarehouseNotFound` (code 40401) — the success
51 // condition here. An `inactive` status (bucket lingering) also counts as
52 // disabled.
53 const expectGone = (accountId: string, bucketName: string) =>
54 getCatalog(accountId, bucketName).pipe(
55 Effect.flatMap((catalog) =>
56 catalog.status === "active"
57 ? Effect.fail({ _tag: "CatalogStillActive" } as const)
58 : Effect.void,
59 ),
60 Effect.catchTag("WarehouseNotFound", () => Effect.void),
61 Effect.retry({
62 while: (e) => e._tag === "CatalogStillActive",
63 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
64 }),
65 );
66
67 test.provider(
68 "register credential before initial maintenance config, update, destroy",
69 (stack) =>
70 Effect.gen(function* () {
71 const env = yield* yield* CloudflareEnvironment;
72 const { accountId } = env;
73
74 yield* stack.destroy();
75
76 // Create — when API-token credentials are available, reproduce the
77 // greenfield case where the catalog needs both a credential and a
78 // non-default maintenance config in the same reconciliation.
79 const initial = yield* stack.deploy(
80 program(
81 env.type === "apiToken"
82 ? {
83 compaction: { state: "enabled", targetSizeMb: "256" },
84 token: env.apiToken,
85 }
86 : {},
87 ),
88 );
89
90 expect(initial.catalog.catalogId).toBeTruthy();
91 expect(initial.catalog.bucketName).toEqual(initial.bucket.bucketName);
92 expect(initial.catalog.accountId).toEqual(accountId);
93 expect(initial.catalog.status).toEqual("active");
94 expect(initial.catalog.name).toEqual(`${accountId}_${initial.bucket.bucketName}`);
95 expect(initial.catalog.catalogUri).toEqual(
96 `https://catalog.cloudflarestorage.com/${accountId}/${initial.bucket.bucketName}`,
97 );
98 expect(initial.catalog.credentialStatus).toEqual(
99 env.type === "apiToken" ? "present" : "absent",
100 );
101
102 const live = yield* getCatalog(accountId, initial.bucket.bucketName);
103 expect(live.id).toEqual(initial.catalog.catalogId);
104 expect(live.status).toEqual("active");
105
106 if (env.type === "apiToken") {
107 expect(initial.catalog.compaction).toEqual({
108 state: "enabled",
109 targetSizeMb: "256",
110 });
111 expect(live.credentialStatus).toEqual("present");
112 expect(live.maintenanceConfig?.compaction).toEqual({
113 state: "enabled",
114 targetSizeMb: "256",
115 });
116
117 // Update — sync maintenance config in place (same catalog id).
118 const updated = yield* stack.deploy(
119 program({
120 compaction: { state: "disabled", targetSizeMb: "256" },
121 snapshotExpiration: {
122 state: "disabled",
123 maxSnapshotAge: "3d",
124 minSnapshotsToKeep: 5,
125 },
126 token: env.apiToken,
127 }),
128 );
129
130 expect(updated.catalog.catalogId).toEqual(initial.catalog.catalogId);
131 expect(updated.catalog.compaction).toEqual({
132 state: "disabled",
133 targetSizeMb: "256",
134 });
135 expect(updated.catalog.snapshotExpiration).toEqual({
136 state: "disabled",
137 maxSnapshotAge: "3d",
138 minSnapshotsToKeep: 5,
139 });
140
141 const liveUpdated = yield* getCatalog(accountId, initial.bucket.bucketName);
142 expect(liveUpdated.credentialStatus).toEqual("present");
143 expect(liveUpdated.maintenanceConfig?.compaction).toEqual({
144 state: "disabled",
145 targetSizeMb: "256",
146 });
147 expect(liveUpdated.maintenanceConfig?.snapshotExpiration).toEqual({
148 state: "disabled",
149 maxSnapshotAge: "3d",
150 minSnapshotsToKeep: 5,
151 });
152 }
153
154 yield* stack.destroy();
155
156 yield* expectGone(accountId, initial.bucket.bucketName);
157 }).pipe(logLevel),
158 {
159 tags: ["provider:cloudflare", "provider:cloudflare:r2", "live"],
160 timeout: 240_000,
161 },
162 );
163
164 test.provider(
165 "switching between bucket forms keeps the same catalog",
166 (stack) =>
167 Effect.gen(function* () {
168 const { accountId } = yield* yield* CloudflareEnvironment;
169
170 yield* stack.destroy();
171
172 // The deprecated `bucketName` string form.
173 const legacy = yield* stack.deploy(
174 Effect.gen(function* () {
175 const bucket = yield* Cloudflare.R2.Bucket("CatalogBucket", { forceDestroy: true });
176 const catalog = yield* Cloudflare.R2.DataCatalog("Catalog", {
177 bucketName: bucket.bucketName,
178 });
179 return { bucket, catalog };
180 }),
181 );
182
183 // The bucket resource, then a plain bucket name: neither is a change.
184 const viaResource = yield* stack.deploy(program());
185 expect(viaResource.catalog.catalogId).toEqual(legacy.catalog.catalogId);
186
187 const viaName = yield* stack.deploy(
188 Effect.gen(function* () {
189 const bucket = yield* Cloudflare.R2.Bucket("CatalogBucket", { forceDestroy: true });
190 const catalog = yield* Cloudflare.R2.DataCatalog("Catalog", {
191 bucket: legacy.bucket.bucketName,
192 });
193 return { bucket, catalog };
194 }),
195 );
196 expect(viaName.catalog.catalogId).toEqual(legacy.catalog.catalogId);
197 expect(viaName.catalog.bucketName).toEqual(legacy.bucket.bucketName);
198
199 yield* stack.destroy();
200
201 yield* expectGone(accountId, legacy.bucket.bucketName);
202 }).pipe(logLevel),
203 {
204 tags: ["provider:cloudflare", "provider:cloudflare:r2", "live"],
205 timeout: 240_000,
206 },
207 );
208
209 test.provider(
210 "list enumerates the deployed catalog",
211 (stack) =>
212 Effect.gen(function* () {
213 const { accountId } = yield* yield* CloudflareEnvironment;
214
215 yield* stack.destroy();
216
217 const deployed = yield* stack.deploy(program());
218 expect(deployed.catalog.status).toEqual("active");
219
220 const provider = yield* Provider.findProvider(Cloudflare.R2.DataCatalog);
221 const all = yield* provider.list();
222
223 // The freshly-enabled catalog is present in the account-wide listing,
224 // hydrated into the exact `read` Attributes shape.
225 const found = all.find((c) => c.catalogId === deployed.catalog.catalogId);
226 expect(found).toBeDefined();
227 expect(found?.bucketName).toEqual(deployed.bucket.bucketName);
228 expect(found?.accountId).toEqual(deployed.catalog.accountId);
229 expect(found?.name).toEqual(deployed.catalog.name);
230 expect(found?.catalogUri).toEqual(deployed.catalog.catalogUri);
231 expect(found?.status).toEqual("active");
232 // Every entry is an active warehouse mapped to full Attributes.
233 expect(all.every((c) => c.status === "active")).toBe(true);
234
235 yield* stack.destroy();
236
237 yield* expectGone(accountId, deployed.bucket.bucketName);
238 }).pipe(logLevel),
239 {
240 tags: ["provider:cloudflare", "provider:cloudflare:r2", "live"],
241 timeout: 240_000,
242 },
243 );
244
245 test.provider(
246 "re-enables after out-of-band disable",
247 (stack) =>
248 Effect.gen(function* () {
249 const { accountId } = yield* yield* CloudflareEnvironment;
250
251 yield* stack.destroy();
252
253 const initial = yield* stack.deploy(program({ compaction: { targetSizeMb: "128" } }));
254 expect(initial.catalog.status).toEqual("active");
255
256 // Disable the catalog out-of-band. A redeploy with identical props is a
257 // planner no-op, so change a prop to force reconcile — it must observe
258 // the catalog as inactive and re-enable it instead of failing.
259 yield* rdc
260 .disableR2DataCatalog({
261 accountId,
262 bucketName: initial.bucket.bucketName,
263 })
264 .pipe(
265 Effect.retry({
266 while: (e) => e._tag === "Forbidden",
267 schedule: Schedule.exponential("500 millis"),
268 times: 8,
269 }),
270 );
271
272 const healed = yield* stack.deploy(program({ compaction: { targetSizeMb: "64" } }));
273
274 // The warehouse id is stable across disable/enable cycles — this is a
275 // re-enable of the same catalog, not a replacement.
276 expect(healed.catalog.catalogId).toEqual(initial.catalog.catalogId);
277 expect(healed.catalog.status).toEqual("active");
278 expect(healed.catalog.compaction?.targetSizeMb).toEqual("64");
279
280 const live = yield* getCatalog(accountId, initial.bucket.bucketName);
281 expect(live.status).toEqual("active");
282 expect(live.maintenanceConfig?.compaction?.targetSizeMb).toEqual("64");
283
284 yield* stack.destroy();
285
286 yield* expectGone(accountId, initial.bucket.bucketName);
287 }).pipe(logLevel),
288 {
289 tags: ["provider:cloudflare", "provider:cloudflare:r2", "live"],
290 timeout: 240_000,
291 },
292 );