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 { AWSEnvironment } from "@alchemy.run/aws/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as ag from "@distilled.cloud/aws/api-gateway"; |
| 5 | import { createInternalTags, hasTags } from "alchemy/Tags"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Duration from "effect/Duration"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import * as Stream from "effect/Stream"; |
| 13 | import RestApiEventSourceFunctionLive, { |
| 14 | RestApiEventSourceFunction, |
| 15 | } from "./fixtures/rest-api-event-source-handler.ts"; |
| 16 | import * as Test from "./Test.ts"; |
| 17 | |
| 18 | const { test } = Test.make({ providers: AWS.providers() }); |
| 19 | |
| 20 | // Allow bounded propagation of function URLs, stages, and invoke permissions. |
| 21 | const readinessSchedule = Schedule.max([ |
| 22 | Schedule.exponential(500).pipe( |
| 23 | Schedule.modifyDelay(({ duration: d }) => |
| 24 | Effect.succeed(Duration.isGreaterThan(d, Duration.seconds(5)) ? Duration.seconds(5) : d), |
| 25 | ), |
| 26 | ), |
| 27 | Schedule.recurs(10), |
| 28 | ]); |
| 29 | |
| 30 | const getJson = (url: string) => |
| 31 | HttpClient.get(url).pipe( |
| 32 | Effect.flatMap((response) => |
| 33 | response.status === 200 |
| 34 | ? response.json |
| 35 | : Effect.fail(new Error(`GET ${url} returned ${response.status}`)), |
| 36 | ), |
| 37 | ); |
| 38 | |
| 39 | // Ownership tags survive name truncation and isolate concurrent test stages. |
| 40 | const findRestApis = Effect.fn(function* (logicalId: string) { |
| 41 | const tags = yield* createInternalTags(logicalId); |
| 42 | return yield* ag.getRestApis.pages({}).pipe( |
| 43 | Stream.runCollect, |
| 44 | Effect.map((pages) => |
| 45 | Array.from(pages).flatMap((page) => |
| 46 | (page.items ?? []).filter( |
| 47 | (api): api is ag.RestApi & { id: string } => api.id != null && hasTags(tags, api.tags), |
| 48 | ), |
| 49 | ), |
| 50 | ), |
| 51 | ); |
| 52 | }); |
| 53 | |
| 54 | class RestApiNotVisible extends Data.TaggedError("RestApiNotVisible") {} |
| 55 | |
| 56 | test.provider.skipIf(!!process.env.FAST)( |
| 57 | "onRestApiRoute serves REST routes from the hosting Lambda", |
| 58 | (stack) => |
| 59 | Effect.gen(function* () { |
| 60 | yield* stack.destroy(); |
| 61 | const { region } = yield* AWSEnvironment.current; |
| 62 | |
| 63 | const { functionUrl } = yield* stack.deploy( |
| 64 | Effect.gen(function* () { |
| 65 | return yield* RestApiEventSourceFunction; |
| 66 | }).pipe(Effect.provide(RestApiEventSourceFunctionLive)), |
| 67 | ); |
| 68 | expect(functionUrl).toBeTruthy(); |
| 69 | const baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 70 | |
| 71 | // Wait for the function URL to serve — the readiness signal that the |
| 72 | // Lambda (and everything deployed with it) is live. |
| 73 | yield* HttpClient.get(`${baseUrl}/`).pipe( |
| 74 | Effect.flatMap((response): Effect.Effect<void, Error> => |
| 75 | response.status === 200 |
| 76 | ? Effect.void |
| 77 | : Effect.fail(new Error(`probe returned ${response.status}`)), |
| 78 | ), |
| 79 | Effect.retry({ schedule: readinessSchedule }), |
| 80 | ); |
| 81 | |
| 82 | // Discover the REST API without awaiting outputs inside its host. |
| 83 | const apis = yield* findRestApis("AgEsApi").pipe( |
| 84 | Effect.filterOrFail( |
| 85 | (found) => found.length === 1, |
| 86 | () => new RestApiNotVisible(), |
| 87 | ), |
| 88 | Effect.retry({ |
| 89 | while: (e): boolean => e._tag === "RestApiNotVisible", |
| 90 | schedule: Schedule.spaced("3 seconds"), |
| 91 | times: 10, |
| 92 | }), |
| 93 | ); |
| 94 | const invokeUrl = `https://${apis[0].id}.execute-api.${region}.amazonaws.com/test`; |
| 95 | |
| 96 | // GET /items — the event source dispatched the proxy event to the |
| 97 | // registered handler. |
| 98 | const items = (yield* getJson(`${invokeUrl}/items`).pipe( |
| 99 | Effect.retry({ schedule: readinessSchedule }), |
| 100 | )) as { items: string[] }; |
| 101 | expect(items.items).toEqual(["alpha", "beta"]); |
| 102 | |
| 103 | // POST /echo — method, resource path, and body arrive intact. |
| 104 | const echoed = (yield* HttpClient.execute( |
| 105 | HttpClientRequest.post(`${invokeUrl}/echo`).pipe( |
| 106 | HttpClientRequest.bodyJsonUnsafe({ hello: "world" }), |
| 107 | ), |
| 108 | ).pipe( |
| 109 | Effect.flatMap((response) => |
| 110 | response.status === 200 |
| 111 | ? response.json |
| 112 | : Effect.fail(new Error(`POST /echo returned ${response.status}`)), |
| 113 | ), |
| 114 | Effect.retry({ schedule: readinessSchedule }), |
| 115 | )) as { method: string; resource: string; echoed: { hello: string } }; |
| 116 | expect(echoed.method).toBe("POST"); |
| 117 | expect(echoed.resource).toBe("/echo"); |
| 118 | expect(echoed.echoed).toEqual({ hello: "world" }); |
| 119 | |
| 120 | // Unregistered verb on a registered path is rejected by API Gateway |
| 121 | // itself (403 MissingAuthenticationToken) rather than the handler. |
| 122 | const missing = yield* HttpClient.execute( |
| 123 | HttpClientRequest.make("DELETE")(`${invokeUrl}/items`), |
| 124 | ); |
| 125 | expect(missing.status).toBeGreaterThanOrEqual(400); |
| 126 | |
| 127 | yield* stack.destroy(); |
| 128 | |
| 129 | const deleted = yield* ag |
| 130 | .getRestApi({ restApiId: apis[0].id }) |
| 131 | .pipe(Effect.catchTag("NotFoundException", () => Effect.succeed(undefined))); |
| 132 | expect(deleted).toBeUndefined(); |
| 133 | const leftover = yield* findRestApis("AgEsApi"); |
| 134 | expect(leftover).toHaveLength(0); |
| 135 | }), |
| 136 | { |
| 137 | tags: ["provider:aws", "provider:aws:apigateway", "provider:aws:lambda", "live"], |
| 138 | timeout: 120_000, |
| 139 | }, |
| 140 | ); |