@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, 03:37 UTC

main
ts 147 lines 4.54 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Subnet, Vpc } from "@alchemy.run/aws/EC2";
3 import { expect } from "@alchemy.run/test";
4 import * as EC2 from "@distilled.cloud/aws/ec2";
5 import * as Provider from "alchemy/Provider";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10 import * as Test from "./VpcTest.ts";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 test.provider(
17 "create, update, delete subnet",
18 (stack) =>
19 Effect.gen(function* () {
20 yield* stack.destroy();
21
22 const { vpc, subnet } = yield* stack.deploy(
23 Effect.gen(function* () {
24 const vpc = yield* Vpc("TestVpc", {
25 cidrBlock: "10.0.0.0/16",
26 });
27 const subnet = yield* Subnet("TestSubnet", {
28 vpcId: vpc.vpcId,
29 cidrBlock: "10.0.1.0/24",
30 });
31 return { vpc, subnet };
32 }),
33 );
34
35 const actualSubnet = yield* EC2.describeSubnets({
36 SubnetIds: [subnet.subnetId],
37 });
38
39 expect(actualSubnet.Subnets?.[0]?.SubnetId).toEqual(subnet.subnetId);
40 expect(actualSubnet.Subnets?.[0]?.CidrBlock).toEqual("10.0.1.0/24");
41 expect(actualSubnet.Subnets?.[0]?.VpcId).toEqual(vpc.vpcId);
42 expect(actualSubnet.Subnets?.[0]?.State).toEqual("available");
43 expect(actualSubnet.Subnets?.[0]?.MapPublicIpOnLaunch).toEqual(false);
44
45 // Update subnet attributes
46 const { subnet: updatedSubnet } = yield* stack.deploy(
47 Effect.gen(function* () {
48 const vpc = yield* Vpc("TestVpc", {
49 cidrBlock: "10.0.0.0/16",
50 });
51 const subnet = yield* Subnet("TestSubnet", {
52 vpcId: vpc.vpcId,
53 cidrBlock: "10.0.1.0/24",
54 mapPublicIpOnLaunch: true,
55 });
56 return { vpc, subnet };
57 }),
58 );
59
60 yield* expectSubnetAttribute({
61 SubnetId: updatedSubnet.subnetId,
62 Attribute: "mapPublicIpOnLaunch",
63 Value: true,
64 });
65
66 // Delete subnet and VPC
67 yield* stack.destroy();
68
69 yield* assertSubnetDeleted(subnet.subnetId);
70 }).pipe(logLevel),
71 { tags: ["provider:aws", "provider:aws:ec2", "live"] },
72 );
73
74 test.provider(
75 "list enumerates the deployed subnet",
76 (stack) =>
77 Effect.gen(function* () {
78 yield* stack.destroy();
79
80 const { subnet } = yield* stack.deploy(
81 Effect.gen(function* () {
82 const vpc = yield* Vpc("ListVpc", {
83 cidrBlock: "10.0.0.0/16",
84 });
85 const subnet = yield* Subnet("ListSubnet", {
86 vpcId: vpc.vpcId,
87 cidrBlock: "10.0.1.0/24",
88 });
89 return { vpc, subnet };
90 }),
91 );
92
93 const provider = yield* Provider.findProvider(Subnet);
94 const all = yield* provider.list();
95
96 expect(all.some((s) => s.subnetId === subnet.subnetId)).toBe(true);
97
98 yield* stack.destroy();
99
100 yield* assertSubnetDeleted(subnet.subnetId);
101 }).pipe(logLevel),
102 { tags: ["provider:aws", "provider:aws:ec2", "live"] },
103 );
104
105 const expectSubnetAttribute = Effect.fn(function* (props: {
106 SubnetId: string;
107 Attribute: "mapPublicIpOnLaunch" | "assignIpv6AddressOnCreation";
108 Value: boolean;
109 }) {
110 yield* EC2.describeSubnets({
111 SubnetIds: [props.SubnetId],
112 }).pipe(
113 Effect.tap(Effect.logDebug),
114 Effect.flatMap((result) => {
115 const subnet = result.Subnets?.[0];
116 const actualValue =
117 props.Attribute === "mapPublicIpOnLaunch"
118 ? subnet?.MapPublicIpOnLaunch
119 : subnet?.AssignIpv6AddressOnCreation;
120
121 return actualValue === props.Value
122 ? Effect.succeed(result)
123 : Effect.fail(new SubnetAttributeStale());
124 }),
125 Effect.retry({
126 while: (e) => e instanceof SubnetAttributeStale,
127 schedule: Schedule.max([Schedule.exponential(100), Schedule.recurs(8)]),
128 }),
129 );
130 });
131
132 const assertSubnetDeleted = Effect.fn(function* (subnetId: string) {
133 yield* EC2.describeSubnets({
134 SubnetIds: [subnetId],
135 }).pipe(
136 Effect.flatMap(() => Effect.fail(new SubnetStillExists())),
137 Effect.retry({
138 while: (e) => e instanceof SubnetStillExists,
139 schedule: Schedule.max([Schedule.exponential(100), Schedule.recurs(8)]),
140 }),
141 Effect.catchTag("InvalidSubnetID.NotFound", () => Effect.void),
142 );
143 });
144
145 class SubnetStillExists extends Data.TaggedError("SubnetStillExists") {}
146
147 class SubnetAttributeStale extends Data.TaggedError("SubnetAttributeStale") {}