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 { Bucket } from "@alchemy.run/aws/S3"; |
| 3 | import { |
| 4 | Portfolio, |
| 5 | PortfolioProductAssociation, |
| 6 | PrincipalPortfolioAssociation, |
| 7 | Product, |
| 8 | } from "@alchemy.run/aws/ServiceCatalog"; |
| 9 | import { describe, expect } from "@alchemy.run/test"; |
| 10 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 11 | import * as Core from "@alchemy.run/test/Core"; |
| 12 | import * as s3 from "@distilled.cloud/aws/s3"; |
| 13 | import * as servicecatalog from "@distilled.cloud/aws/service-catalog"; |
| 14 | import * as Data from "effect/Data"; |
| 15 | import * as Effect from "effect/Effect"; |
| 16 | import * as HttpClient from "effect/http/HttpClient"; |
| 17 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 18 | import * as Schedule from "effect/Schedule"; |
| 19 | import ServiceCatalogTestFunctionLive, { ServiceCatalogTestFunction } from "./handler"; |
| 20 | |
| 21 | const testOptions = { providers: AWS.providers() }; |
| 22 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 23 | const sharedStack = Core.scratchStack(testOptions, "ServiceCatalogBindings"); |
| 24 | |
| 25 | // Deterministic provisioned-product name — the physical anchor for |
| 26 | // idempotency and cross-run cleanup. |
| 27 | const PP_NAME = "alchemy-sc-bindings-pp"; |
| 28 | |
| 29 | // Minimal valid CloudFormation template (a no-op resource) so provisioning |
| 30 | // needs no resource permissions beyond CloudFormation itself. Generated |
| 31 | // once and inlined — never at deploy time. |
| 32 | const TEMPLATE = JSON.stringify({ |
| 33 | AWSTemplateFormatVersion: "2010-09-09", |
| 34 | Description: "alchemy Service Catalog bindings test template", |
| 35 | Resources: { NoOp: { Type: "AWS::CloudFormation::WaitConditionHandle" } }, |
| 36 | }); |
| 37 | |
| 38 | class ProvisionedProductStillExists extends Data.TaggedError( |
| 39 | "ScBindingsProvisionedProductStillExists", |
| 40 | )<{ name: string }> {} |
| 41 | |
| 42 | // beforeAll/afterAll hooks run without the providers layer (unlike |
| 43 | // `test.provider` bodies) — out-of-band distilled calls need it provided |
| 44 | // explicitly. |
| 45 | const outOfBand = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 46 | Core.withProviders(effect, testOptions, sharedStack.name); |
| 47 | |
| 48 | /** |
| 49 | * Out-of-band cleanup: terminate any provisioned product a previous run |
| 50 | * left behind (its terminate token derives from the instance's unique ID |
| 51 | * so a retried cleanup converges) and wait — bounded — until it is gone. |
| 52 | */ |
| 53 | const ensureProvisionedProductGone = Effect.gen(function* () { |
| 54 | const existing = yield* servicecatalog |
| 55 | .describeProvisionedProduct({ Name: PP_NAME }) |
| 56 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined))); |
| 57 | const id = existing?.ProvisionedProductDetail?.Id; |
| 58 | if (id === undefined) return; |
| 59 | yield* servicecatalog |
| 60 | .terminateProvisionedProduct({ |
| 61 | ProvisionedProductId: id, |
| 62 | TerminateToken: id.replaceAll(/[^a-zA-Z0-9_-]/g, ""), |
| 63 | IgnoreErrors: true, |
| 64 | }) |
| 65 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 66 | yield* servicecatalog.describeProvisionedProduct({ Name: PP_NAME }).pipe( |
| 67 | Effect.flatMap(() => Effect.fail(new ProvisionedProductStillExists({ name: PP_NAME }))), |
| 68 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 69 | Effect.retry({ |
| 70 | while: (e): boolean => e._tag === "ScBindingsProvisionedProductStillExists", |
| 71 | schedule: Schedule.max([Schedule.fixed("4 seconds"), Schedule.recurs(20)]), |
| 72 | }), |
| 73 | ); |
| 74 | }); |
| 75 | |
| 76 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 77 | // over 60s on a fresh deploy. |
| 78 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 79 | |
| 80 | let baseUrl: string; |
| 81 | let productId: string; |
| 82 | let provisioningArtifactId: string; |
| 83 | |
| 84 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 85 | readonly status: number; |
| 86 | readonly body: string; |
| 87 | }> {} |
| 88 | |
| 89 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 90 | // (cold re-init, IAM propagation on the freshly attached policy that the |
| 91 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 92 | // 4xx/assertion failure surfaces immediately. |
| 93 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 94 | HttpClient.execute(request).pipe( |
| 95 | Effect.flatMap((response) => |
| 96 | response.status >= 500 |
| 97 | ? response.text.pipe( |
| 98 | Effect.flatMap((body) => |
| 99 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 100 | ), |
| 101 | ) |
| 102 | : Effect.succeed(response), |
| 103 | ), |
| 104 | Effect.retry({ |
| 105 | while: (e) => e._tag === "TransientUpstream", |
| 106 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 107 | }), |
| 108 | ); |
| 109 | |
| 110 | const getJson = (path: string) => |
| 111 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 112 | |
| 113 | describe.sequential( |
| 114 | "ServiceCatalog Bindings", |
| 115 | { |
| 116 | tags: [ |
| 117 | "provider:aws", |
| 118 | "provider:aws:lambda", |
| 119 | "provider:aws:s3", |
| 120 | "provider:aws:servicecatalog", |
| 121 | "live", |
| 122 | ], |
| 123 | }, |
| 124 | () => { |
| 125 | beforeAll( |
| 126 | Effect.gen(function* () { |
| 127 | yield* Effect.logInfo("ServiceCatalog test setup: destroying previous resources"); |
| 128 | yield* outOfBand(ensureProvisionedProductGone); |
| 129 | yield* sharedStack.destroy(); |
| 130 | |
| 131 | // Phase 1: a bucket to host the CloudFormation template — it must |
| 132 | // exist in S3 before the product can be created. |
| 133 | const phase1 = yield* sharedStack.deploy( |
| 134 | Effect.gen(function* () { |
| 135 | const bucket = yield* Bucket("ScBindingsTemplateBucket", { forceDestroy: true }); |
| 136 | return { bucketName: bucket.bucketName, region: bucket.region }; |
| 137 | }), |
| 138 | ); |
| 139 | yield* outOfBand( |
| 140 | s3.putObject({ |
| 141 | Bucket: phase1.bucketName, |
| 142 | Key: "bindings-template.json", |
| 143 | Body: new TextEncoder().encode(TEMPLATE), |
| 144 | ContentType: "application/json", |
| 145 | }), |
| 146 | ); |
| 147 | const templateUrl = `https://${phase1.bucketName}.s3.${phase1.region}.amazonaws.com/bindings-template.json`; |
| 148 | |
| 149 | // Phase 2: portfolio + product + the fixture Lambda, whose execution |
| 150 | // role becomes the portfolio principal so the function's runtime |
| 151 | // bindings can browse and provision. |
| 152 | yield* Effect.logInfo("ServiceCatalog test setup: deploying fixture"); |
| 153 | const deployed = yield* sharedStack.deploy( |
| 154 | Effect.gen(function* () { |
| 155 | yield* Bucket("ScBindingsTemplateBucket", { forceDestroy: true }); |
| 156 | const portfolio = yield* Portfolio("ScBindingsPortfolio", { |
| 157 | providerName: "alchemy-tests", |
| 158 | }); |
| 159 | const product = yield* Product("ScBindingsProduct", { |
| 160 | owner: "alchemy-tests", |
| 161 | description: "bindings test product", |
| 162 | provisioningArtifact: { templateUrl }, |
| 163 | }); |
| 164 | yield* PortfolioProductAssociation("ScBindingsProductInPortfolio", { |
| 165 | portfolioId: portfolio.portfolioId, |
| 166 | productId: product.productId, |
| 167 | }); |
| 168 | const fn = yield* ServiceCatalogTestFunction; |
| 169 | yield* PrincipalPortfolioAssociation("ScBindingsFunctionAccess", { |
| 170 | portfolioId: portfolio.portfolioId, |
| 171 | principalArn: fn.roleArn, |
| 172 | }); |
| 173 | return { |
| 174 | functionUrl: fn.functionUrl, |
| 175 | productId: product.productId, |
| 176 | provisioningArtifactId: product.provisioningArtifactId, |
| 177 | }; |
| 178 | }).pipe(Effect.provide(ServiceCatalogTestFunctionLive)), |
| 179 | ); |
| 180 | |
| 181 | expect(deployed.functionUrl).toBeTruthy(); |
| 182 | baseUrl = deployed.functionUrl!.replace(/\/+$/, ""); |
| 183 | productId = deployed.productId; |
| 184 | provisioningArtifactId = deployed.provisioningArtifactId; |
| 185 | |
| 186 | const readinessUrl = `${baseUrl}/bindings`; |
| 187 | yield* Effect.logInfo(`ServiceCatalog test setup: probing readiness at ${readinessUrl}`); |
| 188 | yield* HttpClient.get(readinessUrl).pipe( |
| 189 | Effect.flatMap((response) => |
| 190 | response.status === 200 |
| 191 | ? Effect.succeed(response) |
| 192 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 193 | ), |
| 194 | Effect.tapError((error) => |
| 195 | Effect.logWarning( |
| 196 | `ServiceCatalog test setup: fixture not ready yet (${String(error)})`, |
| 197 | ), |
| 198 | ), |
| 199 | Effect.retry({ schedule: readinessPolicy }), |
| 200 | ); |
| 201 | }), |
| 202 | { timeout: 300_000 }, |
| 203 | ); |
| 204 | |
| 205 | afterAll( |
| 206 | Effect.gen(function* () { |
| 207 | // The provisioned product must be gone before the product/portfolio |
| 208 | // can be deleted. |
| 209 | yield* outOfBand(ensureProvisionedProductGone); |
| 210 | yield* sharedStack.destroy(); |
| 211 | }), |
| 212 | { timeout: 180_000 }, |
| 213 | ); |
| 214 | |
| 215 | describe("binding registration", () => { |
| 216 | test.provider("all 15 capabilities initialize in the runtime", (_stack) => |
| 217 | Effect.gen(function* () { |
| 218 | const response = (yield* getJson("/bindings")) as any; |
| 219 | expect(response.bound).toHaveLength(15); |
| 220 | }), |
| 221 | ); |
| 222 | }); |
| 223 | |
| 224 | describe("SearchProducts", () => { |
| 225 | test.provider( |
| 226 | "searches the caller's product view", |
| 227 | (_stack) => |
| 228 | Effect.gen(function* () { |
| 229 | // Search indexing can lag well behind the principal association — |
| 230 | // assert the binding round-trips (poll past IAM propagation); |
| 231 | // catalog visibility is asserted via ListLaunchPaths below. |
| 232 | const response = (yield* getJson("/search").pipe( |
| 233 | Effect.repeat({ |
| 234 | schedule: Schedule.spaced("3 seconds"), |
| 235 | until: (r): boolean => (r as any).tag === "Ok", |
| 236 | times: 20, |
| 237 | }), |
| 238 | )) as any; |
| 239 | expect(response.tag).toBe("Ok"); |
| 240 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 241 | }), |
| 242 | { timeout: 60_000 }, |
| 243 | ); |
| 244 | }); |
| 245 | |
| 246 | describe("DescribeProduct + ListLaunchPaths + DescribeProvisioningParameters", () => { |
| 247 | test.provider( |
| 248 | "browses the associated product through the portfolio", |
| 249 | (_stack) => |
| 250 | Effect.gen(function* () { |
| 251 | // The principal association is eventually consistent — poll the |
| 252 | // launch paths (bounded) until the portfolio grants access. |
| 253 | const paths = (yield* getJson(`/launch-paths?productId=${productId}`).pipe( |
| 254 | Effect.repeat({ |
| 255 | schedule: Schedule.spaced("3 seconds"), |
| 256 | until: (r): boolean => (r as any).tag === "Ok" && (r as any).count > 0, |
| 257 | times: 30, |
| 258 | }), |
| 259 | )) as any; |
| 260 | expect(paths.tag).toBe("Ok"); |
| 261 | expect(paths.count).toBeGreaterThan(0); |
| 262 | |
| 263 | const product = (yield* getJson(`/product?id=${productId}`)) as any; |
| 264 | expect(product.tag).toBe("Ok"); |
| 265 | expect(typeof product.name).toBe("string"); |
| 266 | |
| 267 | const params = (yield* getJson( |
| 268 | `/params?productId=${productId}&artifactId=${provisioningArtifactId}&pathId=${paths.pathId}`, |
| 269 | )) as any; |
| 270 | expect(params.tag).toBe("Ok"); |
| 271 | // the no-op template declares no parameters |
| 272 | expect(params.count).toBe(0); |
| 273 | }), |
| 274 | { timeout: 180_000 }, |
| 275 | ); |
| 276 | }); |
| 277 | |
| 278 | describe("ProvisionProduct + DescribeRecord + provisioned-product reads + TerminateProvisionedProduct", () => { |
| 279 | test.provider( |
| 280 | "provisions, tracks, reads outputs, and terminates a product", |
| 281 | (_stack) => |
| 282 | Effect.gen(function* () { |
| 283 | // request tokens are per-attempt idempotency tokens, not physical |
| 284 | // names — the deterministic anchor is PP_NAME. |
| 285 | const runToken = crypto.randomUUID().replaceAll("-", ""); |
| 286 | |
| 287 | // Retry-safety: a previous (failed) attempt may have left the |
| 288 | // provisioned product behind — terminate it first so provisioning |
| 289 | // with the deterministic name converges. |
| 290 | const existing = (yield* getJson(`/pp?name=${PP_NAME}`)) as any; |
| 291 | if (existing.tag === "Ok") { |
| 292 | yield* getJson(`/terminate?name=${PP_NAME}&token=c${runToken}`); |
| 293 | yield* getJson(`/pp?name=${PP_NAME}`).pipe( |
| 294 | Effect.repeat({ |
| 295 | schedule: Schedule.spaced("4 seconds"), |
| 296 | until: (r): boolean => (r as any).tag === "ResourceNotFoundException", |
| 297 | times: 30, |
| 298 | }), |
| 299 | ); |
| 300 | } |
| 301 | |
| 302 | // Provision — retry (bounded) while portfolio access propagates; |
| 303 | // the constant token makes retried calls converge on one record. |
| 304 | const provisioned = (yield* getJson( |
| 305 | `/provision?productId=${productId}&artifactId=${provisioningArtifactId}&name=${PP_NAME}&token=p${runToken}`, |
| 306 | ).pipe( |
| 307 | Effect.repeat({ |
| 308 | schedule: Schedule.spaced("3 seconds"), |
| 309 | until: (r): boolean => (r as any).tag === "Ok", |
| 310 | times: 20, |
| 311 | }), |
| 312 | )) as any; |
| 313 | expect(provisioned.tag).toBe("Ok"); |
| 314 | expect(provisioned.recordId).toMatch(/^rec-/); |
| 315 | |
| 316 | // DescribeRecord — poll the record to completion. |
| 317 | const record = (yield* getJson(`/record?id=${provisioned.recordId}`).pipe( |
| 318 | Effect.repeat({ |
| 319 | schedule: Schedule.spaced("4 seconds"), |
| 320 | until: (r): boolean => |
| 321 | (r as any).status === "SUCCEEDED" || (r as any).status === "FAILED", |
| 322 | times: 30, |
| 323 | }), |
| 324 | )) as any; |
| 325 | expect(record.errors ?? []).toEqual([]); |
| 326 | expect(record.status).toBe("SUCCEEDED"); |
| 327 | |
| 328 | // DescribeProvisionedProduct — available by name. |
| 329 | const pp = (yield* getJson(`/pp?name=${PP_NAME}`)) as any; |
| 330 | expect(pp.tag).toBe("Ok"); |
| 331 | expect(pp.status).toBe("AVAILABLE"); |
| 332 | expect(pp.provisionedProductId).toMatch(/^pp-/); |
| 333 | |
| 334 | // SearchProvisionedProducts sees it. |
| 335 | const searched = (yield* getJson("/search-pp")) as any; |
| 336 | expect(searched.tag).toBe("Ok"); |
| 337 | expect(searched.count).toBeGreaterThan(0); |
| 338 | |
| 339 | // GetProvisionedProductOutputs — the no-op template declares no |
| 340 | // outputs, but Service Catalog injects `CloudformationStackARN`. |
| 341 | const outputs = (yield* getJson(`/outputs?name=${PP_NAME}`)) as any; |
| 342 | expect(outputs.tag).toBe("Ok"); |
| 343 | expect(outputs.count).toBeGreaterThanOrEqual(0); |
| 344 | |
| 345 | // ListRecordHistory includes the provision record. |
| 346 | const history = (yield* getJson("/history")) as any; |
| 347 | expect(history.tag).toBe("Ok"); |
| 348 | expect(history.count).toBeGreaterThan(0); |
| 349 | |
| 350 | // Terminate and wait — bounded — until the provisioned product |
| 351 | // is gone. |
| 352 | const terminated = (yield* getJson( |
| 353 | `/terminate?name=${PP_NAME}&token=t${runToken}`, |
| 354 | )) as any; |
| 355 | expect(terminated.tag).toBe("Ok"); |
| 356 | const gone = (yield* getJson(`/pp?name=${PP_NAME}`).pipe( |
| 357 | Effect.repeat({ |
| 358 | schedule: Schedule.spaced("4 seconds"), |
| 359 | until: (r): boolean => (r as any).tag === "ResourceNotFoundException", |
| 360 | times: 30, |
| 361 | }), |
| 362 | )) as any; |
| 363 | expect(gone.tag).toBe("ResourceNotFoundException"); |
| 364 | }), |
| 365 | { timeout: 300_000 }, |
| 366 | ); |
| 367 | }); |
| 368 | |
| 369 | describe("UpdateProvisionedProduct", () => { |
| 370 | test.provider( |
| 371 | "surfaces the typed not-found for a nonexistent provisioned product (proving the grant)", |
| 372 | (_stack) => |
| 373 | Effect.gen(function* () { |
| 374 | const response = (yield* getJson("/update-nonexistent")) as any; |
| 375 | expect(["ResourceNotFoundException", "InvalidParametersException"]).toContain( |
| 376 | response.tag, |
| 377 | ); |
| 378 | }), |
| 379 | { timeout: 60_000 }, |
| 380 | ); |
| 381 | }); |
| 382 | |
| 383 | describe("ListStackInstancesForProvisionedProduct", () => { |
| 384 | test.provider( |
| 385 | "surfaces the typed not-found for a nonexistent provisioned product (proving the grant)", |
| 386 | (_stack) => |
| 387 | Effect.gen(function* () { |
| 388 | const response = (yield* getJson("/stack-instances-nonexistent")) as any; |
| 389 | expect(["ResourceNotFoundException", "InvalidParametersException"]).toContain( |
| 390 | response.tag, |
| 391 | ); |
| 392 | }), |
| 393 | { timeout: 60_000 }, |
| 394 | ); |
| 395 | }); |
| 396 | |
| 397 | describe("DescribeServiceActionExecutionParameters", () => { |
| 398 | test.provider( |
| 399 | "surfaces the typed not-found for a nonexistent service action (proving the grant)", |
| 400 | (_stack) => |
| 401 | Effect.gen(function* () { |
| 402 | const response = (yield* getJson("/service-action-params-nonexistent")) as any; |
| 403 | expect(["ResourceNotFoundException", "InvalidParametersException"]).toContain( |
| 404 | response.tag, |
| 405 | ); |
| 406 | }), |
| 407 | { timeout: 60_000 }, |
| 408 | ); |
| 409 | }); |
| 410 | |
| 411 | describe("ExecuteProvisionedProductServiceAction", () => { |
| 412 | test.provider( |
| 413 | "surfaces the typed not-found for a nonexistent service action (proving the grant)", |
| 414 | (_stack) => |
| 415 | Effect.gen(function* () { |
| 416 | const response = (yield* getJson("/execute-nonexistent")) as any; |
| 417 | expect([ |
| 418 | "ProvisionedProductNotFound", |
| 419 | // pre-patch distilled lib delivers the raw tag |
| 420 | "ValidationException", |
| 421 | "ResourceNotFoundException", |
| 422 | "InvalidParametersException", |
| 423 | "InvalidStateException", |
| 424 | ]).toContain(response.tag); |
| 425 | }), |
| 426 | { timeout: 60_000 }, |
| 427 | ); |
| 428 | }); |
| 429 | }, |
| 430 | ); |