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