@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 168 lines 6.2 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { TargetGroup } from "@alchemy.run/aws/ELBv2";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as elbv2 from "@distilled.cloud/aws/elastic-load-balancing-v2";
6 import * as Drift from "alchemy/Drift";
7 import * as Provider from "alchemy/Provider";
8 import * as Effect from "effect/Effect";
9 import { getDefaultVpc } from "../DefaultVpc.ts";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 // Canonical `list()` test for an account/region-scoped resource: a target group
14 // only needs a VPC id, so reuse the default VPC in the account/region (avoids
15 // the per-region VPC limit), deploy a TargetGroup, resolve the provider from
16 // context with the typed `findProvider` helper, call `list()` (which
17 // exhaustively paginates describeTargetGroups in the account/region), and assert
18 // the deployed target group appears in the result.
19 test.provider(
20 "list enumerates the deployed target group",
21 (stack) =>
22 Effect.gen(function* () {
23 yield* stack.destroy();
24
25 const defaultVpc = yield* getDefaultVpc;
26 const deployed = yield* stack.deploy(
27 Effect.gen(function* () {
28 const targetGroup = yield* TargetGroup("ListTargetGroup", {
29 vpcId: defaultVpc.vpcId,
30 port: 80,
31 protocol: "HTTP",
32 targetType: "ip",
33 // Duration.Input props — verified on the wire below.
34 healthCheckInterval: "45 seconds",
35 healthCheckTimeout: "20 seconds",
36 });
37
38 return { targetGroup };
39 }),
40 );
41
42 expect(deployed.targetGroup.targetGroupArn).toBeDefined();
43
44 // Out-of-band: Duration.Input health-check props reach the wire as
45 // whole seconds.
46 const observed = yield* elbv2
47 .describeTargetGroups({
48 TargetGroupArns: [deployed.targetGroup.targetGroupArn],
49 })
50 .pipe(Effect.map((r) => r.TargetGroups?.[0]));
51 expect(observed?.HealthCheckIntervalSeconds).toBe(45);
52 expect(observed?.HealthCheckTimeoutSeconds).toBe(20);
53
54 const provider = yield* Provider.findProvider(TargetGroup);
55 const all = yield* provider.list();
56
57 expect(all.some((tg) => tg.targetGroupArn === deployed.targetGroup.targetGroupArn)).toBe(
58 true,
59 );
60
61 yield* stack.destroy();
62
63 // Out-of-band: the target group is gone after destroy.
64 const after = yield* elbv2
65 .describeTargetGroups({
66 TargetGroupArns: [deployed.targetGroup.targetGroupArn],
67 })
68 .pipe(
69 Effect.map((r) => r.TargetGroups?.length ?? 0),
70 Effect.catchTag("TargetGroupNotFoundException", () => Effect.succeed(0)),
71 );
72 expect(after).toBe(0);
73 }),
74 {
75 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:elbv2", "live"],
76 timeout: 240_000,
77 },
78 );
79
80 const targetGroupAttributes = (targetGroupArn: string) =>
81 elbv2
82 .describeTargetGroupAttributes({ TargetGroupArn: targetGroupArn })
83 .pipe(
84 Effect.map((r) =>
85 Object.fromEntries((r.Attributes ?? []).map((a) => [a.Key ?? "", a.Value ?? ""])),
86 ),
87 );
88
89 // #1969: attribute changes made outside alchemy are detected as drift and
90 // repaired back to the declared values. Undeclared attributes (AWS defaults)
91 // never surface.
92 test.provider(
93 "detects and repairs out-of-band attribute drift",
94 (stack) =>
95 Effect.gen(function* () {
96 yield* stack.destroy();
97
98 const defaultVpc = yield* getDefaultVpc;
99 const attributes = {
100 "stickiness.enabled": "true",
101 "stickiness.type": "lb_cookie",
102 "deregistration_delay.timeout_seconds": "30",
103 };
104 const deployed = yield* stack.deploy(
105 Effect.gen(function* () {
106 return yield* TargetGroup("DriftTargetGroup", {
107 vpcId: defaultVpc.vpcId,
108 port: 80,
109 protocol: "HTTP",
110 targetType: "ip",
111 attributes,
112 healthCheckPath: "/health",
113 tags: { team: "declared" },
114 });
115 }),
116 );
117 const arn = deployed.targetGroupArn;
118 expect(deployed.attributes).toEqual(attributes);
119 expect(yield* targetGroupAttributes(arn)).toMatchObject(attributes);
120
121 const target = { name: stack.name, stage: stack.stage };
122 const clean = yield* Drift.detect(target).pipe(Effect.provide(stack.state));
123 expect(clean.resources.DriftTargetGroup?.action).toBe("unchanged");
124
125 // Drift a declared attribute and an undeclared one out of band.
126 yield* elbv2.modifyTargetGroupAttributes({
127 TargetGroupArn: arn,
128 Attributes: [
129 { Key: "stickiness.enabled", Value: "false" },
130 { Key: "slow_start.duration_seconds", Value: "60" },
131 ],
132 });
133 // #2688: the declared health check and the tags drift too.
134 yield* elbv2.modifyTargetGroup({ TargetGroupArn: arn, HealthCheckPath: "/oob" });
135 yield* elbv2.addTags({ ResourceArns: [arn], Tags: [{ Key: "team", Value: "out-of-band" }] });
136
137 const drifted = yield* Drift.detect(target).pipe(Effect.provide(stack.state));
138 expect(drifted.resources.DriftTargetGroup).toMatchObject({
139 action: "drifted",
140 attr: {
141 attributes: { ...attributes, "stickiness.enabled": "false" },
142 healthCheck: { HealthCheckPath: "/oob" },
143 tags: { team: "out-of-band" },
144 },
145 });
146
147 const repaired = yield* Drift.repair(target).pipe(Effect.provide(stack.state));
148 expect(repaired.resources.DriftTargetGroup).toMatchObject({
149 action: "repaired",
150 attr: { attributes },
151 });
152 const live = yield* targetGroupAttributes(arn);
153 expect(live).toMatchObject(attributes);
154 // Undeclared attributes are not alchemy's to reset.
155 expect(live["slow_start.duration_seconds"]).toBe("60");
156 const [group] = (yield* elbv2.describeTargetGroups({ TargetGroupArns: [arn] })).TargetGroups!;
157 expect(group?.HealthCheckPath).toBe("/health");
158
159 const settled = yield* Drift.detect(target).pipe(Effect.provide(stack.state));
160 expect(settled.resources.DriftTargetGroup?.action).toBe("unchanged");
161
162 yield* stack.destroy();
163 }),
164 {
165 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:elbv2", "live"],
166 timeout: 240_000,
167 },
168 );