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, Subnet, Vpc } from "@alchemy.run/aws/EC2"; |
| 4 | import { InstanceProfile, Role } from "@alchemy.run/aws/IAM"; |
| 5 | import { expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import * as autoscaling from "@distilled.cloud/aws/auto-scaling"; |
| 8 | import * as ec2 from "@distilled.cloud/aws/ec2"; |
| 9 | import * as Provider from "alchemy/Provider"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import { getAutoScalingTestSubnetId, getTestAmiId } from "./TestNetwork.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: AWS.providers() }); |
| 15 | |
| 16 | // Out-of-band proof that an Auto Scaling Group is fully deleted after the |
| 17 | // trailing stack.destroy(): describeAutoScalingGroups with a name filter |
| 18 | // returns an empty list once deletion completes (bounded poll — the provider's |
| 19 | // delete already waits, this is a cheap final confirmation). |
| 20 | const assertGroupGone = (name: string) => |
| 21 | autoscaling.describeAutoScalingGroups({ AutoScalingGroupNames: [name] } as any).pipe( |
| 22 | Effect.map((r) => (r.AutoScalingGroups ?? []).length), |
| 23 | Effect.repeat({ |
| 24 | until: (count) => count === 0, |
| 25 | schedule: Schedule.spaced("3 seconds"), |
| 26 | times: 10, |
| 27 | }), |
| 28 | Effect.map((count) => expect(count).toBe(0)), |
| 29 | ); |
| 30 | |
| 31 | const launchTemplateName = "alchemy-test-asg-lt-oob"; |
| 32 | |
| 33 | // The launch template is created out-of-band via distilled `ec2` rather than the |
| 34 | // `AWS.AutoScaling.LaunchTemplate` resource: that resource's read-before-create |
| 35 | // path currently dies on an untyped `ec2` NotFound (`describeLaunchTemplates` |
| 36 | // does not declare `InvalidLaunchTemplateName.NotFoundException`), which is a gap |
| 37 | // in a different provider. Creating the template directly isolates this test to |
| 38 | // the ASG `list()` operation under test. |
| 39 | const cleanupLaunchTemplate = ec2 |
| 40 | .deleteLaunchTemplate({ LaunchTemplateName: launchTemplateName } as any) |
| 41 | .pipe(Effect.catch(() => Effect.void)); |
| 42 | |
| 43 | // `list()` enumerates every Auto Scaling Group in the account/region via the |
| 44 | // paginated `autoscaling.describeAutoScalingGroups` op. Deploy a real ASG (sized |
| 45 | // to zero so no EC2 instances launch), resolve the provider from context via the |
| 46 | // typed `findProvider`, call `list()`, and assert the deployed group appears in |
| 47 | // the exhaustively paginated result. |
| 48 | test.provider( |
| 49 | "list enumerates the deployed auto scaling group", |
| 50 | (stack) => |
| 51 | Effect.gen(function* () { |
| 52 | yield* stack.destroy(); |
| 53 | |
| 54 | // The launch template is created out-of-band with the raw SDK, so the |
| 55 | // AMI id must be a plain string — `amazonLinux2023()` returns an |
| 56 | // Output, which only resolves inside `stack.deploy`. |
| 57 | const imageId = yield* getTestAmiId; |
| 58 | |
| 59 | yield* cleanupLaunchTemplate; |
| 60 | yield* ec2.createLaunchTemplate({ |
| 61 | LaunchTemplateName: launchTemplateName, |
| 62 | LaunchTemplateData: { ImageId: imageId, InstanceType: "t3.micro" }, |
| 63 | } as any); |
| 64 | |
| 65 | const group = yield* stack.deploy( |
| 66 | Effect.gen(function* () { |
| 67 | const vpc = yield* Vpc("TestVpc", { cidrBlock: "10.0.0.0/16" }); |
| 68 | const subnet = yield* Subnet("TestSubnet", { |
| 69 | vpcId: vpc.vpcId, |
| 70 | cidrBlock: "10.0.1.0/24", |
| 71 | }); |
| 72 | return yield* AutoScalingGroup("ListAutoScalingGroup", { |
| 73 | autoScalingGroupName: "alchemy-test-asg-list", |
| 74 | launchTemplate: { launchTemplateName }, |
| 75 | subnetIds: [subnet.subnetId], |
| 76 | minSize: 0, |
| 77 | maxSize: 0, |
| 78 | desiredCapacity: 0, |
| 79 | }); |
| 80 | }), |
| 81 | ); |
| 82 | |
| 83 | const provider = yield* Provider.findProvider(AutoScalingGroup); |
| 84 | const all = yield* provider.list(); |
| 85 | |
| 86 | expect(all.some((g) => g.autoScalingGroupName === group.autoScalingGroupName)).toBe(true); |
| 87 | |
| 88 | yield* stack.destroy(); |
| 89 | yield* assertGroupGone("alchemy-test-asg-list"); |
| 90 | }).pipe(Effect.ensuring(cleanupLaunchTemplate)), |
| 91 | { |
| 92 | tags: ["provider:aws", "provider:aws:autoscaling", "provider:aws:ec2", "live"], |
| 93 | timeout: 240_000, |
| 94 | }, |
| 95 | ); |
| 96 | |
| 97 | // Whole-resource `launchTemplate: template` spelling. The engine resolves the |
| 98 | // resource reference to its bare Attributes record before it reaches the |
| 99 | // provider — no resource `Type` marker survives — so `toLaunchTemplateSpec` |
| 100 | // must narrow on the attributes shape. Pre-fix it fell through to the |
| 101 | // reference branch, which sent BOTH LaunchTemplateId and LaunchTemplateName |
| 102 | // to `createAutoScalingGroup` (AWS rejects a spec carrying both) with the |
| 103 | // version defaulted to `$Default` instead of the template's resolved default |
| 104 | // version. |
| 105 | // |
| 106 | // The fleet is placed into a subnet of the account's default VPC (resolved |
| 107 | // out-of-band) rather than a throwaway `Vpc` resource: a failed run loses its |
| 108 | // in-memory scratch state, and orphaned VPCs quickly exhaust the 5-per-region |
| 109 | // quota. |
| 110 | const wholeAsgName = "alchemy-test-asg-whole-lt"; |
| 111 | const cleanupWholeAsg = autoscaling |
| 112 | .deleteAutoScalingGroup({ |
| 113 | AutoScalingGroupName: wholeAsgName, |
| 114 | ForceDelete: true, |
| 115 | } as any) |
| 116 | .pipe(Effect.catch(() => Effect.void)); |
| 117 | |
| 118 | test.provider( |
| 119 | "wires a whole LaunchTemplate resource by id and pinned default version", |
| 120 | (stack) => |
| 121 | Effect.gen(function* () { |
| 122 | yield* cleanupWholeAsg; |
| 123 | yield* stack.destroy(); |
| 124 | |
| 125 | // Launch templates do not validate the AMI at creation time; fall back |
| 126 | // to a syntactically valid id if the lookup returns nothing. |
| 127 | const imageId = amazonLinux2023(); |
| 128 | |
| 129 | const subnetId = yield* getAutoScalingTestSubnetId; |
| 130 | |
| 131 | const deployed = yield* stack.deploy( |
| 132 | Effect.gen(function* () { |
| 133 | const template = yield* LaunchTemplate("WholeLtTemplate", { |
| 134 | imageId, |
| 135 | instanceType: "t3.micro", |
| 136 | }); |
| 137 | const group = yield* AutoScalingGroup("WholeLtGroup", { |
| 138 | autoScalingGroupName: wholeAsgName, |
| 139 | // Whole resource — resolves to bare Attributes at deploy time. |
| 140 | launchTemplate: template, |
| 141 | subnetIds: [subnetId], |
| 142 | minSize: 0, |
| 143 | maxSize: 0, |
| 144 | desiredCapacity: 0, |
| 145 | // Duration.Input props — whole seconds on the wire. |
| 146 | defaultCooldown: "45 seconds", |
| 147 | healthCheckGracePeriod: "2 minutes", |
| 148 | }); |
| 149 | return { |
| 150 | templateId: template.launchTemplateId.as<string>(), |
| 151 | templateDefaultVersion: template.defaultVersionNumber.as<number>(), |
| 152 | group, |
| 153 | }; |
| 154 | }), |
| 155 | ); |
| 156 | |
| 157 | // The group is wired to the template by ID with the version pinned to |
| 158 | // the template's resolved default version (not "$Default"). |
| 159 | expect(deployed.group.launchTemplateId).toEqual(deployed.templateId); |
| 160 | expect(deployed.group.launchTemplateVersion).toEqual(String(deployed.templateDefaultVersion)); |
| 161 | |
| 162 | // Duration.Input props round-trip as whole seconds in the attributes. |
| 163 | expect(deployed.group.defaultCooldown).toEqual(45); |
| 164 | expect(deployed.group.healthCheckGracePeriod).toEqual(120); |
| 165 | |
| 166 | // Out-of-band: the live group carries the id-only spec and the |
| 167 | // Duration-derived second counts on the wire. |
| 168 | const described = yield* autoscaling.describeAutoScalingGroups({ |
| 169 | AutoScalingGroupNames: [wholeAsgName], |
| 170 | } as any); |
| 171 | const live = described.AutoScalingGroups?.[0]; |
| 172 | expect(live?.LaunchTemplate?.LaunchTemplateId).toEqual(deployed.templateId); |
| 173 | expect(live?.DefaultCooldown).toEqual(45); |
| 174 | expect(live?.HealthCheckGracePeriod).toEqual(120); |
| 175 | |
| 176 | yield* stack.destroy(); |
| 177 | yield* assertGroupGone(wholeAsgName); |
| 178 | }).pipe(Effect.ensuring(cleanupWholeAsg)), |
| 179 | { |
| 180 | tags: ["provider:aws", "provider:aws:autoscaling", "provider:aws:ec2", "live"], |
| 181 | timeout: 240_000, |
| 182 | }, |
| 183 | ); |
| 184 | |
| 185 | // An omitted `desiredCapacity` belongs to whoever scales the group (ECS |
| 186 | // managed scaling, a scaling policy): redeploys must not reset it to |
| 187 | // `minSize`. The launch template names an instance profile created in the |
| 188 | // same deploy, which IAM may not have propagated yet (`InvalidIamInstanceProfile` |
| 189 | // is retried). `Launch` is suspended before scaling out so no instance starts. |
| 190 | test.provider( |
| 191 | "keeps an externally scaled desired capacity across redeploys", |
| 192 | (stack) => |
| 193 | Effect.gen(function* () { |
| 194 | yield* stack.destroy(); |
| 195 | const subnetId = yield* getAutoScalingTestSubnetId; |
| 196 | |
| 197 | const program = (maxSize: number) => |
| 198 | Effect.gen(function* () { |
| 199 | const role = yield* Role("ScaledGroupRole", { |
| 200 | assumeRolePolicyDocument: { |
| 201 | Version: "2012-10-17", |
| 202 | Statement: [ |
| 203 | { |
| 204 | Effect: "Allow", |
| 205 | Principal: { Service: "ec2.amazonaws.com" }, |
| 206 | Action: ["sts:AssumeRole"], |
| 207 | }, |
| 208 | ], |
| 209 | }, |
| 210 | }); |
| 211 | const profile = yield* InstanceProfile("ScaledGroupProfile", { |
| 212 | roleName: role.roleName, |
| 213 | }); |
| 214 | const template = yield* LaunchTemplate("ScaledGroupTemplate", { |
| 215 | imageId: amazonLinux2023(), |
| 216 | instanceType: "t3.micro", |
| 217 | instanceProfileName: profile.instanceProfileName, |
| 218 | }); |
| 219 | return yield* AutoScalingGroup("ScaledGroup", { |
| 220 | launchTemplate: template, |
| 221 | subnetIds: [subnetId], |
| 222 | minSize: 0, |
| 223 | maxSize, |
| 224 | }); |
| 225 | }); |
| 226 | |
| 227 | const describe = (name: string) => |
| 228 | autoscaling |
| 229 | .describeAutoScalingGroups({ AutoScalingGroupNames: [name] } as any) |
| 230 | .pipe(Effect.map((r) => r.AutoScalingGroups?.[0])); |
| 231 | |
| 232 | const created = yield* stack.deploy(program(1)); |
| 233 | const name = created.autoScalingGroupName; |
| 234 | expect((yield* describe(name))?.DesiredCapacity).toBe(0); |
| 235 | |
| 236 | // An external scaler raises the desired capacity. |
| 237 | yield* autoscaling.suspendProcesses({ |
| 238 | AutoScalingGroupName: name, |
| 239 | ScalingProcesses: ["Launch"], |
| 240 | } as any); |
| 241 | yield* autoscaling.setDesiredCapacity({ |
| 242 | AutoScalingGroupName: name, |
| 243 | DesiredCapacity: 1, |
| 244 | } as any); |
| 245 | |
| 246 | // A redeploy that updates the group leaves the live value alone. |
| 247 | yield* stack.deploy(program(2)); |
| 248 | const live = yield* describe(name); |
| 249 | expect(live?.MaxSize).toBe(2); |
| 250 | expect(live?.DesiredCapacity).toBe(1); |
| 251 | |
| 252 | yield* stack.destroy(); |
| 253 | yield* assertGroupGone(name); |
| 254 | }), |
| 255 | { |
| 256 | tags: [ |
| 257 | "provider:aws", |
| 258 | "provider:aws:autoscaling", |
| 259 | "provider:aws:ec2", |
| 260 | "provider:aws:iam", |
| 261 | "live", |
| 262 | ], |
| 263 | timeout: 300_000, |
| 264 | }, |
| 265 | ); |
| 266 | |
| 267 | // `instanceRequirements` moves the launch template into a mixed instances |
| 268 | // policy with one attribute-based override; dropping it switches the group |
| 269 | // back to the plain launch template, and adding it again switches back. |
| 270 | // `availabilityZoneDistribution` is diffed against the live group and resets |
| 271 | // to the AWS default when removed. The group stays at zero capacity, so no |
| 272 | // instance launches. |
| 273 | test.provider( |
| 274 | "switches between instance requirements and a plain launch template", |
| 275 | (stack) => |
| 276 | Effect.gen(function* () { |
| 277 | yield* stack.destroy(); |
| 278 | const subnetId = yield* getAutoScalingTestSubnetId; |
| 279 | |
| 280 | const instanceRequirements = { |
| 281 | VCpuCount: { Min: 1, Max: 2 }, |
| 282 | MemoryMiB: { Min: 512, Max: 4096 }, |
| 283 | BurstablePerformance: "required", |
| 284 | } as const; |
| 285 | |
| 286 | const program = (mixed: boolean) => |
| 287 | Effect.gen(function* () { |
| 288 | const template = yield* LaunchTemplate("RequirementsTemplate", { |
| 289 | imageId: amazonLinux2023(), |
| 290 | instanceType: "t3.micro", |
| 291 | }); |
| 292 | return yield* AutoScalingGroup("RequirementsGroup", { |
| 293 | launchTemplate: template, |
| 294 | subnetIds: [subnetId], |
| 295 | minSize: 0, |
| 296 | maxSize: 1, |
| 297 | desiredCapacity: 0, |
| 298 | ...(mixed |
| 299 | ? { |
| 300 | instanceRequirements, |
| 301 | availabilityZoneDistribution: { |
| 302 | CapacityDistributionStrategy: "balanced-only", |
| 303 | }, |
| 304 | } |
| 305 | : {}), |
| 306 | }); |
| 307 | }); |
| 308 | |
| 309 | const describe = (name: string) => |
| 310 | autoscaling |
| 311 | .describeAutoScalingGroups({ AutoScalingGroupNames: [name] }) |
| 312 | .pipe(Effect.map((r) => r.AutoScalingGroups?.[0])); |
| 313 | |
| 314 | const expectMixed = Effect.fn(function* (group: AutoScalingGroup["Attributes"]) { |
| 315 | expect(group.instanceRequirements?.VCpuCount).toEqual({ Min: 1, Max: 2 }); |
| 316 | expect(group.instanceRequirements?.BurstablePerformance).toEqual("required"); |
| 317 | expect(group.availabilityZoneDistribution?.CapacityDistributionStrategy).toEqual( |
| 318 | "balanced-only", |
| 319 | ); |
| 320 | expect(group.launchTemplateId).toMatch(/^lt-/); |
| 321 | |
| 322 | const live = yield* describe(group.autoScalingGroupName); |
| 323 | expect(live?.LaunchTemplate).toBeUndefined(); |
| 324 | const mixedTemplate = live?.MixedInstancesPolicy?.LaunchTemplate; |
| 325 | expect(mixedTemplate?.LaunchTemplateSpecification?.LaunchTemplateId).toEqual( |
| 326 | group.launchTemplateId, |
| 327 | ); |
| 328 | expect(mixedTemplate?.Overrides?.[0]?.InstanceRequirements?.VCpuCount).toEqual({ |
| 329 | Min: 1, |
| 330 | Max: 2, |
| 331 | }); |
| 332 | expect(mixedTemplate?.Overrides?.[0]?.InstanceRequirements?.MemoryMiB).toEqual({ |
| 333 | Min: 512, |
| 334 | Max: 4096, |
| 335 | }); |
| 336 | expect(live?.AvailabilityZoneDistribution?.CapacityDistributionStrategy).toEqual( |
| 337 | "balanced-only", |
| 338 | ); |
| 339 | }); |
| 340 | |
| 341 | const created = yield* stack.deploy(program(true)); |
| 342 | yield* expectMixed(created); |
| 343 | |
| 344 | // An unchanged redeploy plans no update. |
| 345 | const unchanged = yield* stack.plan(program(true)); |
| 346 | expect(unchanged.resources.RequirementsGroup?.action).toBe("noop"); |
| 347 | |
| 348 | // Drop the requirements: back to the plain launch template, default |
| 349 | // zone distribution. |
| 350 | const plain = yield* stack.deploy(program(false)); |
| 351 | expect(plain.instanceRequirements).toBeUndefined(); |
| 352 | expect(plain.launchTemplateId).toEqual(created.launchTemplateId); |
| 353 | const livePlain = yield* describe(plain.autoScalingGroupName); |
| 354 | expect(livePlain?.MixedInstancesPolicy).toBeUndefined(); |
| 355 | expect(livePlain?.LaunchTemplate?.LaunchTemplateId).toEqual(created.launchTemplateId); |
| 356 | expect( |
| 357 | livePlain?.AvailabilityZoneDistribution?.CapacityDistributionStrategy ?? |
| 358 | "balanced-best-effort", |
| 359 | ).toEqual("balanced-best-effort"); |
| 360 | |
| 361 | // And back to the mixed instances policy. |
| 362 | const again = yield* stack.deploy(program(true)); |
| 363 | yield* expectMixed(again); |
| 364 | |
| 365 | yield* stack.destroy(); |
| 366 | yield* assertGroupGone(again.autoScalingGroupName); |
| 367 | }), |
| 368 | { |
| 369 | tags: ["provider:aws", "provider:aws:autoscaling", "provider:aws:ec2", "live"], |
| 370 | timeout: 300_000, |
| 371 | }, |
| 372 | ); |