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 "@distilled.cloud/aws/redshift-data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import RedshiftDataApiFunctionLive, { RedshiftDataApiFunction } from "./fixtures/data-api-handler"; |
| 10 | |
| 11 | const testOptions = { providers: AWS.providers() }; |
| 12 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 13 | const sharedStack = Core.scratchStack(testOptions, "RedshiftDataClient"); |
| 14 | |
| 15 | // --------------------------------------------------------------------------- |
| 16 | // Ungated typed-error probes: prove the distilled redshift-data error union |
| 17 | // carries the tags the Statements client depends on, at near-zero cost. |
| 18 | // --------------------------------------------------------------------------- |
| 19 | |
| 20 | test.provider( |
| 21 | "describeStatement on a nonexistent statement fails with a typed error", |
| 22 | () => |
| 23 | Effect.gen(function* () { |
| 24 | const error = yield* Effect.flip( |
| 25 | data.describeStatement({ Id: "d9b6c0c9-0747-4bf4-b142-e8883122f766" }), |
| 26 | ); |
| 27 | expect(["ResourceNotFoundException", "ValidationException"]).toContain(error._tag); |
| 28 | }), |
| 29 | { tags: ["provider:aws", "provider:aws:redshiftdata", "live"] }, |
| 30 | ); |
| 31 | |
| 32 | test.provider( |
| 33 | "listDatabases against a nonexistent workgroup fails with a typed error", |
| 34 | () => |
| 35 | Effect.gen(function* () { |
| 36 | const error = yield* Effect.flip( |
| 37 | data.listDatabases({ Database: "dev", WorkgroupName: "alchemy-test-rsd-does-not-exist" }), |
| 38 | ); |
| 39 | expect([ |
| 40 | "ValidationException", |
| 41 | "ResourceNotFoundException", |
| 42 | "DatabaseConnectionException", |
| 43 | ]).toContain(error._tag); |
| 44 | }), |
| 45 | { tags: ["provider:aws", "provider:aws:redshiftdata", "live"] }, |
| 46 | ); |
| 47 | |
| 48 | // --------------------------------------------------------------------------- |
| 49 | // Full live suite: deploys a Redshift Serverless namespace + workgroup (a |
| 50 | // real-money RPU floor while it exists) plus a Lambda, so it is skipped under |
| 51 | // `--fast` and destroys everything in afterAll. A run without the env |
| 52 | // var is skip-clean: no AWS calls, no side effects. |
| 53 | // --------------------------------------------------------------------------- |
| 54 | |
| 55 | let baseUrl: string; |
| 56 | |
| 57 | const get = (route: string) => |
| 58 | HttpClient.get(`${baseUrl}${route}`).pipe( |
| 59 | Effect.flatMap((res) => |
| 60 | res.status === 200 |
| 61 | ? Effect.succeed(res) |
| 62 | : res.text.pipe( |
| 63 | Effect.flatMap((body) => |
| 64 | Effect.fail(new Error(`${route} returned ${res.status}: ${body}`)), |
| 65 | ), |
| 66 | ), |
| 67 | ), |
| 68 | Effect.retry({ |
| 69 | schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]), |
| 70 | }), |
| 71 | Effect.flatMap((res) => res.json), |
| 72 | ); |
| 73 | |
| 74 | describe.skipIf(!!process.env.FAST)( |
| 75 | "RedshiftData client", |
| 76 | { |
| 77 | tags: [ |
| 78 | "provider:aws", |
| 79 | "provider:aws:lambda", |
| 80 | "provider:aws:redshiftdata", |
| 81 | "provider:aws:redshiftserverless", |
| 82 | "live", |
| 83 | ], |
| 84 | }, |
| 85 | () => { |
| 86 | beforeAll( |
| 87 | Effect.gen(function* () { |
| 88 | yield* Effect.logInfo("RedshiftData client setup: destroying previous run"); |
| 89 | yield* sharedStack.destroy(); |
| 90 | |
| 91 | yield* Effect.logInfo("RedshiftData client setup: deploying fixture"); |
| 92 | const { functionUrl } = yield* sharedStack.deploy( |
| 93 | Effect.gen(function* () { |
| 94 | return yield* RedshiftDataApiFunction; |
| 95 | }).pipe(Effect.provide(RedshiftDataApiFunctionLive)), |
| 96 | ); |
| 97 | |
| 98 | expect(functionUrl).toBeTruthy(); |
| 99 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 100 | yield* Effect.logInfo(`RedshiftData client setup: function URL ready (${functionUrl})`); |
| 101 | }), |
| 102 | // namespace (~1 min) + workgroup create (~2-5 min) + Lambda deploy. |
| 103 | { timeout: 900_000 }, |
| 104 | ); |
| 105 | |
| 106 | afterAll(sharedStack.destroy(), { timeout: 600_000 }); |
| 107 | |
| 108 | test.provider( |
| 109 | "query: execute + describe + getResult round-trip", |
| 110 | (_stack) => |
| 111 | Effect.gen(function* () { |
| 112 | const body = (yield* get("/query")) as { |
| 113 | columns: (string | undefined)[]; |
| 114 | records: { longValue?: number }[][]; |
| 115 | totalNumRows: number; |
| 116 | }; |
| 117 | expect(body.records).toHaveLength(1); |
| 118 | expect(body.records[0][0]).toMatchObject({ longValue: 1 }); |
| 119 | }), |
| 120 | { timeout: 240_000 }, |
| 121 | ); |
| 122 | |
| 123 | test.provider( |
| 124 | "executeBatch: runs sub-statements and fetches a sub-statement result", |
| 125 | (_stack) => |
| 126 | Effect.gen(function* () { |
| 127 | const body = (yield* get("/batch")) as { |
| 128 | status: string; |
| 129 | subStatementCount: number; |
| 130 | secondRecords: { longValue?: number }[][] | undefined; |
| 131 | }; |
| 132 | expect(body.status).toBe("FINISHED"); |
| 133 | expect(body.subStatementCount).toBe(2); |
| 134 | expect(body.secondRecords?.[0]?.[0]).toMatchObject({ longValue: 2 }); |
| 135 | }), |
| 136 | { timeout: 240_000 }, |
| 137 | ); |
| 138 | |
| 139 | test.provider( |
| 140 | "metadata: listDatabases + listSchemas + listTables + describeTable", |
| 141 | (_stack) => |
| 142 | Effect.gen(function* () { |
| 143 | const body = (yield* get("/metadata")) as { |
| 144 | databases: string[]; |
| 145 | schemas: string[]; |
| 146 | tables: string[]; |
| 147 | columnCount: number; |
| 148 | }; |
| 149 | expect(body.databases).toContain("dev"); |
| 150 | expect(body.schemas).toContain("pg_catalog"); |
| 151 | expect(body.tables).toContain("pg_class"); |
| 152 | expect(body.columnCount).toBeGreaterThan(0); |
| 153 | }), |
| 154 | { timeout: 240_000 }, |
| 155 | ); |
| 156 | |
| 157 | test.provider( |
| 158 | "listStatements: sees the caller's submitted statement", |
| 159 | (_stack) => |
| 160 | Effect.gen(function* () { |
| 161 | const body = (yield* get("/statements")) as { count: number; hasSubmitted: boolean }; |
| 162 | expect(body.count).toBeGreaterThan(0); |
| 163 | expect(body.hasSubmitted).toBe(true); |
| 164 | }), |
| 165 | { timeout: 240_000 }, |
| 166 | ); |
| 167 | |
| 168 | test.provider( |
| 169 | "cancel: cancellation request round-trips", |
| 170 | (_stack) => |
| 171 | Effect.gen(function* () { |
| 172 | const body = (yield* get("/cancel")) as { canceled: boolean }; |
| 173 | // Cancellation races completion; either outcome proves the wiring. |
| 174 | expect(typeof body.canceled).toBe("boolean"); |
| 175 | }), |
| 176 | { timeout: 240_000 }, |
| 177 | ); |
| 178 | |
| 179 | test.provider( |
| 180 | "getResultV2: CSV result format round-trips", |
| 181 | (_stack) => |
| 182 | Effect.gen(function* () { |
| 183 | const body = (yield* get("/result-v2")) as { |
| 184 | status: string; |
| 185 | resultFormat: string | undefined; |
| 186 | records: { CSVRecords?: string }[]; |
| 187 | }; |
| 188 | expect(body.status).toBe("FINISHED"); |
| 189 | expect(body.records.length).toBeGreaterThan(0); |
| 190 | expect(body.records[0]?.CSVRecords).toContain("7"); |
| 191 | }), |
| 192 | { timeout: 240_000 }, |
| 193 | ); |
| 194 | }, |
| 195 | ); |