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 redshift from "@distilled.cloud/aws/redshift"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import RedshiftConnectFunctionLive, { RedshiftConnectFunction } from "./fixtures/connect-handler"; |
| 10 | |
| 11 | const testOptions = { providers: AWS.providers() }; |
| 12 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 13 | const sharedStack = Core.scratchStack(testOptions, "RedshiftConnect"); |
| 14 | |
| 15 | // Ungated typed-error probes: prove the distilled error unions carry the |
| 16 | // not-found tag both Connect credential strategies can surface. Run in every |
| 17 | // CI pass at near-zero cost. |
| 18 | test.provider( |
| 19 | "getClusterCredentialsWithIAM on a nonexistent cluster fails with ClusterNotFoundFault", |
| 20 | () => |
| 21 | Effect.gen(function* () { |
| 22 | const error = yield* Effect.flip( |
| 23 | redshift.getClusterCredentialsWithIAM({ |
| 24 | ClusterIdentifier: "alchemy-nonexistent-redshift-connect-probe", |
| 25 | }), |
| 26 | ); |
| 27 | expect(error._tag).toBe("ClusterNotFoundFault"); |
| 28 | }), |
| 29 | { tags: ["provider:aws", "provider:aws:redshift", "live"] }, |
| 30 | ); |
| 31 | |
| 32 | test.provider( |
| 33 | "getClusterCredentials on a nonexistent cluster fails with ClusterNotFoundFault", |
| 34 | () => |
| 35 | Effect.gen(function* () { |
| 36 | const error = yield* Effect.flip( |
| 37 | redshift.getClusterCredentials({ |
| 38 | ClusterIdentifier: "alchemy-nonexistent-redshift-connect-probe", |
| 39 | DbUser: "probe", |
| 40 | }), |
| 41 | ); |
| 42 | expect(error._tag).toBe("ClusterNotFoundFault"); |
| 43 | }), |
| 44 | { tags: ["provider:aws", "provider:aws:redshift", "live"] }, |
| 45 | ); |
| 46 | |
| 47 | let baseUrl: string; |
| 48 | |
| 49 | const getInfo = (path: string) => |
| 50 | HttpClient.get(`${baseUrl}${path}`).pipe( |
| 51 | Effect.flatMap((res) => |
| 52 | res.status === 200 |
| 53 | ? Effect.succeed(res) |
| 54 | : Effect.flatMap(res.text, (body) => |
| 55 | Effect.fail(new Error(`${path} returned ${res.status}: ${body}`)), |
| 56 | ), |
| 57 | ), |
| 58 | // Bounded (~1 min) — covers function-URL warm-up and IAM propagation |
| 59 | // without masking a deterministic failure behind the test timeout. |
| 60 | Effect.retry({ schedule: Schedule.exponential("1 second"), times: 6 }), |
| 61 | Effect.flatMap((res) => res.json), |
| 62 | Effect.map( |
| 63 | (body) => |
| 64 | body as { |
| 65 | host: string; |
| 66 | port: number; |
| 67 | database: string; |
| 68 | username: string | undefined; |
| 69 | hasPassword: boolean; |
| 70 | ssl: boolean; |
| 71 | urlScheme: string; |
| 72 | expiresInFuture: boolean; |
| 73 | }, |
| 74 | ), |
| 75 | ); |
| 76 | |
| 77 | // The full lifecycle deploys a provisioned Redshift cluster (~5-10 min to |
| 78 | // reach `available`, bills hourly per node while it exists) plus a Lambda, |
| 79 | // so it is gated behind AWS_TEST_REDSHIFT=1 and destroys everything in |
| 80 | // afterAll. |
| 81 | describe.skipIf(!process.env.AWS_TEST_REDSHIFT)( |
| 82 | "Redshift.Connect", |
| 83 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:redshift", "live"] }, |
| 84 | () => { |
| 85 | beforeAll( |
| 86 | Effect.gen(function* () { |
| 87 | yield* Effect.logInfo("Redshift.Connect setup: destroying previous run"); |
| 88 | yield* sharedStack.destroy(); |
| 89 | |
| 90 | yield* Effect.logInfo("Redshift.Connect setup: deploying fixture"); |
| 91 | const { functionUrl } = yield* sharedStack.deploy( |
| 92 | Effect.gen(function* () { |
| 93 | return yield* RedshiftConnectFunction; |
| 94 | }).pipe(Effect.provide(RedshiftConnectFunctionLive)), |
| 95 | ); |
| 96 | |
| 97 | expect(functionUrl).toBeTruthy(); |
| 98 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 99 | yield* Effect.logInfo(`Redshift.Connect setup: function URL ready (${functionUrl})`); |
| 100 | }), |
| 101 | // cluster create (~5-10 min) + Lambda deploy. |
| 102 | { timeout: 1_200_000 }, |
| 103 | ); |
| 104 | |
| 105 | afterAll(sharedStack.destroy(), { timeout: 900_000 }); |
| 106 | |
| 107 | describe("Connect", () => { |
| 108 | test.provider( |
| 109 | "mints IAM-mapped temporary credentials (GetClusterCredentialsWithIAM)", |
| 110 | (_stack) => |
| 111 | Effect.gen(function* () { |
| 112 | const body = yield* getInfo("/info"); |
| 113 | expect(body.host).toContain("redshift"); |
| 114 | expect(body.port).toBe(5439); |
| 115 | expect(body.database).toBe("dev"); |
| 116 | // WithIAM maps the caller's IAM identity to a database user |
| 117 | // prefixed with `IAM:`/`IAMR:`. |
| 118 | expect(body.username).toMatch(/^IAM/); |
| 119 | expect(body.hasPassword).toBe(true); |
| 120 | expect(body.ssl).toBe(true); |
| 121 | expect(body.urlScheme).toBe("postgresql"); |
| 122 | expect(body.expiresInFuture).toBe(true); |
| 123 | }), |
| 124 | { timeout: 240_000 }, |
| 125 | ); |
| 126 | |
| 127 | test.provider( |
| 128 | "mints named-user temporary credentials (GetClusterCredentials + AutoCreate)", |
| 129 | (_stack) => |
| 130 | Effect.gen(function* () { |
| 131 | const body = yield* getInfo("/info-dbuser"); |
| 132 | expect(body.port).toBe(5439); |
| 133 | // AutoCreate=true prefixes the user with `IAMA:`. |
| 134 | expect(body.username).toBe("IAMA:alchemy_etl"); |
| 135 | expect(body.hasPassword).toBe(true); |
| 136 | expect(body.urlScheme).toBe("postgresql"); |
| 137 | expect(body.expiresInFuture).toBe(true); |
| 138 | }), |
| 139 | { timeout: 240_000 }, |
| 140 | ); |
| 141 | }); |
| 142 | }, |
| 143 | ); |