| 1 | import * as AWS from "@/AWS"; |
| 2 | import * as Core from "@/Test/Core"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { describe, expect } from "alchemy-test"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import AIOpsTestFunctionLive, { AIOpsTestFunction } from "./fixtures/handler"; |
| 11 | |
| 12 | const testOptions = { providers: AWS.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "AIOpsBindings"); |
| 15 | |
| 16 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 17 | // over 60s on a fresh deploy. |
| 18 | const readinessPolicy = Schedule.max([ |
| 19 | Schedule.fixed("2 seconds"), |
| 20 | Schedule.recurs(75), |
| 21 | ]); |
| 22 | |
| 23 | let baseUrl: string; |
| 24 | |
| 25 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 26 | readonly status: number; |
| 27 | readonly body: string; |
| 28 | }> {} |
| 29 | |
| 30 | // The Lambda fixture occasionally answers a transient 5xx under load (cold |
| 31 | // re-init, IAM propagation on the freshly attached policy that the handler's |
| 32 | // `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine 4xx/assertion |
| 33 | // failure surfaces immediately. |
| 34 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 35 | HttpClient.execute(request).pipe( |
| 36 | Effect.flatMap((response) => |
| 37 | response.status >= 500 |
| 38 | ? response.text.pipe( |
| 39 | Effect.flatMap((body) => |
| 40 | Effect.fail( |
| 41 | new TransientUpstream({ status: response.status, body }), |
| 42 | ), |
| 43 | ), |
| 44 | ) |
| 45 | : Effect.succeed(response), |
| 46 | ), |
| 47 | Effect.retry({ |
| 48 | while: (e) => e._tag === "TransientUpstream", |
| 49 | schedule: Schedule.max([ |
| 50 | Schedule.exponential("500 millis"), |
| 51 | Schedule.recurs(6), |
| 52 | ]), |
| 53 | }), |
| 54 | ); |
| 55 | |
| 56 | const getJson = (path: string) => |
| 57 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe( |
| 58 | Effect.flatMap((r) => r.json), |
| 59 | ); |
| 60 | |
| 61 | // AWS enforces ONE AIOps investigation group per account per Region, so this |
| 62 | // file must never overlap with InvestigationGroup.test.ts (whose live |
| 63 | // lifecycle is gated behind AWS_TEST_AIOPS and destroys its group on the way |
| 64 | // in and out). The single-fork suite run executes files sequentially and this |
| 65 | // file destroys its group on the way in and out. |
| 66 | describe.sequential( |
| 67 | "AIOps Bindings", |
| 68 | { |
| 69 | tags: [ |
| 70 | "provider:aws", |
| 71 | "provider:aws:aiops", |
| 72 | "provider:aws:iam", |
| 73 | "provider:aws:lambda", |
| 74 | "live", |
| 75 | ], |
| 76 | }, |
| 77 | () => { |
| 78 | beforeAll( |
| 79 | Effect.gen(function* () { |
| 80 | yield* Effect.logInfo( |
| 81 | "AIOps test setup: destroying previous resources", |
| 82 | ); |
| 83 | yield* sharedStack.destroy(); |
| 84 | |
| 85 | yield* Effect.logInfo("AIOps test setup: deploying fixture"); |
| 86 | const attrs = yield* sharedStack.deploy( |
| 87 | Effect.gen(function* () { |
| 88 | return yield* AIOpsTestFunction; |
| 89 | }).pipe(Effect.provide(AIOpsTestFunctionLive)), |
| 90 | ); |
| 91 | |
| 92 | expect(attrs.functionUrl).toBeTruthy(); |
| 93 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 94 | |
| 95 | const readinessUrl = `${baseUrl}/bindings`; |
| 96 | yield* Effect.logInfo( |
| 97 | `AIOps test setup: probing readiness at ${readinessUrl}`, |
| 98 | ); |
| 99 | yield* HttpClient.get(readinessUrl).pipe( |
| 100 | Effect.flatMap((response) => |
| 101 | response.status === 200 |
| 102 | ? Effect.succeed(response) |
| 103 | : Effect.fail( |
| 104 | new Error(`Function not ready: ${response.status}`), |
| 105 | ), |
| 106 | ), |
| 107 | Effect.tapError((error) => |
| 108 | Effect.logWarning( |
| 109 | `AIOps test setup: fixture not ready yet (${String(error)})`, |
| 110 | ), |
| 111 | ), |
| 112 | Effect.retry({ schedule: readinessPolicy }), |
| 113 | ); |
| 114 | }), |
| 115 | { timeout: 240_000 }, |
| 116 | ); |
| 117 | |
| 118 | afterAll(sharedStack.destroy(), { timeout: 240_000 }); |
| 119 | |
| 120 | describe("binding registration", () => { |
| 121 | test.provider("the capabilities initialize in the runtime", (_stack) => |
| 122 | Effect.gen(function* () { |
| 123 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 124 | expect(response.bound).toEqual([ |
| 125 | "getInvestigationGroup", |
| 126 | "getInvestigationGroupPolicy", |
| 127 | "listTagsForResource", |
| 128 | "listInvestigationGroups", |
| 129 | ]); |
| 130 | }), |
| 131 | ); |
| 132 | }); |
| 133 | |
| 134 | describe("GetInvestigationGroup", () => { |
| 135 | test.provider( |
| 136 | "reads the bound group's configuration (injected group ARN)", |
| 137 | (_stack) => |
| 138 | Effect.gen(function* () { |
| 139 | const response = (yield* getJson("/group")) as { |
| 140 | name: string; |
| 141 | arn: string; |
| 142 | retentionInDays: number; |
| 143 | }; |
| 144 | expect(response.arn).toContain(":investigation-group/"); |
| 145 | expect(response.name).toBeTruthy(); |
| 146 | expect(response.retentionInDays).toBe(7); |
| 147 | }), |
| 148 | ); |
| 149 | }); |
| 150 | |
| 151 | describe("GetInvestigationGroupPolicy", () => { |
| 152 | test.provider( |
| 153 | "reads the group's resource policy (typed not-found when unattached)", |
| 154 | (_stack) => |
| 155 | Effect.gen(function* () { |
| 156 | // The fixture attaches no resource policy, so the runtime observes |
| 157 | // the typed ResourceNotFoundException — proving both the IAM grant |
| 158 | // and the injected group ARN end-to-end. |
| 159 | const response = (yield* getJson("/policy")) as { |
| 160 | hasPolicy: boolean; |
| 161 | }; |
| 162 | expect(response.hasPolicy).toBe(false); |
| 163 | }), |
| 164 | ); |
| 165 | }); |
| 166 | |
| 167 | describe("ListTagsForResource", () => { |
| 168 | test.provider("reads the bound group's tags", (_stack) => |
| 169 | Effect.gen(function* () { |
| 170 | const response = (yield* getJson("/tags")) as { |
| 171 | tags: Record<string, string>; |
| 172 | }; |
| 173 | expect(response.tags.Purpose).toBe("bindings-test"); |
| 174 | expect(response.tags["alchemy::id"]).toBe("BindingGroup"); |
| 175 | }), |
| 176 | ); |
| 177 | }); |
| 178 | |
| 179 | describe("ListInvestigationGroups", () => { |
| 180 | test.provider( |
| 181 | "enumerates the Region's investigation groups (account-level)", |
| 182 | (_stack) => |
| 183 | Effect.gen(function* () { |
| 184 | const group = (yield* getJson("/group")) as { arn: string }; |
| 185 | const response = (yield* getJson("/groups")) as { |
| 186 | groupArns: string[]; |
| 187 | }; |
| 188 | expect(response.groupArns).toContain(group.arn); |
| 189 | }), |
| 190 | ); |
| 191 | }); |
| 192 | }, |
| 193 | ); |