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

main
ts 150 lines 5.22 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 dns from "@distilled.cloud/cloudflare/dns";
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 // Ride out fresh-token 403 blips on out-of-band calls.
17 const retryForbidden = <A, E extends { _tag: string }, R>(eff: Effect.Effect<A, E, R>) =>
18 eff.pipe(
19 Effect.retry({
20 while: (e) => e._tag === "Forbidden",
21 schedule: Schedule.exponential("500 millis"),
22 times: 8,
23 }),
24 );
25
26 const getAcl = (accountId: string, aclId: string) =>
27 retryForbidden(dns.getZoneTransferAcl({ accountId, aclId }));
28
29 // A deleted ACL surfaces as the typed `AclNotFound` (HTTP 404) — poll
30 // until the GET reports it gone.
31 const expectGone = (accountId: string, aclId: string) =>
32 getAcl(accountId, aclId).pipe(
33 Effect.flatMap(() => Effect.fail({ _tag: "AclNotDeleted" } as const)),
34 Effect.catchTag("AclNotFound", () => Effect.void),
35 Effect.retry({
36 while: (e) => e._tag === "AclNotDeleted",
37 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
38 }),
39 );
40
41 test.provider(
42 "create, update in place, and delete a zone transfer ACL",
43 (stack) =>
44 Effect.gen(function* () {
45 const { accountId } = yield* yield* CloudflareEnvironment;
46
47 yield* stack.destroy();
48
49 const created = yield* stack.deploy(
50 Cloudflare.DNS.ZoneTransferAcl("TestAcl", {
51 name: testName("alchemy-dnszt-acl-test"),
52 // Cloudflare normalizes the range to its network address —
53 // use one that is already normalized for stable diffs.
54 ipRange: "192.0.2.48/28",
55 }),
56 );
57 expect(created.aclId).toBeDefined();
58 expect(created.accountId).toEqual(accountId);
59 expect(created.name).toEqual(testName("alchemy-dnszt-acl-test"));
60 expect(created.ipRange).toEqual("192.0.2.48/28");
61
62 // Out-of-band verify via the SDK.
63 const live = yield* getAcl(accountId, created.aclId);
64 expect(live.name).toEqual(testName("alchemy-dnszt-acl-test"));
65 expect(live.ipRange).toEqual("192.0.2.48/28");
66
67 // Update both mutable fields in place — same physical ACL.
68 const updated = yield* stack.deploy(
69 Cloudflare.DNS.ZoneTransferAcl("TestAcl", {
70 name: testName("alchemy-dnszt-acl-test-renamed"),
71 ipRange: "198.51.100.0/28",
72 }),
73 );
74 expect(updated.aclId).toEqual(created.aclId);
75 expect(updated.name).toEqual(testName("alchemy-dnszt-acl-test-renamed"));
76 expect(updated.ipRange).toEqual("198.51.100.0/28");
77
78 const liveUpdated = yield* getAcl(accountId, created.aclId);
79 expect(liveUpdated.name).toEqual(testName("alchemy-dnszt-acl-test-renamed"));
80 expect(liveUpdated.ipRange).toEqual("198.51.100.0/28");
81
82 yield* stack.destroy();
83 yield* expectGone(accountId, created.aclId);
84
85 // Re-running destroy is idempotent.
86 yield* stack.destroy();
87 }).pipe(logLevel),
88 {
89 tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"],
90 timeout: 120_000,
91 },
92 );
93
94 test.provider(
95 "generates a deterministic name when none is provided",
96 (stack) =>
97 Effect.gen(function* () {
98 const { accountId } = yield* yield* CloudflareEnvironment;
99
100 yield* stack.destroy();
101
102 const created = yield* stack.deploy(
103 Cloudflare.DNS.ZoneTransferAcl("DefaultNameAcl", {
104 ipRange: "203.0.113.0/28",
105 }),
106 );
107 expect(created.name).toBeDefined();
108 expect(created.name.length).toBeGreaterThan(0);
109 expect(created.ipRange).toEqual("203.0.113.0/28");
110
111 yield* stack.destroy();
112 yield* expectGone(accountId, created.aclId);
113 }).pipe(logLevel),
114 {
115 tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"],
116 timeout: 120_000,
117 },
118 );
119
120 test.provider(
121 "list enumerates the deployed zone transfer ACLs",
122 (stack) =>
123 Effect.gen(function* () {
124 yield* stack.destroy();
125
126 const deployed = yield* stack.deploy(
127 Cloudflare.DNS.ZoneTransferAcl("ListAcl", {
128 name: testName("alchemy-dnszt-acl-list"),
129 ipRange: "192.0.2.64/28",
130 }),
131 );
132
133 const provider = yield* Provider.findProvider(Cloudflare.DNS.ZoneTransferAcl);
134 const all = yield* provider.list();
135
136 // Exhaustively-paginated result contains the deployed ACL, in the
137 // exact `read` Attributes shape.
138 const found = all.find((a) => a.aclId === deployed.aclId);
139 expect(found).toBeDefined();
140 expect(found?.accountId).toEqual(deployed.accountId);
141 expect(found?.name).toEqual(testName("alchemy-dnszt-acl-list"));
142 expect(found?.ipRange).toEqual("192.0.2.64/28");
143
144 yield* stack.destroy();
145 }).pipe(logLevel),
146 {
147 tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"],
148 timeout: 120_000,
149 },
150 );