The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 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 Data from "effect/Data"; |
| 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 LatticeTestFunctionLive, { LatticeTestFunction } from "./handler"; |
| 12 | |
| 13 | const testOptions = { providers: AWS.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "VpcLatticeBindings"); |
| 16 | |
| 17 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 18 | // over 60s on a fresh deploy. |
| 19 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 20 | |
| 21 | let baseUrl: string; |
| 22 | let functionArn: string; |
| 23 | |
| 24 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 25 | readonly status: number; |
| 26 | readonly body: string; |
| 27 | }> {} |
| 28 | |
| 29 | // Retry only 5xx (cold re-init, IAM propagation surfaced by the handler's |
| 30 | // `Effect.orDie` as a 500); a genuine 4xx/assertion failure surfaces |
| 31 | // immediately. |
| 32 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 33 | HttpClient.execute(request).pipe( |
| 34 | Effect.flatMap((response) => |
| 35 | response.status >= 500 |
| 36 | ? response.text.pipe( |
| 37 | Effect.flatMap((body) => |
| 38 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 39 | ), |
| 40 | ) |
| 41 | : Effect.succeed(response), |
| 42 | ), |
| 43 | Effect.retry({ |
| 44 | while: (e): boolean => e._tag === "TransientUpstream", |
| 45 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 46 | }), |
| 47 | ); |
| 48 | |
| 49 | const getJson = (path: string) => |
| 50 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 51 | |
| 52 | const postJson = (path: string, body: object) => |
| 53 | send( |
| 54 | HttpClientRequest.post(`${baseUrl}${path}`).pipe(HttpClientRequest.bodyJsonUnsafe(body)), |
| 55 | ).pipe(Effect.flatMap((r) => r.json)); |
| 56 | |
| 57 | describe.sequential( |
| 58 | "VpcLattice Bindings", |
| 59 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:vpclattice", "live"] }, |
| 60 | () => { |
| 61 | beforeAll( |
| 62 | Effect.gen(function* () { |
| 63 | yield* Effect.logInfo("VpcLattice bindings setup: destroying previous resources"); |
| 64 | yield* sharedStack.destroy(); |
| 65 | |
| 66 | yield* Effect.logInfo("VpcLattice bindings setup: deploying fixture"); |
| 67 | const attrs = yield* sharedStack.deploy( |
| 68 | Effect.gen(function* () { |
| 69 | const fn = yield* LatticeTestFunction; |
| 70 | // Registering a Lambda target requires the function to allow |
| 71 | // vpc-lattice.amazonaws.com to invoke it — RegisterTargets rejects |
| 72 | // the target as `InvalidTarget` otherwise. |
| 73 | yield* Lambda.Permission("LatticeInvokePermission", { |
| 74 | action: "lambda:InvokeFunction", |
| 75 | functionName: fn.functionName, |
| 76 | principal: "vpc-lattice.amazonaws.com", |
| 77 | }); |
| 78 | return fn; |
| 79 | }).pipe(Effect.provide(LatticeTestFunctionLive)), |
| 80 | ); |
| 81 | |
| 82 | expect(attrs.functionUrl).toBeTruthy(); |
| 83 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 84 | functionArn = attrs.functionArn; |
| 85 | |
| 86 | const readinessUrl = `${baseUrl}/bindings`; |
| 87 | yield* Effect.logInfo(`VpcLattice bindings setup: probing readiness at ${readinessUrl}`); |
| 88 | yield* HttpClient.get(readinessUrl).pipe( |
| 89 | Effect.flatMap((response) => |
| 90 | response.status === 200 |
| 91 | ? Effect.succeed(response) |
| 92 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 93 | ), |
| 94 | Effect.tapError((error) => |
| 95 | Effect.logWarning( |
| 96 | `VpcLattice bindings setup: fixture not ready yet (${String(error)})`, |
| 97 | ), |
| 98 | ), |
| 99 | Effect.retry({ schedule: readinessPolicy }), |
| 100 | ); |
| 101 | }), |
| 102 | { timeout: 240_000 }, |
| 103 | ); |
| 104 | |
| 105 | afterAll(sharedStack.destroy(), { timeout: 240_000 }); |
| 106 | |
| 107 | describe("binding registration", () => { |
| 108 | test.provider("the capabilities initialize in the runtime", (_stack) => |
| 109 | Effect.gen(function* () { |
| 110 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 111 | expect(response.bound).toEqual(["deregisterTargets", "listTargets", "registerTargets"]); |
| 112 | }), |
| 113 | ); |
| 114 | }); |
| 115 | |
| 116 | describe("ListTargets", () => { |
| 117 | test.provider("lists an empty target group (injected target group id)", (_stack) => |
| 118 | Effect.gen(function* () { |
| 119 | const response = (yield* getJson("/targets")) as { targets: { id: string }[] }; |
| 120 | expect(response.targets).toEqual([]); |
| 121 | }), |
| 122 | ); |
| 123 | }); |
| 124 | |
| 125 | describe("RegisterTargets / DeregisterTargets", () => { |
| 126 | test.provider( |
| 127 | "the function registers and deregisters itself as a Lambda target", |
| 128 | (_stack) => |
| 129 | Effect.gen(function* () { |
| 130 | // Register the fixture's own function ARN into the LAMBDA group. |
| 131 | const registered = (yield* postJson("/register", { id: functionArn })) as { |
| 132 | successful: string[]; |
| 133 | unsuccessful: unknown[]; |
| 134 | }; |
| 135 | expect(registered.unsuccessful).toEqual([]); |
| 136 | expect(registered.successful).toEqual([functionArn]); |
| 137 | |
| 138 | // The registration is observable through the list binding. |
| 139 | const listed = (yield* getJson("/targets")) as { targets: { id: string }[] }; |
| 140 | expect(listed.targets.map((t) => t.id)).toEqual([functionArn]); |
| 141 | |
| 142 | // Deregister so the shared stack destroy is drain-free. |
| 143 | const deregistered = (yield* postJson("/deregister", { id: functionArn })) as { |
| 144 | successful: string[]; |
| 145 | unsuccessful: unknown[]; |
| 146 | }; |
| 147 | expect(deregistered.unsuccessful).toEqual([]); |
| 148 | expect(deregistered.successful).toEqual([functionArn]); |
| 149 | }), |
| 150 | { timeout: 120_000 }, |
| 151 | ); |
| 152 | }); |
| 153 | }, |
| 154 | ); |