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 athena from "@distilled.cloud/aws/athena"; |
| 6 | import * as glue from "@distilled.cloud/aws/glue"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import AthenaTestFunctionLive, { AthenaTestFunction } from "./handler"; |
| 13 | |
| 14 | const testOptions = { providers: AWS.providers() }; |
| 15 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 16 | const sharedStack = Core.scratchStack(testOptions, "AthenaQuery"); |
| 17 | |
| 18 | let baseUrl: string; |
| 19 | |
| 20 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 21 | readonly status: number; |
| 22 | readonly body: string; |
| 23 | }> { |
| 24 | // Without this the runner prints a bare `TransientUpstream:` when the retry |
| 25 | // budget is exhausted, which is unactionable — the status and the handler's |
| 26 | // body are the whole diagnosis. |
| 27 | override get message() { |
| 28 | const body = this.body.trim(); |
| 29 | return `upstream returned ${this.status}: ${ |
| 30 | body.length > 800 ? `${body.slice(0, 800)}… (${body.length} bytes)` : body |
| 31 | }`; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // Fresh Lambda role + Athena/S3 permissions propagate eventually — the first |
| 36 | // queries can 500 with AccessDenied under the handler's `Effect.orDie`. |
| 37 | // Retry 5xx only; a genuine 4xx fails immediately. |
| 38 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 39 | HttpClient.execute(request).pipe( |
| 40 | Effect.flatMap((response) => |
| 41 | response.status >= 500 |
| 42 | ? response.text.pipe( |
| 43 | Effect.flatMap((body) => |
| 44 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 45 | ), |
| 46 | ) |
| 47 | : Effect.succeed(response), |
| 48 | ), |
| 49 | Effect.retry({ |
| 50 | while: (e) => e._tag === "TransientUpstream", |
| 51 | // Bounded well under the 180s test timeout (~63s of sleeps) so a |
| 52 | // persistent 500 surfaces its body instead of an opaque timeout. |
| 53 | // 31s was not enough under a loaded full-suite run (`--concurrency 64`), |
| 54 | // where the emulator's Lambda invokes queue behind everything else. |
| 55 | schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(6)]), |
| 56 | }), |
| 57 | ); |
| 58 | |
| 59 | describe( |
| 60 | "Athena Query", |
| 61 | { |
| 62 | tags: [ |
| 63 | "provider:aws", |
| 64 | "provider:aws:athena", |
| 65 | "provider:aws:batch", |
| 66 | "provider:aws:glue", |
| 67 | "provider:aws:lambda", |
| 68 | "provider:aws:s3", |
| 69 | "live", |
| 70 | ], |
| 71 | }, |
| 72 | () => { |
| 73 | beforeAll( |
| 74 | Effect.gen(function* () { |
| 75 | yield* Effect.logInfo("Athena Query setup: destroying previous resources"); |
| 76 | yield* sharedStack.destroy(); |
| 77 | |
| 78 | yield* Effect.logInfo( |
| 79 | "Athena Query setup: deploying bucket -> Glue db/table -> workgroup -> Lambda", |
| 80 | ); |
| 81 | const { functionUrl } = yield* sharedStack.deploy( |
| 82 | Effect.gen(function* () { |
| 83 | return yield* AthenaTestFunction; |
| 84 | }).pipe(Effect.provide(AthenaTestFunctionLive)), |
| 85 | ); |
| 86 | |
| 87 | expect(functionUrl).toBeTruthy(); |
| 88 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 89 | |
| 90 | // Readiness probe — fresh function URLs take seconds to serve 200s. |
| 91 | yield* HttpClient.get(`${baseUrl}/nope`).pipe( |
| 92 | Effect.flatMap((response) => |
| 93 | response.status === 404 |
| 94 | ? Effect.succeed(response) |
| 95 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 96 | ), |
| 97 | Effect.retry({ |
| 98 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]), |
| 99 | }), |
| 100 | ); |
| 101 | }), |
| 102 | { timeout: 300_000 }, |
| 103 | ); |
| 104 | // No NO_DESTROY escape hatch here: scratch-stack state is in-memory per |
| 105 | // process, so a skipped destroy would orphan the whole stack forever. |
| 106 | afterAll( |
| 107 | Effect.gen(function* () { |
| 108 | yield* sharedStack.destroy(); |
| 109 | |
| 110 | // Assert-gone out-of-band via the fixture's fixed-name resources — |
| 111 | // engine-named resources (bucket/workgroup/Lambda) hang off the same |
| 112 | // stack, so these two vanishing proves the destroy actually ran. |
| 113 | // `afterAll` effects don't get the providers layer automatically the |
| 114 | // way `test.provider` bodies do, so wrap the distilled calls in |
| 115 | // `Core.withProviders` to supply AWS credentials. |
| 116 | yield* Core.withProviders( |
| 117 | Effect.gen(function* () { |
| 118 | const db = yield* glue.getDatabase({ Name: "alchemy_athena_e2e" }).pipe( |
| 119 | Effect.map((res) => res.Database), |
| 120 | Effect.catchTag("EntityNotFoundException", () => Effect.succeed(undefined)), |
| 121 | ); |
| 122 | expect(db).toBeUndefined(); |
| 123 | |
| 124 | const catalog = yield* athena |
| 125 | .getDataCatalog({ Name: "alchemy_athena_e2e_catalog", WorkGroup: "primary" }) |
| 126 | .pipe( |
| 127 | Effect.map((res) => res.DataCatalog), |
| 128 | Effect.catchTag("DataCatalogNotFound", () => Effect.succeed(undefined)), |
| 129 | ); |
| 130 | expect(catalog).toBeUndefined(); |
| 131 | }), |
| 132 | testOptions, |
| 133 | sharedStack.name, |
| 134 | ); |
| 135 | }), |
| 136 | { timeout: 300_000 }, |
| 137 | ); |
| 138 | |
| 139 | test.provider( |
| 140 | "runs SELECT 1 through the binding (execute + poll + results)", |
| 141 | () => |
| 142 | Effect.gen(function* () { |
| 143 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/select-one`)); |
| 144 | expect(response.status).toBe(200); |
| 145 | const body = (yield* response.json) as { |
| 146 | state: string; |
| 147 | columns: string[]; |
| 148 | rows: string[][]; |
| 149 | }; |
| 150 | expect(body.state).toBe("SUCCEEDED"); |
| 151 | // SELECT 1 → header row then the value row. |
| 152 | expect(body.rows.at(-1)?.[0]).toBe("1"); |
| 153 | }), |
| 154 | { timeout: 180_000 }, |
| 155 | ); |
| 156 | |
| 157 | test.provider( |
| 158 | "runs SELECT COUNT(*) over a Glue table on S3 CSV data", |
| 159 | () => |
| 160 | Effect.gen(function* () { |
| 161 | // Seed the CSV source data first. |
| 162 | const seeded = yield* send(HttpClientRequest.post(`${baseUrl}/seed`)); |
| 163 | expect(seeded.status).toBe(200); |
| 164 | |
| 165 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/count`)); |
| 166 | expect(response.status).toBe(200); |
| 167 | const body = (yield* response.json) as { |
| 168 | state: string; |
| 169 | columns: string[]; |
| 170 | rows: string[][]; |
| 171 | }; |
| 172 | expect(body.state).toBe("SUCCEEDED"); |
| 173 | // Three CSV rows → COUNT(*) = 3 (last row is the value, first is header). |
| 174 | expect(body.rows.at(-1)?.[0]).toBe("3"); |
| 175 | }), |
| 176 | { timeout: 180_000 }, |
| 177 | ); |
| 178 | |
| 179 | // One completed SELECT 1 execution shared by every query-execution binding |
| 180 | // below (each vitest file runs its tests sequentially). |
| 181 | let execId: string | undefined; |
| 182 | const ensureExecId = Effect.gen(function* () { |
| 183 | if (execId) return execId; |
| 184 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/exec/run`)); |
| 185 | expect(response.status).toBe(200); |
| 186 | const body = (yield* response.json) as { id: string }; |
| 187 | execId = body.id; |
| 188 | return body.id; |
| 189 | }); |
| 190 | |
| 191 | describe("GetQueryExecution", () => { |
| 192 | test.provider( |
| 193 | "reads the execution's terminal state", |
| 194 | () => |
| 195 | Effect.gen(function* () { |
| 196 | const id = yield* ensureExecId; |
| 197 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/exec/get?id=${id}`)); |
| 198 | expect(response.status).toBe(200); |
| 199 | const body = (yield* response.json) as { state: string; workGroup: string }; |
| 200 | expect(body.state).toBe("SUCCEEDED"); |
| 201 | expect(body.workGroup).toBeTruthy(); |
| 202 | }), |
| 203 | { timeout: 120_000 }, |
| 204 | ); |
| 205 | }); |
| 206 | |
| 207 | describe("GetQueryResults", () => { |
| 208 | test.provider( |
| 209 | "reads the raw result page", |
| 210 | () => |
| 211 | Effect.gen(function* () { |
| 212 | const id = yield* ensureExecId; |
| 213 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/exec/results?id=${id}`)); |
| 214 | expect(response.status).toBe(200); |
| 215 | const body = (yield* response.json) as { rows: number; columns: string[] }; |
| 216 | // SELECT 1 → header row + value row. |
| 217 | expect(body.rows).toBe(2); |
| 218 | expect(body.columns.length).toBe(1); |
| 219 | }), |
| 220 | { timeout: 120_000 }, |
| 221 | ); |
| 222 | }); |
| 223 | |
| 224 | describe("GetQueryRuntimeStatistics", () => { |
| 225 | test.provider( |
| 226 | "reads the execution timeline", |
| 227 | () => |
| 228 | Effect.gen(function* () { |
| 229 | const id = yield* ensureExecId; |
| 230 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/exec/stats?id=${id}`)); |
| 231 | expect(response.status).toBe(200); |
| 232 | const body = (yield* response.json) as { totalMillis: number }; |
| 233 | expect(body.totalMillis).toBeGreaterThan(0); |
| 234 | }), |
| 235 | { timeout: 120_000 }, |
| 236 | ); |
| 237 | }); |
| 238 | |
| 239 | describe("BatchGetQueryExecution", () => { |
| 240 | test.provider( |
| 241 | "reads executions in bulk", |
| 242 | () => |
| 243 | Effect.gen(function* () { |
| 244 | const id = yield* ensureExecId; |
| 245 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/exec/batch?id=${id}`)); |
| 246 | expect(response.status).toBe(200); |
| 247 | const body = (yield* response.json) as { states: string[] }; |
| 248 | expect(body.states).toEqual(["SUCCEEDED"]); |
| 249 | }), |
| 250 | { timeout: 120_000 }, |
| 251 | ); |
| 252 | }); |
| 253 | |
| 254 | describe("ListQueryExecutions", () => { |
| 255 | test.provider( |
| 256 | "lists the workgroup's executions (workgroup injected)", |
| 257 | () => |
| 258 | Effect.gen(function* () { |
| 259 | const id = yield* ensureExecId; |
| 260 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/exec/list`)); |
| 261 | expect(response.status).toBe(200); |
| 262 | const body = (yield* response.json) as { ids: string[] }; |
| 263 | expect(body.ids).toContain(id); |
| 264 | }), |
| 265 | { timeout: 120_000 }, |
| 266 | ); |
| 267 | }); |
| 268 | |
| 269 | describe("StopQueryExecution", () => { |
| 270 | test.provider( |
| 271 | "stop on a completed execution is an idempotent no-op", |
| 272 | () => |
| 273 | Effect.gen(function* () { |
| 274 | const id = yield* ensureExecId; |
| 275 | const stopped = yield* send(HttpClientRequest.post(`${baseUrl}/exec/stop?id=${id}`)); |
| 276 | expect(stopped.status).toBe(200); |
| 277 | // Still SUCCEEDED — stop did not flip a terminal state. |
| 278 | const after = yield* send(HttpClientRequest.get(`${baseUrl}/exec/get?id=${id}`)); |
| 279 | const body = (yield* after.json) as { state: string }; |
| 280 | expect(body.state).toBe("SUCCEEDED"); |
| 281 | }), |
| 282 | { timeout: 120_000 }, |
| 283 | ); |
| 284 | }); |
| 285 | |
| 286 | describe("ListNamedQueries", () => { |
| 287 | test.provider( |
| 288 | "lists the workgroup's saved queries (workgroup injected)", |
| 289 | () => |
| 290 | Effect.gen(function* () { |
| 291 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/named/list`)); |
| 292 | expect(response.status).toBe(200); |
| 293 | const body = (yield* response.json) as { ids: string[] }; |
| 294 | // The fixture saves exactly one named query in the (fresh) workgroup. |
| 295 | expect(body.ids.length).toBeGreaterThanOrEqual(1); |
| 296 | }), |
| 297 | { timeout: 120_000 }, |
| 298 | ); |
| 299 | }); |
| 300 | |
| 301 | describe("ListPreparedStatements", () => { |
| 302 | test.provider( |
| 303 | "lists the workgroup's prepared statements (workgroup injected)", |
| 304 | () => |
| 305 | Effect.gen(function* () { |
| 306 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/prepared/list`)); |
| 307 | expect(response.status).toBe(200); |
| 308 | const body = (yield* response.json) as { names: string[] }; |
| 309 | expect(body.names).toContain("alchemy_athena_e2e_stmt"); |
| 310 | }), |
| 311 | { timeout: 120_000 }, |
| 312 | ); |
| 313 | }); |
| 314 | |
| 315 | describe("ListDatabases", () => { |
| 316 | test.provider( |
| 317 | "lists databases through the GLUE-backed catalog", |
| 318 | () => |
| 319 | Effect.gen(function* () { |
| 320 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/catalog/databases`)); |
| 321 | expect(response.status).toBe(200); |
| 322 | const body = (yield* response.json) as { names: string[] }; |
| 323 | expect(body.names).toContain("alchemy_athena_e2e"); |
| 324 | }), |
| 325 | { timeout: 120_000 }, |
| 326 | ); |
| 327 | }); |
| 328 | |
| 329 | describe("GetDatabase", () => { |
| 330 | test.provider( |
| 331 | "reads a single database's metadata", |
| 332 | () => |
| 333 | Effect.gen(function* () { |
| 334 | const response = yield* send( |
| 335 | HttpClientRequest.get(`${baseUrl}/catalog/database?name=alchemy_athena_e2e`), |
| 336 | ); |
| 337 | expect(response.status).toBe(200); |
| 338 | const body = (yield* response.json) as { name: string }; |
| 339 | expect(body.name).toBe("alchemy_athena_e2e"); |
| 340 | }), |
| 341 | { timeout: 120_000 }, |
| 342 | ); |
| 343 | }); |
| 344 | |
| 345 | describe("ListTableMetadata", () => { |
| 346 | test.provider( |
| 347 | "lists the database's tables", |
| 348 | () => |
| 349 | Effect.gen(function* () { |
| 350 | const response = yield* send( |
| 351 | HttpClientRequest.get(`${baseUrl}/catalog/tables?db=alchemy_athena_e2e`), |
| 352 | ); |
| 353 | expect(response.status).toBe(200); |
| 354 | const body = (yield* response.json) as { names: string[] }; |
| 355 | expect(body.names).toContain("people"); |
| 356 | }), |
| 357 | { timeout: 120_000 }, |
| 358 | ); |
| 359 | }); |
| 360 | |
| 361 | describe("GetTableMetadata", () => { |
| 362 | test.provider( |
| 363 | "reads a table's columns", |
| 364 | () => |
| 365 | Effect.gen(function* () { |
| 366 | const response = yield* send( |
| 367 | HttpClientRequest.get(`${baseUrl}/catalog/table?db=alchemy_athena_e2e&name=people`), |
| 368 | ); |
| 369 | expect(response.status).toBe(200); |
| 370 | const body = (yield* response.json) as { columns: string[] }; |
| 371 | expect(body.columns).toEqual(["id", "name"]); |
| 372 | }), |
| 373 | { timeout: 120_000 }, |
| 374 | ); |
| 375 | }); |
| 376 | |
| 377 | describe("QueryStateChangeEventSource", () => { |
| 378 | test.provider( |
| 379 | "delivers query state-change events to the handler", |
| 380 | () => |
| 381 | Effect.gen(function* () { |
| 382 | // The probe runs a query and polls S3 for the marker the event |
| 383 | // handler writes; retry the whole probe a few times to ride out |
| 384 | // fresh-rule propagation on the default bus. |
| 385 | const seen = yield* Effect.gen(function* () { |
| 386 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/events/probe`)); |
| 387 | expect(response.status).toBe(200); |
| 388 | const body = (yield* response.json) as { seen: boolean; id: string }; |
| 389 | return body.seen; |
| 390 | }).pipe( |
| 391 | Effect.repeat({ |
| 392 | schedule: Schedule.spaced("2 seconds"), |
| 393 | until: (s): boolean => s, |
| 394 | times: 3, |
| 395 | }), |
| 396 | ); |
| 397 | expect(seen).toBe(true); |
| 398 | }), |
| 399 | { timeout: 180_000 }, |
| 400 | ); |
| 401 | }); |
| 402 | }, |
| 403 | ); |