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 config from "@distilled.cloud/aws/config-service"; |
| 6 | import * as iam from "@distilled.cloud/aws/iam"; |
| 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 ConfigTestFunctionLive, { ConfigTestFunction } from "./handler"; |
| 13 | import { makeConfigTestLease } from "./TestLease.ts"; |
| 14 | |
| 15 | // Runtime binding coverage for every Config capability that can be |
| 16 | // exercised against fixture-deployable resources. One binding has no route |
| 17 | // here (documented, not silently dropped): |
| 18 | // |
| 19 | // - DeliverConfigSnapshot — binds to the account's singleton |
| 20 | // DeliveryChannel, whose lifecycle is gated behind |
| 21 | // AWS_TEST_CONFIG_RECORDER (see ConfigurationRecorder.test.ts); an |
| 22 | // ungated fixture must not clobber the account's Config delivery setup. |
| 23 | |
| 24 | const testOptions = { providers: AWS.providers() }; |
| 25 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 26 | const sharedStack = Core.scratchStack(testOptions, "ConfigBindings"); |
| 27 | const testLease = makeConfigTestLease(); |
| 28 | |
| 29 | // PutConfigRule (the fixture's rule) requires a configuration recorder in |
| 30 | // the account/region. Capture-and-restore: if the account has none, stand |
| 31 | // up a minimal recorder on the Config service-linked role for the duration |
| 32 | // of the suite and delete it afterwards; an existing foreign recorder is |
| 33 | // left untouched. The deterministic name means a leftover from a |
| 34 | // previously-killed run is RECLAIMED (and cleaned up at the end) instead of |
| 35 | // orphaning forever. |
| 36 | const TEST_RECORDER_NAME = "alchemy-test-config-bindings-recorder"; |
| 37 | |
| 38 | const ensureRecorder = Effect.gen(function* () { |
| 39 | const existing = yield* config.describeConfigurationRecorders({}); |
| 40 | const recorders = existing.ConfigurationRecorders ?? []; |
| 41 | if (recorders.some((r) => r.name === TEST_RECORDER_NAME)) { |
| 42 | return true; |
| 43 | } |
| 44 | if (recorders.length > 0) { |
| 45 | return false; |
| 46 | } |
| 47 | yield* iam |
| 48 | .createServiceLinkedRole({ AWSServiceName: "config.amazonaws.com" }) |
| 49 | .pipe(Effect.catchTag("InvalidInputException", () => Effect.succeed(undefined))); |
| 50 | const role = yield* iam.getRole({ RoleName: "AWSServiceRoleForConfig" }); |
| 51 | yield* config |
| 52 | .putConfigurationRecorder({ |
| 53 | ConfigurationRecorder: { |
| 54 | name: TEST_RECORDER_NAME, |
| 55 | roleARN: role.Role.Arn, |
| 56 | recordingGroup: { resourceTypes: ["AWS::S3::Bucket"] }, |
| 57 | }, |
| 58 | }) |
| 59 | .pipe( |
| 60 | Effect.retry({ |
| 61 | while: (e) => e._tag === "InvalidRoleException", |
| 62 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(15)]), |
| 63 | }), |
| 64 | ); |
| 65 | return true; |
| 66 | }); |
| 67 | |
| 68 | const removeRecorderIfCreated = (created: boolean) => |
| 69 | created |
| 70 | ? config.deleteConfigurationRecorder({ ConfigurationRecorderName: TEST_RECORDER_NAME }).pipe( |
| 71 | Effect.catchTag("NoSuchConfigurationRecorderException", () => Effect.void), |
| 72 | Effect.orDie, |
| 73 | ) |
| 74 | : Effect.void; |
| 75 | |
| 76 | let recorderCreated = false; |
| 77 | let baseUrl: string; |
| 78 | |
| 79 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 80 | |
| 81 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 82 | readonly status: number; |
| 83 | readonly body: string; |
| 84 | }> {} |
| 85 | |
| 86 | // The shared Lambda fixture occasionally answers a transient 5xx under |
| 87 | // cold re-init or an IAM-propagation race. Only 5xx is retried; a genuine |
| 88 | // 4xx/assertion failure is returned immediately. |
| 89 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 90 | HttpClient.execute(request).pipe( |
| 91 | Effect.flatMap((response) => |
| 92 | response.status >= 500 |
| 93 | ? response.text.pipe( |
| 94 | Effect.flatMap((body) => |
| 95 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 96 | ), |
| 97 | ) |
| 98 | : Effect.succeed(response), |
| 99 | ), |
| 100 | Effect.retry({ |
| 101 | while: (e) => e._tag === "TransientUpstream", |
| 102 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 103 | }), |
| 104 | ); |
| 105 | |
| 106 | const getJson = (path: string) => |
| 107 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 108 | |
| 109 | const postJson = (path: string) => |
| 110 | send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 111 | |
| 112 | describe( |
| 113 | "Config Bindings", |
| 114 | { |
| 115 | tags: [ |
| 116 | "provider:aws", |
| 117 | "provider:aws:batch", |
| 118 | "provider:aws:config", |
| 119 | "provider:aws:iam", |
| 120 | "provider:aws:lambda", |
| 121 | "live", |
| 122 | ], |
| 123 | }, |
| 124 | () => { |
| 125 | beforeAll( |
| 126 | Effect.gen(function* () { |
| 127 | yield* testLease.acquire; |
| 128 | yield* Effect.logInfo("Config test setup: ensuring recorder"); |
| 129 | // Direct distilled calls need the AWS provider context (credentials, |
| 130 | // region) that `test.provider` bodies get implicitly. |
| 131 | recorderCreated = yield* Core.withProviders(ensureRecorder, testOptions, "ConfigBindings"); |
| 132 | |
| 133 | yield* Effect.logInfo("Config test setup: destroying previous stack"); |
| 134 | yield* sharedStack.destroy(); |
| 135 | |
| 136 | yield* Effect.logInfo("Config test setup: deploying fixture"); |
| 137 | const { functionUrl } = yield* sharedStack.deploy( |
| 138 | Effect.gen(function* () { |
| 139 | return yield* ConfigTestFunction; |
| 140 | }).pipe(Effect.provide(ConfigTestFunctionLive)), |
| 141 | ); |
| 142 | |
| 143 | expect(functionUrl).toBeTruthy(); |
| 144 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 145 | |
| 146 | yield* Effect.logInfo(`Config test setup: probing readiness at ${baseUrl}/health`); |
| 147 | yield* HttpClient.get(`${baseUrl}/health`).pipe( |
| 148 | Effect.flatMap((response) => |
| 149 | response.status === 200 |
| 150 | ? Effect.succeed(response) |
| 151 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 152 | ), |
| 153 | Effect.retry({ schedule: readinessPolicy }), |
| 154 | ); |
| 155 | }), |
| 156 | { timeout: 240_000 }, |
| 157 | ); |
| 158 | |
| 159 | afterAll( |
| 160 | sharedStack |
| 161 | .destroy() |
| 162 | .pipe( |
| 163 | Effect.ensuring( |
| 164 | Effect.suspend(() => |
| 165 | Core.withProviders( |
| 166 | removeRecorderIfCreated(recorderCreated), |
| 167 | testOptions, |
| 168 | "ConfigBindings", |
| 169 | ), |
| 170 | ), |
| 171 | ), |
| 172 | Effect.ensuring(testLease.release), |
| 173 | ), |
| 174 | { timeout: 180_000 }, |
| 175 | ); |
| 176 | |
| 177 | // ── Discovering resources / querying ──────────────────────────────────── |
| 178 | |
| 179 | describe("SelectResourceConfig", () => { |
| 180 | test.provider("runs a SQL query over recorded state", (_stack) => |
| 181 | Effect.gen(function* () { |
| 182 | const response = (yield* getJson("/select-resource-config")) as { results: unknown[] }; |
| 183 | expect(Array.isArray(response.results)).toBe(true); |
| 184 | }), |
| 185 | ); |
| 186 | }); |
| 187 | |
| 188 | describe("ListDiscoveredResources", () => { |
| 189 | test.provider("lists discovered bucket identifiers", (_stack) => |
| 190 | Effect.gen(function* () { |
| 191 | const response = (yield* getJson("/list-discovered-resources")) as { |
| 192 | identifiers: unknown[]; |
| 193 | }; |
| 194 | expect(Array.isArray(response.identifiers)).toBe(true); |
| 195 | }), |
| 196 | ); |
| 197 | }); |
| 198 | |
| 199 | describe("GetDiscoveredResourceCounts", () => { |
| 200 | test.provider("counts discovered resources", (_stack) => |
| 201 | Effect.gen(function* () { |
| 202 | const response = (yield* getJson("/get-discovered-resource-counts")) as { |
| 203 | total: number; |
| 204 | counts: unknown[]; |
| 205 | }; |
| 206 | expect(typeof response.total).toBe("number"); |
| 207 | expect(Array.isArray(response.counts)).toBe(true); |
| 208 | }), |
| 209 | ); |
| 210 | }); |
| 211 | |
| 212 | describe("BatchGetResourceConfig", () => { |
| 213 | test.provider("returns the undiscovered probe key as unprocessed", (_stack) => |
| 214 | Effect.gen(function* () { |
| 215 | const response = (yield* postJson("/batch-get-resource-config")) as { |
| 216 | items: unknown[]; |
| 217 | unprocessed: { resourceId?: string }[]; |
| 218 | }; |
| 219 | expect(Array.isArray(response.items)).toBe(true); |
| 220 | expect(Array.isArray(response.unprocessed)).toBe(true); |
| 221 | }), |
| 222 | ); |
| 223 | }); |
| 224 | |
| 225 | describe("GetResourceConfigHistory", () => { |
| 226 | test.provider("answers ok or the typed ResourceNotDiscoveredException", (_stack) => |
| 227 | Effect.gen(function* () { |
| 228 | const response = (yield* getJson("/get-resource-config-history")) as { |
| 229 | items?: unknown[]; |
| 230 | errorTag?: string; |
| 231 | }; |
| 232 | if (response.errorTag !== undefined) { |
| 233 | expect(response.errorTag).toBe("ResourceNotDiscoveredException"); |
| 234 | } else { |
| 235 | expect(Array.isArray(response.items)).toBe(true); |
| 236 | } |
| 237 | }), |
| 238 | ); |
| 239 | }); |
| 240 | |
| 241 | // ── Compliance reads ───────────────────────────────────────────────────── |
| 242 | |
| 243 | describe("DescribeComplianceByConfigRule", () => { |
| 244 | test.provider("reads the fixture rule's compliance", (_stack) => |
| 245 | Effect.gen(function* () { |
| 246 | const response = (yield* getJson("/describe-compliance-by-config-rule")) as { |
| 247 | ruleName: string; |
| 248 | compliances: unknown[]; |
| 249 | }; |
| 250 | expect(response.ruleName).toBeTruthy(); |
| 251 | expect(Array.isArray(response.compliances)).toBe(true); |
| 252 | }), |
| 253 | ); |
| 254 | }); |
| 255 | |
| 256 | describe("DescribeComplianceByResource", () => { |
| 257 | test.provider("reads compliance by resource type", (_stack) => |
| 258 | Effect.gen(function* () { |
| 259 | const response = (yield* getJson("/describe-compliance-by-resource")) as { |
| 260 | compliances: unknown[]; |
| 261 | }; |
| 262 | expect(Array.isArray(response.compliances)).toBe(true); |
| 263 | }), |
| 264 | ); |
| 265 | }); |
| 266 | |
| 267 | describe("GetComplianceDetailsByResource", () => { |
| 268 | test.provider("reads evaluation results for a resource", (_stack) => |
| 269 | Effect.gen(function* () { |
| 270 | const response = (yield* getJson("/get-compliance-details-by-resource")) as { |
| 271 | evaluations: unknown[]; |
| 272 | }; |
| 273 | expect(Array.isArray(response.evaluations)).toBe(true); |
| 274 | }), |
| 275 | ); |
| 276 | }); |
| 277 | |
| 278 | describe("GetComplianceSummaryByConfigRule", () => { |
| 279 | test.provider("reads the account rule-compliance summary", (_stack) => |
| 280 | Effect.gen(function* () { |
| 281 | const response = (yield* getJson("/get-compliance-summary-by-config-rule")) as { |
| 282 | summary: unknown; |
| 283 | }; |
| 284 | expect(response).toHaveProperty("summary"); |
| 285 | }), |
| 286 | ); |
| 287 | }); |
| 288 | |
| 289 | describe("GetComplianceSummaryByResourceType", () => { |
| 290 | test.provider("reads per-type compliance summaries", (_stack) => |
| 291 | Effect.gen(function* () { |
| 292 | const response = (yield* getJson("/get-compliance-summary-by-resource-type")) as { |
| 293 | summaries: unknown[]; |
| 294 | }; |
| 295 | expect(Array.isArray(response.summaries)).toBe(true); |
| 296 | }), |
| 297 | ); |
| 298 | }); |
| 299 | |
| 300 | describe("GetComplianceDetailsByConfigRule", () => { |
| 301 | test.provider("reads the bound rule's evaluation results (injected name)", (_stack) => |
| 302 | Effect.gen(function* () { |
| 303 | const response = (yield* getJson("/get-compliance-details-by-config-rule")) as { |
| 304 | ruleName: string; |
| 305 | evaluations: unknown[]; |
| 306 | }; |
| 307 | expect(response.ruleName).toBeTruthy(); |
| 308 | expect(Array.isArray(response.evaluations)).toBe(true); |
| 309 | }), |
| 310 | ); |
| 311 | }); |
| 312 | |
| 313 | // ── Rule evaluation ────────────────────────────────────────────────────── |
| 314 | |
| 315 | describe("DescribeConfigRuleEvaluationStatus", () => { |
| 316 | test.provider("reads the fixture rule's evaluation status", (_stack) => |
| 317 | Effect.gen(function* () { |
| 318 | const response = (yield* getJson("/describe-config-rule-evaluation-status")) as { |
| 319 | ruleName: string; |
| 320 | statuses: string[]; |
| 321 | }; |
| 322 | expect(response.statuses).toContain(response.ruleName); |
| 323 | }), |
| 324 | ); |
| 325 | }); |
| 326 | |
| 327 | describe("StartConfigRulesEvaluation", () => { |
| 328 | test.provider("starts an on-demand evaluation of the bound rule", (_stack) => |
| 329 | Effect.gen(function* () { |
| 330 | const response = (yield* postJson("/start-config-rules-evaluation")) as { |
| 331 | ok?: boolean; |
| 332 | errorTag?: string; |
| 333 | }; |
| 334 | if (response.errorTag !== undefined) { |
| 335 | expect(["ResourceInUseException", "LimitExceededException"]).toContain( |
| 336 | response.errorTag, |
| 337 | ); |
| 338 | } else { |
| 339 | expect(response.ok).toBe(true); |
| 340 | } |
| 341 | }), |
| 342 | ); |
| 343 | }); |
| 344 | |
| 345 | describe("PutEvaluations", () => { |
| 346 | test.provider("validates evaluations in test mode", (_stack) => |
| 347 | Effect.gen(function* () { |
| 348 | const response = (yield* postJson("/put-evaluations")) as { |
| 349 | failed?: unknown[]; |
| 350 | errorTag?: string; |
| 351 | }; |
| 352 | if (response.errorTag !== undefined) { |
| 353 | expect(response.errorTag).toBe("InvalidResultTokenException"); |
| 354 | } else { |
| 355 | expect(Array.isArray(response.failed)).toBe(true); |
| 356 | } |
| 357 | }), |
| 358 | ); |
| 359 | }); |
| 360 | |
| 361 | describe("PutExternalEvaluation", () => { |
| 362 | test.provider( |
| 363 | "rejects with the typed InvalidParameterValueException on a managed rule", |
| 364 | (_stack) => |
| 365 | Effect.gen(function* () { |
| 366 | const response = (yield* postJson("/put-external-evaluation")) as { |
| 367 | ok?: boolean; |
| 368 | errorTag?: string; |
| 369 | }; |
| 370 | if (response.errorTag !== undefined) { |
| 371 | expect(response.errorTag).toBe("InvalidParameterValueException"); |
| 372 | } else { |
| 373 | expect(response.ok).toBe(true); |
| 374 | } |
| 375 | }), |
| 376 | ); |
| 377 | }); |
| 378 | |
| 379 | // ── Custom resource recording ──────────────────────────────────────────── |
| 380 | |
| 381 | describe("PutResourceConfig", () => { |
| 382 | test.provider( |
| 383 | "records a custom resource (or the typed no-running-recorder error)", |
| 384 | (_stack) => |
| 385 | Effect.gen(function* () { |
| 386 | const response = (yield* postJson("/put-resource-config")) as { |
| 387 | ok?: boolean; |
| 388 | errorTag?: string; |
| 389 | }; |
| 390 | if (response.errorTag !== undefined) { |
| 391 | expect([ |
| 392 | "NoRunningConfigurationRecorderException", |
| 393 | "ValidationException", |
| 394 | "MaxActiveResourcesExceededException", |
| 395 | ]).toContain(response.errorTag); |
| 396 | } else { |
| 397 | expect(response.ok).toBe(true); |
| 398 | } |
| 399 | }), |
| 400 | ); |
| 401 | }); |
| 402 | |
| 403 | describe("DeleteResourceConfig", () => { |
| 404 | test.provider( |
| 405 | "deletes the custom resource (or the typed no-running-recorder error)", |
| 406 | (_stack) => |
| 407 | Effect.gen(function* () { |
| 408 | const response = (yield* postJson("/delete-resource-config")) as { |
| 409 | ok?: boolean; |
| 410 | errorTag?: string; |
| 411 | }; |
| 412 | if (response.errorTag !== undefined) { |
| 413 | expect(["NoRunningConfigurationRecorderException", "ValidationException"]).toContain( |
| 414 | response.errorTag, |
| 415 | ); |
| 416 | } else { |
| 417 | expect(response.ok).toBe(true); |
| 418 | } |
| 419 | }), |
| 420 | ); |
| 421 | }); |
| 422 | |
| 423 | // ── Proactive resource evaluation ──────────────────────────────────────── |
| 424 | |
| 425 | describe("StartResourceEvaluation / GetResourceEvaluationSummary", () => { |
| 426 | test.provider("starts a proactive evaluation and reads its summary", (_stack) => |
| 427 | Effect.gen(function* () { |
| 428 | const started = (yield* postJson("/start-resource-evaluation")) as { |
| 429 | id?: string; |
| 430 | errorTag?: string; |
| 431 | errorMessage?: string; |
| 432 | }; |
| 433 | if (started.errorTag !== undefined) { |
| 434 | expect(started.errorTag, started.errorMessage).toBe("InvalidParameterValueException"); |
| 435 | return; |
| 436 | } |
| 437 | expect(started.id).toBeTruthy(); |
| 438 | |
| 439 | const summary = (yield* getJson(`/get-resource-evaluation-summary?id=${started.id}`)) as { |
| 440 | id?: string; |
| 441 | status?: string; |
| 442 | errorTag?: string; |
| 443 | }; |
| 444 | if (summary.errorTag !== undefined) { |
| 445 | expect(summary.errorTag).toBe("ResourceNotFoundException"); |
| 446 | } else { |
| 447 | expect(summary.id).toBe(started.id); |
| 448 | expect(summary.status).toBeTruthy(); |
| 449 | } |
| 450 | }), |
| 451 | ); |
| 452 | }); |
| 453 | |
| 454 | describe("ListResourceEvaluations", () => { |
| 455 | test.provider("lists proactive evaluations", (_stack) => |
| 456 | Effect.gen(function* () { |
| 457 | const response = (yield* getJson("/list-resource-evaluations")) as { |
| 458 | evaluations: unknown[]; |
| 459 | }; |
| 460 | expect(Array.isArray(response.evaluations)).toBe(true); |
| 461 | }), |
| 462 | ); |
| 463 | }); |
| 464 | }, |
| 465 | ); |