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

main
ts 244 lines 8.5 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { HostedZone } from "@alchemy.run/aws/Route53";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as route53 from "@distilled.cloud/aws/route-53";
6 import { isResourceState, State, type ResourceState } from "alchemy/State";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 const normalizeId = (id: string) => id.replace(/^\/hostedzone\//, "");
13
14 // Deterministic per-test zone names (reserved-domain-safe TLD `.alchemy`).
15 const zoneName = "alchemy-hostedzone-crud.alchemy.";
16
17 const assertZoneGone = (id: string) =>
18 route53.getHostedZone({ Id: normalizeId(id) }).pipe(
19 Effect.flatMap(() => Effect.fail(new Error("zone still exists"))),
20 Effect.catchTag("NoSuchHostedZone", () => Effect.void),
21 Effect.retry({
22 while: (e) => e instanceof Error,
23 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
24 }),
25 );
26
27 test.provider(
28 "create, update comment, tag, and delete hosted zone",
29 (stack) =>
30 Effect.gen(function* () {
31 yield* stack.destroy();
32
33 // Create.
34 const zone = yield* stack.deploy(
35 Effect.gen(function* () {
36 return yield* HostedZone("Zone", {
37 name: zoneName,
38 comment: "initial comment",
39 tags: { env: "test" },
40 });
41 }),
42 );
43
44 expect(zone.id).toBeDefined();
45 expect(zone.name).toBe(zoneName);
46 // Public zones get exactly 4 authoritative name servers.
47 expect(zone.nameServers.length).toBe(4);
48 expect(zone.comment).toBe("initial comment");
49
50 // Verify out of band.
51 const observed = yield* route53.getHostedZone({
52 Id: normalizeId(zone.id),
53 });
54 expect(observed.HostedZone.Config?.Comment).toBe("initial comment");
55
56 const tags = yield* route53.listTagsForResource({
57 ResourceType: "hostedzone",
58 ResourceId: normalizeId(zone.id),
59 });
60 const tagMap = Object.fromEntries(
61 (tags.ResourceTagSet.Tags ?? []).map((t) => [t.Key, t.Value]),
62 );
63 expect(tagMap.env).toBe("test");
64 expect(tagMap["alchemy::id"]).toBeDefined();
65
66 // Update comment + tags in place.
67 const updated = yield* stack.deploy(
68 Effect.gen(function* () {
69 return yield* HostedZone("Zone", {
70 name: zoneName,
71 comment: "updated comment",
72 tags: { env: "prod" },
73 });
74 }),
75 );
76 expect(updated.id).toBe(zone.id);
77 expect(updated.comment).toBe("updated comment");
78
79 const observed2 = yield* route53.getHostedZone({
80 Id: normalizeId(zone.id),
81 });
82 expect(observed2.HostedZone.Config?.Comment).toBe("updated comment");
83
84 const tags2 = yield* route53.listTagsForResource({
85 ResourceType: "hostedzone",
86 ResourceId: normalizeId(zone.id),
87 });
88 const tagMap2 = Object.fromEntries(
89 (tags2.ResourceTagSet.Tags ?? []).map((t) => [t.Key, t.Value]),
90 );
91 expect(tagMap2.env).toBe("prod");
92
93 yield* stack.destroy();
94 yield* assertZoneGone(zone.id);
95 }),
96 { tags: ["provider:aws", "provider:aws:route53", "live"], timeout: 180_000 },
97 );
98
99 const forceZoneName = "alchemy-hostedzone-force.alchemy.";
100
101 test.provider(
102 "forceDestroy deletes leftover records before deleting the zone",
103 (stack) =>
104 Effect.gen(function* () {
105 yield* stack.destroy();
106
107 const zone = yield* stack.deploy(
108 Effect.gen(function* () {
109 return yield* HostedZone("ForceZone", {
110 name: forceZoneName,
111 forceDestroy: true,
112 });
113 }),
114 );
115
116 // Seed a leftover record out of band so the zone is non-empty.
117 yield* route53
118 .changeResourceRecordSets({
119 HostedZoneId: normalizeId(zone.id),
120 ChangeBatch: {
121 Comment: "leftover record",
122 Changes: [
123 {
124 Action: "UPSERT",
125 ResourceRecordSet: {
126 Name: `leftover.${forceZoneName}`,
127 Type: "TXT",
128 TTL: 60,
129 ResourceRecords: [{ Value: '"leftover"' }],
130 },
131 },
132 ],
133 },
134 })
135 .pipe(Effect.asVoid);
136
137 // destroy() must purge the record then delete the zone.
138 yield* stack.destroy();
139 yield* assertZoneGone(zone.id);
140 }),
141 { tags: ["provider:aws", "provider:aws:route53", "live"], timeout: 180_000 },
142 );
143
144 // Regression test for https://github.com/alchemy-run/alchemy/issues/736.
145 //
146 // An interrupted first deploy persists the zone as `status: "creating"` with
147 // no attributes — and an Output-valued `name` does not survive the state
148 // round-trip: it deserializes as `undefined`. Plan's recovery branch then
149 // calls `provider.read` with those junk props, which crashed in
150 // `normalizeName(undefined)` (`undefined is not an object (evaluating
151 // 'name.endsWith')`) and wedged the stack. When `read` reports "not found",
152 // the same junk `olds` flow into `diff`, whose unguarded
153 // `normalizeName(olds.name)` was the second crash site — so this one wedged
154 // redeploy exercises both guards.
155 //
156 // Simulate exactly that state row after a real deploy and assert the next
157 // deploy recovers: `read` returns undefined, `diff` falls through to the
158 // create recovery path, and reconcile converges on the half-created zone via
159 // its stable CallerReference (HostedZoneAlreadyExists → findByName) — SAME
160 // zone id, no duplicate created.
161 const recoveryZoneName = "pr770-recovery-hz.alchemy.";
162
163 test.provider(
164 "recovers a half-created hosted zone whose creating-state lost Output-valued props (#736)",
165 (stack) =>
166 Effect.gen(function* () {
167 yield* stack.destroy();
168
169 const deployZone = () =>
170 stack.deploy(
171 Effect.gen(function* () {
172 return yield* HostedZone("RecoveryZone", {
173 name: recoveryZoneName,
174 comment: "pr770 recovery",
175 });
176 }),
177 );
178
179 const created = yield* deployZone();
180 expect(created.id).toBeDefined();
181
182 // Safety net: if the recovery redeploy defects (the pre-fix crash), the
183 // zone would otherwise leak — a fresh zone only holds its SOA/NS records
184 // so it deletes cleanly; on the happy path it's already gone (ignored).
185 yield* Effect.addFinalizer(() =>
186 route53.deleteHostedZone({ Id: normalizeId(created.id) }).pipe(Effect.ignore),
187 );
188
189 // Rewrite the zone's persisted row into the wedged shape an interrupted
190 // deploy leaves behind: `creating`, no attributes, and the Output-valued
191 // `name` lost in the state round-trip.
192 const state = yield* yield* State;
193 const stage = stack.stage;
194 const fqns = yield* state.list({ stack: stack.name, stage });
195 const rows = yield* Effect.forEach(fqns, (fqn) =>
196 state.get({ stack: stack.name, stage, fqn }).pipe(Effect.map((row) => ({ fqn, row }))),
197 );
198 const wedged = rows.find(
199 (r): r is { fqn: string; row: ResourceState } =>
200 isResourceState(r.row) && r.row.resourceType === "AWS.Route53.HostedZone",
201 );
202 if (!wedged) {
203 return yield* Effect.die(
204 new Error("no AWS.Route53.HostedZone state row found after deploy"),
205 );
206 }
207 yield* state.set({
208 stack: stack.name,
209 stage,
210 fqn: wedged.fqn,
211 value: {
212 ...wedged.row,
213 status: "creating",
214 attr: undefined,
215 props: { ...wedged.row.props, name: undefined },
216 },
217 });
218
219 // Before the fix this crashed in plan with
220 // `TypeError: undefined is not an object (evaluating 'name.endsWith')`.
221 const recovered = yield* deployZone();
222 expect(recovered.id).toEqual(created.id);
223 expect(recovered.name).toBe(recoveryZoneName);
224
225 yield* stack.destroy();
226 yield* assertZoneGone(created.id);
227 }),
228 { tags: ["provider:aws", "provider:aws:route53", "live"], timeout: 240_000 },
229 );
230
231 test.provider(
232 "idempotent delete tolerates an already-gone zone",
233 () =>
234 Effect.gen(function* () {
235 // Deleting a non-existent zone id should be a no-op (NoSuchHostedZone).
236 yield* route53.deleteHostedZone({ Id: "Z0000000000000NONEXIST" }).pipe(
237 Effect.asVoid,
238 Effect.catchTag("NoSuchHostedZone", () => Effect.void),
239 Effect.catchTag("InvalidInput", () => Effect.void),
240 );
241 expect(true).toBe(true);
242 }),
243 { tags: ["provider:aws", "provider:aws:route53", "live"], timeout: 60_000 },
244 );