1import * as Cloudflare from "@/Cloudflare";
2import { CloudflareEnvironment } from "@/Cloudflare/CloudflareEnvironment";
3import { findZoneByName } from "@/Cloudflare/Zone/lookup";
4import * as Provider from "@/Provider";
5import * as Test from "@/Test/Alchemy";
6import * as zones from "@distilled.cloud/cloudflare/zones";
7import { expect } from "alchemy-test";
8import * as Effect from "effect/Effect";
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
19const zoneName =
20 process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
21
22// Zone holds are Enterprise-only. On the testing account's zone every
23// createHold/patchHold fails with Cloudflare code 1005 ("Zone holds are only
24// available on Enterprise zones (1005)"), surfaced as the typed
25// `ZoneHoldsRequireEnterprise` error. The full lifecycle test below is gated
26// behind an Enterprise zone id supplied via env.
27const enterpriseZoneId = process.env.CLOUDFLARE_TEST_ENTERPRISE_ZONE_ID;
28
29const resolveZoneId = Effect.gen(function* () {
30 const { accountId } = yield* yield* CloudflareEnvironment;
31 const zone = yield* findZoneByName({ accountId, name: zoneName });
32 if (!zone) {
33 return yield* Effect.die(
34 new Error(`zone "${zoneName}" not found in account`),
35 );
36 }
37 return zone.id;
38});
39
40// The scoped API token the test harness mints propagates eventually-
41// consistently across Cloudflare's edge — ride out 403 blips on the test's
42// own out-of-band calls by retrying the typed `Forbidden` error.
43const forbiddenRetrySchedule = Schedule.exponential("500 millis");
44
45const getHold = (zoneId: string) =>
46 zones.getHold({ zoneId }).pipe(
47 Effect.retry({
48 while: (e) => e._tag === "Forbidden",
49 schedule: forbiddenRetrySchedule,
50 times: 8,
51 }),
52 );
53
54// Normalize to a no-hold baseline so each run starts from the same cloud
55// state regardless of what a previous (possibly interrupted) run left behind.
56// deleteHold is naturally idempotent — it succeeds even when no hold exists.
57const removeHoldBaseline = (zoneId: string) =>
58 zones.deleteHold({ zoneId }).pipe(
59 Effect.retry({
60 while: (e) => e._tag === "Forbidden",
61 schedule: forbiddenRetrySchedule,
62 times: 8,
63 }),
64 );
65
66const isHeld = (hold: zones.GetHoldResponse): boolean => hold.hold === true;
67
68const includesSubdomains = (hold: zones.GetHoldResponse): boolean =>
69 hold.includeSubdomains === true || hold.includeSubdomains === "true";
70
71test.provider(
72 "surfaces the typed ZoneHoldsRequireEnterprise error on non-Enterprise zones",
73 (stack) =>
74 Effect.gen(function* () {
75 const zoneId = yield* resolveZoneId;
76
77 yield* stack.destroy();
78
79 // getHold works on every plan and reports the zone as not held.
80 const observed = yield* getHold(zoneId);
81 expect(isHeld(observed)).toBe(false);
82
83 // deleteHold is idempotent — removing a non-existent hold succeeds.
84 yield* removeHoldBaseline(zoneId);
85
86 // Placing a hold on a non-Enterprise zone fails with the typed
87 // entitlement tag.
88 const error = yield* zones.createHold({ zoneId }).pipe(
89 Effect.retry({
90 while: (e) => e._tag === "Forbidden",
91 schedule: forbiddenRetrySchedule,
92 times: 8,
93 }),
94 Effect.flip,
95 );
96 expect(error._tag).toEqual("ZoneHoldsRequireEnterprise");
97
98 yield* stack.destroy();
99 }).pipe(logLevel),
100 { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] },
101);
102
103// Canonical `list()` test (zone-scoped singleton): a hold is a per-zone
104// singleton — `getHold` returns a record for every zone — and there is no
105// account-wide enumeration API, so `list()` enumerates every zone via
106// `listAllZones` and reads the hold state in each. Assert the result is
107// non-empty and contains the standing test zone. This works on any plan
108// (reading the hold state needs no Enterprise entitlement).
109test.provider(
110 "list enumerates the hold state across all zones",
111 (stack) =>
112 Effect.gen(function* () {
113 const zoneId = yield* resolveZoneId;
114
115 yield* stack.destroy();
116
117 const provider = yield* Provider.findProvider(Cloudflare.Zone.Hold);
118 const all = yield* provider.list();
119
120 expect(all.length).toBeGreaterThan(0);
121 expect(all.some((h) => h.zoneId === zoneId)).toBe(true);
122
123 yield* stack.destroy();
124 }).pipe(logLevel),
125 { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] },
126);
127
128test.provider.skipIf(!enterpriseZoneId)(
129 "places a hold, updates includeSubdomains in place, and removes it on destroy",
130 (stack) =>
131 Effect.gen(function* () {
132 const zoneId = enterpriseZoneId!;
133
134 yield* stack.destroy();
135 // Known baseline: no hold on the zone.
136 yield* removeHoldBaseline(zoneId);
137
138 const hold = yield* stack.deploy(
139 Effect.gen(function* () {
140 return yield* Cloudflare.Zone.Hold("Hold", {
141 zoneId,
142 });
143 }),
144 );
145
146 expect(hold.zoneId).toEqual(zoneId);
147 expect(hold.hold).toBe(true);
148 expect(hold.includeSubdomains).toBe(false);
149
150 // Out-of-band verification via the distilled API.
151 const live = yield* getHold(zoneId);
152 expect(isHeld(live)).toBe(true);
153 expect(includesSubdomains(live)).toBe(false);
154
155 // Update in place — extend the hold to subdomains via patchHold.
156 const updated = yield* stack.deploy(
157 Effect.gen(function* () {
158 return yield* Cloudflare.Zone.Hold("Hold", {
159 zoneId,
160 includeSubdomains: true,
161 });
162 }),
163 );
164
165 expect(updated.hold).toBe(true);
166 expect(updated.includeSubdomains).toBe(true);
167
168 const liveUpdated = yield* getHold(zoneId);
169 expect(isHeld(liveUpdated)).toBe(true);
170 expect(includesSubdomains(liveUpdated)).toBe(true);
171
172 yield* stack.destroy();
173
174 // Destroy removed the hold.
175 const removed = yield* getHold(zoneId);
176 expect(isHeld(removed)).toBe(false);
177 }).pipe(logLevel),
178 { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] },
179);