The best way to control Claude Code, Codex and more from any computer or phone

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