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
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { Subnet } from "@alchemy.run/aws/EC2"; |
| 3 | import { Cluster } from "@alchemy.run/aws/ECS/Cluster"; |
| 4 | import { describe, expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as Core from "@alchemy.run/test/Core"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import { getDefaultVpc } from "../DefaultVpc.ts"; |
| 13 | import OneShotTask from "./fixtures/oneshot-task.ts"; |
| 14 | import EcsBindingsTestFunctionLive, { EcsBindingsTestFunction } from "./handler.ts"; |
| 15 | |
| 16 | const testOptions = { providers: AWS.providers() }; |
| 17 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 18 | const sharedStack = Core.scratchStack(testOptions, "EcsBindings"); |
| 19 | |
| 20 | // Infra the fixture Lambda references via `Resource.ref(...)` (see |
| 21 | // handler.ts): a public subnet in the default VPC, a scratch cluster, and the |
| 22 | // one-shot busybox task definition. Yielded by BOTH deploy phases so the |
| 23 | // second (Lambda) deploy doesn't delete them. |
| 24 | const infra = Effect.gen(function* () { |
| 25 | // ECS bindings are not EC2 VPC coverage. Reuse the standing default VPC so |
| 26 | // this fixture does not compete for the account's small VPC quota during a |
| 27 | // high-concurrency AWS sweep. The stack still owns its subnet, preserving |
| 28 | // Resource.ref coverage and deterministic cleanup. |
| 29 | const vpc = yield* getDefaultVpc; |
| 30 | const subnet = yield* Subnet("EcsBindingsSubnet", { |
| 31 | vpcId: vpc.vpcId, |
| 32 | cidrBlock: vpc.subnetCidrBlock(240), |
| 33 | mapPublicIpOnLaunch: true, |
| 34 | }); |
| 35 | const cluster = yield* Cluster("EcsBindingsCluster", { |
| 36 | clusterName: "alchemy-test-ecs-bindings", |
| 37 | }); |
| 38 | const task = yield* OneShotTask; |
| 39 | return { cluster, task, subnet }; |
| 40 | }); |
| 41 | |
| 42 | // Deploy is heavy: default-VPC subnet + cluster + the one-shot task definition |
| 43 | // + Lambda. Keep readiness polling bounded after the deploy completes. |
| 44 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]); |
| 45 | |
| 46 | let baseUrl: string; |
| 47 | |
| 48 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 49 | readonly status: number; |
| 50 | readonly body: string; |
| 51 | }> {} |
| 52 | |
| 53 | // Retry transient fixture 5xx (cold re-init under parallel load); a genuine |
| 54 | // 4xx/assertion failure is surfaced immediately. |
| 55 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 56 | HttpClient.execute(request).pipe( |
| 57 | Effect.flatMap((response) => |
| 58 | response.status >= 500 |
| 59 | ? response.text.pipe( |
| 60 | Effect.flatMap((body) => |
| 61 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 62 | ), |
| 63 | ) |
| 64 | : Effect.succeed(response), |
| 65 | ), |
| 66 | Effect.retry({ |
| 67 | while: (e) => e._tag === "TransientUpstream", |
| 68 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 69 | }), |
| 70 | ); |
| 71 | |
| 72 | interface RunResponse { |
| 73 | taskArn?: string; |
| 74 | lastStatus?: string; |
| 75 | failures?: { reason?: string; detail?: string }[]; |
| 76 | } |
| 77 | |
| 78 | interface DescribedTask { |
| 79 | taskArn?: string; |
| 80 | lastStatus?: string; |
| 81 | stoppedReason?: string; |
| 82 | startedBy?: string; |
| 83 | containers?: { name?: string; exitCode?: number; lastStatus?: string }[]; |
| 84 | } |
| 85 | |
| 86 | const runTask = (body: { command?: string[]; startedBy?: string }) => |
| 87 | send(HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.post(`${baseUrl}/run`), body)).pipe( |
| 88 | Effect.flatMap((r) => r.json), |
| 89 | Effect.map((json) => json as RunResponse), |
| 90 | ); |
| 91 | |
| 92 | const describeTask = (taskArn: string) => |
| 93 | send(HttpClientRequest.get(`${baseUrl}/describe?task=${encodeURIComponent(taskArn)}`)).pipe( |
| 94 | Effect.flatMap((r) => r.json), |
| 95 | Effect.map( |
| 96 | (json) => (json as { tasks?: DescribedTask[] }).tasks?.[0] as DescribedTask | undefined, |
| 97 | ), |
| 98 | ); |
| 99 | |
| 100 | // Poll the fixture's /describe route until the task reaches STOPPED. |
| 101 | // Bounded: 16 × 5s = 80s of polling. A one-shot busybox task typically stops |
| 102 | // within ~40s, but ECS can remain DEPROVISIONING for around a minute when the |
| 103 | // account is under a high-concurrency test sweep. |
| 104 | const describeUntilStopped = (taskArn: string) => |
| 105 | describeTask(taskArn).pipe( |
| 106 | Effect.repeat({ |
| 107 | schedule: Schedule.spaced("5 seconds"), |
| 108 | until: (t) => t?.lastStatus === "STOPPED", |
| 109 | times: 16, |
| 110 | }), |
| 111 | ); |
| 112 | |
| 113 | describe( |
| 114 | "ECS Bindings", |
| 115 | { tags: ["provider:aws", "provider:aws:ec2", "provider:aws:ecs", "provider:aws:lambda", "live"] }, |
| 116 | () => { |
| 117 | beforeAll( |
| 118 | Effect.gen(function* () { |
| 119 | // scratchStack state is in-memory, so this destroy is a tag-based |
| 120 | // cloud sweep — it is what reclaims resources any previous |
| 121 | // (possibly interrupted) run left behind. Do not remove it. |
| 122 | yield* Effect.logInfo("ECS bindings setup: destroying previous stack"); |
| 123 | yield* sharedStack.destroy(); |
| 124 | |
| 125 | // Phase 1: deploy the infra alone so its state exists before the |
| 126 | // Lambda's `Resource.ref(...)`s resolve (refs read stack state, not |
| 127 | // the in-flight plan — see handler.ts). |
| 128 | yield* Effect.logInfo("ECS bindings setup: deploying infra (phase 1)"); |
| 129 | yield* sharedStack.deploy(infra); |
| 130 | |
| 131 | // Phase 2: same infra (idempotent re-reconcile) + the fixture Lambda. |
| 132 | yield* Effect.logInfo("ECS bindings setup: deploying fixture (phase 2)"); |
| 133 | const { functionUrl } = yield* sharedStack.deploy( |
| 134 | Effect.gen(function* () { |
| 135 | yield* infra; |
| 136 | return yield* EcsBindingsTestFunction; |
| 137 | }).pipe(Effect.provide(EcsBindingsTestFunctionLive)), |
| 138 | ); |
| 139 | |
| 140 | expect(functionUrl).toBeTruthy(); |
| 141 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 142 | const readinessUrl = `${baseUrl}/list`; |
| 143 | |
| 144 | yield* Effect.logInfo(`ECS bindings setup: probing readiness at ${readinessUrl}`); |
| 145 | yield* HttpClient.get(readinessUrl).pipe( |
| 146 | Effect.flatMap((response) => |
| 147 | response.status === 200 |
| 148 | ? Effect.succeed(response) |
| 149 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 150 | ), |
| 151 | Effect.tapError((error) => |
| 152 | Effect.logWarning(`ECS bindings setup: fixture not ready yet (${String(error)})`), |
| 153 | ), |
| 154 | Effect.retry({ schedule: readinessPolicy }), |
| 155 | ); |
| 156 | yield* Effect.logInfo("ECS bindings setup: fixture ready"); |
| 157 | }), |
| 158 | // High-concurrency full-suite sweeps starve the container build/push + |
| 159 | // phase-2 Lambda bundle. After a nuke the ECR repo is gone, so every |
| 160 | // round is a cold build — 210s tripped at c96 (convergence round 6). |
| 161 | { timeout: 330_000 }, |
| 162 | ); |
| 163 | |
| 164 | // NO_DESTROY=1 keeps the deployment around between runs while iterating — |
| 165 | // without it an interrupted run tears everything down and the next run |
| 166 | // pays the full cold build again. |
| 167 | afterAll.skipIf(!!process.env.NO_DESTROY)(sharedStack.destroy(), { timeout: 180_000 }); |
| 168 | |
| 169 | describe("RunTask", () => { |
| 170 | test.provider( |
| 171 | "runs a one-shot Fargate task", |
| 172 | (_stack) => |
| 173 | Effect.gen(function* () { |
| 174 | const response = yield* runTask({ startedBy: "alchemy-run-test" }); |
| 175 | expect(response.failures ?? []).toEqual([]); |
| 176 | expect(response.taskArn).toMatch(/:task\//); |
| 177 | // RunTask returns the task in its initial state. |
| 178 | expect(response.lastStatus).toBeTruthy(); |
| 179 | }), |
| 180 | { timeout: 120_000 }, |
| 181 | ); |
| 182 | }); |
| 183 | |
| 184 | describe("DescribeTasks", () => { |
| 185 | test.provider( |
| 186 | "observes a one-shot task run to completion with exit code 0", |
| 187 | (_stack) => |
| 188 | Effect.gen(function* () { |
| 189 | const run = yield* runTask({ startedBy: "alchemy-describe-test" }); |
| 190 | expect(run.taskArn).toBeTruthy(); |
| 191 | |
| 192 | const stopped = yield* describeUntilStopped(run.taskArn!); |
| 193 | expect(stopped?.lastStatus).toBe("STOPPED"); |
| 194 | expect(stopped?.startedBy).toBe("alchemy-describe-test"); |
| 195 | expect(stopped?.containers?.[0]?.exitCode).toBe(0); |
| 196 | }), |
| 197 | { timeout: 240_000 }, |
| 198 | ); |
| 199 | |
| 200 | test.provider( |
| 201 | "reports MISSING for an unknown task id", |
| 202 | (_stack) => |
| 203 | Effect.gen(function* () { |
| 204 | const response = yield* send( |
| 205 | HttpClientRequest.get(`${baseUrl}/describe?task=00000000000000000000000000000000`), |
| 206 | ).pipe(Effect.flatMap((r) => r.json)); |
| 207 | const failures = (response as { failures?: { reason?: string }[] }).failures; |
| 208 | expect(failures?.length).toBe(1); |
| 209 | expect(failures?.[0]?.reason).toBe("MISSING"); |
| 210 | }), |
| 211 | { timeout: 60_000 }, |
| 212 | ); |
| 213 | }); |
| 214 | |
| 215 | describe("ListTasks", () => { |
| 216 | test.provider( |
| 217 | "lists tasks in the bound cluster by startedBy", |
| 218 | (_stack) => |
| 219 | Effect.gen(function* () { |
| 220 | const run = yield* runTask({ startedBy: "alchemy-list-test" }); |
| 221 | expect(run.taskArn).toBeTruthy(); |
| 222 | |
| 223 | // A one-shot echo task can stop before we ever observe it RUNNING, |
| 224 | // so poll the STOPPED listing until it appears (stopped tasks stay |
| 225 | // listed for ~an hour). |
| 226 | const taskArns = yield* send( |
| 227 | HttpClientRequest.get(`${baseUrl}/list?status=STOPPED&startedBy=alchemy-list-test`), |
| 228 | ).pipe( |
| 229 | Effect.flatMap((r) => r.json), |
| 230 | Effect.map((json) => (json as { taskArns: string[] }).taskArns ?? []), |
| 231 | Effect.repeat({ |
| 232 | schedule: Schedule.spaced("5 seconds"), |
| 233 | until: (arns) => arns.includes(run.taskArn!), |
| 234 | times: 10, |
| 235 | }), |
| 236 | ); |
| 237 | expect(taskArns).toContain(run.taskArn!); |
| 238 | }), |
| 239 | { timeout: 240_000 }, |
| 240 | ); |
| 241 | }); |
| 242 | |
| 243 | describe("ListServices", () => { |
| 244 | test.provider( |
| 245 | "lists services in the bound cluster (none deployed)", |
| 246 | (_stack) => |
| 247 | Effect.gen(function* () { |
| 248 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/services`)).pipe( |
| 249 | Effect.flatMap((r) => r.json), |
| 250 | ); |
| 251 | expect((response as { serviceArns: string[] }).serviceArns).toEqual([]); |
| 252 | }), |
| 253 | { timeout: 60_000 }, |
| 254 | ); |
| 255 | }); |
| 256 | |
| 257 | describe("DescribeServices", () => { |
| 258 | test.provider( |
| 259 | "reports MISSING for an unknown service", |
| 260 | (_stack) => |
| 261 | Effect.gen(function* () { |
| 262 | const response = yield* send( |
| 263 | HttpClientRequest.get(`${baseUrl}/describe-services?service=alchemy-no-such-service`), |
| 264 | ).pipe(Effect.flatMap((r) => r.json)); |
| 265 | const failures = (response as { failures?: { reason?: string }[] }).failures; |
| 266 | expect(failures?.length).toBe(1); |
| 267 | expect(failures?.[0]?.reason).toBe("MISSING"); |
| 268 | }), |
| 269 | { timeout: 60_000 }, |
| 270 | ); |
| 271 | }); |
| 272 | |
| 273 | describe("ListContainerInstances", () => { |
| 274 | test.provider( |
| 275 | "returns no container instances for a Fargate-only cluster", |
| 276 | (_stack) => |
| 277 | Effect.gen(function* () { |
| 278 | const response = yield* send( |
| 279 | HttpClientRequest.get(`${baseUrl}/container-instances`), |
| 280 | ).pipe(Effect.flatMap((r) => r.json)); |
| 281 | expect((response as { containerInstanceArns: string[] }).containerInstanceArns).toEqual( |
| 282 | [], |
| 283 | ); |
| 284 | }), |
| 285 | { timeout: 60_000 }, |
| 286 | ); |
| 287 | }); |
| 288 | |
| 289 | describe("TaskProtection", () => { |
| 290 | test.provider( |
| 291 | "get/update task protection round-trips for a standalone task", |
| 292 | (_stack) => |
| 293 | Effect.gen(function* () { |
| 294 | // Task protection only applies to service-managed tasks; for a |
| 295 | // standalone RunTask task AWS reports TASK_NOT_VALID (as a failure |
| 296 | // entry or a typed InvalidParameterException). Either way the |
| 297 | // probe proves IAM, the cluster injection, and (for /protect) the |
| 298 | // Duration → expiresInMinutes mapping reached AWS intact. |
| 299 | const run = yield* runTask({ |
| 300 | command: ["sh", "-c", "sleep 300"], |
| 301 | startedBy: "alchemy-protection-test", |
| 302 | }); |
| 303 | expect(run.taskArn).toBeTruthy(); |
| 304 | |
| 305 | interface ProtectionResponse { |
| 306 | error?: string; |
| 307 | failures?: { reason?: string }[]; |
| 308 | protectedTasks?: unknown[]; |
| 309 | } |
| 310 | const isTaskNotValid = (r: ProtectionResponse): boolean => |
| 311 | r.error === "InvalidParameterException" || |
| 312 | r.failures?.[0]?.reason === "TASK_NOT_VALID"; |
| 313 | |
| 314 | const protect = (yield* send( |
| 315 | HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.post(`${baseUrl}/protect`), { |
| 316 | taskArn: run.taskArn, |
| 317 | }), |
| 318 | ).pipe(Effect.flatMap((r) => r.json))) as ProtectionResponse; |
| 319 | expect(isTaskNotValid(protect)).toBe(true); |
| 320 | |
| 321 | const protection = (yield* send( |
| 322 | HttpClientRequest.get( |
| 323 | `${baseUrl}/protection?task=${encodeURIComponent(run.taskArn!)}`, |
| 324 | ), |
| 325 | ).pipe(Effect.flatMap((r) => r.json))) as ProtectionResponse; |
| 326 | expect(isTaskNotValid(protection)).toBe(true); |
| 327 | |
| 328 | // Clean up the sleeping task. |
| 329 | yield* send( |
| 330 | HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.post(`${baseUrl}/stop`), { |
| 331 | taskArn: run.taskArn, |
| 332 | reason: "protection test done", |
| 333 | }), |
| 334 | ); |
| 335 | }), |
| 336 | { timeout: 120_000 }, |
| 337 | ); |
| 338 | }); |
| 339 | |
| 340 | describe("StopTask", () => { |
| 341 | test.provider( |
| 342 | "stops a long-running task with a reason", |
| 343 | (_stack) => |
| 344 | Effect.gen(function* () { |
| 345 | // Override the one-shot command so the task stays alive until |
| 346 | // stopped (containerOverrides replaces the image CMD). |
| 347 | const run = yield* runTask({ |
| 348 | command: ["sh", "-c", "sleep 300"], |
| 349 | startedBy: "alchemy-stop-test", |
| 350 | }); |
| 351 | expect(run.taskArn).toBeTruthy(); |
| 352 | |
| 353 | const stop = yield* send( |
| 354 | HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.post(`${baseUrl}/stop`), { |
| 355 | taskArn: run.taskArn, |
| 356 | reason: "alchemy stop test", |
| 357 | }), |
| 358 | ).pipe(Effect.flatMap((r) => r.json)); |
| 359 | expect((stop as { taskArn?: string }).taskArn).toBe(run.taskArn); |
| 360 | expect((stop as { desiredStatus?: string }).desiredStatus).toBe("STOPPED"); |
| 361 | |
| 362 | const stopped = yield* describeUntilStopped(run.taskArn!); |
| 363 | expect(stopped?.lastStatus).toBe("STOPPED"); |
| 364 | expect(stopped?.stoppedReason).toBe("alchemy stop test"); |
| 365 | }), |
| 366 | { timeout: 240_000 }, |
| 367 | ); |
| 368 | }); |
| 369 | }, |
| 370 | ); |