The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 iam from "@distilled.cloud/aws/iam"; |
| 6 | import * as licensemanager from "@distilled.cloud/aws/license-manager"; |
| 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 LicenseManagerTestFunctionLive, { LicenseManagerTestFunction } from "./handler"; |
| 13 | |
| 14 | const testOptions = { providers: AWS.providers() }; |
| 15 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 16 | const sharedStack = Core.scratchStack(testOptions, "LicenseManagerBindings"); |
| 17 | const RUN_CREATE_LIFECYCLE = process.env.AWS_TEST_LICENSE_MANAGER_CREATE === "1"; |
| 18 | |
| 19 | // NOTE: CreateLicenseConfiguration has a small DAILY account quota (~10 |
| 20 | // creates/day; deletes do NOT refund it — soft-deleted configurations still |
| 21 | // count). This suite's fixture consumes 1 create per run, so keep the whole |
| 22 | // shared fixture behind the same explicit gate as LicenseConfiguration.test. |
| 23 | // Otherwise a quota rejection in beforeAll cascades into 16 misleading test |
| 24 | // failures. Enable with AWS_TEST_LICENSE_MANAGER_CREATE=1 on a fresh quota |
| 25 | // day to exercise the live binding fixture. |
| 26 | |
| 27 | // License Manager requires one-time account onboarding (see |
| 28 | // LicenseConfiguration.test.ts): create the service-linked role |
| 29 | // idempotently, then probe with a bounded typed retry through IAM |
| 30 | // propagation. |
| 31 | const ensureOnboarded = Effect.gen(function* () { |
| 32 | yield* iam |
| 33 | .createServiceLinkedRole({ AWSServiceName: "license-manager.amazonaws.com" }) |
| 34 | .pipe(Effect.catchTag("InvalidInputException", () => Effect.void)); |
| 35 | yield* licensemanager.listLicenseConfigurations({ MaxResults: 1 }).pipe( |
| 36 | Effect.retry({ |
| 37 | while: (e) => e._tag === "AccessDeniedException", |
| 38 | schedule: Schedule.exponential("1 second"), |
| 39 | times: 8, |
| 40 | }), |
| 41 | ); |
| 42 | }); |
| 43 | |
| 44 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 45 | // over 60s on a fresh deploy. |
| 46 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 47 | |
| 48 | let baseUrl: string; |
| 49 | |
| 50 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 51 | readonly status: number; |
| 52 | readonly body: string; |
| 53 | }> {} |
| 54 | |
| 55 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 56 | // (cold re-init, IAM propagation on the freshly attached policy that the |
| 57 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 58 | // 4xx/assertion failure surfaces immediately. |
| 59 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 60 | HttpClient.execute(request).pipe( |
| 61 | Effect.flatMap((response) => |
| 62 | response.status >= 500 |
| 63 | ? response.text.pipe( |
| 64 | Effect.flatMap((body) => |
| 65 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 66 | ), |
| 67 | ) |
| 68 | : Effect.succeed(response), |
| 69 | ), |
| 70 | Effect.retry({ |
| 71 | while: (e) => e._tag === "TransientUpstream", |
| 72 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 73 | }), |
| 74 | ); |
| 75 | |
| 76 | const getJson = (path: string) => |
| 77 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 78 | |
| 79 | const describeCreateLifecycle = describe.skipIf(!RUN_CREATE_LIFECYCLE); |
| 80 | |
| 81 | describeCreateLifecycle.sequential("LicenseManager Bindings", () => { |
| 82 | beforeAll( |
| 83 | Effect.gen(function* () { |
| 84 | // beforeAll runs outside the provider context — provide the AWS |
| 85 | // client environment explicitly for the raw distilled calls. |
| 86 | yield* Core.withProviders(ensureOnboarded, testOptions, "LicenseManagerBindings"); |
| 87 | |
| 88 | yield* Effect.logInfo("LicenseManager test setup: destroying previous resources"); |
| 89 | yield* sharedStack.destroy(); |
| 90 | |
| 91 | yield* Effect.logInfo("LicenseManager test setup: deploying fixture"); |
| 92 | const { functionUrl } = yield* sharedStack.deploy( |
| 93 | Effect.gen(function* () { |
| 94 | return yield* LicenseManagerTestFunction; |
| 95 | }).pipe(Effect.provide(LicenseManagerTestFunctionLive)), |
| 96 | ); |
| 97 | |
| 98 | expect(functionUrl).toBeTruthy(); |
| 99 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 100 | |
| 101 | const readinessUrl = `${baseUrl}/bindings`; |
| 102 | yield* Effect.logInfo(`LicenseManager test setup: probing readiness at ${readinessUrl}`); |
| 103 | yield* HttpClient.get(readinessUrl).pipe( |
| 104 | Effect.flatMap((response) => |
| 105 | response.status === 200 |
| 106 | ? Effect.succeed(response) |
| 107 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 108 | ), |
| 109 | Effect.tapError((error) => |
| 110 | Effect.logWarning(`LicenseManager test setup: fixture not ready yet (${String(error)})`), |
| 111 | ), |
| 112 | Effect.retry({ schedule: readinessPolicy }), |
| 113 | ); |
| 114 | }), |
| 115 | { timeout: 240_000 }, |
| 116 | ); |
| 117 | |
| 118 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 119 | |
| 120 | describe( |
| 121 | "binding registration", |
| 122 | { |
| 123 | tags: [ |
| 124 | "provider:aws", |
| 125 | "provider:aws:iam", |
| 126 | "provider:aws:lambda", |
| 127 | "provider:aws:licensemanager", |
| 128 | "live", |
| 129 | ], |
| 130 | }, |
| 131 | () => { |
| 132 | test.provider("all 23 capabilities initialize in the runtime", (_stack) => |
| 133 | Effect.gen(function* () { |
| 134 | const response = yield* getJson("/bindings"); |
| 135 | expect((response as any).bound).toHaveLength(23); |
| 136 | }), |
| 137 | ); |
| 138 | }, |
| 139 | ); |
| 140 | |
| 141 | describe( |
| 142 | "GetLicenseConfiguration", |
| 143 | { |
| 144 | tags: [ |
| 145 | "provider:aws", |
| 146 | "provider:aws:iam", |
| 147 | "provider:aws:lambda", |
| 148 | "provider:aws:licensemanager", |
| 149 | "live", |
| 150 | ], |
| 151 | }, |
| 152 | () => { |
| 153 | test.provider( |
| 154 | "reads the bound configuration (proving ARN injection + the scoped grant)", |
| 155 | (_stack) => |
| 156 | Effect.gen(function* () { |
| 157 | const response = (yield* getJson("/configuration")) as any; |
| 158 | expect(response.countingType).toBe("vCPU"); |
| 159 | expect(response.licenseCount).toBe(5); |
| 160 | expect(response.name).toBeTruthy(); |
| 161 | }), |
| 162 | { timeout: 60_000 }, |
| 163 | ); |
| 164 | }, |
| 165 | ); |
| 166 | |
| 167 | describe( |
| 168 | "ListAssociationsForLicenseConfiguration", |
| 169 | { |
| 170 | tags: [ |
| 171 | "provider:aws", |
| 172 | "provider:aws:iam", |
| 173 | "provider:aws:lambda", |
| 174 | "provider:aws:licensemanager", |
| 175 | "live", |
| 176 | ], |
| 177 | }, |
| 178 | () => { |
| 179 | test.provider( |
| 180 | "lists the configuration's associations", |
| 181 | (_stack) => |
| 182 | Effect.gen(function* () { |
| 183 | const response = (yield* getJson("/associations")) as any; |
| 184 | expect(response.count).toBe(0); |
| 185 | }), |
| 186 | { timeout: 60_000 }, |
| 187 | ); |
| 188 | }, |
| 189 | ); |
| 190 | |
| 191 | describe( |
| 192 | "ListUsageForLicenseConfiguration", |
| 193 | { |
| 194 | tags: [ |
| 195 | "provider:aws", |
| 196 | "provider:aws:iam", |
| 197 | "provider:aws:lambda", |
| 198 | "provider:aws:licensemanager", |
| 199 | "live", |
| 200 | ], |
| 201 | }, |
| 202 | () => { |
| 203 | test.provider( |
| 204 | "lists per-resource usage", |
| 205 | (_stack) => |
| 206 | Effect.gen(function* () { |
| 207 | const response = (yield* getJson("/usage")) as any; |
| 208 | expect(response.count).toBe(0); |
| 209 | }), |
| 210 | { timeout: 60_000 }, |
| 211 | ); |
| 212 | }, |
| 213 | ); |
| 214 | |
| 215 | describe( |
| 216 | "ListFailuresForLicenseConfigurationOperations", |
| 217 | { |
| 218 | tags: [ |
| 219 | "provider:aws", |
| 220 | "provider:aws:iam", |
| 221 | "provider:aws:lambda", |
| 222 | "provider:aws:licensemanager", |
| 223 | "live", |
| 224 | ], |
| 225 | }, |
| 226 | () => { |
| 227 | test.provider( |
| 228 | "lists failed license operations", |
| 229 | (_stack) => |
| 230 | Effect.gen(function* () { |
| 231 | const response = (yield* getJson("/failures")) as any; |
| 232 | expect(response.count).toBe(0); |
| 233 | }), |
| 234 | { timeout: 60_000 }, |
| 235 | ); |
| 236 | }, |
| 237 | ); |
| 238 | |
| 239 | describe( |
| 240 | "ListLicenses", |
| 241 | { |
| 242 | tags: [ |
| 243 | "provider:aws", |
| 244 | "provider:aws:iam", |
| 245 | "provider:aws:lambda", |
| 246 | "provider:aws:licensemanager", |
| 247 | "live", |
| 248 | ], |
| 249 | }, |
| 250 | () => { |
| 251 | test.provider( |
| 252 | "lists owned seller licenses", |
| 253 | (_stack) => |
| 254 | Effect.gen(function* () { |
| 255 | const response = (yield* getJson("/licenses")) as any; |
| 256 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 257 | }), |
| 258 | { timeout: 60_000 }, |
| 259 | ); |
| 260 | }, |
| 261 | ); |
| 262 | |
| 263 | describe( |
| 264 | "ListReceivedLicenses", |
| 265 | { |
| 266 | tags: [ |
| 267 | "provider:aws", |
| 268 | "provider:aws:iam", |
| 269 | "provider:aws:lambda", |
| 270 | "provider:aws:licensemanager", |
| 271 | "live", |
| 272 | ], |
| 273 | }, |
| 274 | () => { |
| 275 | test.provider( |
| 276 | "lists received licenses", |
| 277 | (_stack) => |
| 278 | Effect.gen(function* () { |
| 279 | const response = (yield* getJson("/received-licenses")) as any; |
| 280 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 281 | }), |
| 282 | { timeout: 60_000 }, |
| 283 | ); |
| 284 | }, |
| 285 | ); |
| 286 | |
| 287 | describe( |
| 288 | "ListReceivedGrants", |
| 289 | { |
| 290 | tags: [ |
| 291 | "provider:aws", |
| 292 | "provider:aws:iam", |
| 293 | "provider:aws:lambda", |
| 294 | "provider:aws:licensemanager", |
| 295 | "live", |
| 296 | ], |
| 297 | }, |
| 298 | () => { |
| 299 | test.provider( |
| 300 | "lists received grants", |
| 301 | (_stack) => |
| 302 | Effect.gen(function* () { |
| 303 | const response = (yield* getJson("/received-grants")) as any; |
| 304 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 305 | }), |
| 306 | { timeout: 60_000 }, |
| 307 | ); |
| 308 | }, |
| 309 | ); |
| 310 | |
| 311 | describe( |
| 312 | "ListDistributedGrants", |
| 313 | { |
| 314 | tags: [ |
| 315 | "provider:aws", |
| 316 | "provider:aws:iam", |
| 317 | "provider:aws:lambda", |
| 318 | "provider:aws:licensemanager", |
| 319 | "live", |
| 320 | ], |
| 321 | }, |
| 322 | () => { |
| 323 | test.provider( |
| 324 | "lists distributed grants", |
| 325 | (_stack) => |
| 326 | Effect.gen(function* () { |
| 327 | const response = (yield* getJson("/distributed-grants")) as any; |
| 328 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 329 | }), |
| 330 | { timeout: 60_000 }, |
| 331 | ); |
| 332 | }, |
| 333 | ); |
| 334 | |
| 335 | describe( |
| 336 | "ListResourceInventory", |
| 337 | { |
| 338 | tags: [ |
| 339 | "provider:aws", |
| 340 | "provider:aws:iam", |
| 341 | "provider:aws:lambda", |
| 342 | "provider:aws:licensemanager", |
| 343 | "live", |
| 344 | ], |
| 345 | }, |
| 346 | () => { |
| 347 | test.provider( |
| 348 | "lists the resource inventory (or the typed SSM dependency rejection)", |
| 349 | (_stack) => |
| 350 | Effect.gen(function* () { |
| 351 | const response = (yield* getJson("/inventory")) as any; |
| 352 | expect(["Ok", "FailedDependencyException"]).toContain(response.tag); |
| 353 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 354 | }), |
| 355 | { timeout: 60_000 }, |
| 356 | ); |
| 357 | }, |
| 358 | ); |
| 359 | |
| 360 | describe( |
| 361 | "GetServiceSettings", |
| 362 | { |
| 363 | tags: [ |
| 364 | "provider:aws", |
| 365 | "provider:aws:iam", |
| 366 | "provider:aws:lambda", |
| 367 | "provider:aws:licensemanager", |
| 368 | "live", |
| 369 | ], |
| 370 | }, |
| 371 | () => { |
| 372 | test.provider( |
| 373 | "reads the account's service settings", |
| 374 | (_stack) => |
| 375 | Effect.gen(function* () { |
| 376 | const response = (yield* getJson("/service-settings")) as any; |
| 377 | expect(response.tag).toBe("Ok"); |
| 378 | expect(typeof response.hasSnsTopic).toBe("boolean"); |
| 379 | }), |
| 380 | { timeout: 60_000 }, |
| 381 | ); |
| 382 | }, |
| 383 | ); |
| 384 | |
| 385 | describe( |
| 386 | "CheckoutLicense", |
| 387 | { |
| 388 | tags: [ |
| 389 | "provider:aws", |
| 390 | "provider:aws:iam", |
| 391 | "provider:aws:lambda", |
| 392 | "provider:aws:licensemanager", |
| 393 | "live", |
| 394 | ], |
| 395 | }, |
| 396 | () => { |
| 397 | test.provider( |
| 398 | "surfaces a typed rejection for a nonexistent product SKU (proving the grant)", |
| 399 | (_stack) => |
| 400 | Effect.gen(function* () { |
| 401 | const response = (yield* getJson("/checkout-invalid")) as any; |
| 402 | expect([ |
| 403 | "ResourceNotFoundException", |
| 404 | "NoEntitlementsAllowedException", |
| 405 | "InvalidParameterValueException", |
| 406 | "ValidationException", |
| 407 | ]).toContain(response.tag); |
| 408 | }), |
| 409 | { timeout: 60_000 }, |
| 410 | ); |
| 411 | }, |
| 412 | ); |
| 413 | |
| 414 | describe( |
| 415 | "GetAccessToken", |
| 416 | { |
| 417 | tags: [ |
| 418 | "provider:aws", |
| 419 | "provider:aws:iam", |
| 420 | "provider:aws:lambda", |
| 421 | "provider:aws:licensemanager", |
| 422 | "live", |
| 423 | ], |
| 424 | }, |
| 425 | () => { |
| 426 | test.provider( |
| 427 | "surfaces a typed rejection for a malformed refresh token (proving the grant)", |
| 428 | (_stack) => |
| 429 | Effect.gen(function* () { |
| 430 | const response = (yield* getJson("/access-token-invalid")) as any; |
| 431 | expect([ |
| 432 | "InvalidParameterValueException", |
| 433 | "ValidationException", |
| 434 | "AuthorizationException", |
| 435 | ]).toContain(response.tag); |
| 436 | }), |
| 437 | { timeout: 60_000 }, |
| 438 | ); |
| 439 | }, |
| 440 | ); |
| 441 | |
| 442 | describe( |
| 443 | "GetLicense", |
| 444 | { |
| 445 | tags: [ |
| 446 | "provider:aws", |
| 447 | "provider:aws:iam", |
| 448 | "provider:aws:lambda", |
| 449 | "provider:aws:licensemanager", |
| 450 | "live", |
| 451 | ], |
| 452 | }, |
| 453 | () => { |
| 454 | test.provider( |
| 455 | "surfaces a typed rejection for a nonexistent license ARN (proving the grant)", |
| 456 | (_stack) => |
| 457 | Effect.gen(function* () { |
| 458 | const response = (yield* getJson("/license-invalid")) as any; |
| 459 | expect([ |
| 460 | "ResourceNotFoundException", |
| 461 | "InvalidParameterValueException", |
| 462 | "ValidationException", |
| 463 | ]).toContain(response.tag); |
| 464 | }), |
| 465 | { timeout: 60_000 }, |
| 466 | ); |
| 467 | }, |
| 468 | ); |
| 469 | |
| 470 | describe( |
| 471 | "GetGrant", |
| 472 | { |
| 473 | tags: [ |
| 474 | "provider:aws", |
| 475 | "provider:aws:iam", |
| 476 | "provider:aws:lambda", |
| 477 | "provider:aws:licensemanager", |
| 478 | "live", |
| 479 | ], |
| 480 | }, |
| 481 | () => { |
| 482 | test.provider( |
| 483 | "surfaces a typed rejection for a nonexistent grant ARN (proving the grant)", |
| 484 | (_stack) => |
| 485 | Effect.gen(function* () { |
| 486 | const response = (yield* getJson("/grant-invalid")) as any; |
| 487 | expect([ |
| 488 | "ResourceNotFoundException", |
| 489 | "InvalidParameterValueException", |
| 490 | "ValidationException", |
| 491 | ]).toContain(response.tag); |
| 492 | }), |
| 493 | { timeout: 60_000 }, |
| 494 | ); |
| 495 | }, |
| 496 | ); |
| 497 | |
| 498 | describe( |
| 499 | "ListLicenseSpecificationsForResource", |
| 500 | { |
| 501 | tags: [ |
| 502 | "provider:aws", |
| 503 | "provider:aws:iam", |
| 504 | "provider:aws:lambda", |
| 505 | "provider:aws:licensemanager", |
| 506 | "live", |
| 507 | ], |
| 508 | }, |
| 509 | () => { |
| 510 | test.provider( |
| 511 | "surfaces an empty list or the typed rejection for a bogus resource ARN", |
| 512 | (_stack) => |
| 513 | Effect.gen(function* () { |
| 514 | const response = (yield* getJson("/specifications-invalid")) as any; |
| 515 | expect(["Ok", "InvalidParameterValueException"]).toContain(response.tag); |
| 516 | }), |
| 517 | { timeout: 60_000 }, |
| 518 | ); |
| 519 | }, |
| 520 | ); |
| 521 | }); |