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 { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Core from "@alchemy.run/test/Core"; |
| 5 | import * as autoscaling from "@distilled.cloud/aws/auto-scaling"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import LifecycleTestFunctionLive, { |
| 11 | LifecycleTestFunction, |
| 12 | lifecycleFleetAsgName, |
| 13 | lifecycleHookName, |
| 14 | } from "./fixtures/lifecycle-handler.ts"; |
| 15 | |
| 16 | const testOptions = { providers: AWS.providers() }; |
| 17 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 18 | const sharedStack = Core.scratchStack(testOptions, "AutoScalingLifecycle"); |
| 19 | |
| 20 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 21 | |
| 22 | let baseUrl: string; |
| 23 | |
| 24 | // Deploys a Lambda that (a) creates a LAUNCHING lifecycle hook + EventBridge |
| 25 | // subscription via `consumeLifecycleActions` and (b) binds |
| 26 | // `CompleteLifecycleAction`. The fleet is sized to zero so no EC2 instances |
| 27 | // launch — this verifies the DX wiring (hook creation + binding IAM) without a |
| 28 | // >90s instance-launch round-trip. The full "instance pauses → Lambda completes |
| 29 | // CONTINUE → InService" flow is left to a live run with a non-zero desired |
| 30 | // capacity. |
| 31 | describe( |
| 32 | "AutoScaling LifecycleHook event source + CompleteLifecycleAction", |
| 33 | { |
| 34 | tags: [ |
| 35 | "provider:aws", |
| 36 | "provider:aws:autoscaling", |
| 37 | "provider:aws:ec2", |
| 38 | "provider:aws:lambda", |
| 39 | "live", |
| 40 | ], |
| 41 | }, |
| 42 | () => { |
| 43 | beforeAll( |
| 44 | Effect.gen(function* () { |
| 45 | yield* sharedStack.destroy(); |
| 46 | |
| 47 | const { fn } = yield* sharedStack.deploy( |
| 48 | Effect.gen(function* () { |
| 49 | const fn = yield* LifecycleTestFunction; |
| 50 | return { fn }; |
| 51 | }).pipe(Effect.provide(LifecycleTestFunctionLive)), |
| 52 | ); |
| 53 | |
| 54 | expect(fn.functionUrl).toBeTruthy(); |
| 55 | baseUrl = fn.functionUrl!.replace(/\/+$/, ""); |
| 56 | |
| 57 | yield* HttpClient.get(`${baseUrl}/health`).pipe( |
| 58 | Effect.flatMap((response) => |
| 59 | response.status === 200 |
| 60 | ? Effect.succeed(response) |
| 61 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 62 | ), |
| 63 | Effect.retry({ schedule: readinessPolicy }), |
| 64 | ); |
| 65 | }), |
| 66 | { timeout: 300_000 }, |
| 67 | ); |
| 68 | |
| 69 | afterAll( |
| 70 | Effect.gen(function* () { |
| 71 | yield* sharedStack.destroy(); |
| 72 | // Assert the fixture ASG is fully gone — the suite must leave nothing. |
| 73 | // The hook context has no AWS providers of its own, so provide them |
| 74 | // explicitly for the out-of-band distilled call. |
| 75 | const remaining = yield* Core.withProviders( |
| 76 | autoscaling |
| 77 | .describeAutoScalingGroups({ AutoScalingGroupNames: [lifecycleFleetAsgName] } as any) |
| 78 | .pipe( |
| 79 | Effect.map((r) => (r.AutoScalingGroups ?? []).length), |
| 80 | Effect.repeat({ |
| 81 | until: (count) => count === 0, |
| 82 | schedule: Schedule.spaced("3 seconds"), |
| 83 | times: 10, |
| 84 | }), |
| 85 | ), |
| 86 | testOptions, |
| 87 | sharedStack.name, |
| 88 | ); |
| 89 | expect(remaining).toBe(0); |
| 90 | }), |
| 91 | { timeout: 180_000 }, |
| 92 | ); |
| 93 | |
| 94 | test.provider( |
| 95 | "consumeLifecycleActions creates the launch lifecycle hook on the ASG", |
| 96 | (_stack) => |
| 97 | Effect.gen(function* () { |
| 98 | const hooks = yield* autoscaling.describeLifecycleHooks({ |
| 99 | AutoScalingGroupName: lifecycleFleetAsgName, |
| 100 | LifecycleHookNames: [lifecycleHookName], |
| 101 | } as any); |
| 102 | const hook = hooks.LifecycleHooks?.[0]; |
| 103 | expect(hook?.LifecycleHookName).toEqual(lifecycleHookName); |
| 104 | expect(hook?.LifecycleTransition).toEqual("autoscaling:EC2_INSTANCE_LAUNCHING"); |
| 105 | expect(hook?.HeartbeatTimeout).toEqual(300); |
| 106 | }), |
| 107 | ); |
| 108 | |
| 109 | test.provider( |
| 110 | "CompleteLifecycleAction binding is IAM-wired (no AccessDenied)", |
| 111 | (_stack) => |
| 112 | Effect.gen(function* () { |
| 113 | // A freshly attached IAM policy is eventually consistent — the first |
| 114 | // invocations after deploy can still see AccessDenied. Poll (bounded) |
| 115 | // until the call stops failing authorization, then assert. |
| 116 | const body = yield* HttpClient.execute( |
| 117 | HttpClientRequest.post(`${baseUrl}/complete-bogus`), |
| 118 | ).pipe( |
| 119 | Effect.flatMap((response) => |
| 120 | response.status === 200 |
| 121 | ? response.json |
| 122 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 123 | ), |
| 124 | Effect.map((json) => json as { ok: boolean; tag: string }), |
| 125 | // `repeat` only re-runs successes: a transient 502 from the |
| 126 | // Function URL (cold start right after deploy) must be retried |
| 127 | // too, the way the /health probe is. |
| 128 | Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: 10 }), |
| 129 | Effect.repeat({ |
| 130 | until: (b) => b.tag !== "AccessDenied" && b.tag !== "AccessDeniedException", |
| 131 | schedule: Schedule.spaced("3 seconds"), |
| 132 | times: 10, |
| 133 | }), |
| 134 | ); |
| 135 | // The SDK call reached AWS with valid credentials/policy: either it |
| 136 | // succeeded, or it failed for a resource reason (no active lifecycle |
| 137 | // action) — never an authorization failure. |
| 138 | expect(["AccessDenied", "AccessDeniedException"]).not.toContain(body.tag); |
| 139 | }), |
| 140 | { timeout: 60_000 }, |
| 141 | ); |
| 142 | }, |
| 143 | ); |