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, ScheduledAction } from "@alchemy.run/aws/AutoScaling"; |
| 3 | import { amazonLinux2023 } from "@alchemy.run/aws/EC2"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as autoscaling from "@distilled.cloud/aws/auto-scaling"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { getAutoScalingTestSubnetId } from "./TestNetwork.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: AWS.providers() }); |
| 12 | |
| 13 | // Deterministic, account-unique names so leftover cleanup from an interrupted |
| 14 | // run works. The action uses a cron `recurrence` (no StartTime) so nothing is |
| 15 | // scheduled in the past and no instances launch (min/max/desired 0). |
| 16 | const asgName = "alchemy-test-scheduled-action-asg"; |
| 17 | const actionName = "alchemy-test-scheduled-action"; |
| 18 | |
| 19 | const cleanupAsg = autoscaling |
| 20 | .deleteAutoScalingGroup({ AutoScalingGroupName: asgName, ForceDelete: true }) |
| 21 | .pipe(Effect.catch(() => Effect.void)); |
| 22 | |
| 23 | // `describeScheduledActions` for a deleted Auto Scaling Group raises AWS wire |
| 24 | // code `ValidationError` ("AutoScalingGroup ... not found"), typed as |
| 25 | // `AutoScalingGroupNotFound` via the auto-scaling distilled patch. Treat it as |
| 26 | // "action gone" for the out-of-band assertions after teardown. |
| 27 | const describeAction = autoscaling |
| 28 | .describeScheduledActions({ |
| 29 | AutoScalingGroupName: asgName, |
| 30 | ScheduledActionNames: [actionName], |
| 31 | } as any) |
| 32 | .pipe( |
| 33 | Effect.map((r) => r.ScheduledUpdateGroupActions?.[0]), |
| 34 | Effect.catchTag("AutoScalingGroupNotFound", () => Effect.succeed(undefined)), |
| 35 | ); |
| 36 | |
| 37 | test.provider( |
| 38 | "creates, updates, and deletes a scheduled action on an ASG", |
| 39 | (stack) => |
| 40 | Effect.gen(function* () { |
| 41 | yield* cleanupAsg; |
| 42 | yield* stack.destroy(); |
| 43 | |
| 44 | const imageId = amazonLinux2023(); |
| 45 | |
| 46 | const subnetId = yield* getAutoScalingTestSubnetId; |
| 47 | |
| 48 | const deployAction = (recurrence: string, desiredCapacity: number) => |
| 49 | stack.deploy( |
| 50 | Effect.gen(function* () { |
| 51 | const template = yield* LaunchTemplate("ScheduleTemplate", { |
| 52 | imageId, |
| 53 | instanceType: "t3.micro", |
| 54 | }); |
| 55 | const group = yield* AutoScalingGroup("ScheduleGroup", { |
| 56 | autoScalingGroupName: asgName, |
| 57 | launchTemplate: template, |
| 58 | subnetIds: [subnetId], |
| 59 | minSize: 0, |
| 60 | maxSize: 0, |
| 61 | desiredCapacity: 0, |
| 62 | }); |
| 63 | return yield* ScheduledAction("Action", { |
| 64 | scheduledActionName: actionName, |
| 65 | autoScalingGroup: group.autoScalingGroupName, |
| 66 | recurrence, |
| 67 | timeZone: "America/New_York", |
| 68 | minSize: 0, |
| 69 | maxSize: 3, |
| 70 | desiredCapacity, |
| 71 | }); |
| 72 | }), |
| 73 | ); |
| 74 | |
| 75 | // Create — daily 09:00 recurrence. |
| 76 | const created = yield* deployAction("0 9 * * *", 1); |
| 77 | expect(created.scheduledActionName).toEqual(actionName); |
| 78 | expect(created.autoScalingGroupName).toEqual(asgName); |
| 79 | expect(created.recurrence).toEqual("0 9 * * *"); |
| 80 | expect(created.timeZone).toEqual("America/New_York"); |
| 81 | expect(created.minSize).toEqual(0); |
| 82 | expect(created.maxSize).toEqual(3); |
| 83 | expect(created.desiredCapacity).toEqual(1); |
| 84 | expect(created.scheduledActionARN).toMatch(/^arn:aws:autoscaling:/); |
| 85 | |
| 86 | const liveCreated = yield* describeAction; |
| 87 | expect(liveCreated?.ScheduledActionName).toEqual(actionName); |
| 88 | expect(liveCreated?.Recurrence).toEqual("0 9 * * *"); |
| 89 | expect(liveCreated?.DesiredCapacity).toEqual(1); |
| 90 | |
| 91 | // Update the recurrence + desired capacity in place (same name → no |
| 92 | // replacement). |
| 93 | const updated = yield* deployAction("0 18 * * *", 2); |
| 94 | expect(updated.scheduledActionName).toEqual(actionName); |
| 95 | expect(updated.recurrence).toEqual("0 18 * * *"); |
| 96 | expect(updated.desiredCapacity).toEqual(2); |
| 97 | |
| 98 | const liveUpdated = yield* describeAction; |
| 99 | expect(liveUpdated?.Recurrence).toEqual("0 18 * * *"); |
| 100 | expect(liveUpdated?.DesiredCapacity).toEqual(2); |
| 101 | |
| 102 | // Delete via stack teardown; assert the action is gone out-of-band. |
| 103 | yield* stack.destroy(); |
| 104 | const gone = yield* describeAction.pipe( |
| 105 | Effect.repeat({ |
| 106 | until: (action) => action === undefined, |
| 107 | schedule: Schedule.spaced("2 seconds"), |
| 108 | times: 8, |
| 109 | }), |
| 110 | ); |
| 111 | expect(gone).toBeUndefined(); |
| 112 | |
| 113 | // The ASG itself is deleted by the stack teardown too — prove the suite |
| 114 | // left nothing behind. |
| 115 | const groupsLeft = yield* autoscaling |
| 116 | .describeAutoScalingGroups({ AutoScalingGroupNames: [asgName] } as any) |
| 117 | .pipe( |
| 118 | Effect.map((r) => (r.AutoScalingGroups ?? []).length), |
| 119 | Effect.repeat({ |
| 120 | until: (count) => count === 0, |
| 121 | schedule: Schedule.spaced("3 seconds"), |
| 122 | times: 10, |
| 123 | }), |
| 124 | ); |
| 125 | expect(groupsLeft).toBe(0); |
| 126 | }).pipe(Effect.ensuring(cleanupAsg)), |
| 127 | { |
| 128 | tags: ["provider:aws", "provider:aws:autoscaling", "provider:aws:ec2", "live"], |
| 129 | timeout: 240_000, |
| 130 | }, |
| 131 | ); |