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 serverless from "@distilled.cloud/aws/redshift-serverless"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import ServerlessConnectFunctionLive, { |
| 10 | ServerlessConnectFunction, |
| 11 | } from "./fixtures/connect-handler"; |
| 12 | |
| 13 | const testOptions = { providers: AWS.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "RedshiftServerlessConnect"); |
| 16 | |
| 17 | // Ungated typed-error probe: proves the distilled error union carries the |
| 18 | // not-found tag the Connect runtime path can surface. Runs in every CI pass |
| 19 | // at near-zero cost. |
| 20 | test.provider( |
| 21 | "getCredentials on a nonexistent workgroup fails with ResourceNotFoundException", |
| 22 | () => |
| 23 | Effect.gen(function* () { |
| 24 | const error = yield* Effect.flip( |
| 25 | serverless.getCredentials({ workgroupName: "alchemy-nonexistent-rsconn-probe" }), |
| 26 | ); |
| 27 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 28 | }), |
| 29 | { tags: ["provider:aws", "provider:aws:redshiftserverless", "live"] }, |
| 30 | ); |
| 31 | |
| 32 | let baseUrl: string; |
| 33 | |
| 34 | // The full lifecycle deploys a Redshift Serverless namespace + workgroup (a |
| 35 | // real-money RPU floor while it exists, ~2-5 min to provision) plus a Lambda, |
| 36 | // so it is gated behind AWS_TEST_REDSHIFT=1 and destroys everything in |
| 37 | // afterAll. |
| 38 | describe.skipIf(!process.env.AWS_TEST_REDSHIFT)( |
| 39 | "RedshiftServerless.Connect", |
| 40 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:redshiftserverless", "live"] }, |
| 41 | () => { |
| 42 | beforeAll( |
| 43 | Effect.gen(function* () { |
| 44 | yield* Effect.logInfo("RedshiftServerless.Connect setup: destroying previous run"); |
| 45 | yield* sharedStack.destroy(); |
| 46 | |
| 47 | yield* Effect.logInfo("RedshiftServerless.Connect setup: deploying fixture"); |
| 48 | const { functionUrl } = yield* sharedStack.deploy( |
| 49 | Effect.gen(function* () { |
| 50 | return yield* ServerlessConnectFunction; |
| 51 | }).pipe(Effect.provide(ServerlessConnectFunctionLive)), |
| 52 | ); |
| 53 | |
| 54 | expect(functionUrl).toBeTruthy(); |
| 55 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 56 | yield* Effect.logInfo( |
| 57 | `RedshiftServerless.Connect setup: function URL ready (${functionUrl})`, |
| 58 | ); |
| 59 | }), |
| 60 | // namespace (~1 min) + workgroup create (~2-5 min) + Lambda deploy. |
| 61 | { timeout: 900_000 }, |
| 62 | ); |
| 63 | |
| 64 | afterAll(sharedStack.destroy(), { timeout: 600_000 }); |
| 65 | |
| 66 | describe("Connect", () => { |
| 67 | test.provider( |
| 68 | "mints temporary credentials and formats a pgwire connection URL", |
| 69 | (_stack) => |
| 70 | Effect.gen(function* () { |
| 71 | const response = yield* HttpClient.get(`${baseUrl}/info`).pipe( |
| 72 | Effect.flatMap((res) => |
| 73 | res.status === 200 |
| 74 | ? Effect.succeed(res) |
| 75 | : Effect.flatMap(res.text, (body) => |
| 76 | Effect.fail(new Error(`info returned ${res.status}: ${body}`)), |
| 77 | ), |
| 78 | ), |
| 79 | // Bounded (~1 min) — covers function-URL warm-up and IAM |
| 80 | // propagation without masking a deterministic failure. |
| 81 | Effect.retry({ schedule: Schedule.exponential("1 second"), times: 6 }), |
| 82 | Effect.flatMap((res) => res.json), |
| 83 | ); |
| 84 | |
| 85 | const body = response as { |
| 86 | host: string; |
| 87 | port: number; |
| 88 | database: string; |
| 89 | username: string | undefined; |
| 90 | hasPassword: boolean; |
| 91 | ssl: boolean; |
| 92 | urlScheme: string; |
| 93 | expiresInFuture: boolean; |
| 94 | }; |
| 95 | expect(body.host).toContain("redshift-serverless"); |
| 96 | expect(body.port).toBe(5439); |
| 97 | expect(body.database).toBe("dev"); |
| 98 | // GetCredentials maps the caller's IAM identity to a database |
| 99 | // user prefixed with `IAM:`/`IAMR:`. |
| 100 | expect(body.username).toMatch(/^IAM/); |
| 101 | expect(body.hasPassword).toBe(true); |
| 102 | expect(body.ssl).toBe(true); |
| 103 | expect(body.urlScheme).toBe("postgresql"); |
| 104 | expect(body.expiresInFuture).toBe(true); |
| 105 | }), |
| 106 | { timeout: 240_000 }, |
| 107 | ); |
| 108 | }); |
| 109 | }, |
| 110 | ); |