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 { Bucket } from "@alchemy.run/aws/S3"; |
| 3 | import { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as Core from "@alchemy.run/test/Core"; |
| 6 | import * as s3 from "@distilled.cloud/aws/s3"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import ElbBindingsFunctionLive, { |
| 12 | bundleKey, |
| 13 | CA_BUNDLE_PEM, |
| 14 | ElbBindingsFunction, |
| 15 | testTargetIp, |
| 16 | } from "./fixtures/bindings-handler.ts"; |
| 17 | |
| 18 | const testOptions = { providers: AWS.providers() }; |
| 19 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 20 | const sharedStack = Core.scratchStack(testOptions, "ELBv2Bindings"); |
| 21 | |
| 22 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 23 | |
| 24 | let baseUrl: string; |
| 25 | |
| 26 | interface RouteResult { |
| 27 | ok: boolean; |
| 28 | tag: string; |
| 29 | message?: string; |
| 30 | targets?: { id?: string; state?: string }[]; |
| 31 | states?: string[]; |
| 32 | location?: string; |
| 33 | } |
| 34 | |
| 35 | // Call a fixture route, repeating (bounded) while the response still shows an |
| 36 | // authorization failure — a freshly attached IAM policy is eventually |
| 37 | // consistent and the first invocations after deploy can see AccessDenied. |
| 38 | const callRoute = (method: "GET" | "POST", path: string) => |
| 39 | Effect.gen(function* () { |
| 40 | const request = |
| 41 | method === "GET" |
| 42 | ? HttpClientRequest.get(`${baseUrl}${path}`) |
| 43 | : HttpClientRequest.post(`${baseUrl}${path}`); |
| 44 | return yield* HttpClient.execute(request).pipe( |
| 45 | Effect.flatMap((response) => |
| 46 | response.status === 200 |
| 47 | ? response.json |
| 48 | : Effect.fail(new Error(`Route ${path} not ready: ${response.status}`)), |
| 49 | ), |
| 50 | Effect.map((json) => json as unknown as RouteResult), |
| 51 | Effect.repeat({ |
| 52 | until: (body): boolean => |
| 53 | body.tag !== "AccessDenied" && body.tag !== "AccessDeniedException", |
| 54 | schedule: Schedule.spaced("3 seconds"), |
| 55 | times: 10, |
| 56 | }), |
| 57 | ); |
| 58 | }); |
| 59 | |
| 60 | // Deploys a Lambda bound to an unattached ip target group (RegisterTargets / |
| 61 | // DeregisterTargets / DescribeTargetHealth) and an internal ALB |
| 62 | // (Describe/ModifyCapacityReservation), then drives each binding over the |
| 63 | // function URL against live cloud state. |
| 64 | describe( |
| 65 | "ELBv2 runtime bindings", |
| 66 | { |
| 67 | tags: [ |
| 68 | "provider:aws", |
| 69 | "provider:aws:ec2", |
| 70 | "provider:aws:elbv2", |
| 71 | "provider:aws:lambda", |
| 72 | "provider:aws:s3", |
| 73 | "live", |
| 74 | ], |
| 75 | }, |
| 76 | () => { |
| 77 | beforeAll( |
| 78 | Effect.gen(function* () { |
| 79 | yield* sharedStack.destroy(); |
| 80 | |
| 81 | // Phase 1 — the trust store's CA bundle must exist in S3 before the |
| 82 | // TrustStore resource can be created, and there is no declarative S3 |
| 83 | // object resource yet: deploy the bucket alone, upload the bundle |
| 84 | // out-of-band, then deploy the full fixture (same logical id, same |
| 85 | // stack, so phase 2 reconciles the bucket as a no-op). |
| 86 | const { bucketName } = yield* sharedStack.deploy( |
| 87 | Effect.gen(function* () { |
| 88 | const bucket = yield* Bucket("ElbBindingsBundleBucket", { |
| 89 | forceDestroy: true, |
| 90 | }); |
| 91 | return { bucketName: bucket.bucketName }; |
| 92 | }), |
| 93 | ); |
| 94 | // beforeAll runs outside the provider env, so the raw distilled call |
| 95 | // needs the AWS layers (credentials/region) provided explicitly. |
| 96 | yield* Core.withProviders( |
| 97 | s3.putObject({ |
| 98 | Bucket: bucketName, |
| 99 | Key: bundleKey, |
| 100 | Body: CA_BUNDLE_PEM, |
| 101 | ContentType: "application/x-pem-file", |
| 102 | }), |
| 103 | testOptions, |
| 104 | sharedStack.name, |
| 105 | ); |
| 106 | |
| 107 | const { fn } = yield* sharedStack.deploy( |
| 108 | Effect.gen(function* () { |
| 109 | const fn = yield* ElbBindingsFunction; |
| 110 | return { fn }; |
| 111 | }).pipe(Effect.provide(ElbBindingsFunctionLive)), |
| 112 | ); |
| 113 | |
| 114 | expect(fn.functionUrl).toBeTruthy(); |
| 115 | baseUrl = fn.functionUrl!.replace(/\/+$/, ""); |
| 116 | |
| 117 | yield* HttpClient.get(`${baseUrl}/health`).pipe( |
| 118 | Effect.flatMap((response) => |
| 119 | response.status === 200 |
| 120 | ? Effect.succeed(response) |
| 121 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 122 | ), |
| 123 | Effect.retry({ schedule: readinessPolicy }), |
| 124 | ); |
| 125 | }), |
| 126 | { timeout: 300_000 }, |
| 127 | ); |
| 128 | |
| 129 | // The ALB delete blocks until the balancer is fully gone (~2-4 min of ENI |
| 130 | // release), so give teardown headroom. |
| 131 | afterAll(sharedStack.destroy(), { timeout: 400_000 }); |
| 132 | |
| 133 | test.provider( |
| 134 | "RegisterTargets -> DescribeTargetHealth -> DeregisterTargets round-trip", |
| 135 | (_stack) => |
| 136 | Effect.gen(function* () { |
| 137 | const registered = yield* callRoute("POST", "/register"); |
| 138 | // Compare tag AND message so a failure diff surfaces the AWS error. |
| 139 | expect({ tag: registered.tag, message: registered.message }).toEqual({ |
| 140 | tag: "Success", |
| 141 | message: undefined, |
| 142 | }); |
| 143 | |
| 144 | const health = yield* callRoute("GET", "/target-health"); |
| 145 | expect(health.ok).toBe(true); |
| 146 | expect(health.targets?.some((t) => t.id === testTargetIp)).toBe(true); |
| 147 | |
| 148 | const deregistered = yield* callRoute("POST", "/deregister"); |
| 149 | expect(deregistered.tag).toEqual("Success"); |
| 150 | }), |
| 151 | { timeout: 120_000 }, |
| 152 | ); |
| 153 | |
| 154 | test.provider( |
| 155 | "DescribeCapacityReservation reads the ALB's reservation state", |
| 156 | (_stack) => |
| 157 | Effect.gen(function* () { |
| 158 | const body = yield* callRoute("GET", "/capacity"); |
| 159 | expect(body.ok).toBe(true); |
| 160 | // A fresh ALB has no reservation: the per-AZ state list is empty (or |
| 161 | // absent) — the successful call is what proves the read grant. |
| 162 | expect(body.tag).toEqual("Success"); |
| 163 | }), |
| 164 | { timeout: 60_000 }, |
| 165 | ); |
| 166 | |
| 167 | test.provider( |
| 168 | "ModifyCapacityReservation reset is IAM-wired", |
| 169 | (_stack) => |
| 170 | Effect.gen(function* () { |
| 171 | const body = yield* callRoute("POST", "/capacity-reset"); |
| 172 | // Resetting a reservation that was never set is accepted (no-op) or |
| 173 | // rejected with a typed configuration error — never an |
| 174 | // authorization failure. |
| 175 | expect([ |
| 176 | "Success", |
| 177 | "InvalidConfigurationRequestException", |
| 178 | "CapacityReservationPendingException", |
| 179 | ]).toContain(body.tag); |
| 180 | }), |
| 181 | { timeout: 60_000 }, |
| 182 | ); |
| 183 | |
| 184 | test.provider( |
| 185 | "GetTrustStoreCaCertificatesBundle returns a presigned bundle location", |
| 186 | (_stack) => |
| 187 | Effect.gen(function* () { |
| 188 | const body = yield* callRoute("GET", "/truststore-bundle"); |
| 189 | expect(body.tag).toEqual("Success"); |
| 190 | expect(body.location).toMatch(/^https:\/\//); |
| 191 | }), |
| 192 | { timeout: 60_000 }, |
| 193 | ); |
| 194 | |
| 195 | test.provider( |
| 196 | "GetTrustStoreRevocationContent surfaces the typed not-found tag", |
| 197 | (_stack) => |
| 198 | Effect.gen(function* () { |
| 199 | const body = yield* callRoute("GET", "/truststore-revocation-missing"); |
| 200 | // The fixture queries a revocation id that was never added — the miss |
| 201 | // must surface as the typed tag (never an authorization or catch-all |
| 202 | // failure), proving both the IAM grant and the typed error path. |
| 203 | expect(body.ok).toBe(false); |
| 204 | expect(body.tag).toEqual("RevocationIdNotFoundException"); |
| 205 | }), |
| 206 | { timeout: 60_000 }, |
| 207 | ); |
| 208 | }, |
| 209 | ); |