@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 105 lines 4.17 KB
Raw
1 /**
2 * Typed out-of-band "assert gone" helpers shared by the EC2 test suites.
3 *
4 * Each helper describes the resource by id after the final `stack.destroy()`
5 * and succeeds only when the API returns the typed NotFound tag (or, for
6 * instances, a terminal state). A bounded retry absorbs EC2's eventual
7 * consistency; anything else fails the test — proving the suite left zero
8 * orphans.
9 */
10 import * as ec2 from "@distilled.cloud/aws/ec2";
11 import * as Data from "effect/Data";
12 import * as Effect from "effect/Effect";
13 import * as Schedule from "effect/Schedule";
14
15 class ResourceStillExists extends Data.TaggedError("ResourceStillExists")<{
16 readonly id: string;
17 }> {}
18
19 const goneRetry = {
20 while: (e: { _tag: string }): boolean => e._tag === "ResourceStillExists",
21 schedule: Schedule.spaced("3 seconds"),
22 times: 10,
23 } as const;
24
25 export const assertVpcGone = Effect.fn(function* (vpcId: string) {
26 yield* ec2.describeVpcs({ VpcIds: [vpcId] }).pipe(
27 Effect.flatMap(() => Effect.fail(new ResourceStillExists({ id: vpcId }))),
28 Effect.retry(goneRetry),
29 Effect.catchTag("InvalidVpcID.NotFound", () => Effect.void),
30 );
31 });
32
33 export const assertEipGone = Effect.fn(function* (allocationId: string) {
34 yield* ec2.describeAddresses({ AllocationIds: [allocationId] }).pipe(
35 Effect.flatMap(() => Effect.fail(new ResourceStillExists({ id: allocationId }))),
36 Effect.retry(goneRetry),
37 Effect.catchTag("InvalidAllocationID.NotFound", () => Effect.void),
38 );
39 });
40
41 export const assertInternetGatewayGone = Effect.fn(function* (igwId: string) {
42 yield* ec2.describeInternetGateways({ InternetGatewayIds: [igwId] }).pipe(
43 Effect.flatMap(() => Effect.fail(new ResourceStillExists({ id: igwId }))),
44 Effect.retry(goneRetry),
45 Effect.catchTag("InvalidInternetGatewayID.NotFound", () => Effect.void),
46 );
47 });
48
49 export const assertRouteTableGone = Effect.fn(function* (routeTableId: string) {
50 yield* ec2.describeRouteTables({ RouteTableIds: [routeTableId] }).pipe(
51 Effect.flatMap(() => Effect.fail(new ResourceStillExists({ id: routeTableId }))),
52 Effect.retry(goneRetry),
53 Effect.catchTag("InvalidRouteTableID.NotFound", () => Effect.void),
54 );
55 });
56
57 export const assertSecurityGroupGone = Effect.fn(function* (groupId: string) {
58 yield* ec2.describeSecurityGroups({ GroupIds: [groupId] }).pipe(
59 Effect.flatMap(() => Effect.fail(new ResourceStillExists({ id: groupId }))),
60 Effect.retry(goneRetry),
61 Effect.catchTag("InvalidGroup.NotFound", () => Effect.void),
62 );
63 });
64
65 export const assertNetworkAclGone = Effect.fn(function* (networkAclId: string) {
66 yield* ec2.describeNetworkAcls({ NetworkAclIds: [networkAclId] }).pipe(
67 Effect.flatMap(() => Effect.fail(new ResourceStillExists({ id: networkAclId }))),
68 Effect.retry(goneRetry),
69 Effect.catchTag("InvalidNetworkAclID.NotFound", () => Effect.void),
70 );
71 });
72
73 export const assertSubnetGone = Effect.fn(function* (subnetId: string) {
74 yield* ec2.describeSubnets({ SubnetIds: [subnetId] }).pipe(
75 Effect.flatMap(() => Effect.fail(new ResourceStillExists({ id: subnetId }))),
76 Effect.retry(goneRetry),
77 Effect.catchTag("InvalidSubnetID.NotFound", () => Effect.void),
78 );
79 });
80
81 export const assertKeyPairGone = Effect.fn(function* (keyPairId: string) {
82 yield* ec2.describeKeyPairs({ KeyPairIds: [keyPairId] }).pipe(
83 Effect.flatMap(() => Effect.fail(new ResourceStillExists({ id: keyPairId }))),
84 Effect.retry(goneRetry),
85 Effect.catchTag("InvalidKeyPair.NotFound", () => Effect.void),
86 );
87 });
88
89 /**
90 * A terminated instance remains visible in `describeInstances` for a while;
91 * `terminated` (or the id no longer resolving) is the terminal, non-billing
92 * state that proves the suite left nothing running.
93 */
94 export const assertInstanceTerminated = Effect.fn(function* (instanceId: string) {
95 yield* ec2.describeInstances({ InstanceIds: [instanceId] }).pipe(
96 Effect.flatMap((result) => {
97 const state = result.Reservations?.[0]?.Instances?.[0]?.State?.Name;
98 return state === undefined || state === "terminated"
99 ? Effect.void
100 : Effect.fail(new ResourceStillExists({ id: instanceId }));
101 }),
102 Effect.retry(goneRetry),
103 Effect.catchTag("InvalidInstanceID.NotFound", () => Effect.void),
104 );
105 });