@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 282 lines 11 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { SecurityGroup } from "@alchemy.run/aws/EC2";
3 import { Cluster } from "@alchemy.run/aws/ECS/Cluster";
4 import { Service } from "@alchemy.run/aws/ECS/Service";
5 import * as Cloudflare from "@alchemy.run/cloudflare";
6 import { expect } from "@alchemy.run/test";
7 import * as Test from "@alchemy.run/test/Alchemy";
8 import * as ecs from "@distilled.cloud/aws/ecs";
9 import * as elbv2 from "@distilled.cloud/aws/elastic-load-balancing-v2";
10 import * as ConfigProvider from "effect/ConfigProvider";
11 import * as Effect from "effect/Effect";
12 import * as HttpClient from "effect/http/HttpClient";
13 import * as Layer from "effect/Layer";
14 import * as Schedule from "effect/Schedule";
15 import * as pathe from "pathe";
16 import { getDefaultVpcNetwork } from "../DefaultVpc.ts";
17 import type { OtelSink } from "./fixtures/otel-collector-worker.ts";
18 import TestTask from "./fixtures/task.ts";
19 import {
20 E2E_CLUSTER_NAME,
21 E2E_TEST_TITLE,
22 reclaimTaskE2EOrphans,
23 scanTaskE2EOrphans,
24 } from "./reclaimTaskE2EOrphans.ts";
25
26 const { test } = Test.make({
27 providers: Layer.mergeAll(AWS.providers(), Cloudflare.providers()),
28 });
29
30 // The OTLP sink the task exports to (a workers.dev URL is reachable from
31 // Fargate). Declared identically in both deploy steps so the second deploy
32 // keeps it.
33 const collectorMain = pathe.resolve(import.meta.dirname, "fixtures/otel-collector-worker.ts");
34 const collectorWorker = () =>
35 Cloudflare.Worker("EcsOtelCollector", {
36 main: collectorMain,
37 env: {
38 SINK: Cloudflare.DurableObject<OtelSink>("OtelSink"),
39 },
40 compatibility: { date: "2024-09-23" },
41 });
42
43 // Full end-to-end: build + push the bundled Task image, run it on Fargate
44 // behind an Alchemy-managed public ALB, and prove over HTTP that (a) the
45 // `{ fetch }` handler is served and (b) the `ServerHost.run` background loop is
46 // actually executing inside the deployed container (`/ticks` keeps climbing).
47 //
48 // This is the real-deploy regression for #706. Docker/ECR + Fargate placement
49 // + the ALB health ramp takes about five minutes (~4.6 min observed) on the
50 // standing default VPC; `--fast` skips the lifecycle.
51 test.provider.skipIf(!!process.env.FAST)(
52 // The title doubles as the scratch stack name, which prefixes every
53 // physical name — the orphan sweep prefix-matches on it.
54 E2E_TEST_TITLE,
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 // The scratch stack's state is in-memory, so a hard-killed prior run
60 // (vitest hard timeout / OOM) orphans everything it deployed — the
61 // engine can never see it again. Reclaim those leftovers up front, and
62 // guarantee the same sweep runs on success, failure, AND interruption.
63 yield* reclaimTaskE2EOrphans;
64 yield* Effect.addFinalizer(() => reclaimTaskE2EOrphans.pipe(Effect.orDie));
65
66 // Reuse the standing public network. Creating a dedicated VPC here is
67 // both unnecessary for the Task regression and races the account's VPC
68 // quota during c128 sweeps. An internet-facing ALB needs two AZs.
69 const { vpcId, subnetIds } = yield* getDefaultVpcNetwork;
70 if (subnetIds.length < 2) {
71 return yield* Effect.die(new Error("default VPC has fewer than 2 subnets"));
72 }
73 const publicSubnetIds = subnetIds.slice(0, 2);
74
75 // Deploy the OTLP collector first: the task fixture resolves
76 // `COLLECTOR_URL` from the deployer's config at deploy time.
77 const collector = yield* stack.deploy(
78 Effect.gen(function* () {
79 return yield* collectorWorker();
80 }),
81 );
82 const collected = `${collector.url}/collected`;
83
84 const currentConfig = yield* ConfigProvider.ConfigProvider;
85 const { url, targetGroupArn, clusterArn, serviceName, taskDefinitionArn } = yield* stack
86 .deploy(
87 Effect.gen(function* () {
88 yield* collectorWorker();
89 // The default VPC supplies public routing and subnets; this managed
90 // security group remains isolated to the test and is swept exactly.
91 const securityGroup = yield* SecurityGroup("EcsE2ESg", {
92 vpcId,
93 description: "alchemy ecs task e2e",
94 ingress: [
95 {
96 ipProtocol: "tcp",
97 fromPort: 80,
98 toPort: 80,
99 cidrIpv4: "0.0.0.0/0",
100 description: "ALB ingress",
101 },
102 {
103 ipProtocol: "tcp",
104 fromPort: 3000,
105 toPort: 3000,
106 cidrIpv4: "0.0.0.0/0",
107 description: "container traffic",
108 },
109 ],
110 egress: [
111 {
112 ipProtocol: "-1",
113 cidrIpv4: "0.0.0.0/0",
114 description: "all outbound",
115 },
116 ],
117 });
118
119 const cluster = yield* Cluster("EcsE2ECluster", {
120 clusterName: E2E_CLUSTER_NAME,
121 });
122
123 // The bundled long-running Task (builds + pushes the image).
124 const task = yield* TestTask;
125
126 const service = yield* Service("EcsE2EService", {
127 cluster,
128 task: {
129 taskDefinitionArn: task.taskDefinitionArn,
130 containerName: task.containerName,
131 port: task.port,
132 },
133 desiredCount: 1,
134 public: true,
135 listenerPort: 80,
136 healthCheckPath: "/health",
137 vpcId,
138 subnets: publicSubnetIds,
139 securityGroups: [securityGroup.groupId],
140 assignPublicIp: true,
141 });
142
143 return {
144 url: service.url,
145 targetGroupArn: service.targetGroupArn,
146 clusterArn: service.clusterArn,
147 serviceName: service.serviceName,
148 taskDefinitionArn: service.taskDefinitionArn,
149 };
150 }),
151 )
152 .pipe(
153 Effect.provideService(
154 ConfigProvider.ConfigProvider,
155 ConfigProvider.orElse(
156 ConfigProvider.fromUnknown({ COLLECTOR_URL: collector.url }),
157 currentConfig,
158 ),
159 ),
160 );
161
162 expect(url).toBeTruthy();
163 expect(targetGroupArn).toBeTruthy();
164
165 // Service.deploy now blocks until the exact task definition is the sole
166 // completed deployment, ECS counts have settled, and every registered
167 // target is healthy. Prove that contract immediately out-of-band.
168 const describedService = yield* ecs.describeServices({
169 cluster: clusterArn,
170 services: [serviceName],
171 });
172 const deployedService = describedService.services?.[0];
173 expect(deployedService?.taskDefinition).toBe(taskDefinitionArn);
174 expect(deployedService?.desiredCount).toBe(1);
175 expect(deployedService?.runningCount).toBe(1);
176 expect(deployedService?.pendingCount).toBe(0);
177 expect(deployedService?.deployments).toHaveLength(1);
178 expect(deployedService?.deployments?.[0]?.status).toBe("PRIMARY");
179
180 const targetHealth = yield* elbv2.describeTargetHealth({
181 TargetGroupArn: targetGroupArn!,
182 });
183 expect(
184 (targetHealth.TargetHealthDescriptions ?? []).map((target) => target.TargetHealth?.State),
185 ).toEqual(["healthy"]);
186
187 // The ALB has been active for a while now, so its DNS resolves. Probe the
188 // public endpoint (still retry through edge/DNS propagation).
189 const health = yield* HttpClient.get(`${url}/health`).pipe(
190 Effect.flatMap((res) =>
191 res.status === 200
192 ? Effect.succeed(res)
193 : Effect.fail(new Error(`/health returned ${res.status}`)),
194 ),
195 Effect.tapError((error) => Effect.logError(error)),
196 Effect.retry({ schedule: Schedule.spaced("6 seconds"), times: 30 }),
197 );
198 expect(health.status).toBe(200);
199 expect(yield* health.json).toEqual({ ok: true });
200
201 // Prove the ServerHost.run background loop is executing in-container:
202 // the tick counter climbs between two reads.
203 const readTicks = HttpClient.get(`${url}/ticks`).pipe(
204 Effect.flatMap((res) => res.json),
205 Effect.map((body) => (body as { ticks: number }).ticks),
206 );
207 const first = yield* readTicks;
208 yield* Effect.sleep("3 seconds");
209 const second = yield* readTicks;
210 expect(second).toBeGreaterThan(first);
211
212 // Process-mode telemetry: spans from SEPARATE requests must arrive in
213 // one batched traces payload (interval batching on the process root
214 // scope) — a per-request flush would put each span in its own POST
215 // and the pair would never share a payload item. Retry attempts with
216 // fresh tags in case a pair straddles a 5s export window.
217 const fetchCollected = HttpClient.get(collected).pipe(
218 Effect.flatMap((res) => res.json),
219 Effect.map((body) => (body as { items: { signal: string; payload: unknown }[] }).items),
220 );
221 const attemptBatch = (i: number) =>
222 Effect.gen(function* () {
223 yield* HttpClient.get(`${url}/work?tag=a${i}`);
224 yield* HttpClient.get(`${url}/work?tag=b${i}`);
225 return yield* fetchCollected.pipe(
226 Effect.map((items) =>
227 items.some((item) => {
228 if (item.signal !== "traces") return false;
229 const payload = JSON.stringify(item.payload);
230 return payload.includes(`work:a${i}`) && payload.includes(`work:b${i}`);
231 }),
232 ),
233 Effect.repeat({
234 schedule: Schedule.spaced("3 seconds"),
235 until: (found): boolean => found,
236 times: 8,
237 }),
238 Effect.catchCause(() => Effect.succeed(false)),
239 );
240 });
241 let batched = false;
242 for (let i = 0; i < 3 && !batched; i++) {
243 batched = yield* attemptBatch(i);
244 }
245 expect(batched).toBe(true);
246
247 // Logs from the same requests ship through the process-mode logger.
248 const items = yield* fetchCollected;
249 expect(
250 items.some(
251 (item) =>
252 item.signal === "logs" && JSON.stringify(item.payload).includes("ecs-work-log:"),
253 ),
254 ).toBe(true);
255 // And the process-mode resource attributes are stamped.
256 expect(JSON.stringify(items)).toContain("otel-ecs-e2e");
257
258 yield* stack.destroy();
259
260 // Sweep the stragglers `stack.destroy()` cannot reach: the INACTIVE
261 // task-definition revisions left by `deregisterTaskDefinition`, plus
262 // anything a partial destroy failure abandoned. This is what makes a
263 // PASSING run leave zero cloud resources behind.
264 yield* reclaimTaskE2EOrphans;
265
266 // Clean-slate proof: a passing run leaves ZERO cloud resources.
267 expect(yield* scanTaskE2EOrphans).toEqual([]);
268 }),
269 {
270 tags: [
271 "provider:aws",
272 "provider:aws:ec2",
273 "provider:aws:ecr",
274 "provider:aws:ecs",
275 "provider:aws:iam",
276 "provider:cloudflare",
277 "provider:cloudflare:worker",
278 "live",
279 ],
280 timeout: 1_200_000,
281 },
282 );