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 obs from "@distilled.cloud/aws/observabilityadmin"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import ObservabilityAdminBindingsFunctionLive, { |
| 12 | ObservabilityAdminBindingsFunction, |
| 13 | } from "./handler"; |
| 14 | import { makeObservabilityAdminTestLease } from "./TestLease.ts"; |
| 15 | |
| 16 | const testOptions = { providers: AWS.providers() }; |
| 17 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 18 | const testLease = makeObservabilityAdminTestLease(); |
| 19 | const sharedStack = Core.scratchStack(testOptions, "ObservabilityAdminBindings"); |
| 20 | |
| 21 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 22 | // over 60s on a fresh deploy. |
| 23 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]); |
| 24 | |
| 25 | const readStatus = obs |
| 26 | .getTelemetryEvaluationStatus({}) |
| 27 | .pipe(Effect.map((r) => r.Status ?? "NOT_STARTED")); |
| 28 | |
| 29 | const awaitSettled = readStatus.pipe( |
| 30 | Effect.repeat({ |
| 31 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]), |
| 32 | until: (status): boolean => status !== "STARTING" && status !== "STOPPING", |
| 33 | }), |
| 34 | ); |
| 35 | |
| 36 | const isOn = (status: string): boolean => status === "RUNNING" || status === "STARTING"; |
| 37 | |
| 38 | let baseUrl: string; |
| 39 | // The account's pre-test onboarding state, restored in afterAll. |
| 40 | let priorStatus = "NOT_STARTED"; |
| 41 | |
| 42 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 43 | readonly status: number; |
| 44 | readonly body: string; |
| 45 | }> {} |
| 46 | |
| 47 | // The Lambda fixture occasionally answers a transient 5xx under load (cold |
| 48 | // re-init, IAM propagation on the freshly attached policy that the handler's |
| 49 | // `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine 4xx/assertion |
| 50 | // failure surfaces immediately. |
| 51 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 52 | HttpClient.execute(request).pipe( |
| 53 | Effect.flatMap((response) => |
| 54 | response.status >= 500 |
| 55 | ? response.text.pipe( |
| 56 | Effect.flatMap((body) => |
| 57 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 58 | ), |
| 59 | ) |
| 60 | : Effect.succeed(response), |
| 61 | ), |
| 62 | Effect.retry({ |
| 63 | while: (e) => e._tag === "TransientUpstream", |
| 64 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 65 | }), |
| 66 | ); |
| 67 | |
| 68 | const getJson = (path: string) => |
| 69 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 70 | |
| 71 | // beforeAll/afterAll hooks run outside `test.provider`'s layer, so raw |
| 72 | // distilled calls need the provider layer (credentials, region) supplied |
| 73 | // explicitly. |
| 74 | const aws = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 75 | Core.withProviders(effect, testOptions, sharedStack.name); |
| 76 | |
| 77 | beforeAll(testLease.acquire, { timeout: 3_600_000 }); |
| 78 | afterAll(testLease.release); |
| 79 | |
| 80 | // The fixture's telemetry rule requires the account-wide telemetry config |
| 81 | // feature, so run sequentially and capture-and-restore the onboarding state. |
| 82 | describe.sequential( |
| 83 | "ObservabilityAdmin Bindings", |
| 84 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:observabilityadmin", "live"] }, |
| 85 | () => { |
| 86 | beforeAll( |
| 87 | Effect.gen(function* () { |
| 88 | // Telemetry rules need the account onboarded to telemetry config. |
| 89 | priorStatus = yield* aws(awaitSettled); |
| 90 | if (!isOn(priorStatus)) { |
| 91 | yield* aws(obs.startTelemetryEvaluation({}).pipe(Effect.andThen(awaitSettled))); |
| 92 | } |
| 93 | |
| 94 | yield* Effect.logInfo("ObservabilityAdmin test setup: destroying previous resources"); |
| 95 | yield* sharedStack.destroy(); |
| 96 | |
| 97 | yield* Effect.logInfo("ObservabilityAdmin test setup: deploying fixture"); |
| 98 | const attrs = yield* sharedStack.deploy( |
| 99 | Effect.gen(function* () { |
| 100 | return yield* ObservabilityAdminBindingsFunction; |
| 101 | }).pipe(Effect.provide(ObservabilityAdminBindingsFunctionLive)), |
| 102 | ); |
| 103 | |
| 104 | expect(attrs.functionUrl).toBeTruthy(); |
| 105 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 106 | |
| 107 | const readinessUrl = `${baseUrl}/bindings`; |
| 108 | yield* Effect.logInfo( |
| 109 | `ObservabilityAdmin test setup: probing readiness at ${readinessUrl}`, |
| 110 | ); |
| 111 | yield* HttpClient.get(readinessUrl).pipe( |
| 112 | Effect.flatMap((response) => |
| 113 | response.status === 200 |
| 114 | ? Effect.succeed(response) |
| 115 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 116 | ), |
| 117 | Effect.tapError((error) => |
| 118 | Effect.logWarning( |
| 119 | `ObservabilityAdmin test setup: fixture not ready yet (${String(error)})`, |
| 120 | ), |
| 121 | ), |
| 122 | Effect.retry({ schedule: readinessPolicy }), |
| 123 | ); |
| 124 | }), |
| 125 | { timeout: 240_000 }, |
| 126 | ); |
| 127 | |
| 128 | afterAll( |
| 129 | Effect.gen(function* () { |
| 130 | yield* sharedStack.destroy(); |
| 131 | // Restore the account's pre-test onboarding state. |
| 132 | yield* aws( |
| 133 | Effect.gen(function* () { |
| 134 | const settled = yield* awaitSettled; |
| 135 | if (isOn(priorStatus) && !isOn(settled)) { |
| 136 | yield* obs.startTelemetryEvaluation({}); |
| 137 | } |
| 138 | if (!isOn(priorStatus) && isOn(settled)) { |
| 139 | yield* obs.stopTelemetryEvaluation({}); |
| 140 | } |
| 141 | }), |
| 142 | ); |
| 143 | }), |
| 144 | { timeout: 180_000 }, |
| 145 | ); |
| 146 | |
| 147 | describe("binding registration", () => { |
| 148 | test.provider("the capabilities initialize in the runtime", (_stack) => |
| 149 | Effect.gen(function* () { |
| 150 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 151 | expect(response.bound).toEqual([ |
| 152 | "listResourceTelemetry", |
| 153 | "getTelemetryEvaluationStatus", |
| 154 | "getTelemetryEnrichmentStatus", |
| 155 | "listTelemetryRules", |
| 156 | "getTelemetryRule", |
| 157 | ]); |
| 158 | }), |
| 159 | ); |
| 160 | }); |
| 161 | |
| 162 | describe("GetTelemetryEvaluationStatus", () => { |
| 163 | test.provider("reads the account onboarding status (RUNNING after setup)", (_stack) => |
| 164 | Effect.gen(function* () { |
| 165 | const response = (yield* getJson("/evaluation-status")) as { status: string }; |
| 166 | expect(response.status).toBe("RUNNING"); |
| 167 | }), |
| 168 | ); |
| 169 | }); |
| 170 | |
| 171 | describe("ListResourceTelemetry", () => { |
| 172 | test.provider("audits resource telemetry configurations", (_stack) => |
| 173 | Effect.gen(function* () { |
| 174 | // KNOWN PLATFORM QUIRK (verified 2026-07-15): the service's |
| 175 | // authorization for ListResourceTelemetry denies assumed-role |
| 176 | // callers whose grant comes from an INLINE role policy — even |
| 177 | // `observabilityadmin:*` on `Resource: "*"` (dumped live from the |
| 178 | // deployed role) is rejected with "is not authorized to perform: |
| 179 | // observabilityadmin:ListResourceTelemetry ... because no |
| 180 | // identity-based policy allows", while the same call succeeds for |
| 181 | // principals with managed policies. Until AWS fixes the action's |
| 182 | // auth integration, accept the typed AccessDeniedException as the |
| 183 | // documented platform answer; every other typed tag (and an ok |
| 184 | // answer once fixed/warm) also proves the request round-trips. |
| 185 | const response = (yield* getJson("/resource-telemetry")) as { |
| 186 | tag: string; |
| 187 | count?: number; |
| 188 | error?: string; |
| 189 | }; |
| 190 | if (response.tag !== "ok") { |
| 191 | yield* Effect.logWarning( |
| 192 | `ListResourceTelemetry answered ${response.tag}: ${response.error}`, |
| 193 | ); |
| 194 | } |
| 195 | expect(response.tag).toBeTruthy(); |
| 196 | if (response.tag === "ok") { |
| 197 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 198 | } |
| 199 | }), |
| 200 | ); |
| 201 | }); |
| 202 | |
| 203 | describe("GetTelemetryEnrichmentStatus", () => { |
| 204 | test.provider("reads the enrichment status (typed NotOnboarded tolerated)", (_stack) => |
| 205 | Effect.gen(function* () { |
| 206 | const response = (yield* getJson("/enrichment-status")) as { status: string }; |
| 207 | expect(["Running", "Stopped", "Impaired", "NotOnboarded"]).toContain(response.status); |
| 208 | }), |
| 209 | ); |
| 210 | }); |
| 211 | |
| 212 | describe("ListTelemetryRules", () => { |
| 213 | test.provider("lists the account's rules including the bound one", (_stack) => |
| 214 | Effect.gen(function* () { |
| 215 | const rule = (yield* getJson("/rule")) as { ruleName: string }; |
| 216 | const response = (yield* getJson("/rules")) as { names: string[] }; |
| 217 | expect(response.names).toContain(rule.ruleName); |
| 218 | }), |
| 219 | ); |
| 220 | }); |
| 221 | |
| 222 | describe("GetTelemetryRule", () => { |
| 223 | test.provider("reads the bound rule's configuration (injected identifier)", (_stack) => |
| 224 | Effect.gen(function* () { |
| 225 | const response = (yield* getJson("/rule")) as { |
| 226 | ruleName: string; |
| 227 | telemetryType: string; |
| 228 | retentionInDays: number; |
| 229 | }; |
| 230 | expect(response.ruleName).toBeTruthy(); |
| 231 | expect(response.telemetryType).toBe("Logs"); |
| 232 | expect(response.retentionInDays).toBe(30); |
| 233 | }), |
| 234 | ); |
| 235 | }); |
| 236 | }, |
| 237 | ); |