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 Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import ApplicationSignalsTestFunctionLive, { ApplicationSignalsTestFunction } from "./handler"; |
| 11 | |
| 12 | const testOptions = { providers: AWS.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "ApplicationSignalsBindings"); |
| 15 | |
| 16 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 17 | // over 60s on a fresh deploy under parallel-suite load. |
| 18 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 19 | |
| 20 | let baseUrl: string; |
| 21 | |
| 22 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 23 | readonly status: number; |
| 24 | readonly body: string; |
| 25 | }> {} |
| 26 | |
| 27 | // The shared Lambda fixture occasionally answers a transient 5xx under |
| 28 | // full-suite parallel load (cold re-init, IAM propagation on the freshly |
| 29 | // attached policy that the handler's `Effect.orDie` surfaces as a 500). |
| 30 | // Retry only 5xx; a genuine 4xx/assertion failure surfaces immediately. |
| 31 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 32 | HttpClient.execute(request).pipe( |
| 33 | Effect.flatMap((response) => |
| 34 | response.status >= 500 |
| 35 | ? response.text.pipe( |
| 36 | Effect.flatMap((body) => |
| 37 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 38 | ), |
| 39 | ) |
| 40 | : Effect.succeed(response), |
| 41 | ), |
| 42 | Effect.retry({ |
| 43 | while: (e) => e._tag === "TransientUpstream", |
| 44 | // Fresh-role IAM propagation can take ~60s right after deploy; the |
| 45 | // handler's Effect.orDie surfaces a still-propagating grant as a 500. |
| 46 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(12)]), |
| 47 | }), |
| 48 | ); |
| 49 | |
| 50 | const getJson = (path: string) => |
| 51 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 52 | |
| 53 | describe.sequential( |
| 54 | "ApplicationSignals Bindings", |
| 55 | { |
| 56 | tags: [ |
| 57 | "provider:aws", |
| 58 | "provider:aws:applicationsignals", |
| 59 | "provider:aws:batch", |
| 60 | "provider:aws:lambda", |
| 61 | "live", |
| 62 | ], |
| 63 | }, |
| 64 | () => { |
| 65 | beforeAll( |
| 66 | Effect.gen(function* () { |
| 67 | yield* Effect.logInfo("ApplicationSignals test setup: destroying previous resources"); |
| 68 | yield* sharedStack.destroy(); |
| 69 | |
| 70 | yield* Effect.logInfo("ApplicationSignals test setup: deploying fixture"); |
| 71 | const { functionUrl } = yield* sharedStack.deploy( |
| 72 | Effect.gen(function* () { |
| 73 | return yield* ApplicationSignalsTestFunction; |
| 74 | }).pipe(Effect.provide(ApplicationSignalsTestFunctionLive)), |
| 75 | ); |
| 76 | |
| 77 | expect(functionUrl).toBeTruthy(); |
| 78 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 79 | |
| 80 | const readinessUrl = `${baseUrl}/bindings`; |
| 81 | yield* Effect.logInfo( |
| 82 | `ApplicationSignals test setup: probing readiness at ${readinessUrl}`, |
| 83 | ); |
| 84 | yield* HttpClient.get(readinessUrl).pipe( |
| 85 | Effect.flatMap((response) => |
| 86 | response.status === 200 |
| 87 | ? Effect.succeed(response) |
| 88 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 89 | ), |
| 90 | Effect.tapError((error) => |
| 91 | Effect.logWarning( |
| 92 | `ApplicationSignals test setup: fixture not ready yet (${String(error)})`, |
| 93 | ), |
| 94 | ), |
| 95 | Effect.retry({ schedule: readinessPolicy }), |
| 96 | ); |
| 97 | |
| 98 | // Warm up the most permission-hungry route: budget reports exercise |
| 99 | // both the application-signals grant on the SLO ARN and the dependent |
| 100 | // cloudwatch:GetMetricData grant, so a 200 here means the fresh |
| 101 | // role's policies have fully propagated. |
| 102 | yield* HttpClient.get(`${baseUrl}/budget-report`).pipe( |
| 103 | Effect.flatMap((response) => |
| 104 | response.status === 200 |
| 105 | ? Effect.succeed(response) |
| 106 | : Effect.fail(new Error(`IAM not propagated yet: ${response.status}`)), |
| 107 | ), |
| 108 | Effect.retry({ |
| 109 | schedule: Schedule.max([Schedule.fixed("4 seconds"), Schedule.recurs(30)]), |
| 110 | }), |
| 111 | ); |
| 112 | }), |
| 113 | { timeout: 240_000 }, |
| 114 | ); |
| 115 | |
| 116 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 117 | |
| 118 | describe("binding registration", () => { |
| 119 | test.provider("all 14 capabilities initialize in the runtime", (_stack) => |
| 120 | Effect.gen(function* () { |
| 121 | const response = yield* getJson("/bindings"); |
| 122 | expect((response as any).bound).toHaveLength(14); |
| 123 | }), |
| 124 | ); |
| 125 | }); |
| 126 | |
| 127 | describe("ListServices", () => { |
| 128 | test.provider("lists discovered services", (_stack) => |
| 129 | Effect.gen(function* () { |
| 130 | const response = yield* getJson("/services"); |
| 131 | expect(typeof (response as any).count).toBe("number"); |
| 132 | }), |
| 133 | ); |
| 134 | }); |
| 135 | |
| 136 | describe("GetService", () => { |
| 137 | test.provider("returns an empty service for unknown key attributes", (_stack) => |
| 138 | Effect.gen(function* () { |
| 139 | const response = yield* getJson("/service"); |
| 140 | expect((response as any).discovered).toBe(false); |
| 141 | }), |
| 142 | ); |
| 143 | }); |
| 144 | |
| 145 | describe("ListServiceDependencies / ListServiceDependents / ListServiceOperations", () => { |
| 146 | test.provider("return well-formed empty pages", (_stack) => |
| 147 | Effect.gen(function* () { |
| 148 | const dependencies = yield* getJson("/dependencies"); |
| 149 | const dependents = yield* getJson("/dependents"); |
| 150 | const operations = yield* getJson("/operations"); |
| 151 | expect((dependencies as any).count).toBe(0); |
| 152 | expect((dependents as any).count).toBe(0); |
| 153 | expect((operations as any).count).toBe(0); |
| 154 | }), |
| 155 | ); |
| 156 | }); |
| 157 | |
| 158 | describe("ListServiceStates", () => { |
| 159 | test.provider("lists recent service states", (_stack) => |
| 160 | Effect.gen(function* () { |
| 161 | const response = yield* getJson("/states"); |
| 162 | expect(typeof (response as any).count).toBe("number"); |
| 163 | }), |
| 164 | ); |
| 165 | }); |
| 166 | |
| 167 | describe("ListEntityEvents", () => { |
| 168 | test.provider("lists change events for an entity", (_stack) => |
| 169 | Effect.gen(function* () { |
| 170 | const response = yield* getJson("/entity-events"); |
| 171 | expect(typeof (response as any).count).toBe("number"); |
| 172 | }), |
| 173 | ); |
| 174 | }); |
| 175 | |
| 176 | describe("ListAuditFindings", () => { |
| 177 | test.provider("lists audit findings for a service target", (_stack) => |
| 178 | Effect.gen(function* () { |
| 179 | const response = yield* getJson("/audit-findings"); |
| 180 | expect(typeof (response as any).count).toBe("number"); |
| 181 | }), |
| 182 | ); |
| 183 | }); |
| 184 | |
| 185 | describe("ListServiceLevelObjectives", () => { |
| 186 | test.provider("lists the account's SLOs including the fixture SLO", (_stack) => |
| 187 | Effect.gen(function* () { |
| 188 | const response = yield* getJson("/slos"); |
| 189 | expect((response as any).count).toBeGreaterThanOrEqual(1); |
| 190 | expect( |
| 191 | (response as any).names.some((name: string) => |
| 192 | name.toLowerCase().includes("bindingsslo"), |
| 193 | ), |
| 194 | ).toBe(true); |
| 195 | }), |
| 196 | ); |
| 197 | }); |
| 198 | |
| 199 | describe("GetServiceLevelObjective", () => { |
| 200 | test.provider("reads the bound SLO's configuration", (_stack) => |
| 201 | Effect.gen(function* () { |
| 202 | const response = yield* getJson("/slo"); |
| 203 | expect((response as any).arn).toContain(":slo/"); |
| 204 | expect((response as any).attainmentGoal).toBe(99); |
| 205 | }), |
| 206 | ); |
| 207 | }); |
| 208 | |
| 209 | describe("BatchGetServiceLevelObjectiveBudgetReport", () => { |
| 210 | test.provider("returns the bound SLO's budget report", (_stack) => |
| 211 | Effect.gen(function* () { |
| 212 | const response = yield* getJson("/budget-report"); |
| 213 | expect((response as any).errors).toBe(0); |
| 214 | expect((response as any).reports).toBe(1); |
| 215 | expect((response as any).budgetStatus).toBeTruthy(); |
| 216 | }), |
| 217 | ); |
| 218 | }); |
| 219 | |
| 220 | describe("BatchUpdateExclusionWindows / ListServiceLevelObjectiveExclusionWindows", () => { |
| 221 | test.provider("adds, observes, and removes a maintenance window", (_stack) => |
| 222 | Effect.gen(function* () { |
| 223 | const before = yield* getJson("/exclusion-windows"); |
| 224 | expect((before as any).count).toBe(0); |
| 225 | |
| 226 | const response = yield* send(HttpClientRequest.post(`${baseUrl}/exclusion-windows`)).pipe( |
| 227 | Effect.flatMap((r) => r.json), |
| 228 | ); |
| 229 | expect((response as any).addErrors).toBe(0); |
| 230 | expect((response as any).afterAdd).toBe(1); |
| 231 | expect((response as any).removeErrors).toBe(0); |
| 232 | expect((response as any).afterRemove).toBe(0); |
| 233 | }), |
| 234 | ); |
| 235 | }); |
| 236 | |
| 237 | describe("GetInstrumentationConfigurationStatus", () => { |
| 238 | test.provider("returns the bound configuration's (empty) status history", (_stack) => |
| 239 | Effect.gen(function* () { |
| 240 | const response = yield* getJson("/ic-status"); |
| 241 | expect((response as any).events).toBe(0); |
| 242 | }), |
| 243 | ); |
| 244 | }); |
| 245 | }, |
| 246 | ); |