| 1 | import * as AWS from "@/AWS"; |
| 2 | import * as Core from "@/Test/Core"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as eventbridge from "@distilled.cloud/aws/eventbridge"; |
| 5 | import { describe, expect } from "alchemy-test"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 11 | import GlueTestFunctionLive, { GlueTestFunction } from "./handler"; |
| 12 | |
| 13 | const testOptions = { providers: AWS.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "GlueBindings"); |
| 16 | |
| 17 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 18 | // over 60s on a fresh deploy. |
| 19 | const readinessPolicy = Schedule.max([ |
| 20 | Schedule.fixed("2 seconds"), |
| 21 | Schedule.recurs(75), |
| 22 | ]); |
| 23 | |
| 24 | let baseUrl: string; |
| 25 | let functionArn: string; |
| 26 | |
| 27 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 28 | readonly status: number; |
| 29 | readonly body: string; |
| 30 | }> {} |
| 31 | |
| 32 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 33 | // (cold re-init, IAM propagation on the freshly attached policy that the |
| 34 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 35 | // 4xx/assertion failure surfaces immediately. |
| 36 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 37 | HttpClient.execute(request).pipe( |
| 38 | Effect.flatMap((response) => |
| 39 | response.status >= 500 |
| 40 | ? response.text.pipe( |
| 41 | Effect.flatMap((body) => |
| 42 | Effect.fail( |
| 43 | new TransientUpstream({ status: response.status, body }), |
| 44 | ), |
| 45 | ), |
| 46 | ) |
| 47 | : Effect.succeed(response), |
| 48 | ), |
| 49 | Effect.retry({ |
| 50 | while: (e) => e._tag === "TransientUpstream", |
| 51 | schedule: Schedule.max([ |
| 52 | Schedule.exponential("500 millis"), |
| 53 | Schedule.recurs(6), |
| 54 | ]), |
| 55 | }), |
| 56 | ); |
| 57 | |
| 58 | const getJson = (path: string) => |
| 59 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe( |
| 60 | Effect.flatMap((r) => r.json), |
| 61 | ); |
| 62 | |
| 63 | const postJson = (path: string) => |
| 64 | send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe( |
| 65 | Effect.flatMap((r) => r.json), |
| 66 | ); |
| 67 | |
| 68 | describe.sequential( |
| 69 | "Glue Bindings", |
| 70 | { |
| 71 | tags: [ |
| 72 | "provider:aws", |
| 73 | "provider:aws:batch", |
| 74 | "provider:aws:glue", |
| 75 | "provider:aws:iam", |
| 76 | "provider:aws:lambda", |
| 77 | "provider:aws:s3", |
| 78 | "live", |
| 79 | ], |
| 80 | }, |
| 81 | () => { |
| 82 | beforeAll( |
| 83 | Effect.gen(function* () { |
| 84 | yield* Effect.logInfo("Glue test setup: destroying previous resources"); |
| 85 | yield* sharedStack.destroy(); |
| 86 | |
| 87 | yield* Effect.logInfo("Glue test setup: deploying fixture"); |
| 88 | const attrs = yield* sharedStack.deploy( |
| 89 | Effect.gen(function* () { |
| 90 | return yield* GlueTestFunction; |
| 91 | }).pipe(Effect.provide(GlueTestFunctionLive)), |
| 92 | ); |
| 93 | |
| 94 | expect(attrs.functionUrl).toBeTruthy(); |
| 95 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 96 | functionArn = attrs.functionArn; |
| 97 | |
| 98 | const readinessUrl = `${baseUrl}/bindings`; |
| 99 | yield* Effect.logInfo( |
| 100 | `Glue test setup: probing readiness at ${readinessUrl}`, |
| 101 | ); |
| 102 | yield* HttpClient.get(readinessUrl).pipe( |
| 103 | Effect.flatMap((response) => |
| 104 | response.status === 200 |
| 105 | ? Effect.succeed(response) |
| 106 | : Effect.fail( |
| 107 | new Error(`Function not ready: ${response.status}`), |
| 108 | ), |
| 109 | ), |
| 110 | Effect.tapError((error) => |
| 111 | Effect.logWarning( |
| 112 | `Glue test setup: fixture not ready yet (${String(error)})`, |
| 113 | ), |
| 114 | ), |
| 115 | Effect.retry({ schedule: readinessPolicy }), |
| 116 | ); |
| 117 | |
| 118 | // Glue's authorization layer caches IAM evaluations: a freshly created |
| 119 | // role + inline policy is denied (`AccessDeniedException`) by the Glue |
| 120 | // endpoint for ~3–5 minutes after `PutRolePolicy`, even though the |
| 121 | // policy is attached (verified via CloudTrail: policy attached |
| 122 | // 17:10:47Z, every glue:* call denied until exactly 17:15:50Z). Gate |
| 123 | // the suite on the first job-scoped call succeeding so the tests |
| 124 | // measure binding behavior, not IAM propagation. |
| 125 | yield* Effect.logInfo("Glue test setup: waiting for IAM propagation"); |
| 126 | yield* HttpClient.get(`${baseUrl}/job-runs`).pipe( |
| 127 | Effect.flatMap((response) => |
| 128 | response.status === 200 |
| 129 | ? Effect.succeed(response) |
| 130 | : Effect.fail( |
| 131 | new Error(`Glue IAM not propagated: ${response.status}`), |
| 132 | ), |
| 133 | ), |
| 134 | Effect.retry({ |
| 135 | schedule: Schedule.max([ |
| 136 | Schedule.fixed("5 seconds"), |
| 137 | Schedule.recurs(84), |
| 138 | ]), |
| 139 | }), |
| 140 | ); |
| 141 | }), |
| 142 | { timeout: 900_000 }, |
| 143 | ); |
| 144 | |
| 145 | afterAll.skipIf(!!process.env.NO_DESTROY)(sharedStack.destroy(), { |
| 146 | timeout: 300_000, |
| 147 | }); |
| 148 | |
| 149 | describe("binding registration", () => { |
| 150 | test.provider("all twenty capabilities initialize in the runtime", () => |
| 151 | Effect.gen(function* () { |
| 152 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 153 | expect(response.bound).toHaveLength(20); |
| 154 | }), |
| 155 | ); |
| 156 | }); |
| 157 | |
| 158 | describe("GetJobRuns", () => { |
| 159 | test.provider("lists the job's runs (injected job name)", () => |
| 160 | Effect.gen(function* () { |
| 161 | const response = (yield* getJson("/job-runs")) as { count: number }; |
| 162 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 163 | }), |
| 164 | ); |
| 165 | }); |
| 166 | |
| 167 | describe("GetJobBookmark / ResetJobBookmark", () => { |
| 168 | test.provider("both surface the typed missing-bookmark error", () => |
| 169 | Effect.gen(function* () { |
| 170 | const response = (yield* getJson("/job-bookmark")) as { |
| 171 | bookmark: string; |
| 172 | reset: string; |
| 173 | }; |
| 174 | expect(response.bookmark).toBe("none"); |
| 175 | expect(response.reset).toBe("none"); |
| 176 | }), |
| 177 | ); |
| 178 | }); |
| 179 | |
| 180 | describe("StartJobRun / GetJobRun / BatchStopJobRun", () => { |
| 181 | test.provider( |
| 182 | "starts, reads, and stops a live run", |
| 183 | () => |
| 184 | Effect.gen(function* () { |
| 185 | const response = (yield* postJson("/job-run")) as { |
| 186 | runId: string; |
| 187 | state: string; |
| 188 | stopSubmitted: number; |
| 189 | stopErrors: number; |
| 190 | }; |
| 191 | expect(response.runId).toMatch(/^jr_/); |
| 192 | expect(response.state).toBeTruthy(); |
| 193 | expect(response.stopSubmitted + response.stopErrors).toBe(1); |
| 194 | }), |
| 195 | { timeout: 120_000 }, |
| 196 | ); |
| 197 | }); |
| 198 | |
| 199 | describe("GetCrawler", () => { |
| 200 | test.provider("reads the bound crawler's live state", () => |
| 201 | Effect.gen(function* () { |
| 202 | const response = (yield* getJson("/crawler")) as { |
| 203 | name: string; |
| 204 | state: string; |
| 205 | }; |
| 206 | expect(response.name).toBeTruthy(); |
| 207 | expect(["READY", "RUNNING", "STOPPING"]).toContain(response.state); |
| 208 | }), |
| 209 | ); |
| 210 | }); |
| 211 | |
| 212 | describe("StartCrawler / StopCrawler", () => { |
| 213 | test.provider("stop on an idle crawler is the typed not-running", () => |
| 214 | Effect.gen(function* () { |
| 215 | const response = (yield* postJson("/crawler-stop")) as { |
| 216 | result: string; |
| 217 | }; |
| 218 | expect(response.result).toBe("not-running"); |
| 219 | }), |
| 220 | ); |
| 221 | |
| 222 | test.provider( |
| 223 | "starts a crawl and cancels it", |
| 224 | () => |
| 225 | Effect.gen(function* () { |
| 226 | const response = (yield* postJson("/crawler-run")) as { |
| 227 | started: string; |
| 228 | stopped: string; |
| 229 | }; |
| 230 | expect(["started", "already-running"]).toContain(response.started); |
| 231 | expect(["stopped", "stopping", "not-running"]).toContain( |
| 232 | response.stopped, |
| 233 | ); |
| 234 | }), |
| 235 | { timeout: 120_000 }, |
| 236 | ); |
| 237 | }); |
| 238 | |
| 239 | describe("GetTable / GetTables", () => { |
| 240 | test.provider( |
| 241 | "reads the bound table's schema and the database's tables", |
| 242 | () => |
| 243 | Effect.gen(function* () { |
| 244 | const response = (yield* getJson("/table")) as { |
| 245 | name: string; |
| 246 | columns: string[]; |
| 247 | partitionKeys: string[]; |
| 248 | tables: string[]; |
| 249 | }; |
| 250 | expect(response.columns).toEqual(["id", "amount"]); |
| 251 | expect(response.partitionKeys).toEqual(["dt"]); |
| 252 | expect(response.tables).toContain(response.name); |
| 253 | }), |
| 254 | ); |
| 255 | }); |
| 256 | |
| 257 | describe("Partitions", () => { |
| 258 | test.provider( |
| 259 | "create, get, update, batch-create/update/get, list, batch-delete", |
| 260 | () => |
| 261 | Effect.gen(function* () { |
| 262 | const response = (yield* postJson("/partitions")) as { |
| 263 | created: string[]; |
| 264 | updatedParam: string; |
| 265 | batchErrors: number; |
| 266 | batchUpdateErrors: number; |
| 267 | bulkRead: number; |
| 268 | bulkUpdatedParams: number; |
| 269 | count: number; |
| 270 | batchDeleteErrors: number; |
| 271 | remaining: number; |
| 272 | }; |
| 273 | expect(response.created).toEqual(["2026-01-01"]); |
| 274 | expect(response.updatedParam).toBe("true"); |
| 275 | expect(response.batchErrors).toBe(0); |
| 276 | expect(response.batchUpdateErrors).toBe(0); |
| 277 | expect(response.bulkRead).toBe(3); |
| 278 | expect(response.bulkUpdatedParams).toBe(2); |
| 279 | expect(response.count).toBe(3); |
| 280 | expect(response.batchDeleteErrors).toBe(0); |
| 281 | expect(response.remaining).toBe(0); |
| 282 | }), |
| 283 | { timeout: 120_000 }, |
| 284 | ); |
| 285 | }); |
| 286 | |
| 287 | describe( |
| 288 | "consumeJobEvents / consumeCrawlerEvents", |
| 289 | { tags: ["provider:aws:eventbridge"] }, |
| 290 | () => { |
| 291 | test.provider( |
| 292 | "the deploy created EventBridge rules targeting the function", |
| 293 | () => |
| 294 | Effect.gen(function* () { |
| 295 | // Out-of-band via distilled: the fixture's two consume* calls must |
| 296 | // have materialized as rules on the default bus with the Lambda as |
| 297 | // target. |
| 298 | const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({ |
| 299 | TargetArn: functionArn, |
| 300 | }); |
| 301 | expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(2); |
| 302 | }), |
| 303 | ); |
| 304 | }, |
| 305 | ); |
| 306 | }, |
| 307 | ); |