@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 205 lines 6.96 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { HealthCheck } 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 * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import { driftOf, repairDrift, waitForDrift } from "../drift.ts";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 const assertCheckGone = (id: string) =>
13 route53.getHealthCheck({ HealthCheckId: id }).pipe(
14 Effect.flatMap(() => Effect.fail(new Error("health check still exists"))),
15 Effect.catchTag("NoSuchHealthCheck", () => Effect.void),
16 Effect.retry({
17 while: (e) => e instanceof Error,
18 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
19 }),
20 );
21
22 test.provider(
23 "create, update in place, tag, and delete health check",
24 (stack) =>
25 Effect.gen(function* () {
26 yield* stack.destroy();
27
28 // Create.
29 const check = yield* stack.deploy(
30 Effect.gen(function* () {
31 return yield* HealthCheck("Check", {
32 type: "HTTP",
33 fullyQualifiedDomainName: "example.com",
34 resourcePath: "/",
35 port: 80,
36 requestInterval: "30 seconds",
37 failureThreshold: 3,
38 tags: { env: "test" },
39 });
40 }),
41 );
42
43 expect(check.id).toBeDefined();
44 expect(check.healthCheckId).toBe(check.id);
45 expect(check.type).toBe("HTTP");
46
47 const observed = yield* route53.getHealthCheck({
48 HealthCheckId: check.id,
49 });
50 expect(observed.HealthCheck.HealthCheckConfig.FailureThreshold).toBe(3);
51 expect(observed.HealthCheck.HealthCheckConfig.ResourcePath).toBe("/");
52 // Duration.Input prop round-trips to the wire as integer seconds.
53 expect(observed.HealthCheck.HealthCheckConfig.RequestInterval).toBe(30);
54
55 const tags = yield* route53.listTagsForResource({
56 ResourceType: "healthcheck",
57 ResourceId: check.id,
58 });
59 const tagMap = Object.fromEntries(
60 (tags.ResourceTagSet.Tags ?? []).map((t) => [t.Key, t.Value]),
61 );
62 expect(tagMap.env).toBe("test");
63 expect(tagMap["alchemy::id"]).toBeDefined();
64
65 // Update mutable fields in place (version-locked).
66 const updated = yield* stack.deploy(
67 Effect.gen(function* () {
68 return yield* HealthCheck("Check", {
69 type: "HTTP",
70 fullyQualifiedDomainName: "example.com",
71 resourcePath: "/health",
72 port: 80,
73 requestInterval: "30 seconds",
74 failureThreshold: 5,
75 tags: { env: "prod" },
76 });
77 }),
78 );
79 // In-place update keeps the same id.
80 expect(updated.id).toBe(check.id);
81
82 const observed2 = yield* route53.getHealthCheck({
83 HealthCheckId: check.id,
84 });
85 expect(observed2.HealthCheck.HealthCheckConfig.FailureThreshold).toBe(5);
86 expect(observed2.HealthCheck.HealthCheckConfig.ResourcePath).toBe("/health");
87
88 const tags2 = yield* route53.listTagsForResource({
89 ResourceType: "healthcheck",
90 ResourceId: check.id,
91 });
92 const tagMap2 = Object.fromEntries(
93 (tags2.ResourceTagSet.Tags ?? []).map((t) => [t.Key, t.Value]),
94 );
95 expect(tagMap2.env).toBe("prod");
96
97 yield* stack.destroy();
98 yield* assertCheckGone(check.id);
99 }),
100 { tags: ["provider:aws", "provider:aws:route53", "live"], timeout: 180_000 },
101 );
102
103 test.provider(
104 "changing Type replaces the health check",
105 (stack) =>
106 Effect.gen(function* () {
107 yield* stack.destroy();
108
109 const check = yield* stack.deploy(
110 Effect.gen(function* () {
111 return yield* HealthCheck("ReplaceCheck", {
112 type: "HTTP",
113 fullyQualifiedDomainName: "example.com",
114 port: 80,
115 requestInterval: "30 seconds",
116 });
117 }),
118 );
119 const originalId = check.id;
120
121 // Type is immutable — changing it forces replacement (new id).
122 const replaced = yield* stack.deploy(
123 Effect.gen(function* () {
124 return yield* HealthCheck("ReplaceCheck", {
125 type: "HTTPS",
126 fullyQualifiedDomainName: "example.com",
127 port: 443,
128 requestInterval: "30 seconds",
129 });
130 }),
131 );
132
133 expect(replaced.type).toBe("HTTPS");
134 expect(replaced.id).not.toBe(originalId);
135
136 // The old health check must have been deleted.
137 yield* assertCheckGone(originalId);
138
139 yield* stack.destroy();
140 yield* assertCheckGone(replaced.id);
141 }),
142 { tags: ["provider:aws", "provider:aws:route53", "live"], timeout: 180_000 },
143 );
144
145 // #2688: `read` must observe the endpoint, thresholds, disabled state and
146 // tags, or an out-of-band change reads as `unchanged`.
147 test.provider(
148 "drift detects and repairs out-of-band health check changes",
149 (stack) =>
150 Effect.gen(function* () {
151 yield* stack.destroy();
152
153 const check = yield* stack.deploy(
154 HealthCheck("DriftCheck", {
155 type: "HTTP",
156 fullyQualifiedDomainName: "example.com",
157 resourcePath: "/health",
158 port: 80,
159 failureThreshold: 3,
160 tags: { env: "declared" },
161 }),
162 );
163 expect((yield* driftOf(stack, "DriftCheck"))?.action).toBe("unchanged");
164
165 const current = yield* route53.getHealthCheck({ HealthCheckId: check.id });
166 yield* route53.updateHealthCheck({
167 HealthCheckId: check.id,
168 HealthCheckVersion: current.HealthCheck.HealthCheckVersion,
169 ResourcePath: "/out-of-band",
170 Disabled: true,
171 });
172 yield* route53.changeTagsForResource({
173 ResourceType: "healthcheck",
174 ResourceId: check.id,
175 AddTags: [{ Key: "env", Value: "out-of-band" }],
176 });
177
178 const drifted = yield* waitForDrift(stack, "DriftCheck", "drifted");
179 expect(drifted?.attr?.configuration).toMatchObject({
180 resourcePath: "/out-of-band",
181 disabled: true,
182 tags: { env: "out-of-band" },
183 });
184
185 const repaired = yield* repairDrift(stack);
186 expect(repaired.resources.DriftCheck?.action).toBe("repaired");
187 // Route 53 reads lag writes, so poll for the restored config.
188 const restored = yield* route53.getHealthCheck({ HealthCheckId: check.id }).pipe(
189 Effect.repeat({
190 until: (r) =>
191 r.HealthCheck.HealthCheckConfig.ResourcePath === "/health" &&
192 !r.HealthCheck.HealthCheckConfig.Disabled,
193 schedule: Schedule.spaced("2 seconds"),
194 times: 15,
195 }),
196 );
197 expect(restored.HealthCheck.HealthCheckConfig.ResourcePath).toBe("/health");
198 expect(restored.HealthCheck.HealthCheckConfig.Disabled ?? false).toBe(false);
199 expect((yield* waitForDrift(stack, "DriftCheck", "unchanged"))?.action).toBe("unchanged");
200
201 yield* stack.destroy();
202 yield* assertCheckGone(check.id);
203 }),
204 { tags: ["provider:aws", "provider:aws:route53", "live"] },
205 );