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 { AutoScalingGroup, LaunchTemplate } from "@alchemy.run/aws/AutoScaling"; |
| 3 | import { amazonLinux2023 } from "@alchemy.run/aws/EC2"; |
| 4 | import { CapacityProvider } from "@alchemy.run/aws/ECS"; |
| 5 | import { expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import * as ecs from "@distilled.cloud/aws/ecs"; |
| 8 | import * as Provider from "alchemy/Provider"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import { getDefaultVpcNetwork } from "../DefaultVpc.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: AWS.providers() }); |
| 14 | |
| 15 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 16 | |
| 17 | // `list()` enumerates every capacity provider in the account/region via the |
| 18 | // `describeCapacityProviders` op (paginated with `nextToken`), filtering out the |
| 19 | // AWS-managed `FARGATE`/`FARGATE_SPOT` reserved providers. This ungated case |
| 20 | // proves the pagination + typing run live against the real API without needing |
| 21 | // an ASG-backed provider to exist. |
| 22 | test.provider( |
| 23 | "list enumerates capacity providers in the account/region", |
| 24 | () => |
| 25 | Effect.gen(function* () { |
| 26 | const provider = yield* Provider.findProvider(CapacityProvider); |
| 27 | const all = yield* provider.list(); |
| 28 | |
| 29 | expect(Array.isArray(all)).toBe(true); |
| 30 | // Reserved AWS-managed providers must be filtered out. |
| 31 | expect(all.some((p) => p.name === "FARGATE")).toBe(false); |
| 32 | expect(all.some((p) => p.name === "FARGATE_SPOT")).toBe(false); |
| 33 | // Every returned item carries the full Attributes shape. |
| 34 | for (const p of all) { |
| 35 | expect(typeof p.name).toBe("string"); |
| 36 | expect(p.capacityProviderArn).toMatch(/^arn:aws:ecs:[^:]+:\d+:capacity-provider\//); |
| 37 | } |
| 38 | }).pipe(logLevel), |
| 39 | { tags: ["provider:aws", "provider:aws:ecs", "live"], timeout: 120_000 }, |
| 40 | ); |
| 41 | |
| 42 | // Full deploy + list assertion. Requires a pre-provisioned EC2 Auto Scaling |
| 43 | // Group ARN (set via TEST_ASG_ARN): a real capacity provider cannot be created |
| 44 | // without an ASG, and the distilled `auto-scaling` service is currently |
| 45 | // non-functional against the live API (aws-query Action derivation bug — see |
| 46 | // AutoScalingGroup.test.ts), so an ASG cannot be provisioned in-test. Run with: |
| 47 | // TEST_ASG_ARN=arn:aws:autoscaling:... bun vitest CapacityProvider |
| 48 | test.provider.skipIf(!process.env.TEST_ASG_ARN)( |
| 49 | "list includes a deployed capacity provider", |
| 50 | (stack) => |
| 51 | Effect.gen(function* () { |
| 52 | const autoScalingGroupArn = process.env.TEST_ASG_ARN!; |
| 53 | yield* stack.destroy(); |
| 54 | |
| 55 | const deployed = yield* stack.deploy( |
| 56 | CapacityProvider("ListCapacityProvider", { |
| 57 | autoScalingGroupArn, |
| 58 | managedScaling: { |
| 59 | status: "ENABLED", |
| 60 | targetCapacity: 80, |
| 61 | minimumScalingStepSize: 1, |
| 62 | maximumScalingStepSize: 10, |
| 63 | }, |
| 64 | managedTerminationProtection: "DISABLED", |
| 65 | tags: { env: "test" }, |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | const provider = yield* Provider.findProvider(CapacityProvider); |
| 70 | const all = yield* provider.list(); |
| 71 | |
| 72 | expect(all.some((p) => p.name === deployed.name)).toBe(true); |
| 73 | expect(all.some((p) => p.capacityProviderArn === deployed.capacityProviderArn)).toBe(true); |
| 74 | |
| 75 | yield* stack.destroy(); |
| 76 | }).pipe(logLevel), |
| 77 | { tags: ["provider:aws", "provider:aws:ecs", "live"], timeout: 600_000 }, |
| 78 | ); |
| 79 | |
| 80 | // A deleted capacity provider stays describable as INACTIVE for a while and |
| 81 | // can no longer be updated. Removing a provider and adding it back (same |
| 82 | // deterministic name) must create a fresh one rather than try to update the |
| 83 | // INACTIVE leftover. The ASG is sized to zero, so no instance launches. |
| 84 | test.provider( |
| 85 | "recreates a capacity provider that was removed", |
| 86 | (stack) => |
| 87 | Effect.gen(function* () { |
| 88 | yield* stack.destroy(); |
| 89 | const { subnetIds } = yield* getDefaultVpcNetwork; |
| 90 | // Explicit, stage-scoped name: ECS rejects names prefixed with |
| 91 | // `aws`/`ecs`/`fargate`, which a generated name for this suite would be. |
| 92 | const providerName = `alchemy-recreated-${stack.stage.toLowerCase().replace(/[^a-z0-9_-]/g, "-")}`; |
| 93 | |
| 94 | const program = (withProvider: boolean) => |
| 95 | Effect.gen(function* () { |
| 96 | const template = yield* LaunchTemplate("CapacityTemplate", { |
| 97 | imageId: amazonLinux2023(), |
| 98 | instanceType: "t3.micro", |
| 99 | }); |
| 100 | const group = yield* AutoScalingGroup("CapacityGroup", { |
| 101 | launchTemplate: template, |
| 102 | subnetIds: [subnetIds[0] as `subnet-${string}`], |
| 103 | minSize: 0, |
| 104 | maxSize: 0, |
| 105 | desiredCapacity: 0, |
| 106 | }); |
| 107 | const provider = withProvider |
| 108 | ? yield* CapacityProvider("RecreatedProvider", { |
| 109 | name: providerName, |
| 110 | autoScalingGroupArn: group.autoScalingGroupArn, |
| 111 | managedTerminationProtection: "DISABLED", |
| 112 | }) |
| 113 | : undefined; |
| 114 | return { provider }; |
| 115 | }); |
| 116 | |
| 117 | const first = yield* stack.deploy(program(true)); |
| 118 | const name = first.provider!.name; |
| 119 | |
| 120 | yield* stack.deploy(program(false)); |
| 121 | const removed = yield* ecs.describeCapacityProviders({ capacityProviders: [name] }); |
| 122 | expect(removed.capacityProviders?.every((p) => p.status !== "ACTIVE")).toBe(true); |
| 123 | |
| 124 | const second = yield* stack.deploy(program(true)); |
| 125 | expect(second.provider!.name).toBe(name); |
| 126 | expect(second.provider!.status).toBe("ACTIVE"); |
| 127 | const live = yield* ecs.describeCapacityProviders({ capacityProviders: [name] }); |
| 128 | expect(live.capacityProviders?.some((p) => p.status === "ACTIVE")).toBe(true); |
| 129 | |
| 130 | yield* stack.destroy(); |
| 131 | }).pipe(logLevel), |
| 132 | { |
| 133 | tags: ["provider:aws", "provider:aws:autoscaling", "provider:aws:ecs", "live"], |
| 134 | timeout: 300_000, |
| 135 | }, |
| 136 | ); |