| 1 | import * as AWS from "@/AWS"; |
| 2 | import { CapacityProvider } from "@/AWS/ECS"; |
| 3 | import * as Provider from "@/Provider"; |
| 4 | import * as Test from "@/Test/Alchemy"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: AWS.providers() }); |
| 10 | |
| 11 | const logLevel = Effect.provideService( |
| 12 | MinimumLogLevel, |
| 13 | process.env.DEBUG ? "Debug" : "Info", |
| 14 | ); |
| 15 | |
| 16 | // `list()` enumerates every capacity provider in the account/region via the |
| 17 | // `describeCapacityProviders` op (paginated with `nextToken`), filtering out the |
| 18 | // AWS-managed `FARGATE`/`FARGATE_SPOT` reserved providers. This ungated case |
| 19 | // proves the pagination + typing run live against the real API without needing |
| 20 | // an ASG-backed provider to exist. |
| 21 | test.provider( |
| 22 | "list enumerates capacity providers in the account/region", |
| 23 | () => |
| 24 | Effect.gen(function* () { |
| 25 | const provider = yield* Provider.findProvider(CapacityProvider); |
| 26 | const all = yield* provider.list(); |
| 27 | |
| 28 | expect(Array.isArray(all)).toBe(true); |
| 29 | // Reserved AWS-managed providers must be filtered out. |
| 30 | expect(all.some((p) => p.name === "FARGATE")).toBe(false); |
| 31 | expect(all.some((p) => p.name === "FARGATE_SPOT")).toBe(false); |
| 32 | // Every returned item carries the full Attributes shape. |
| 33 | for (const p of all) { |
| 34 | expect(typeof p.name).toBe("string"); |
| 35 | expect(p.capacityProviderArn).toMatch( |
| 36 | /^arn:aws:ecs:[^:]+:\d+:capacity-provider\//, |
| 37 | ); |
| 38 | } |
| 39 | }).pipe(logLevel), |
| 40 | { tags: ["provider:aws", "provider:aws:ecs", "live"], timeout: 120_000 }, |
| 41 | ); |
| 42 | |
| 43 | // Full deploy + list assertion. Requires a pre-provisioned EC2 Auto Scaling |
| 44 | // Group ARN (set via TEST_ASG_ARN): a real capacity provider cannot be created |
| 45 | // without an ASG, and the distilled `auto-scaling` service is currently |
| 46 | // non-functional against the live API (aws-query Action derivation bug — see |
| 47 | // AutoScalingGroup.test.ts), so an ASG cannot be provisioned in-test. Run with: |
| 48 | // TEST_ASG_ARN=arn:aws:autoscaling:... bun vitest CapacityProvider |
| 49 | test.provider.skipIf(!process.env.TEST_ASG_ARN)( |
| 50 | "list includes a deployed capacity provider", |
| 51 | (stack) => |
| 52 | Effect.gen(function* () { |
| 53 | const autoScalingGroupArn = process.env.TEST_ASG_ARN!; |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | const deployed = yield* stack.deploy( |
| 57 | CapacityProvider("ListCapacityProvider", { |
| 58 | autoScalingGroupArn, |
| 59 | managedScaling: { |
| 60 | status: "ENABLED", |
| 61 | targetCapacity: 80, |
| 62 | minimumScalingStepSize: 1, |
| 63 | maximumScalingStepSize: 10, |
| 64 | }, |
| 65 | managedTerminationProtection: "DISABLED", |
| 66 | tags: { env: "test" }, |
| 67 | }), |
| 68 | ); |
| 69 | |
| 70 | const provider = yield* Provider.findProvider(CapacityProvider); |
| 71 | const all = yield* provider.list(); |
| 72 | |
| 73 | expect(all.some((p) => p.name === deployed.name)).toBe(true); |
| 74 | expect( |
| 75 | all.some((p) => p.capacityProviderArn === deployed.capacityProviderArn), |
| 76 | ).toBe(true); |
| 77 | |
| 78 | yield* stack.destroy(); |
| 79 | }).pipe(logLevel), |
| 80 | { tags: ["provider:aws", "provider:aws:ecs", "live"], timeout: 600_000 }, |
| 81 | ); |