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 analytics from "@distilled.cloud/aws/kinesis-analytics-v2"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { deleteCodeBucketIdempotent, provisionCodeBucket } from "./code-bucket.ts"; |
| 12 | import KinesisAnalyticsV2TestFunctionLive, { |
| 13 | FIXTURE_CODE_BUCKET, |
| 14 | KinesisAnalyticsV2TestFunction, |
| 15 | } from "./handler"; |
| 16 | |
| 17 | const testOptions = { providers: AWS.providers() }; |
| 18 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 19 | const sharedStack = Core.scratchStack(testOptions, "KinesisAnalyticsV2Bindings"); |
| 20 | |
| 21 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 22 | // over 60s on a fresh deploy. |
| 23 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 24 | |
| 25 | let baseUrl: string; |
| 26 | // Physical name of the Application the fixture creates — captured in |
| 27 | // beforeAll so afterAll can assert it is GONE out-of-band after destroy. |
| 28 | let applicationName: string | undefined; |
| 29 | |
| 30 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 31 | readonly status: number; |
| 32 | readonly body: string; |
| 33 | }> {} |
| 34 | |
| 35 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 36 | // (cold re-init, IAM propagation on the freshly attached policy). Retry only |
| 37 | // 5xx; a genuine 4xx/assertion failure surfaces 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 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | const getJson = (path: string) => |
| 56 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 57 | |
| 58 | const postJson = (path: string) => |
| 59 | send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 60 | |
| 61 | describe.sequential( |
| 62 | "KinesisAnalyticsV2 Bindings", |
| 63 | { |
| 64 | tags: [ |
| 65 | "provider:aws", |
| 66 | "provider:aws:kinesisanalyticsv2", |
| 67 | "provider:aws:lambda", |
| 68 | "provider:aws:s3", |
| 69 | "live", |
| 70 | ], |
| 71 | }, |
| 72 | () => { |
| 73 | beforeAll( |
| 74 | Effect.gen(function* () { |
| 75 | yield* Effect.logInfo("KinesisAnalyticsV2 test setup: destroying previous resources"); |
| 76 | yield* sharedStack.destroy(); |
| 77 | |
| 78 | // Stage the code object BEFORE the Application resource deploys — the |
| 79 | // service reads (and hashes) the object at create time. Direct |
| 80 | // distilled calls need the AWS provider context (credentials, region) |
| 81 | // that `test.provider` bodies get implicitly. |
| 82 | yield* Core.withProviders( |
| 83 | provisionCodeBucket(FIXTURE_CODE_BUCKET), |
| 84 | testOptions, |
| 85 | "KinesisAnalyticsV2Bindings", |
| 86 | ); |
| 87 | |
| 88 | yield* Effect.logInfo("KinesisAnalyticsV2 test setup: deploying fixture"); |
| 89 | const { functionUrl } = yield* sharedStack.deploy( |
| 90 | Effect.gen(function* () { |
| 91 | return yield* KinesisAnalyticsV2TestFunction; |
| 92 | }).pipe(Effect.provide(KinesisAnalyticsV2TestFunctionLive)), |
| 93 | ); |
| 94 | |
| 95 | expect(functionUrl).toBeTruthy(); |
| 96 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 97 | |
| 98 | const readinessUrl = `${baseUrl}/bindings`; |
| 99 | yield* Effect.logInfo( |
| 100 | `KinesisAnalyticsV2 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(new Error(`Function not ready: ${response.status}`)), |
| 107 | ), |
| 108 | Effect.tapError((error) => |
| 109 | Effect.logWarning( |
| 110 | `KinesisAnalyticsV2 test setup: fixture not ready yet (${String(error)})`, |
| 111 | ), |
| 112 | ), |
| 113 | Effect.retry({ schedule: readinessPolicy }), |
| 114 | ); |
| 115 | |
| 116 | // `/bindings` only proves the function URL is up. The first real |
| 117 | // SDK call (`DescribeApplication`) can still 200 with `{ errorTag }` |
| 118 | // while IAM on the freshly attached policy is propagating — poll |
| 119 | // until the application name is present. |
| 120 | const described = yield* getJson("/describe").pipe( |
| 121 | Effect.flatMap((body) => { |
| 122 | const name = (body as { name?: string }).name; |
| 123 | return typeof name === "string" && name.length > 0 |
| 124 | ? Effect.succeed(body as { name: string }) |
| 125 | : Effect.fail( |
| 126 | new Error(`KinesisAnalyticsV2 describe not ready yet (${JSON.stringify(body)})`), |
| 127 | ); |
| 128 | }), |
| 129 | Effect.retry({ schedule: readinessPolicy }), |
| 130 | ); |
| 131 | applicationName = described.name; |
| 132 | }), |
| 133 | { timeout: 300_000 }, |
| 134 | ); |
| 135 | |
| 136 | afterAll( |
| 137 | Effect.gen(function* () { |
| 138 | yield* sharedStack.destroy(); |
| 139 | |
| 140 | // Assert the fixture Application is GONE out-of-band — the trailing |
| 141 | // destroy must leave zero orphaned cloud resources. |
| 142 | if (applicationName !== undefined) { |
| 143 | const gone = yield* Core.withProviders( |
| 144 | analytics.describeApplication({ ApplicationName: applicationName }).pipe(Effect.flip), |
| 145 | testOptions, |
| 146 | "KinesisAnalyticsV2Bindings", |
| 147 | ); |
| 148 | expect(gone._tag).toEqual("ResourceNotFoundException"); |
| 149 | } |
| 150 | }).pipe( |
| 151 | Effect.ensuring( |
| 152 | Core.withProviders( |
| 153 | deleteCodeBucketIdempotent(FIXTURE_CODE_BUCKET), |
| 154 | testOptions, |
| 155 | "KinesisAnalyticsV2Bindings", |
| 156 | ), |
| 157 | ), |
| 158 | ), |
| 159 | { timeout: 180_000 }, |
| 160 | ); |
| 161 | |
| 162 | describe("binding registration", () => { |
| 163 | test.provider("all 14 capabilities initialize in the runtime", (_stack) => |
| 164 | Effect.gen(function* () { |
| 165 | const response = yield* getJson("/bindings"); |
| 166 | expect((response as any).bound).toHaveLength(14); |
| 167 | }), |
| 168 | ); |
| 169 | }); |
| 170 | |
| 171 | describe("DescribeApplication", () => { |
| 172 | test.provider("reads the READY application detail", (_stack) => |
| 173 | Effect.gen(function* () { |
| 174 | const response = (yield* getJson("/describe")) as { status: string; runtime: string }; |
| 175 | expect(response.status).toBe("READY"); |
| 176 | expect(response.runtime).toBe("FLINK-1_20"); |
| 177 | }), |
| 178 | ); |
| 179 | }); |
| 180 | |
| 181 | describe("ListApplications", () => { |
| 182 | test.provider("account list contains the bound application", (_stack) => |
| 183 | Effect.gen(function* () { |
| 184 | const response = (yield* getJson("/applications")) as { |
| 185 | count?: number; |
| 186 | containsSelf?: boolean; |
| 187 | errorTag?: string; |
| 188 | }; |
| 189 | expect(response.errorTag).toBeUndefined(); |
| 190 | expect(response.containsSelf).toBe(true); |
| 191 | }), |
| 192 | ); |
| 193 | }); |
| 194 | |
| 195 | describe("ListApplicationVersions", () => { |
| 196 | test.provider("lists at least the initial version", (_stack) => |
| 197 | Effect.gen(function* () { |
| 198 | const response = (yield* getJson("/versions")) as { count: number }; |
| 199 | expect(response.count).toBeGreaterThanOrEqual(1); |
| 200 | }), |
| 201 | ); |
| 202 | }); |
| 203 | |
| 204 | describe("DescribeApplicationVersion", () => { |
| 205 | test.provider("reads version 1", (_stack) => |
| 206 | Effect.gen(function* () { |
| 207 | const response = (yield* getJson("/version")) as { versionId: number }; |
| 208 | expect(response.versionId).toBe(1); |
| 209 | }), |
| 210 | ); |
| 211 | }); |
| 212 | |
| 213 | describe("ListApplicationOperations", () => { |
| 214 | test.provider("lists the (possibly empty) operation history", (_stack) => |
| 215 | Effect.gen(function* () { |
| 216 | const response = (yield* getJson("/operations")) as { count: number }; |
| 217 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 218 | }), |
| 219 | ); |
| 220 | }); |
| 221 | |
| 222 | describe("DescribeApplicationOperation", () => { |
| 223 | test.provider( |
| 224 | "surfaces a typed tag for a nonexistent operation (proving the grant)", |
| 225 | (_stack) => |
| 226 | Effect.gen(function* () { |
| 227 | const response = (yield* getJson("/operation")) as { errorTag: string }; |
| 228 | expect([ |
| 229 | "ResourceNotFoundException", |
| 230 | "InvalidArgumentException", |
| 231 | "UnsupportedOperationException", |
| 232 | ]).toContain(response.errorTag); |
| 233 | }), |
| 234 | ); |
| 235 | }); |
| 236 | |
| 237 | describe("ListApplicationSnapshots", () => { |
| 238 | test.provider("lists zero snapshots on the fresh application", (_stack) => |
| 239 | Effect.gen(function* () { |
| 240 | const response = (yield* getJson("/snapshots")) as { count: number }; |
| 241 | expect(response.count).toBe(0); |
| 242 | }), |
| 243 | ); |
| 244 | }); |
| 245 | |
| 246 | describe("DescribeApplicationSnapshot", () => { |
| 247 | test.provider("surfaces the typed not-found tag for a nonexistent snapshot", (_stack) => |
| 248 | Effect.gen(function* () { |
| 249 | const response = (yield* getJson("/snapshot")) as { errorTag: string }; |
| 250 | expect(response.errorTag).toBe("ResourceNotFoundException"); |
| 251 | }), |
| 252 | ); |
| 253 | }); |
| 254 | |
| 255 | describe("CreateApplicationSnapshot", () => { |
| 256 | test.provider("reaches the typed not-running rejection (proving the grant)", (_stack) => |
| 257 | Effect.gen(function* () { |
| 258 | const response = (yield* postJson("/snapshot/create")) as { errorTag: string }; |
| 259 | expect([ |
| 260 | "InvalidApplicationConfigurationException", |
| 261 | "InvalidRequestException", |
| 262 | "ResourceInUseException", |
| 263 | "UnsupportedOperationException", |
| 264 | ]).toContain(response.errorTag); |
| 265 | }), |
| 266 | ); |
| 267 | }); |
| 268 | |
| 269 | describe("DeleteApplicationSnapshot", () => { |
| 270 | test.provider( |
| 271 | "surfaces a typed tag for a nonexistent snapshot (proving the grant)", |
| 272 | (_stack) => |
| 273 | Effect.gen(function* () { |
| 274 | const response = (yield* postJson("/snapshot/delete")) as { errorTag: string }; |
| 275 | expect([ |
| 276 | "ResourceNotFoundException", |
| 277 | "InvalidArgumentException", |
| 278 | "InvalidRequestException", |
| 279 | ]).toContain(response.errorTag); |
| 280 | }), |
| 281 | ); |
| 282 | }); |
| 283 | |
| 284 | describe("CreateApplicationPresignedUrl", () => { |
| 285 | test.provider("mints a URL or reaches a typed rejection (proving the grant)", (_stack) => |
| 286 | Effect.gen(function* () { |
| 287 | const response = (yield* getJson("/presigned-url")) as { |
| 288 | hasUrl?: boolean; |
| 289 | errorTag?: string; |
| 290 | }; |
| 291 | if (response.errorTag !== undefined) { |
| 292 | expect([ |
| 293 | "ResourceNotFoundException", |
| 294 | "ResourceInUseException", |
| 295 | "InvalidArgumentException", |
| 296 | ]).toContain(response.errorTag); |
| 297 | } else { |
| 298 | expect(response.hasUrl).toBe(true); |
| 299 | } |
| 300 | }), |
| 301 | ); |
| 302 | }); |
| 303 | |
| 304 | describe("RollbackApplication", () => { |
| 305 | test.provider( |
| 306 | "reaches the typed nothing-to-roll-back rejection (proving the grant)", |
| 307 | (_stack) => |
| 308 | Effect.gen(function* () { |
| 309 | const response = (yield* postJson("/rollback")) as { errorTag: string }; |
| 310 | expect([ |
| 311 | "InvalidRequestException", |
| 312 | "InvalidArgumentException", |
| 313 | "ResourceInUseException", |
| 314 | "ConcurrentModificationException", |
| 315 | "UnsupportedOperationException", |
| 316 | ]).toContain(response.errorTag); |
| 317 | }), |
| 318 | ); |
| 319 | }); |
| 320 | |
| 321 | describe("StopApplication", () => { |
| 322 | test.provider("reaches the typed not-running rejection (proving the grant)", (_stack) => |
| 323 | Effect.gen(function* () { |
| 324 | const response = (yield* postJson("/stop")) as { errorTag?: string; ok?: boolean }; |
| 325 | if (response.errorTag !== undefined) { |
| 326 | expect([ |
| 327 | "InvalidApplicationConfigurationException", |
| 328 | "InvalidRequestException", |
| 329 | "ResourceInUseException", |
| 330 | "ConcurrentModificationException", |
| 331 | ]).toContain(response.errorTag); |
| 332 | } else { |
| 333 | expect(response.ok).toBe(true); |
| 334 | } |
| 335 | }), |
| 336 | ); |
| 337 | }); |
| 338 | |
| 339 | // Last on purpose: if the service unexpectedly accepts the start, the |
| 340 | // route force-stops immediately and no other test observes the transition. |
| 341 | describe("StartApplication", () => { |
| 342 | test.provider( |
| 343 | "rejects a restore from a nonexistent snapshot (proving the grant)", |
| 344 | (_stack) => |
| 345 | Effect.gen(function* () { |
| 346 | const response = (yield* postJson("/start")) as { |
| 347 | errorTag?: string; |
| 348 | started?: boolean; |
| 349 | }; |
| 350 | if (response.errorTag !== undefined) { |
| 351 | expect([ |
| 352 | "ResourceNotFoundException", |
| 353 | "InvalidArgumentException", |
| 354 | "InvalidApplicationConfigurationException", |
| 355 | "InvalidRequestException", |
| 356 | "ResourceInUseException", |
| 357 | ]).toContain(response.errorTag); |
| 358 | } else { |
| 359 | expect(response.started).toBe(true); |
| 360 | } |
| 361 | }), |
| 362 | { timeout: 120_000 }, |
| 363 | ); |
| 364 | }); |
| 365 | }, |
| 366 | ); |