@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 130 lines 5.12 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AutoScalingGroup, LaunchTemplate, LifecycleHook } from "@alchemy.run/aws/AutoScaling";
3 import { amazonLinux2023 } from "@alchemy.run/aws/EC2";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as autoscaling from "@distilled.cloud/aws/auto-scaling";
7 import type * as Duration from "effect/Duration";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import { getAutoScalingTestSubnetId } from "./TestNetwork.ts";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 // Deterministic, account-unique names so leftover cleanup from an interrupted
15 // run works. The fleet is sized to zero (min/max/desired 0) so no EC2 instances
16 // launch — the hook is created and observed against a stable, instance-free ASG.
17 const asgName = "alchemy-test-lifecycle-hook-asg";
18 const hookName = "alchemy-test-lifecycle-hook";
19
20 const cleanupAsg = autoscaling
21 .deleteAutoScalingGroup({ AutoScalingGroupName: asgName, ForceDelete: true })
22 .pipe(Effect.catch(() => Effect.void));
23
24 // `describeLifecycleHooks` for a deleted Auto Scaling Group raises AWS wire code
25 // `ValidationError` ("AutoScalingGroup ... not found"), typed as
26 // `AutoScalingGroupNotFound` via the auto-scaling distilled patch. Treat it as
27 // "hook gone" for the out-of-band assertions after teardown.
28 const describeHook = autoscaling
29 .describeLifecycleHooks({
30 AutoScalingGroupName: asgName,
31 LifecycleHookNames: [hookName],
32 } as any)
33 .pipe(
34 Effect.map((r) => r.LifecycleHooks?.[0]),
35 Effect.catchTag("AutoScalingGroupNotFound", () => Effect.succeed(undefined)),
36 );
37
38 test.provider(
39 "creates, updates, and deletes a lifecycle hook on an ASG",
40 (stack) =>
41 Effect.gen(function* () {
42 yield* cleanupAsg;
43 yield* stack.destroy();
44
45 // Launch templates do not validate the AMI at creation time; fall back to
46 // a syntactically valid id if the lookup returns nothing.
47 const imageId = amazonLinux2023();
48
49 // Place the fleet into a subnet of the account's default VPC (resolved
50 // out-of-band) rather than a throwaway Vpc resource — a failed run loses
51 // its scratch state and orphaned VPCs exhaust the 5-per-region quota.
52 const subnetId = yield* getAutoScalingTestSubnetId;
53
54 const deployHook = (heartbeatTimeout: Duration.Input) =>
55 stack.deploy(
56 Effect.gen(function* () {
57 const template = yield* LaunchTemplate("HookTemplate", {
58 imageId,
59 instanceType: "t3.micro",
60 });
61 const group = yield* AutoScalingGroup("HookGroup", {
62 autoScalingGroupName: asgName,
63 launchTemplate: template,
64 subnetIds: [subnetId],
65 minSize: 0,
66 maxSize: 0,
67 desiredCapacity: 0,
68 });
69 return yield* LifecycleHook("Hook", {
70 lifecycleHookName: hookName,
71 autoScalingGroup: group,
72 lifecycleTransition: "TERMINATING",
73 heartbeatTimeout,
74 defaultResult: "CONTINUE",
75 });
76 }),
77 );
78
79 // Create.
80 const created = yield* deployHook("300 seconds");
81 expect(created.lifecycleHookName).toEqual(hookName);
82 expect(created.autoScalingGroupName).toEqual(asgName);
83 expect(created.lifecycleTransition).toEqual("autoscaling:EC2_INSTANCE_TERMINATING");
84 expect(created.heartbeatTimeout).toEqual(300);
85 expect(created.defaultResult).toEqual("CONTINUE");
86
87 // Out-of-band proof.
88 const liveCreated = yield* describeHook;
89 expect(liveCreated?.LifecycleHookName).toEqual(hookName);
90 expect(liveCreated?.HeartbeatTimeout).toEqual(300);
91 expect(liveCreated?.DefaultResult).toEqual("CONTINUE");
92
93 // Update the heartbeat timeout in place (same name → no replacement).
94 const updated = yield* deployHook("120 seconds");
95 expect(updated.lifecycleHookName).toEqual(hookName);
96 expect(updated.heartbeatTimeout).toEqual(120);
97
98 const liveUpdated = yield* describeHook;
99 expect(liveUpdated?.HeartbeatTimeout).toEqual(120);
100
101 // Delete via stack teardown; assert the hook is gone out-of-band.
102 yield* stack.destroy();
103 const gone = yield* describeHook.pipe(
104 Effect.repeat({
105 until: (hook) => hook === undefined,
106 schedule: Schedule.spaced("2 seconds"),
107 times: 8,
108 }),
109 );
110 expect(gone).toBeUndefined();
111
112 // The ASG itself is deleted by the stack teardown too — prove the suite
113 // left nothing behind.
114 const groupsLeft = yield* autoscaling
115 .describeAutoScalingGroups({ AutoScalingGroupNames: [asgName] } as any)
116 .pipe(
117 Effect.map((r) => (r.AutoScalingGroups ?? []).length),
118 Effect.repeat({
119 until: (count) => count === 0,
120 schedule: Schedule.spaced("3 seconds"),
121 times: 10,
122 }),
123 );
124 expect(groupsLeft).toBe(0);
125 }).pipe(Effect.ensuring(cleanupAsg)),
126 {
127 tags: ["provider:aws", "provider:aws:autoscaling", "provider:aws:ec2", "live"],
128 timeout: 240_000,
129 },
130 );