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 { 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 Data from "effect/Data"; |
| 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 AppRegistryTestFunctionLive, { AppRegistryTestFunction } from "./handler"; |
| 11 | import { makeAppRegistryTestLease } from "./TestLease.ts"; |
| 12 | |
| 13 | const testOptions = { providers: AWS.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "AppRegistryBindings"); |
| 16 | const serviceLease = makeAppRegistryTestLease(); |
| 17 | |
| 18 | beforeAll(serviceLease.acquire, { timeout: 3_600_000 }); |
| 19 | // Release at the same (top) level as the acquire so the lease can never be |
| 20 | // left held when the gated describe below is skipped. |
| 21 | afterAll(serviceLease.release); |
| 22 | |
| 23 | // AppRegistry is in maintenance mode (see Application.test.ts) — the fixture |
| 24 | // deploy creates real AppRegistry resources, so the whole suite is gated |
| 25 | // behind AWS_TEST_APPREGISTRY=1 for accounts that retain access. |
| 26 | const gated = !process.env.AWS_TEST_APPREGISTRY; |
| 27 | |
| 28 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 29 | // over 60s on a fresh deploy. |
| 30 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 31 | |
| 32 | let baseUrl: string; |
| 33 | |
| 34 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 35 | readonly status: number; |
| 36 | readonly body: string; |
| 37 | }> {} |
| 38 | |
| 39 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 40 | // (cold re-init, IAM propagation on the freshly attached policy that the |
| 41 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 42 | // 4xx/assertion failure surfaces immediately. |
| 43 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 44 | HttpClient.execute(request).pipe( |
| 45 | Effect.flatMap((response) => |
| 46 | response.status >= 500 |
| 47 | ? response.text.pipe( |
| 48 | Effect.flatMap((body) => |
| 49 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 50 | ), |
| 51 | ) |
| 52 | : Effect.succeed(response), |
| 53 | ), |
| 54 | Effect.retry({ |
| 55 | while: (e) => e._tag === "TransientUpstream", |
| 56 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 57 | }), |
| 58 | ); |
| 59 | |
| 60 | describe |
| 61 | .skipIf(gated) |
| 62 | .sequential( |
| 63 | "AppRegistry Bindings", |
| 64 | { tags: ["provider:aws", "provider:aws:appregistry", "provider:aws:lambda", "live"] }, |
| 65 | () => { |
| 66 | beforeAll( |
| 67 | Effect.gen(function* () { |
| 68 | yield* Effect.logInfo("AppRegistry test setup: destroying previous resources"); |
| 69 | yield* sharedStack.destroy(); |
| 70 | |
| 71 | yield* Effect.logInfo("AppRegistry test setup: deploying fixture"); |
| 72 | const { functionUrl } = yield* sharedStack.deploy( |
| 73 | Effect.gen(function* () { |
| 74 | return yield* AppRegistryTestFunction; |
| 75 | }).pipe(Effect.provide(AppRegistryTestFunctionLive)), |
| 76 | ); |
| 77 | |
| 78 | expect(functionUrl).toBeTruthy(); |
| 79 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 80 | |
| 81 | const readinessUrl = `${baseUrl}/bindings`; |
| 82 | yield* Effect.logInfo(`AppRegistry test setup: probing readiness at ${readinessUrl}`); |
| 83 | yield* HttpClient.get(readinessUrl).pipe( |
| 84 | Effect.flatMap((response) => |
| 85 | response.status === 200 |
| 86 | ? Effect.succeed(response) |
| 87 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 88 | ), |
| 89 | Effect.tapError((error) => |
| 90 | Effect.logWarning(`AppRegistry test setup: fixture not ready yet (${String(error)})`), |
| 91 | ), |
| 92 | Effect.retry({ schedule: readinessPolicy }), |
| 93 | ); |
| 94 | }), |
| 95 | { timeout: 240_000 }, |
| 96 | ); |
| 97 | |
| 98 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 99 | |
| 100 | describe("binding registration", () => { |
| 101 | test.provider("all 9 capabilities initialize in the runtime", (_stack) => |
| 102 | Effect.gen(function* () { |
| 103 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/bindings`)).pipe( |
| 104 | Effect.flatMap((r) => r.json), |
| 105 | ); |
| 106 | expect((response as any).bound).toHaveLength(9); |
| 107 | }), |
| 108 | ); |
| 109 | }); |
| 110 | |
| 111 | describe("GetApplication", () => { |
| 112 | test.provider("reads the fixture application's metadata", (_stack) => |
| 113 | Effect.gen(function* () { |
| 114 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/application`)).pipe( |
| 115 | Effect.flatMap((r) => r.json), |
| 116 | ); |
| 117 | expect(typeof (response as any).name).toBe("string"); |
| 118 | expect((response as any).name.length).toBeGreaterThan(0); |
| 119 | expect(typeof (response as any).associatedResourceCount).toBe("number"); |
| 120 | }), |
| 121 | ); |
| 122 | }); |
| 123 | |
| 124 | describe("GetAttributeGroup", () => { |
| 125 | test.provider("reads the fixture group's JSON attributes", (_stack) => |
| 126 | Effect.gen(function* () { |
| 127 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/attribute-group`)).pipe( |
| 128 | Effect.flatMap((r) => r.json), |
| 129 | ); |
| 130 | expect((response as any).attributes.owner).toBe("alchemy-test"); |
| 131 | expect((response as any).attributes.tier).toBe("bindings"); |
| 132 | }), |
| 133 | ); |
| 134 | }); |
| 135 | |
| 136 | describe("ListAssociatedAttributeGroups", () => { |
| 137 | test.provider("lists the associated attribute group", (_stack) => |
| 138 | Effect.gen(function* () { |
| 139 | const response = yield* send( |
| 140 | HttpClientRequest.get(`${baseUrl}/associated-attribute-groups`), |
| 141 | ).pipe(Effect.flatMap((r) => r.json)); |
| 142 | expect((response as any).count).toBeGreaterThanOrEqual(1); |
| 143 | }), |
| 144 | ); |
| 145 | }); |
| 146 | |
| 147 | describe("ListAttributeGroupsForApplication", () => { |
| 148 | test.provider("lists the associated attribute group details", (_stack) => |
| 149 | Effect.gen(function* () { |
| 150 | const response = yield* send( |
| 151 | HttpClientRequest.get(`${baseUrl}/attribute-groups-details`), |
| 152 | ).pipe(Effect.flatMap((r) => r.json)); |
| 153 | expect((response as any).names.length).toBeGreaterThanOrEqual(1); |
| 154 | }), |
| 155 | ); |
| 156 | }); |
| 157 | |
| 158 | describe("ListAssociatedResources", () => { |
| 159 | test.provider("lists the application's associated resources", (_stack) => |
| 160 | Effect.gen(function* () { |
| 161 | const response = yield* send( |
| 162 | HttpClientRequest.get(`${baseUrl}/associated-resources`), |
| 163 | ).pipe(Effect.flatMap((r) => r.json)); |
| 164 | expect(typeof (response as any).count).toBe("number"); |
| 165 | }), |
| 166 | ); |
| 167 | }); |
| 168 | |
| 169 | describe("GetAssociatedResource", () => { |
| 170 | test.provider("returns the typed not-found error for an unknown resource", (_stack) => |
| 171 | Effect.gen(function* () { |
| 172 | const response = yield* send( |
| 173 | HttpClientRequest.get(`${baseUrl}/associated-resource-not-found`), |
| 174 | ).pipe(Effect.flatMap((r) => r.json)); |
| 175 | expect((response as any).found).toBe(false); |
| 176 | }), |
| 177 | ); |
| 178 | }); |
| 179 | |
| 180 | describe("ListApplications", () => { |
| 181 | test.provider("account listing includes the fixture application", (_stack) => |
| 182 | Effect.gen(function* () { |
| 183 | const app = yield* send(HttpClientRequest.get(`${baseUrl}/application`)).pipe( |
| 184 | Effect.flatMap((r) => r.json), |
| 185 | ); |
| 186 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/applications`)).pipe( |
| 187 | Effect.flatMap((r) => r.json), |
| 188 | ); |
| 189 | expect((response as any).names).toContain((app as any).name); |
| 190 | }), |
| 191 | ); |
| 192 | }); |
| 193 | |
| 194 | describe("ListAttributeGroups", () => { |
| 195 | test.provider("account listing includes the fixture attribute group", (_stack) => |
| 196 | Effect.gen(function* () { |
| 197 | const group = yield* send(HttpClientRequest.get(`${baseUrl}/attribute-group`)).pipe( |
| 198 | Effect.flatMap((r) => r.json), |
| 199 | ); |
| 200 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/attribute-groups`)).pipe( |
| 201 | Effect.flatMap((r) => r.json), |
| 202 | ); |
| 203 | expect((response as any).names).toContain((group as any).name); |
| 204 | }), |
| 205 | ); |
| 206 | }); |
| 207 | |
| 208 | describe("SyncResource", () => { |
| 209 | test.provider( |
| 210 | "surfaces a typed error for a nonexistent stack (proving the grant)", |
| 211 | (_stack) => |
| 212 | Effect.gen(function* () { |
| 213 | const response = yield* send(HttpClientRequest.post(`${baseUrl}/sync-resource`)).pipe( |
| 214 | Effect.flatMap((r) => r.json), |
| 215 | ); |
| 216 | expect(["ResourceNotFoundException", "ValidationException"]).toContain( |
| 217 | (response as any).tag, |
| 218 | ); |
| 219 | }), |
| 220 | ); |
| 221 | }); |
| 222 | }, |
| 223 | ); |