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

main
ts 170 lines 6.37 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 customNameservers from "@distilled.cloud/cloudflare/custom-nameservers";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10 import { testName } from "../../names.ts";
11
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
17
18 // Account custom nameservers require a Business/Enterprise plan (or paid
19 // add-on). On the testing account every call — even the list — fails with
20 // Cloudflare code 1002 "This feature is not enabled for your account.",
21 // surfaced as the typed `CustomNameserversNotEnabled` error. The full
22 // lifecycle test below is gated behind an env flag for entitled accounts.
23 const entitled = !!process.env.CLOUDFLARE_TEST_CUSTOM_NS_ENTITLED;
24
25 // The scoped API token the test harness mints propagates eventually-
26 // consistently across Cloudflare's edge — ride out generic 403 blips
27 // (`Forbidden`) on out-of-band verification calls. The entitlement
28 // failure is a *different* typed tag (`CustomNameserversNotEnabled`),
29 // so this retry never loops on it.
30 const listNameservers = (accountId: string) =>
31 customNameservers.getCustomNameserver({ accountId }).pipe(
32 Effect.retry({
33 while: (e) => e._tag === "Forbidden",
34 schedule: Schedule.exponential("500 millis"),
35 times: 8,
36 }),
37 );
38
39 const findByName = (accountId: string, nsName: string) =>
40 listNameservers(accountId).pipe(
41 Effect.map((page) => page.result.find((ns) => ns.nsName === nsName)),
42 );
43
44 const expectGone = (accountId: string, nsName: string) =>
45 findByName(accountId, nsName).pipe(
46 Effect.flatMap((found) =>
47 found ? Effect.fail({ _tag: "CustomNameserverNotDeleted" } as const) : Effect.void,
48 ),
49 Effect.retry({
50 while: (e) => e._tag === "CustomNameserverNotDeleted",
51 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
52 }),
53 );
54
55 test.provider(
56 "surfaces the typed CustomNameserversNotEnabled error on unentitled accounts",
57 (stack) =>
58 Effect.gen(function* () {
59 const { accountId } = yield* yield* CloudflareEnvironment;
60
61 yield* stack.destroy();
62
63 // The testing account lacks the account custom nameservers
64 // entitlement — the distilled list call must fail with the typed
65 // entitlement tag (not the generic 403 catch-all).
66 const error = yield* listNameservers(accountId).pipe(Effect.flip);
67 expect(error._tag).toEqual("CustomNameserversNotEnabled");
68
69 yield* stack.destroy();
70 }).pipe(logLevel),
71 { tags: ["provider:cloudflare", "provider:cloudflare:customnameserver", "live"] },
72 );
73
74 // Canonical `list()` test (account collection). On the unentitled testing
75 // account the collection endpoint rejects with `CustomNameserversNotEnabled`,
76 // which `list()` maps to an empty collection — so the read-only assertion is
77 // "list returns a well-typed array" (here, `[]`). An entitled account also
78 // runs the deploy+presence variant below.
79 test.provider(
80 "list enumerates account custom nameservers",
81 (stack) =>
82 Effect.gen(function* () {
83 yield* stack.destroy();
84
85 const provider = yield* Provider.findProvider(Cloudflare.CustomNameserver.CustomNameserver);
86 const all = yield* provider.list();
87
88 // Always a well-typed array; `[]` on unentitled accounts.
89 expect(Array.isArray(all)).toBe(true);
90 if (!entitled) {
91 expect(all).toEqual([]);
92 }
93
94 yield* stack.destroy();
95 }).pipe(logLevel),
96 { tags: ["provider:cloudflare", "provider:cloudflare:customnameserver", "live"] },
97 );
98
99 test.provider.skipIf(!entitled)(
100 "list includes a deployed custom nameserver",
101 (stack) =>
102 Effect.gen(function* () {
103 const nsName = `${testName("alchemy-ns-list")}.${zoneName}`;
104
105 yield* stack.destroy();
106
107 const ns = yield* stack.deploy(
108 Cloudflare.CustomNameserver.CustomNameserver("NsList", { nsName }),
109 );
110
111 const provider = yield* Provider.findProvider(Cloudflare.CustomNameserver.CustomNameserver);
112 const all = yield* provider.list();
113 expect(all.some((x) => x.nsName === ns.nsName)).toBe(true);
114
115 yield* stack.destroy();
116 }).pipe(logLevel),
117 {
118 tags: ["provider:cloudflare", "provider:cloudflare:customnameserver", "live"],
119 timeout: 120_000,
120 },
121 );
122
123 test.provider.skipIf(!entitled)(
124 "create, replace on nsSet change, and destroy a custom nameserver",
125 (stack) =>
126 Effect.gen(function* () {
127 const { accountId } = yield* yield* CloudflareEnvironment;
128 const nsName = `${testName("alchemy-ns1")}.${zoneName}`;
129
130 yield* stack.destroy();
131
132 // Create on the default set.
133 const ns = yield* stack.deploy(
134 Cloudflare.CustomNameserver.CustomNameserver("Ns", { nsName }),
135 );
136 expect(ns.nsName).toEqual(nsName);
137 expect(ns.accountId).toEqual(accountId);
138 expect(ns.zoneTag).toBeTruthy();
139 expect(ns.dnsRecords).toBeDefined();
140
141 // Out-of-band verification via the distilled API.
142 const live = yield* findByName(accountId, nsName);
143 expect(live?.nsName).toEqual(nsName);
144
145 // Redeploying identical props is a no-op (reconcile observes the
146 // existing nameserver and converges without an API write).
147 const noop = yield* stack.deploy(
148 Cloudflare.CustomNameserver.CustomNameserver("Ns", { nsName }),
149 );
150 expect(noop.nsName).toEqual(nsName);
151 expect(noop.zoneTag).toEqual(ns.zoneTag);
152
153 // nsSet is immutable — changing it must replace the nameserver.
154 const replaced = yield* stack.deploy(
155 Cloudflare.CustomNameserver.CustomNameserver("Ns", { nsName, nsSet: 2 }),
156 );
157 expect(replaced.nsName).toEqual(nsName);
158 expect(replaced.nsSet).toEqual(2);
159
160 yield* stack.destroy();
161
162 // The nameserver is gone; a second destroy is a no-op.
163 yield* expectGone(accountId, nsName);
164 yield* stack.destroy();
165 }).pipe(logLevel),
166 {
167 tags: ["provider:cloudflare", "provider:cloudflare:customnameserver", "live"],
168 timeout: 120_000,
169 },
170 );