@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 205 lines 8.97 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Accelerator, EndpointGroup, Listener } from "@alchemy.run/aws/GlobalAccelerator";
3 import { Bucket } from "@alchemy.run/aws/S3";
4 import { describe, expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as ga from "@distilled.cloud/aws/global-accelerator";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 // Deterministic (per test case) bucket for accelerator flow logs. Flow-log
13 // delivery requires a bucket policy granting the log-delivery service
14 // principal write access.
15 const FLOW_LOGS_BUCKET = "alchemy-test-ga-flow-logs-4f81c2";
16
17 // The testing profile runs in us-west-2, which is exactly where the Global
18 // Accelerator control plane lives, so out-of-band verification calls the
19 // distilled ops directly.
20
21 const assertAcceleratorGone = (acceleratorArn: string) =>
22 ga.describeAccelerator({ AcceleratorArn: acceleratorArn }).pipe(
23 Effect.flatMap(() => Effect.fail(new Error(`accelerator ${acceleratorArn} still exists`))),
24 Effect.catchTag("AcceleratorNotFoundException", () => Effect.void),
25 Effect.retry({
26 while: (e) => e instanceof Error,
27 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]),
28 }),
29 );
30
31 describe.sequential(
32 "AWS.GlobalAccelerator",
33 { tags: ["provider:aws", "provider:aws:globalaccelerator", "provider:aws:s3", "live"] },
34 () => {
35 test.provider(
36 "creates accelerator + listener + endpoint group, updates in place, destroys in order",
37 (stack) =>
38 Effect.gen(function* () {
39 yield* stack.destroy();
40
41 const program = (opts: {
42 listenerPorts: { fromPort: number; toPort: number }[];
43 clientAffinity?: "NONE" | "SOURCE_IP";
44 trafficDial?: number;
45 flowLogs?: boolean;
46 tags: Record<string, string>;
47 }) =>
48 Effect.gen(function* () {
49 const bucket = yield* Bucket("FlowLogsBucket", {
50 bucketName: FLOW_LOGS_BUCKET,
51 policy: [
52 {
53 Sid: "AWSLogDeliveryWrite",
54 Effect: "Allow",
55 Principal: { Service: "delivery.logs.amazonaws.com" },
56 Action: ["s3:PutObject"],
57 Resource: [`arn:aws:s3:::${FLOW_LOGS_BUCKET}/*`],
58 },
59 {
60 Sid: "AWSLogDeliveryAclCheck",
61 Effect: "Allow",
62 Principal: { Service: "delivery.logs.amazonaws.com" },
63 Action: ["s3:GetBucketAcl"],
64 Resource: [`arn:aws:s3:::${FLOW_LOGS_BUCKET}`],
65 },
66 ],
67 forceDestroy: true,
68 });
69 const accelerator = yield* Accelerator("TestAccelerator", {
70 flowLogs: opts.flowLogs
71 ? { bucket: bucket.bucketName, prefix: "ga-flow-logs" }
72 : undefined,
73 tags: opts.tags,
74 });
75 const listener = yield* Listener("WebListener", {
76 acceleratorArn: accelerator.acceleratorArn,
77 portRanges: opts.listenerPorts,
78 protocol: "TCP",
79 clientAffinity: opts.clientAffinity,
80 });
81 const group = yield* EndpointGroup("UsWest2Group", {
82 listenerArn: listener.listenerArn,
83 endpointGroupRegion: "us-west-2",
84 trafficDialPercentage: opts.trafficDial,
85 healthCheckProtocol: "TCP",
86 healthCheckPort: 80,
87 });
88 return {
89 acceleratorArn: accelerator.acceleratorArn,
90 acceleratorName: accelerator.name,
91 dnsName: accelerator.dnsName,
92 flowLogsEnabled: accelerator.flowLogsEnabled,
93 listenerArn: listener.listenerArn,
94 endpointGroupArn: group.endpointGroupArn,
95 trafficDialPercentage: group.trafficDialPercentage,
96 };
97 });
98
99 // --- create ---
100 const created = yield* stack.deploy(
101 program({
102 listenerPorts: [{ fromPort: 80, toPort: 80 }],
103 tags: { purpose: "alchemy-test" },
104 }),
105 );
106 expect(created.acceleratorArn).toContain(":accelerator/");
107 expect(created.dnsName).toBeTruthy();
108 expect(created.flowLogsEnabled).toBe(false);
109 expect(created.listenerArn).toContain("/listener/");
110 expect(created.endpointGroupArn).toContain("/endpoint-group/");
111
112 // Out-of-band verification via distilled.
113 const acc = yield* ga.describeAccelerator({ AcceleratorArn: created.acceleratorArn });
114 expect(acc.Accelerator?.Name).toEqual(created.acceleratorName);
115 expect(acc.Accelerator?.Enabled).toBe(true);
116 expect(acc.Accelerator?.DnsName).toEqual(created.dnsName);
117
118 const lst = yield* ga.describeListener({ ListenerArn: created.listenerArn });
119 expect(lst.Listener?.Protocol).toEqual("TCP");
120 expect(lst.Listener?.PortRanges).toEqual([{ FromPort: 80, ToPort: 80 }]);
121 expect(lst.Listener?.ClientAffinity).toEqual("NONE");
122
123 const grp = yield* ga.describeEndpointGroup({
124 EndpointGroupArn: created.endpointGroupArn,
125 });
126 expect(grp.EndpointGroup?.EndpointGroupRegion).toEqual("us-west-2");
127 expect(grp.EndpointGroup?.TrafficDialPercentage).toEqual(100);
128 expect(grp.EndpointGroup?.HealthCheckPort).toEqual(80);
129
130 const tags = yield* ga.listTagsForResource({ ResourceArn: created.acceleratorArn });
131 const tagMap = Object.fromEntries((tags.Tags ?? []).map((t) => [t.Key, t.Value]));
132 expect(tagMap.purpose).toEqual("alchemy-test");
133 expect(tagMap["alchemy::id"]).toEqual("TestAccelerator");
134
135 // --- update 1: listener ports + affinity, accelerator tags + flow logs ---
136 const updated = yield* stack.deploy(
137 program({
138 listenerPorts: [
139 { fromPort: 80, toPort: 80 },
140 { fromPort: 443, toPort: 443 },
141 ],
142 clientAffinity: "SOURCE_IP",
143 flowLogs: true,
144 tags: { purpose: "alchemy-test", team: "platform" },
145 }),
146 );
147 // in-place updates — stable ARNs
148 expect(updated.acceleratorArn).toEqual(created.acceleratorArn);
149 expect(updated.listenerArn).toEqual(created.listenerArn);
150 expect(updated.endpointGroupArn).toEqual(created.endpointGroupArn);
151 expect(updated.flowLogsEnabled).toBe(true);
152
153 // Out-of-band: flow logs were enabled via updateAcceleratorAttributes.
154 const attrs = yield* ga.describeAcceleratorAttributes({
155 AcceleratorArn: created.acceleratorArn,
156 });
157 expect(attrs.AcceleratorAttributes?.FlowLogsEnabled).toBe(true);
158 expect(attrs.AcceleratorAttributes?.FlowLogsS3Bucket).toEqual(FLOW_LOGS_BUCKET);
159 expect(attrs.AcceleratorAttributes?.FlowLogsS3Prefix).toEqual("ga-flow-logs");
160
161 const lst2 = yield* ga.describeListener({ ListenerArn: created.listenerArn });
162 const ports = (lst2.Listener?.PortRanges ?? [])
163 .map((r) => r.FromPort)
164 .sort((a, b) => (a ?? 0) - (b ?? 0));
165 expect(ports).toEqual([80, 443]);
166 expect(lst2.Listener?.ClientAffinity).toEqual("SOURCE_IP");
167
168 const tags2 = yield* ga.listTagsForResource({ ResourceArn: created.acceleratorArn });
169 const tagMap2 = Object.fromEntries((tags2.Tags ?? []).map((t) => [t.Key, t.Value]));
170 expect(tagMap2.team).toEqual("platform");
171
172 // --- update 2: endpoint group traffic dial + disable flow logs ---
173 const dialed = yield* stack.deploy(
174 program({
175 listenerPorts: [
176 { fromPort: 80, toPort: 80 },
177 { fromPort: 443, toPort: 443 },
178 ],
179 clientAffinity: "SOURCE_IP",
180 trafficDial: 50,
181 tags: { purpose: "alchemy-test", team: "platform" },
182 }),
183 );
184 expect(dialed.endpointGroupArn).toEqual(created.endpointGroupArn);
185 expect(dialed.trafficDialPercentage).toEqual(50);
186 expect(dialed.flowLogsEnabled).toBe(false);
187
188 const grp2 = yield* ga.describeEndpointGroup({
189 EndpointGroupArn: created.endpointGroupArn,
190 });
191 expect(grp2.EndpointGroup?.TrafficDialPercentage).toEqual(50);
192
193 const attrs2 = yield* ga.describeAcceleratorAttributes({
194 AcceleratorArn: created.acceleratorArn,
195 });
196 expect(attrs2.AcceleratorAttributes?.FlowLogsEnabled).toBe(false);
197
198 // --- destroy: endpoint group -> listener -> disable -> delete ---
199 yield* stack.destroy();
200 yield* assertAcceleratorGone(created.acceleratorArn);
201 }),
202 { timeout: 220_000 },
203 );
204 },
205 );