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 CloudControlTestFunctionLive, { CloudControlTestFunction } from "./handler"; |
| 11 | |
| 12 | const testOptions = { providers: AWS.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "CloudControlBindings"); |
| 15 | |
| 16 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 17 | // over 60s on a fresh deploy. |
| 18 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 19 | |
| 20 | let baseUrl: string; |
| 21 | |
| 22 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 23 | readonly status: number; |
| 24 | readonly body: string; |
| 25 | }> {} |
| 26 | |
| 27 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 28 | // (cold re-init, IAM propagation on the freshly attached policy that the |
| 29 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 30 | // 4xx/assertion failure surfaces immediately. |
| 31 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 32 | HttpClient.execute(request).pipe( |
| 33 | Effect.flatMap((response) => |
| 34 | response.status >= 500 |
| 35 | ? response.text.pipe( |
| 36 | Effect.flatMap((body) => |
| 37 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 38 | ), |
| 39 | ) |
| 40 | : Effect.succeed(response), |
| 41 | ), |
| 42 | Effect.retry({ |
| 43 | while: (e) => e._tag === "TransientUpstream", |
| 44 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | describe.sequential( |
| 49 | "CloudControl Bindings", |
| 50 | { tags: ["provider:aws", "provider:aws:cloudcontrol", "provider:aws:lambda", "live"] }, |
| 51 | () => { |
| 52 | beforeAll( |
| 53 | Effect.gen(function* () { |
| 54 | yield* Effect.logInfo("CloudControl test setup: destroying previous resources"); |
| 55 | yield* sharedStack.destroy(); |
| 56 | |
| 57 | yield* Effect.logInfo("CloudControl test setup: deploying fixture"); |
| 58 | const { functionUrl } = yield* sharedStack.deploy( |
| 59 | Effect.gen(function* () { |
| 60 | return yield* CloudControlTestFunction; |
| 61 | }).pipe(Effect.provide(CloudControlTestFunctionLive)), |
| 62 | ); |
| 63 | |
| 64 | expect(functionUrl).toBeTruthy(); |
| 65 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 66 | |
| 67 | const readinessUrl = `${baseUrl}/bindings`; |
| 68 | yield* Effect.logInfo(`CloudControl test setup: probing readiness at ${readinessUrl}`); |
| 69 | yield* HttpClient.get(readinessUrl).pipe( |
| 70 | Effect.flatMap((response) => |
| 71 | response.status === 200 |
| 72 | ? Effect.succeed(response) |
| 73 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 74 | ), |
| 75 | Effect.tapError((error) => |
| 76 | Effect.logWarning(`CloudControl test setup: fixture not ready yet (${String(error)})`), |
| 77 | ), |
| 78 | Effect.retry({ schedule: readinessPolicy }), |
| 79 | ); |
| 80 | }), |
| 81 | { timeout: 240_000 }, |
| 82 | ); |
| 83 | |
| 84 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 85 | |
| 86 | describe("binding registration", () => { |
| 87 | test.provider("all 8 capabilities initialize in the runtime", (_stack) => |
| 88 | Effect.gen(function* () { |
| 89 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/bindings`)).pipe( |
| 90 | Effect.flatMap((r) => r.json), |
| 91 | ); |
| 92 | expect((response as any).bound).toHaveLength(8); |
| 93 | }), |
| 94 | ); |
| 95 | }); |
| 96 | |
| 97 | describe("GetResource", () => { |
| 98 | test.provider( |
| 99 | "reads the stack-provisioned parameter's live state", |
| 100 | (_stack) => |
| 101 | Effect.gen(function* () { |
| 102 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/fixture`)).pipe( |
| 103 | Effect.flatMap((r) => r.json), |
| 104 | ); |
| 105 | expect((response as any).identifier).toBe( |
| 106 | "/alchemy-test/cloudcontrol/bindings/fixture", |
| 107 | ); |
| 108 | expect((response as any).value).toBe("fixture"); |
| 109 | }), |
| 110 | { timeout: 60_000 }, |
| 111 | ); |
| 112 | }); |
| 113 | |
| 114 | describe("ListResources", () => { |
| 115 | test.provider( |
| 116 | "discovers the stack-provisioned parameter", |
| 117 | (_stack) => |
| 118 | Effect.gen(function* () { |
| 119 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/list`)).pipe( |
| 120 | Effect.flatMap((r) => r.json), |
| 121 | ); |
| 122 | expect((response as any).count).toBeGreaterThanOrEqual(1); |
| 123 | expect((response as any).found).toBe(true); |
| 124 | }), |
| 125 | { timeout: 90_000 }, |
| 126 | ); |
| 127 | }); |
| 128 | |
| 129 | describe("CreateResource + GetResourceRequestStatus", () => { |
| 130 | test.provider( |
| 131 | "creates an SSM parameter at runtime and polls it to SUCCESS", |
| 132 | (_stack) => |
| 133 | Effect.gen(function* () { |
| 134 | const response = yield* send( |
| 135 | HttpClientRequest.post(`${baseUrl}/runtime-create`).pipe( |
| 136 | HttpClientRequest.bodyJsonUnsafe({ value: "one" }), |
| 137 | ), |
| 138 | ).pipe(Effect.flatMap((r) => r.json)); |
| 139 | expect((response as any).status).toBe("SUCCESS"); |
| 140 | expect((response as any).value).toBe("one"); |
| 141 | }), |
| 142 | { timeout: 120_000 }, |
| 143 | ); |
| 144 | }); |
| 145 | |
| 146 | describe("UpdateResource", () => { |
| 147 | test.provider( |
| 148 | "patches the runtime parameter's value", |
| 149 | (_stack) => |
| 150 | Effect.gen(function* () { |
| 151 | const response = yield* send( |
| 152 | HttpClientRequest.post(`${baseUrl}/runtime-update`).pipe( |
| 153 | HttpClientRequest.bodyJsonUnsafe({ value: "two" }), |
| 154 | ), |
| 155 | ).pipe(Effect.flatMap((r) => r.json)); |
| 156 | expect((response as any).status).toBe("SUCCESS"); |
| 157 | expect((response as any).value).toBe("two"); |
| 158 | }), |
| 159 | { timeout: 120_000 }, |
| 160 | ); |
| 161 | }); |
| 162 | |
| 163 | describe("DeleteResource", () => { |
| 164 | test.provider( |
| 165 | "deletes the runtime parameter and observes it gone", |
| 166 | (_stack) => |
| 167 | Effect.gen(function* () { |
| 168 | const response = yield* send(HttpClientRequest.post(`${baseUrl}/runtime-delete`)).pipe( |
| 169 | Effect.flatMap((r) => r.json), |
| 170 | ); |
| 171 | expect((response as any).status).toBe("SUCCESS"); |
| 172 | expect((response as any).gone).toBe(true); |
| 173 | }), |
| 174 | { timeout: 120_000 }, |
| 175 | ); |
| 176 | }); |
| 177 | |
| 178 | describe("ListResourceRequests", () => { |
| 179 | test.provider( |
| 180 | "lists recent resource operation requests", |
| 181 | (_stack) => |
| 182 | Effect.gen(function* () { |
| 183 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/requests`)).pipe( |
| 184 | Effect.flatMap((r) => r.json), |
| 185 | ); |
| 186 | // The runtime lifecycle above just issued create/update/delete |
| 187 | // requests, so at least one summary is visible. |
| 188 | expect((response as any).count).toBeGreaterThanOrEqual(1); |
| 189 | }), |
| 190 | { timeout: 60_000 }, |
| 191 | ); |
| 192 | }); |
| 193 | |
| 194 | describe("GetResourceRequestStatus", () => { |
| 195 | test.provider( |
| 196 | "returns the typed not-found error for an unknown token", |
| 197 | (_stack) => |
| 198 | Effect.gen(function* () { |
| 199 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/status-not-found`)).pipe( |
| 200 | Effect.flatMap((r) => r.json), |
| 201 | ); |
| 202 | expect((response as any).found).toBe(false); |
| 203 | }), |
| 204 | { timeout: 60_000 }, |
| 205 | ); |
| 206 | }); |
| 207 | |
| 208 | describe("CancelResourceRequest", () => { |
| 209 | test.provider( |
| 210 | "surfaces the typed not-found error for an unknown token (proving the grant)", |
| 211 | (_stack) => |
| 212 | Effect.gen(function* () { |
| 213 | const response = yield* send( |
| 214 | HttpClientRequest.post(`${baseUrl}/cancel-not-found`), |
| 215 | ).pipe(Effect.flatMap((r) => r.json)); |
| 216 | expect((response as any).tag).toBe("RequestTokenNotFoundException"); |
| 217 | }), |
| 218 | { timeout: 60_000 }, |
| 219 | ); |
| 220 | }); |
| 221 | }, |
| 222 | ); |