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

main
ts 281 lines 8.87 KB
Raw
1 import * as Hetzner from "@alchemy.run/hetzner";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as zoneRrsets from "@distilled.cloud/hetzner/zone_rrsets";
5 import * as zones from "@distilled.cloud/hetzner/zones";
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
11 const { test } = Test.make({ providers: Hetzner.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 const hasHetznerCreds = !!process.env.HCLOUD_TOKEN;
16
17 const waitUntilGone = (zoneId: number, name: string, type: string) =>
18 zoneRrsets
19 .getZoneRrset({
20 id_or_name: String(zoneId),
21 rr_name: name,
22 rr_type: type,
23 })
24 .pipe(
25 Effect.as("found" as const),
26 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
27 Effect.repeat({
28 schedule: Schedule.spaced("1 second"),
29 until: (status) => status === "gone",
30 times: 10,
31 }),
32 );
33
34 const waitUntilZoneGone = (zoneId: number) =>
35 zones.getZone({ id_or_name: String(zoneId) }).pipe(
36 Effect.as("found" as const),
37 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
38 Effect.repeat({
39 schedule: Schedule.spaced("1 second"),
40 until: (status) => status === "gone",
41 times: 10,
42 }),
43 );
44
45 test.provider.skipIf(!hasHetznerCreds)(
46 "create, update, and delete a record set",
47 (stack) =>
48 Effect.gen(function* () {
49 yield* stack.destroy();
50
51 const created = yield* stack.deploy(
52 Effect.gen(function* () {
53 const zone = yield* Hetzner.Zone("DnsZone", {
54 ttl: 3600,
55 });
56 const recordSet = yield* Hetzner.RecordSet("Www", {
57 zone,
58 name: "www",
59 type: "A",
60 records: [{ value: "192.0.2.1" }],
61 ttl: 300,
62 labels: { env: "test" },
63 });
64 return { zone, recordSet };
65 }),
66 );
67
68 expect(created.recordSet.id).toEqual("www/A");
69 expect(created.recordSet.zoneId).toEqual(created.zone.zoneId);
70 expect(created.recordSet.name).toEqual("www");
71 expect(created.recordSet.type).toEqual("A");
72 expect(created.recordSet.ttl).toEqual(300);
73 expect(created.recordSet.changeProtection).toEqual(false);
74 expect(created.recordSet.labels).toMatchObject({ env: "test" });
75 expect(created.recordSet.records).toEqual(
76 expect.arrayContaining([expect.objectContaining({ value: "192.0.2.1" })]),
77 );
78 expect(created.recordSet.records).toHaveLength(1);
79
80 const fetched = yield* zoneRrsets.getZoneRrset({
81 id_or_name: String(created.zone.zoneId),
82 rr_name: "www",
83 rr_type: "A",
84 });
85 expect(fetched.rrset.id).toEqual("www/A");
86 expect(fetched.rrset.zone).toEqual(created.zone.zoneId);
87 expect(fetched.rrset.ttl).toEqual(300);
88 expect(fetched.rrset.protection.change).toEqual(false);
89 expect(fetched.rrset.labels.env).toEqual("test");
90 expect(fetched.rrset.records.map((record) => record.value).sort()).toEqual(["192.0.2.1"]);
91
92 const updated = yield* stack.deploy(
93 Effect.gen(function* () {
94 const zone = yield* Hetzner.Zone("DnsZone", {
95 name: created.zone.name,
96 ttl: 3600,
97 });
98 const recordSet = yield* Hetzner.RecordSet("Www", {
99 zone,
100 name: "www",
101 type: "A",
102 records: [{ value: "192.0.2.1" }, { value: "192.0.2.2" }],
103 ttl: 600,
104 labels: { env: "prod", role: "dns" },
105 changeProtection: true,
106 });
107 return { zone, recordSet };
108 }),
109 );
110
111 expect(updated.recordSet.id).toEqual("www/A");
112 expect(updated.recordSet.zoneId).toEqual(created.zone.zoneId);
113 expect(updated.recordSet.ttl).toEqual(600);
114 expect(updated.recordSet.changeProtection).toEqual(true);
115 expect(updated.recordSet.labels).toMatchObject({
116 env: "prod",
117 role: "dns",
118 });
119 expect(updated.recordSet.records).toHaveLength(2);
120 expect(updated.recordSet.records.map((record) => record.value).sort()).toEqual([
121 "192.0.2.1",
122 "192.0.2.2",
123 ]);
124
125 const refetched = yield* zoneRrsets.getZoneRrset({
126 id_or_name: String(updated.zone.zoneId),
127 rr_name: "www",
128 rr_type: "A",
129 });
130 expect(refetched.rrset.ttl).toEqual(600);
131 expect(refetched.rrset.protection.change).toEqual(true);
132 expect(refetched.rrset.labels.env).toEqual("prod");
133 expect(refetched.rrset.labels.role).toEqual("dns");
134 expect(refetched.rrset.records.map((record) => record.value).sort()).toEqual([
135 "192.0.2.1",
136 "192.0.2.2",
137 ]);
138
139 yield* stack.destroy();
140
141 const gone = yield* waitUntilGone(created.zone.zoneId, "www", "A");
142 expect(gone).toEqual("gone");
143 const zoneGone = yield* waitUntilZoneGone(created.zone.zoneId);
144 expect(zoneGone).toEqual("gone");
145 }).pipe(logLevel),
146 {
147 tags: [
148 "provider:hetzner",
149 "provider:hetzner:recordset",
150 "provider:hetzner:service",
151 "provider:hetzner:zone",
152 "live",
153 ],
154 timeout: 120_000,
155 },
156 );
157
158 test.provider.skipIf(!hasHetznerCreds)(
159 "replace when name changes",
160 (stack) =>
161 Effect.gen(function* () {
162 yield* stack.destroy();
163
164 const created = yield* stack.deploy(
165 Effect.gen(function* () {
166 const zone = yield* Hetzner.Zone("ReplaceZone", {
167 ttl: 3600,
168 });
169 const recordSet = yield* Hetzner.RecordSet("Host", {
170 zone,
171 name: "www",
172 type: "A",
173 records: [{ value: "192.0.2.10" }],
174 ttl: 300,
175 });
176 return { zone, recordSet };
177 }),
178 );
179
180 expect(created.recordSet.id).toEqual("www/A");
181
182 const replaced = yield* stack.deploy(
183 Effect.gen(function* () {
184 const zone = yield* Hetzner.Zone("ReplaceZone", {
185 name: created.zone.name,
186 ttl: 3600,
187 });
188 const recordSet = yield* Hetzner.RecordSet("Host", {
189 zone,
190 name: "api",
191 type: "A",
192 records: [{ value: "192.0.2.10" }],
193 ttl: 300,
194 });
195 return { zone, recordSet };
196 }),
197 );
198
199 expect(replaced.recordSet.id).toEqual("api/A");
200 expect(replaced.recordSet.id).not.toEqual(created.recordSet.id);
201 expect(replaced.recordSet.zoneId).toEqual(created.zone.zoneId);
202
203 const fetched = yield* zoneRrsets.getZoneRrset({
204 id_or_name: String(replaced.zone.zoneId),
205 rr_name: "api",
206 rr_type: "A",
207 });
208 expect(fetched.rrset.id).toEqual("api/A");
209 expect(fetched.rrset.records.map((record) => record.value)).toEqual(["192.0.2.10"]);
210
211 const oldGone = yield* waitUntilGone(created.zone.zoneId, "www", "A");
212 expect(oldGone).toEqual("gone");
213
214 yield* stack.destroy();
215
216 const gone = yield* waitUntilGone(replaced.zone.zoneId, "api", "A");
217 expect(gone).toEqual("gone");
218 const zoneGone = yield* waitUntilZoneGone(created.zone.zoneId);
219 expect(zoneGone).toEqual("gone");
220 }).pipe(logLevel),
221 {
222 tags: [
223 "provider:hetzner",
224 "provider:hetzner:recordset",
225 "provider:hetzner:service",
226 "provider:hetzner:zone",
227 "live",
228 ],
229 timeout: 180_000,
230 },
231 );
232
233 test.provider.skipIf(!hasHetznerCreds)(
234 "list enumerates the deployed record set",
235 (stack) =>
236 Effect.gen(function* () {
237 yield* stack.destroy();
238
239 const deployed = yield* stack.deploy(
240 Effect.gen(function* () {
241 const zone = yield* Hetzner.Zone("ListZone", {
242 ttl: 3600,
243 });
244 const recordSet = yield* Hetzner.RecordSet("Www", {
245 zone,
246 name: "www",
247 type: "A",
248 records: [{ value: "192.0.2.20" }],
249 });
250 return { zone, recordSet };
251 }),
252 );
253
254 const provider = yield* Provider.findProvider(Hetzner.RecordSet);
255 const all = yield* provider.list();
256 const found = all.find(
257 (rrset) => rrset.zoneId === deployed.zone.zoneId && rrset.id === "www/A",
258 );
259 expect(found).toBeDefined();
260 expect(found?.name).toEqual("www");
261 expect(found?.type).toEqual("A");
262 expect(found?.records.map((record) => record.value)).toEqual(["192.0.2.20"]);
263
264 yield* stack.destroy();
265
266 const gone = yield* waitUntilGone(deployed.zone.zoneId, "www", "A");
267 expect(gone).toEqual("gone");
268 const zoneGone = yield* waitUntilZoneGone(deployed.zone.zoneId);
269 expect(zoneGone).toEqual("gone");
270 }).pipe(logLevel),
271 {
272 tags: [
273 "provider:hetzner",
274 "provider:hetzner:recordset",
275 "provider:hetzner:service",
276 "provider:hetzner:zone",
277 "live",
278 ],
279 timeout: 120_000,
280 },
281 );