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 eventbridge from "@distilled.cloud/aws/eventbridge"; |
| 6 | import * as iam from "@distilled.cloud/aws/iam"; |
| 7 | import * as Lambda from "@distilled.cloud/aws/lambda"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as HttpClient from "effect/http/HttpClient"; |
| 11 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import XRayTestFunctionLive, { XRayTestFunction } from "./handler"; |
| 14 | |
| 15 | const testOptions = { providers: AWS.providers() }; |
| 16 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 17 | const sharedStack = Core.scratchStack(testOptions, "XRayBindings"); |
| 18 | |
| 19 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 20 | // over 60s on a fresh deploy under parallel-suite load. Budget ~150s of |
| 21 | // readiness polling. |
| 22 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 23 | |
| 24 | let baseUrl: string; |
| 25 | let functionName: string; |
| 26 | let functionArn: string; |
| 27 | let roleName: string; |
| 28 | |
| 29 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 30 | readonly status: number; |
| 31 | readonly body: string; |
| 32 | }> {} |
| 33 | |
| 34 | class FunctionStillExists extends Data.TaggedError("FunctionStillExists")<{ |
| 35 | readonly functionName: string; |
| 36 | }> {} |
| 37 | |
| 38 | // The shared Lambda fixture occasionally answers a transient 5xx under |
| 39 | // parallel load (cold re-init, IAM propagation). Retry 5xx only; a genuine |
| 40 | // 4xx/assertion failure surfaces immediately. |
| 41 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 42 | HttpClient.execute(request).pipe( |
| 43 | Effect.flatMap((response) => |
| 44 | response.status >= 500 |
| 45 | ? response.text.pipe( |
| 46 | Effect.flatMap((body) => |
| 47 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 48 | ), |
| 49 | ) |
| 50 | : Effect.succeed(response), |
| 51 | ), |
| 52 | Effect.retry({ |
| 53 | while: (e) => e._tag === "TransientUpstream", |
| 54 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 55 | }), |
| 56 | ); |
| 57 | |
| 58 | describe( |
| 59 | "XRay Bindings", |
| 60 | { |
| 61 | tags: [ |
| 62 | "provider:aws", |
| 63 | "provider:aws:batch", |
| 64 | "provider:aws:lambda", |
| 65 | "provider:aws:xray", |
| 66 | "live", |
| 67 | ], |
| 68 | }, |
| 69 | () => { |
| 70 | beforeAll( |
| 71 | Effect.gen(function* () { |
| 72 | yield* Effect.logInfo("XRay test setup: destroying previous resources"); |
| 73 | yield* sharedStack.destroy(); |
| 74 | |
| 75 | yield* Effect.logInfo("XRay test setup: deploying fixture"); |
| 76 | const attrs = yield* sharedStack.deploy( |
| 77 | Effect.gen(function* () { |
| 78 | return yield* XRayTestFunction; |
| 79 | }).pipe(Effect.provide(XRayTestFunctionLive)), |
| 80 | ); |
| 81 | |
| 82 | expect(attrs.functionUrl).toBeTruthy(); |
| 83 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 84 | functionName = attrs.functionName; |
| 85 | functionArn = attrs.functionArn; |
| 86 | roleName = attrs.roleName; |
| 87 | |
| 88 | const readinessUrl = `${baseUrl}/ping`; |
| 89 | yield* Effect.logInfo(`XRay test setup: probing readiness at ${readinessUrl}`); |
| 90 | yield* HttpClient.get(readinessUrl).pipe( |
| 91 | Effect.flatMap((response) => |
| 92 | response.status === 200 |
| 93 | ? Effect.succeed(response) |
| 94 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 95 | ), |
| 96 | Effect.tapError((error) => |
| 97 | Effect.logWarning(`XRay test setup: fixture not ready yet (${String(error)})`), |
| 98 | ), |
| 99 | Effect.retry({ schedule: readinessPolicy }), |
| 100 | ); |
| 101 | }), |
| 102 | { timeout: 240_000 }, |
| 103 | ); |
| 104 | |
| 105 | afterAll( |
| 106 | Effect.gen(function* () { |
| 107 | yield* sharedStack.destroy(); |
| 108 | // assert the fixture Lambda is really gone (typed not-found only) |
| 109 | if (functionName) { |
| 110 | yield* Core.withProviders( |
| 111 | Lambda.getFunctionConfiguration({ FunctionName: functionName }).pipe( |
| 112 | Effect.flatMap(() => Effect.fail(new FunctionStillExists({ functionName }))), |
| 113 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 114 | Effect.retry({ |
| 115 | while: (e) => e._tag === "FunctionStillExists", |
| 116 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]), |
| 117 | }), |
| 118 | ), |
| 119 | testOptions, |
| 120 | "XRayBindings", |
| 121 | ); |
| 122 | } |
| 123 | }), |
| 124 | { timeout: 120_000 }, |
| 125 | ); |
| 126 | |
| 127 | describe("Function tracing", { tags: ["provider:aws:iam"] }, () => { |
| 128 | test.provider("deployed function has Active tracing and the X-Ray write policy", (_stack) => |
| 129 | Effect.gen(function* () { |
| 130 | const config = yield* Lambda.getFunctionConfiguration({ FunctionName: functionName }); |
| 131 | expect(config.TracingConfig?.Mode).toBe("Active"); |
| 132 | |
| 133 | const attached = yield* iam.listAttachedRolePolicies({ RoleName: roleName }); |
| 134 | const arns = (attached.AttachedPolicies ?? []).map((policy) => policy.PolicyArn); |
| 135 | expect(arns).toContain("arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess"); |
| 136 | }), |
| 137 | ); |
| 138 | }); |
| 139 | |
| 140 | describe("GetTraceSummaries", () => { |
| 141 | test.provider("queries trace summaries through the binding", (_stack) => |
| 142 | Effect.gen(function* () { |
| 143 | // Proves the deploy-time IAM binding: the call succeeds (200) even |
| 144 | // when no traces have been ingested yet. |
| 145 | const response = yield* send( |
| 146 | HttpClientRequest.get(`${baseUrl}/trace-summaries?service=${functionName}`), |
| 147 | ); |
| 148 | expect(response.status).toBe(200); |
| 149 | const body = (yield* response.json) as { traceIds: string[] }; |
| 150 | expect(Array.isArray(body.traceIds)).toBe(true); |
| 151 | }), |
| 152 | ); |
| 153 | }); |
| 154 | |
| 155 | describe("BatchGetTraces", () => { |
| 156 | test.provider("fetches traces by id through the binding", (_stack) => |
| 157 | Effect.gen(function* () { |
| 158 | // A syntactically valid, recent, but non-existent trace id proves the |
| 159 | // IAM binding without depending on ingestion latency. The embedded |
| 160 | // timestamp must be current — X-Ray rejects ids outside its retention |
| 161 | // window. |
| 162 | const epochHex = yield* Effect.sync(() => Math.floor(Date.now() / 1000).toString(16)); |
| 163 | const response = yield* send( |
| 164 | HttpClientRequest.get( |
| 165 | `${baseUrl}/batch-get-traces?ids=1-${epochHex}-abcdef0123456789abcdef01`, |
| 166 | ), |
| 167 | ); |
| 168 | expect(response.status).toBe(200); |
| 169 | const body = (yield* response.json) as { |
| 170 | traces: Array<{ id: string; segments: number }>; |
| 171 | unprocessed: string[]; |
| 172 | }; |
| 173 | expect(body.traces).toEqual([]); |
| 174 | }), |
| 175 | ); |
| 176 | }); |
| 177 | |
| 178 | describe("PutTraceSegments", () => { |
| 179 | test.provider("uploads a custom segment through the binding", (_stack) => |
| 180 | Effect.gen(function* () { |
| 181 | const response = yield* send(HttpClientRequest.post(`${baseUrl}/put-trace-segments`)); |
| 182 | expect(response.status).toBe(200); |
| 183 | const body = (yield* response.json) as { traceId: string; unprocessed: unknown[] }; |
| 184 | expect(body.traceId).toMatch(/^1-[0-9a-f]{8}-[0-9a-f]{24}$/); |
| 185 | expect(body.unprocessed).toEqual([]); |
| 186 | }), |
| 187 | ); |
| 188 | }); |
| 189 | |
| 190 | describe("PutTelemetryRecords", () => { |
| 191 | test.provider("uploads telemetry through the binding", (_stack) => |
| 192 | Effect.gen(function* () { |
| 193 | const response = yield* send(HttpClientRequest.post(`${baseUrl}/telemetry`)); |
| 194 | expect(response.status).toBe(200); |
| 195 | const body = (yield* response.json) as { ok: boolean }; |
| 196 | expect(body.ok).toBe(true); |
| 197 | }), |
| 198 | ); |
| 199 | }); |
| 200 | |
| 201 | describe("Sampling (GetSamplingRules, GetSamplingTargets, GetSamplingStatisticSummaries)", () => { |
| 202 | test.provider("exercises the sampling protocol bindings", (_stack) => |
| 203 | Effect.gen(function* () { |
| 204 | const response = yield* send(HttpClientRequest.get(`${baseUrl}/sampling`)); |
| 205 | expect(response.status).toBe(200); |
| 206 | const body = (yield* response.json) as { |
| 207 | ruleNames: string[]; |
| 208 | statisticSummaries: number; |
| 209 | targets: { outcome: string; documents: number | null }; |
| 210 | }; |
| 211 | // The built-in fallback rule always exists. |
| 212 | expect(body.ruleNames).toContain("Default"); |
| 213 | expect(body.statisticSummaries).toBeGreaterThanOrEqual(0); |
| 214 | // Reporting statistics for the Default rule yields its target. |
| 215 | expect(body.targets.outcome).toBe("ok"); |
| 216 | expect(body.targets.documents).toBeGreaterThanOrEqual(1); |
| 217 | }), |
| 218 | ); |
| 219 | }); |
| 220 | |
| 221 | describe("Service graphs (GetServiceGraph, GetTraceGraph, GetTimeSeriesServiceStatistics)", () => { |
| 222 | test.provider("queries the service graph bindings", (_stack) => |
| 223 | Effect.gen(function* () { |
| 224 | // A typed error tag still proves the IAM grant went through — the |
| 225 | // typed set below excludes AccessDeniedException. |
| 226 | const graphTags = ["InvalidRequestException", "ValidationException"]; |
| 227 | |
| 228 | const graph = (yield* send(HttpClientRequest.get(`${baseUrl}/service-graph`)).pipe( |
| 229 | Effect.flatMap((r) => r.json), |
| 230 | )) as { services: number | null; error: string | null }; |
| 231 | if (graph.error !== null) { |
| 232 | expect(graphTags).toContain(graph.error); |
| 233 | } else { |
| 234 | expect(graph.services).toBeGreaterThanOrEqual(0); |
| 235 | } |
| 236 | |
| 237 | const epochHex = yield* Effect.sync(() => Math.floor(Date.now() / 1000).toString(16)); |
| 238 | const traceGraph = (yield* send( |
| 239 | HttpClientRequest.get( |
| 240 | `${baseUrl}/trace-graph?ids=1-${epochHex}-abcdef0123456789abcdef01`, |
| 241 | ), |
| 242 | ).pipe(Effect.flatMap((r) => r.json))) as { |
| 243 | services: number | null; |
| 244 | error: string | null; |
| 245 | }; |
| 246 | if (traceGraph.error !== null) { |
| 247 | expect(graphTags).toContain(traceGraph.error); |
| 248 | } else { |
| 249 | expect(traceGraph.services).toBe(0); |
| 250 | } |
| 251 | |
| 252 | const timeSeries = (yield* send( |
| 253 | HttpClientRequest.get(`${baseUrl}/time-series?service=${functionName}`), |
| 254 | ).pipe(Effect.flatMap((r) => r.json))) as { points: number | null; error: string | null }; |
| 255 | // A typed validation error (e.g. the entity selector needing |
| 256 | // Transaction Search) still proves the IAM grant. |
| 257 | if (timeSeries.error !== null) { |
| 258 | expect(["InvalidRequestException", "ValidationException"]).toContain(timeSeries.error); |
| 259 | } else { |
| 260 | expect(timeSeries.points).toBeGreaterThanOrEqual(0); |
| 261 | } |
| 262 | }), |
| 263 | ); |
| 264 | }); |
| 265 | |
| 266 | describe("Insights (GetInsight, GetInsightEvents, GetInsightImpactGraph, GetInsightSummaries)", () => { |
| 267 | test.provider("queries insight summaries for the Default group", (_stack) => |
| 268 | Effect.gen(function* () { |
| 269 | const response = yield* send( |
| 270 | HttpClientRequest.get(`${baseUrl}/insight-summaries?group=Default`), |
| 271 | ); |
| 272 | expect(response.status).toBe(200); |
| 273 | const body = (yield* response.json) as { insights: number | null; error: string | null }; |
| 274 | // Insights may not be enabled on the Default group — a typed |
| 275 | // validation error still proves the IAM grant (an IAM failure |
| 276 | // would surface as AccessDeniedException). |
| 277 | if (body.error !== null) { |
| 278 | expect(body.error).toBe("InvalidRequestException"); |
| 279 | } else { |
| 280 | expect(body.insights).toBeGreaterThanOrEqual(0); |
| 281 | } |
| 282 | }), |
| 283 | ); |
| 284 | |
| 285 | test.provider( |
| 286 | "insight lookups answer with typed validation errors (not access denials)", |
| 287 | (_stack) => |
| 288 | Effect.gen(function* () { |
| 289 | // No insight can be provisioned on demand — each binding's IAM |
| 290 | // grant is proven by the API answering with its typed validation |
| 291 | // error for a nonexistent insight id. |
| 292 | const body = (yield* send(HttpClientRequest.get(`${baseUrl}/insight`)).pipe( |
| 293 | Effect.flatMap((r) => r.json), |
| 294 | )) as { getInsight: string; getInsightEvents: string; getInsightImpactGraph: string }; |
| 295 | for (const outcome of [ |
| 296 | body.getInsight, |
| 297 | body.getInsightEvents, |
| 298 | body.getInsightImpactGraph, |
| 299 | ]) { |
| 300 | // ValidationException is a typed CommonErrors member — X-Ray |
| 301 | // answers with it for a nonexistent insight id. |
| 302 | expect(["ok", "InvalidRequestException", "ValidationException"]).toContain(outcome); |
| 303 | } |
| 304 | }), |
| 305 | ); |
| 306 | }); |
| 307 | |
| 308 | describe("Transaction Search (GetTraceSegmentDestination, StartTraceRetrieval, ListRetrievedTraces, GetRetrievedTracesGraph, CancelTraceRetrieval)", () => { |
| 309 | test.provider("exercises the trace retrieval bindings", (_stack) => |
| 310 | Effect.gen(function* () { |
| 311 | const body = (yield* send(HttpClientRequest.get(`${baseUrl}/trace-retrieval`)).pipe( |
| 312 | Effect.flatMap((r) => r.json), |
| 313 | )) as { |
| 314 | destination: string; |
| 315 | startTraceRetrieval: string; |
| 316 | listRetrievedTraces: string; |
| 317 | getRetrievedTracesGraph: string; |
| 318 | cancelTraceRetrieval: string; |
| 319 | }; |
| 320 | expect(["XRay", "CloudWatchLogs"]).toContain(body.destination); |
| 321 | // On the default X-Ray destination the retrieval workflow answers |
| 322 | // with typed errors; with Transaction Search enabled it succeeds. |
| 323 | // Either way each call went through its granted IAM action. |
| 324 | const accepted = ["ok", "InvalidRequestException", "ResourceNotFoundException"]; |
| 325 | expect(accepted).toContain(body.startTraceRetrieval); |
| 326 | expect(accepted).toContain(body.listRetrievedTraces); |
| 327 | expect(accepted).toContain(body.getRetrievedTracesGraph); |
| 328 | expect(accepted).toContain(body.cancelTraceRetrieval); |
| 329 | }), |
| 330 | ); |
| 331 | }); |
| 332 | |
| 333 | describe("consumeInsightEvents", { tags: ["provider:aws:eventbridge"] }, () => { |
| 334 | test.provider("the deploy created an EventBridge rule targeting the function", (_stack) => |
| 335 | Effect.gen(function* () { |
| 336 | // Out-of-band via distilled: the fixture's consumeInsightEvents |
| 337 | // must have materialized as a rule on the default bus with the |
| 338 | // Lambda as target. |
| 339 | const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({ |
| 340 | TargetArn: functionArn, |
| 341 | }); |
| 342 | expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1); |
| 343 | }), |
| 344 | ); |
| 345 | }); |
| 346 | |
| 347 | describe("Trace ingestion", () => { |
| 348 | // X-Ray trace ingestion typically lands within 5-30s of the invocation, |
| 349 | // but can occasionally exceed that under load. The poll below budgets |
| 350 | // ~90s (18 x 5s); a full traced round-trip (invoke -> summary -> batch |
| 351 | // fetch) is asserted once a trace id shows up. |
| 352 | test.provider( |
| 353 | "invoking the traced function produces a queryable trace", |
| 354 | (_stack) => |
| 355 | Effect.gen(function* () { |
| 356 | // Generate a handful of sampled invocations. |
| 357 | yield* Effect.forEach([1, 2, 3], () => send(HttpClientRequest.get(`${baseUrl}/ping`)), { |
| 358 | discard: true, |
| 359 | }); |
| 360 | |
| 361 | const traceIds = yield* send( |
| 362 | HttpClientRequest.get(`${baseUrl}/trace-summaries?service=${functionName}`), |
| 363 | ).pipe( |
| 364 | Effect.flatMap((response) => response.json), |
| 365 | Effect.map((body) => (body as { traceIds: string[] }).traceIds), |
| 366 | // A throttled poll iteration is an empty result, not a failure — |
| 367 | // the bounded repeat keeps polling. |
| 368 | Effect.catchTag("TransientUpstream", () => Effect.succeed([] as string[])), |
| 369 | Effect.repeat({ |
| 370 | schedule: Schedule.spaced("5 seconds"), |
| 371 | until: (ids) => ids.length > 0, |
| 372 | times: 18, |
| 373 | }), |
| 374 | ); |
| 375 | expect(traceIds.length).toBeGreaterThan(0); |
| 376 | |
| 377 | // Round-trip the discovered id through BatchGetTraces. |
| 378 | const batch = (yield* send( |
| 379 | HttpClientRequest.get(`${baseUrl}/batch-get-traces?ids=${traceIds[0]}`), |
| 380 | ).pipe(Effect.flatMap((response) => response.json))) as { |
| 381 | traces: Array<{ id: string; segments: number }>; |
| 382 | }; |
| 383 | expect(batch.traces.length).toBe(1); |
| 384 | expect(batch.traces[0].id).toBe(traceIds[0]); |
| 385 | expect(batch.traces[0].segments).toBeGreaterThan(0); |
| 386 | }), |
| 387 | { timeout: 120_000 }, |
| 388 | ); |
| 389 | }); |
| 390 | }, |
| 391 | ); |