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 dns from "@distilled.cloud/cloudflare/dns";
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// Internal DNS views require the Enterprise Internal DNS entitlement.
23// On the testing account, `POST /accounts/{id}/dns_settings/views` fails
24// with code 1029 — "Internal DNS is not available to this account.
25// Contact support for more information." — surfaced as the typed
26// `InternalDnsNotAvailable` error. The full lifecycle test below is
27// gated behind an entitled account supplied via env.
28const internalDnsEntitled = !!process.env.CLOUDFLARE_TEST_INTERNAL_DNS;
29
30const resolveZoneId = Effect.gen(function* () {
31 const { accountId } = yield* yield* CloudflareEnvironment;
32 const zone = yield* findZoneByName({ accountId, name: zoneName });
33 if (!zone) {
34 return yield* Effect.die(
35 new Error(`zone "${zoneName}" not found in account`),
36 );
37 }
38 return zone.id;
39});
40
41const retryForbidden = <A, E extends { _tag: string }, R>(
42 eff: Effect.Effect<A, E, R>,
43) =>
44 eff.pipe(
45 Effect.retry({
46 while: (e) => e._tag === "Forbidden",
47 schedule: Schedule.exponential("500 millis"),
48 times: 8,
49 }),
50 );
51
52test.provider.skipIf(internalDnsEntitled)(
53 "surfaces the typed InternalDnsNotAvailable error on unentitled accounts",
54 (stack) =>
55 Effect.gen(function* () {
56 const { accountId } = yield* yield* CloudflareEnvironment;
57 const zoneId = yield* resolveZoneId;
58
59 yield* stack.destroy();
60
61 // The testing account lacks the Internal DNS entitlement — the
62 // distilled call must fail with the typed tag (code 1029).
63 const error = yield* retryForbidden(
64 dns.createSettingAccountView({
65 accountId,
66 name: "alchemy-dns-view-test",
67 zones: [zoneId],
68 }),
69 ).pipe(Effect.flip);
70 expect(error._tag).toEqual("InternalDnsNotAvailable");
71 }).pipe(logLevel),
72 {
73 tags: [
74 "provider:cloudflare",
75 "provider:cloudflare:dns",
76 "provider:cloudflare:zone",
77 "live",
78 ],
79 timeout: 120_000,
80 },
81);
82
83// The list endpoint (GET) is enumerable on any account; on unentitled
84// accounts it simply returns no views. We can therefore always assert a
85// well-typed array, and only assert presence of a deployed view when the
86// account carries the Internal DNS entitlement.
87test.provider(
88 "list returns a well-typed array of internal DNS views",
89 (stack) =>
90 Effect.gen(function* () {
91 yield* stack.destroy();
92
93 const provider = yield* Provider.findProvider(Cloudflare.DNS.View);
94 const all = yield* provider.list();
95 expect(Array.isArray(all)).toBe(true);
96
97 yield* stack.destroy();
98 }).pipe(logLevel),
99 {
100 tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"],
101 timeout: 120_000,
102 },
103);
104
105test.provider.skipIf(!internalDnsEntitled)(
106 "list enumerates a deployed internal DNS view",
107 (stack) =>
108 Effect.gen(function* () {
109 const zoneId = yield* resolveZoneId;
110
111 yield* stack.destroy();
112
113 const deployed = yield* stack.deploy(
114 Cloudflare.DNS.View("ListView", {
115 name: "alchemy-dns-view-list",
116 zones: [zoneId],
117 }),
118 );
119
120 const provider = yield* Provider.findProvider(Cloudflare.DNS.View);
121 const all = yield* provider.list();
122 expect(all.some((v) => v.viewId === deployed.viewId)).toBe(true);
123
124 yield* stack.destroy();
125 }).pipe(logLevel),
126 {
127 tags: [
128 "provider:cloudflare",
129 "provider:cloudflare:dns",
130 "provider:cloudflare:zone",
131 "live",
132 ],
133 timeout: 120_000,
134 },
135);
136
137test.provider.skipIf(!internalDnsEntitled)(
138 "create, update zones in place, and delete an internal DNS view",
139 (stack) =>
140 Effect.gen(function* () {
141 const { accountId } = yield* yield* CloudflareEnvironment;
142 const zoneId = yield* resolveZoneId;
143
144 yield* stack.destroy();
145
146 const created = yield* stack.deploy(
147 Cloudflare.DNS.View("TestView", {
148 name: "alchemy-dns-view-test",
149 zones: [zoneId],
150 }),
151 );
152 expect(created.viewId).toBeDefined();
153 expect(created.name).toEqual("alchemy-dns-view-test");
154 expect(created.zones).toEqual([zoneId]);
155
156 // Out-of-band verify via the SDK.
157 const live = yield* retryForbidden(
158 dns.getSettingAccountView({ accountId, viewId: created.viewId }),
159 );
160 expect(live.name).toEqual("alchemy-dns-view-test");
161
162 // Rename in place — same physical view.
163 const renamed = yield* stack.deploy(
164 Cloudflare.DNS.View("TestView", {
165 name: "alchemy-dns-view-test-renamed",
166 zones: [zoneId],
167 }),
168 );
169 expect(renamed.viewId).toEqual(created.viewId);
170 expect(renamed.name).toEqual("alchemy-dns-view-test-renamed");
171
172 yield* stack.destroy();
173
174 // A deleted view surfaces as the typed `ViewNotFound` (code 1015).
175 const gone = yield* retryForbidden(
176 dns.getSettingAccountView({ accountId, viewId: created.viewId }),
177 ).pipe(Effect.flip);
178 expect(gone._tag).toEqual("ViewNotFound");
179
180 // Re-running destroy is idempotent.
181 yield* stack.destroy();
182 }).pipe(logLevel),
183 {
184 tags: [
185 "provider:cloudflare",
186 "provider:cloudflare:dns",
187 "provider:cloudflare:zone",
188 "live",
189 ],
190 timeout: 120_000,
191 },
192);