| 1 | import * as AWS from "@/AWS"; |
| 2 | import * as Core from "@/Test/Core"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as serverless from "@distilled.cloud/aws/redshift-serverless"; |
| 5 | import { describe, expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 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({ |
| 26 | workgroupName: "alchemy-nonexistent-rsconn-probe", |
| 27 | }), |
| 28 | ); |
| 29 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 30 | }), |
| 31 | { tags: ["provider:aws", "provider:aws:redshiftserverless", "live"] }, |
| 32 | ); |
| 33 | |
| 34 | let baseUrl: string; |
| 35 | |
| 36 | // The full lifecycle deploys a Redshift Serverless namespace + workgroup (a |
| 37 | // real-money RPU floor while it exists, ~2-5 min to provision) plus a Lambda, |
| 38 | // so it is gated behind AWS_TEST_REDSHIFT=1 and destroys everything in |
| 39 | // afterAll. |
| 40 | describe.skipIf(!process.env.AWS_TEST_REDSHIFT)( |
| 41 | "RedshiftServerless.Connect", |
| 42 | { |
| 43 | tags: [ |
| 44 | "provider:aws", |
| 45 | "provider:aws:lambda", |
| 46 | "provider:aws:redshiftserverless", |
| 47 | "live", |
| 48 | ], |
| 49 | }, |
| 50 | () => { |
| 51 | beforeAll( |
| 52 | Effect.gen(function* () { |
| 53 | yield* Effect.logInfo( |
| 54 | "RedshiftServerless.Connect setup: destroying previous run", |
| 55 | ); |
| 56 | yield* sharedStack.destroy(); |
| 57 | |
| 58 | yield* Effect.logInfo( |
| 59 | "RedshiftServerless.Connect setup: deploying fixture", |
| 60 | ); |
| 61 | const { functionUrl } = yield* sharedStack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | return yield* ServerlessConnectFunction; |
| 64 | }).pipe(Effect.provide(ServerlessConnectFunctionLive)), |
| 65 | ); |
| 66 | |
| 67 | expect(functionUrl).toBeTruthy(); |
| 68 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 69 | yield* Effect.logInfo( |
| 70 | `RedshiftServerless.Connect setup: function URL ready (${functionUrl})`, |
| 71 | ); |
| 72 | }), |
| 73 | // namespace (~1 min) + workgroup create (~2-5 min) + Lambda deploy. |
| 74 | { timeout: 900_000 }, |
| 75 | ); |
| 76 | |
| 77 | afterAll(sharedStack.destroy(), { timeout: 600_000 }); |
| 78 | |
| 79 | describe("Connect", () => { |
| 80 | test.provider( |
| 81 | "mints temporary credentials and formats a pgwire connection URL", |
| 82 | (_stack) => |
| 83 | Effect.gen(function* () { |
| 84 | const response = yield* HttpClient.get(`${baseUrl}/info`).pipe( |
| 85 | Effect.flatMap((res) => |
| 86 | res.status === 200 |
| 87 | ? Effect.succeed(res) |
| 88 | : Effect.fail(new Error(`info returned ${res.status}`)), |
| 89 | ), |
| 90 | Effect.retry({ |
| 91 | schedule: Schedule.max([ |
| 92 | Schedule.exponential("1 second"), |
| 93 | Schedule.recurs(10), |
| 94 | ]), |
| 95 | }), |
| 96 | Effect.flatMap((res) => res.json), |
| 97 | ); |
| 98 | |
| 99 | const body = response as { |
| 100 | host: string; |
| 101 | port: number; |
| 102 | database: string; |
| 103 | username: string | undefined; |
| 104 | hasPassword: boolean; |
| 105 | ssl: boolean; |
| 106 | urlScheme: string; |
| 107 | expiresInFuture: boolean; |
| 108 | }; |
| 109 | expect(body.host).toContain("redshift-serverless"); |
| 110 | expect(body.port).toBe(5439); |
| 111 | expect(body.database).toBe("dev"); |
| 112 | // GetCredentials maps the caller's IAM identity to a database |
| 113 | // user prefixed with `IAM:`/`IAMR:`. |
| 114 | expect(body.username).toMatch(/^IAM/); |
| 115 | expect(body.hasPassword).toBe(true); |
| 116 | expect(body.ssl).toBe(true); |
| 117 | expect(body.urlScheme).toBe("postgresql"); |
| 118 | expect(body.expiresInFuture).toBe(true); |
| 119 | }), |
| 120 | { timeout: 240_000 }, |
| 121 | ); |
| 122 | }); |
| 123 | }, |
| 124 | ); |