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 CloudHSMV2TestFunctionLive, { CloudHSMV2TestFunction } from "./handler"; |
| 11 | |
| 12 | const testOptions = { providers: AWS.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "CloudHSMV2Bindings"); |
| 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 | const getJson = (path: string) => |
| 49 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 50 | |
| 51 | describe.sequential( |
| 52 | "CloudHSMV2 Bindings", |
| 53 | { tags: ["provider:aws", "provider:aws:cloudhsmv2", "provider:aws:lambda", "live"] }, |
| 54 | () => { |
| 55 | beforeAll( |
| 56 | Effect.gen(function* () { |
| 57 | yield* Effect.logInfo("CloudHSMV2 test setup: destroying previous resources"); |
| 58 | yield* sharedStack.destroy(); |
| 59 | |
| 60 | yield* Effect.logInfo("CloudHSMV2 test setup: deploying fixture"); |
| 61 | const { functionUrl } = yield* sharedStack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | return yield* CloudHSMV2TestFunction; |
| 64 | }).pipe(Effect.provide(CloudHSMV2TestFunctionLive)), |
| 65 | ); |
| 66 | |
| 67 | expect(functionUrl).toBeTruthy(); |
| 68 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 69 | |
| 70 | const readinessUrl = `${baseUrl}/bindings`; |
| 71 | yield* Effect.logInfo(`CloudHSMV2 test setup: probing readiness at ${readinessUrl}`); |
| 72 | yield* HttpClient.get(readinessUrl).pipe( |
| 73 | Effect.flatMap((response) => |
| 74 | response.status === 200 |
| 75 | ? Effect.succeed(response) |
| 76 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 77 | ), |
| 78 | Effect.tapError((error) => |
| 79 | Effect.logWarning(`CloudHSMV2 test setup: fixture not ready yet (${String(error)})`), |
| 80 | ), |
| 81 | Effect.retry({ schedule: readinessPolicy }), |
| 82 | ); |
| 83 | }), |
| 84 | { timeout: 240_000 }, |
| 85 | ); |
| 86 | |
| 87 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 88 | |
| 89 | describe("binding registration", () => { |
| 90 | test.provider("all 10 capabilities initialize in the runtime", (_stack) => |
| 91 | Effect.gen(function* () { |
| 92 | const response = yield* getJson("/bindings"); |
| 93 | expect((response as any).bound).toHaveLength(10); |
| 94 | }), |
| 95 | ); |
| 96 | }); |
| 97 | |
| 98 | describe("DescribeClusters", () => { |
| 99 | test.provider("filter on a nonexistent id returns an empty page", (_stack) => |
| 100 | Effect.gen(function* () { |
| 101 | const response = yield* getJson("/clusters"); |
| 102 | expect((response as any).count).toBe(0); |
| 103 | }), |
| 104 | ); |
| 105 | }); |
| 106 | |
| 107 | describe("DescribeBackups", () => { |
| 108 | test.provider("filter on a nonexistent id returns an empty page", (_stack) => |
| 109 | Effect.gen(function* () { |
| 110 | const response = yield* getJson("/backups"); |
| 111 | expect((response as any).count).toBe(0); |
| 112 | }), |
| 113 | ); |
| 114 | }); |
| 115 | |
| 116 | describe("DeleteBackup", () => { |
| 117 | test.provider("surfaces the typed not-found tag for a nonexistent backup", (_stack) => |
| 118 | Effect.gen(function* () { |
| 119 | const response = yield* getJson("/backup-delete"); |
| 120 | expect((response as any).tag).toBe("CloudHsmResourceNotFoundException"); |
| 121 | }), |
| 122 | ); |
| 123 | }); |
| 124 | |
| 125 | describe("RestoreBackup", () => { |
| 126 | test.provider("surfaces the typed not-found tag for a nonexistent backup", (_stack) => |
| 127 | Effect.gen(function* () { |
| 128 | const response = yield* getJson("/backup-restore"); |
| 129 | expect((response as any).tag).toBe("CloudHsmResourceNotFoundException"); |
| 130 | }), |
| 131 | ); |
| 132 | }); |
| 133 | |
| 134 | describe("ModifyBackupAttributes", () => { |
| 135 | test.provider("surfaces the typed not-found tag for a nonexistent backup", (_stack) => |
| 136 | Effect.gen(function* () { |
| 137 | const response = yield* getJson("/backup-modify"); |
| 138 | expect((response as any).tag).toBe("CloudHsmResourceNotFoundException"); |
| 139 | }), |
| 140 | ); |
| 141 | }); |
| 142 | |
| 143 | describe("CopyBackupToRegion", () => { |
| 144 | test.provider("surfaces a typed tag for a nonexistent backup (proving the grant)", (_stack) => |
| 145 | Effect.gen(function* () { |
| 146 | const response = yield* getJson("/backup-copy"); |
| 147 | expect([ |
| 148 | "CloudHsmResourceNotFoundException", |
| 149 | "CloudHsmInvalidRequestException", |
| 150 | ]).toContain((response as any).tag); |
| 151 | }), |
| 152 | ); |
| 153 | }); |
| 154 | |
| 155 | describe("InitializeCluster", () => { |
| 156 | test.provider( |
| 157 | "surfaces a typed tag for a nonexistent cluster (proving the grant)", |
| 158 | (_stack) => |
| 159 | Effect.gen(function* () { |
| 160 | const response = yield* getJson("/cluster-init"); |
| 161 | expect([ |
| 162 | "CloudHsmResourceNotFoundException", |
| 163 | "CloudHsmInvalidRequestException", |
| 164 | ]).toContain((response as any).tag); |
| 165 | }), |
| 166 | ); |
| 167 | }); |
| 168 | |
| 169 | describe("GetResourcePolicy", () => { |
| 170 | test.provider("reaches service-side validation (proving the grant)", (_stack) => |
| 171 | Effect.gen(function* () { |
| 172 | const response = yield* getJson("/policy-get"); |
| 173 | expect([ |
| 174 | "CloudHsmInvalidRequestException", |
| 175 | "CloudHsmResourceNotFoundException", |
| 176 | ]).toContain((response as any).tag); |
| 177 | }), |
| 178 | ); |
| 179 | }); |
| 180 | |
| 181 | describe("PutResourcePolicy", () => { |
| 182 | test.provider("reaches service-side validation (proving the grant)", (_stack) => |
| 183 | Effect.gen(function* () { |
| 184 | const response = yield* getJson("/policy-put"); |
| 185 | expect([ |
| 186 | "CloudHsmInvalidRequestException", |
| 187 | "CloudHsmResourceNotFoundException", |
| 188 | ]).toContain((response as any).tag); |
| 189 | }), |
| 190 | ); |
| 191 | }); |
| 192 | |
| 193 | describe("DeleteResourcePolicy", () => { |
| 194 | test.provider("reaches service-side validation (proving the grant)", (_stack) => |
| 195 | Effect.gen(function* () { |
| 196 | const response = yield* getJson("/policy-delete"); |
| 197 | expect([ |
| 198 | "CloudHsmInvalidRequestException", |
| 199 | "CloudHsmResourceNotFoundException", |
| 200 | ]).toContain((response as any).tag); |
| 201 | }), |
| 202 | ); |
| 203 | }); |
| 204 | }, |
| 205 | ); |