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 { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Duration from "effect/Duration"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as HttpClient from "effect/http/HttpClient"; |
| 7 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import ApiGatewayV2BindingsFunctionLive, { |
| 10 | ApiGatewayV2BindingsFunction, |
| 11 | } from "./bindings-handler.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: AWS.providers() }); |
| 14 | |
| 15 | // Fresh Lambda function URLs return transient errors for the first |
| 16 | // 30–90s (DNS, IAM propagation, cold init). Poll at a steady cadence. |
| 17 | const readinessSchedule = Schedule.max([ |
| 18 | Schedule.exponential(500).pipe( |
| 19 | Schedule.modifyDelay(({ duration: d }) => |
| 20 | Effect.succeed(Duration.isGreaterThan(d, Duration.seconds(10)) ? Duration.seconds(10) : d), |
| 21 | ), |
| 22 | ), |
| 23 | Schedule.recurs(20), |
| 24 | ]); |
| 25 | |
| 26 | const getJson = (url: string) => |
| 27 | HttpClient.get(url).pipe( |
| 28 | Effect.flatMap((response) => |
| 29 | response.status === 200 |
| 30 | ? response.json |
| 31 | : Effect.fail(new Error(`GET ${url} returned ${response.status}`)), |
| 32 | ), |
| 33 | ); |
| 34 | |
| 35 | const postJson = (url: string) => |
| 36 | HttpClient.execute(HttpClientRequest.post(url)).pipe( |
| 37 | Effect.flatMap((response) => |
| 38 | response.status === 200 |
| 39 | ? response.json |
| 40 | : Effect.fail(new Error(`POST ${url} returned ${response.status}`)), |
| 41 | ), |
| 42 | ); |
| 43 | |
| 44 | test.provider.skipIf(!!process.env.FAST)( |
| 45 | "ApiGatewayV2 bindings export the API and reset authorizer caches from a Lambda", |
| 46 | (stack) => |
| 47 | Effect.gen(function* () { |
| 48 | yield* stack.destroy(); |
| 49 | |
| 50 | const { functionUrl } = yield* stack.deploy( |
| 51 | Effect.gen(function* () { |
| 52 | return yield* ApiGatewayV2BindingsFunction; |
| 53 | }).pipe(Effect.provide(ApiGatewayV2BindingsFunctionLive)), |
| 54 | ); |
| 55 | expect(functionUrl).toBeTruthy(); |
| 56 | const baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 57 | |
| 58 | // ExportApi — the live OpenAPI 3.0 document includes the fixture's |
| 59 | // route (retried through cold start). |
| 60 | const exported = (yield* getJson(`${baseUrl}/export`).pipe( |
| 61 | Effect.retry({ schedule: readinessSchedule }), |
| 62 | )) as { openapi?: string; paths: string[] }; |
| 63 | expect(exported.openapi ?? "").toContain("3.0"); |
| 64 | expect(exported.paths).toContain("/ping"); |
| 65 | |
| 66 | // ResetAuthorizersCache — the stage has no authorizer cache, so the |
| 67 | // API answers 200 (or a typed NotFound the route reports as |
| 68 | // flushed:false). A missing IAM grant would 500 the route instead. |
| 69 | const reset = (yield* postJson(`${baseUrl}/reset-authorizers`)) as { |
| 70 | flushed: boolean; |
| 71 | }; |
| 72 | expect(typeof reset.flushed).toBe("boolean"); |
| 73 | |
| 74 | yield* stack.destroy(); |
| 75 | }), |
| 76 | { |
| 77 | tags: ["provider:aws", "provider:aws:apigatewayv2", "provider:aws:lambda", "live"], |
| 78 | timeout: 600_000, |
| 79 | }, |
| 80 | ); |