@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 225 lines 8.27 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Subnet, Vpc } from "@alchemy.run/aws/EC2";
3 import {
4 Listener,
5 Rule,
6 Service,
7 ServiceNetwork,
8 ServiceNetworkServiceAssociation,
9 TargetGroup,
10 } from "@alchemy.run/aws/VpcLattice";
11 import { expect } from "@alchemy.run/test";
12 import * as Test from "@alchemy.run/test/Alchemy";
13 import * as vpclattice from "@distilled.cloud/aws/vpc-lattice";
14 import * as Data from "effect/Data";
15 import * as Effect from "effect/Effect";
16 import * as Schedule from "effect/Schedule";
17
18 const { test } = Test.make({ providers: AWS.providers() });
19
20 class StillExists extends Data.TaggedError("StillExists")<{
21 readonly id: string;
22 }> {}
23
24 const assertTargetGroupDeleted = (id: string) =>
25 vpclattice.getTargetGroup({ targetGroupIdentifier: id }).pipe(
26 Effect.flatMap(() => Effect.fail(new StillExists({ id }))),
27 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
28 Effect.retry({
29 while: (e): boolean => e._tag === "StillExists",
30 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(15)]),
31 }),
32 );
33
34 const assertServiceDeleted = (id: string) =>
35 vpclattice.getService({ serviceIdentifier: id }).pipe(
36 Effect.flatMap(() => Effect.fail(new StillExists({ id }))),
37 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
38 Effect.retry({
39 while: (e): boolean => e._tag === "StillExists",
40 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(15)]),
41 }),
42 );
43
44 test.provider(
45 "service chain: target group, listener, rule, service association",
46 (stack) =>
47 Effect.gen(function* () {
48 yield* stack.destroy();
49
50 const program = (options: {
51 defaultStatusCode: number;
52 rulePriority: number;
53 targetIp: string;
54 healthCheckEnabled: boolean;
55 }) =>
56 Effect.gen(function* () {
57 const vpc = yield* Vpc("ChainVpc", { cidrBlock: "10.31.0.0/16" });
58 // IP targets must fall inside a subnet of the VPC.
59 const subnet = yield* Subnet("ChainSubnet", {
60 vpcId: vpc.vpcId,
61 cidrBlock: "10.31.0.0/24",
62 });
63 const network = yield* ServiceNetwork("ChainNetwork", {});
64 const service = yield* Service("ChainService", {});
65 const association = yield* ServiceNetworkServiceAssociation("ChainAssociation", {
66 serviceNetworkIdentifier: network.serviceNetworkId,
67 serviceIdentifier: service.serviceId,
68 });
69 const targetGroup = yield* TargetGroup("ChainTargets", {
70 type: "IP",
71 port: 80,
72 protocol: "HTTP",
73 // Reference the VPC through the subnet so target registration
74 // (which requires a subnet covering the target IP) is ordered
75 // after the subnet exists.
76 vpcIdentifier: subnet.vpcId,
77 healthCheck: {
78 enabled: options.healthCheckEnabled,
79 protocol: "HTTP",
80 path: "/health",
81 healthCheckInterval: "30 seconds",
82 healthCheckTimeout: "5 seconds",
83 },
84 targets: [{ id: options.targetIp, port: 80 }],
85 });
86 const listener = yield* Listener("ChainListener", {
87 serviceIdentifier: service.serviceId,
88 protocol: "HTTP",
89 port: 80,
90 defaultAction: {
91 fixedResponse: { statusCode: options.defaultStatusCode },
92 },
93 });
94 const rule = yield* Rule("ChainRule", {
95 serviceIdentifier: service.serviceId,
96 listenerIdentifier: listener.listenerId,
97 priority: options.rulePriority,
98 match: {
99 httpMatch: { pathMatch: { match: { prefix: "/api" } } },
100 },
101 action: {
102 forward: {
103 targetGroups: [
104 {
105 targetGroupIdentifier: targetGroup.targetGroupId,
106 weight: 100,
107 },
108 ],
109 },
110 },
111 });
112 return { association, listener, network, rule, service, targetGroup };
113 });
114
115 const first = yield* stack.deploy(
116 program({
117 defaultStatusCode: 404,
118 rulePriority: 10,
119 targetIp: "10.31.0.10",
120 healthCheckEnabled: false,
121 }),
122 );
123
124 expect(first.targetGroup.targetGroupId).toMatch(/^tg-/);
125 expect(first.targetGroup.type).toBe("IP");
126 expect(first.listener.listenerId).toMatch(/^listener-/);
127 expect(first.rule.ruleId).toMatch(/^rule-/);
128 expect(first.association.associationId).toMatch(/^snsa-/);
129
130 // Live listener has the fixed default action.
131 const liveListener = yield* vpclattice.getListener({
132 serviceIdentifier: first.service.serviceId,
133 listenerIdentifier: first.listener.listenerId,
134 });
135 expect(liveListener.defaultAction).toEqual({
136 fixedResponse: { statusCode: 404 },
137 });
138
139 // Live rule forwards /api to the target group at priority 10.
140 const liveRule = yield* vpclattice.getRule({
141 serviceIdentifier: first.service.serviceId,
142 listenerIdentifier: first.listener.listenerId,
143 ruleIdentifier: first.rule.ruleId,
144 });
145 expect(liveRule.priority).toBe(10);
146 expect(liveRule.action).toEqual({
147 forward: {
148 targetGroups: [
149 {
150 targetGroupIdentifier: first.targetGroup.targetGroupId,
151 weight: 100,
152 },
153 ],
154 },
155 });
156
157 // The IP target is registered.
158 const liveTargets = yield* vpclattice.listTargets({
159 targetGroupIdentifier: first.targetGroup.targetGroupId,
160 });
161 expect(liveTargets.items.map((t) => t.id)).toEqual(["10.31.0.10"]);
162
163 // The service is associated with the network.
164 const liveAssociation = yield* vpclattice.getServiceNetworkServiceAssociation({
165 serviceNetworkServiceAssociationIdentifier: first.association.associationId,
166 });
167 expect(liveAssociation.serviceId).toBe(first.service.serviceId);
168 expect(["ACTIVE", "CREATE_IN_PROGRESS"]).toContain(liveAssociation.status);
169
170 // Update in place: default action, rule priority, health check, target.
171 const second = yield* stack.deploy(
172 program({
173 defaultStatusCode: 500,
174 rulePriority: 20,
175 targetIp: "10.31.0.11",
176 healthCheckEnabled: true,
177 }),
178 );
179 expect(second.listener.listenerId).toBe(first.listener.listenerId);
180 expect(second.rule.ruleId).toBe(first.rule.ruleId);
181 expect(second.targetGroup.targetGroupId).toBe(first.targetGroup.targetGroupId);
182 expect(second.association.associationId).toBe(first.association.associationId);
183
184 const updatedListener = yield* vpclattice.getListener({
185 serviceIdentifier: first.service.serviceId,
186 listenerIdentifier: first.listener.listenerId,
187 });
188 expect(updatedListener.defaultAction).toEqual({
189 fixedResponse: { statusCode: 500 },
190 });
191
192 const updatedRule = yield* vpclattice.getRule({
193 serviceIdentifier: first.service.serviceId,
194 listenerIdentifier: first.listener.listenerId,
195 ruleIdentifier: first.rule.ruleId,
196 });
197 expect(updatedRule.priority).toBe(20);
198
199 const updatedGroup = yield* vpclattice.getTargetGroup({
200 targetGroupIdentifier: first.targetGroup.targetGroupId,
201 });
202 expect(updatedGroup.config?.healthCheck?.enabled).toBe(true);
203 expect(updatedGroup.config?.healthCheck?.path).toBe("/health");
204 expect(updatedGroup.config?.healthCheck?.healthCheckIntervalSeconds).toBe(30);
205
206 const updatedTargets = yield* vpclattice.listTargets({
207 targetGroupIdentifier: first.targetGroup.targetGroupId,
208 });
209 expect(updatedTargets.items.filter((t) => t.status !== "DRAINING").map((t) => t.id)).toEqual([
210 "10.31.0.11",
211 ]);
212
213 yield* stack.destroy();
214 yield* assertTargetGroupDeleted(first.targetGroup.targetGroupId);
215 yield* assertServiceDeleted(first.service.serviceId);
216 }).pipe(
217 // A mid-test failure must not orphan the VPC (default quota is 5 per
218 // region) — always tear the stack down.
219 Effect.ensuring(Effect.ignore(stack.destroy())),
220 ),
221 {
222 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:vpclattice", "live"],
223 timeout: 600_000,
224 },
225 );