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 Effect from "effect/Effect"; |
| 6 | import * as HttpClient from "effect/http/HttpClient"; |
| 7 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import RumBindingsFunctionLive, { RumBindingsFunction } from "./handler"; |
| 10 | |
| 11 | const testOptions = { providers: AWS.providers() }; |
| 12 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 13 | const sharedStack = Core.scratchStack(testOptions, "RumBindings"); |
| 14 | |
| 15 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 16 | // over 60s on a fresh deploy. |
| 17 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 18 | |
| 19 | let baseUrl: string; |
| 20 | |
| 21 | const getJson = (path: string) => |
| 22 | HttpClient.get(`${baseUrl}${path}`).pipe(Effect.flatMap((r) => r.json)); |
| 23 | |
| 24 | const postJson = (path: string) => |
| 25 | HttpClient.execute(HttpClientRequest.post(`${baseUrl}${path}`)).pipe( |
| 26 | Effect.flatMap((r) => r.json), |
| 27 | ); |
| 28 | |
| 29 | interface TagResponse { |
| 30 | tag: string; |
| 31 | error?: string; |
| 32 | count?: number; |
| 33 | } |
| 34 | |
| 35 | // The role policy granting rum:* on the monitor is attached moments before |
| 36 | // the first request — IAM propagation can lag ~10-30s, surfacing as |
| 37 | // AccessDeniedException. Retry (bounded) until the grant lands; any other |
| 38 | // tag surfaces immediately for assertion. |
| 39 | const untilGranted = <E, R>(request: Effect.Effect<unknown, E, R>) => |
| 40 | request.pipe( |
| 41 | Effect.map((r) => r as TagResponse), |
| 42 | Effect.repeat({ |
| 43 | schedule: Schedule.spaced("5 seconds"), |
| 44 | until: (r): boolean => r.tag !== "AccessDeniedException", |
| 45 | times: 10, |
| 46 | }), |
| 47 | ); |
| 48 | |
| 49 | describe.sequential( |
| 50 | "RUM Bindings", |
| 51 | { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:rum", "live"] }, |
| 52 | () => { |
| 53 | beforeAll( |
| 54 | Effect.gen(function* () { |
| 55 | yield* Effect.logInfo("RUM test setup: destroying previous resources"); |
| 56 | yield* sharedStack.destroy(); |
| 57 | |
| 58 | yield* Effect.logInfo("RUM test setup: deploying fixture"); |
| 59 | const attrs = yield* sharedStack.deploy( |
| 60 | Effect.gen(function* () { |
| 61 | return yield* RumBindingsFunction; |
| 62 | }).pipe(Effect.provide(RumBindingsFunctionLive)), |
| 63 | ); |
| 64 | |
| 65 | expect(attrs.functionUrl).toBeTruthy(); |
| 66 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 67 | |
| 68 | yield* Effect.logInfo("RUM test setup: probing readiness"); |
| 69 | yield* HttpClient.get(`${baseUrl}/bindings`).pipe( |
| 70 | Effect.flatMap((response) => |
| 71 | response.status === 200 |
| 72 | ? Effect.succeed(response) |
| 73 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 74 | ), |
| 75 | Effect.retry({ schedule: readinessPolicy }), |
| 76 | ); |
| 77 | }), |
| 78 | { timeout: 300_000 }, |
| 79 | ); |
| 80 | |
| 81 | afterAll(process.env.NO_DESTROY ? Effect.void : sharedStack.destroy(), { timeout: 300_000 }); |
| 82 | |
| 83 | describe("binding registration", () => { |
| 84 | test.provider("both capabilities initialize in the runtime", (_stack) => |
| 85 | Effect.gen(function* () { |
| 86 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 87 | expect(response.bound).toHaveLength(2); |
| 88 | expect(response.bound).toContain("putRumEvents"); |
| 89 | expect(response.bound).toContain("getAppMonitorData"); |
| 90 | }), |
| 91 | ); |
| 92 | }); |
| 93 | |
| 94 | describe("PutRumEvents", () => { |
| 95 | test.provider( |
| 96 | "sends a session's events to the data-plane endpoint", |
| 97 | (_stack) => |
| 98 | Effect.gen(function* () { |
| 99 | const response = (yield* untilGranted(postJson("/events"))) as TagResponse; |
| 100 | yield* Effect.logInfo(`/events response: ${JSON.stringify(response)}`); |
| 101 | // "ok" proves the dataplane host prefix, the id/details injection, |
| 102 | // and the rum:PutRumEvents grant all round-tripped. On failure the |
| 103 | // route's error string surfaces in the assertion diff. |
| 104 | expect(response.error ?? response.tag).toBe("ok"); |
| 105 | }), |
| 106 | { timeout: 120_000 }, |
| 107 | ); |
| 108 | }); |
| 109 | |
| 110 | describe("GetAppMonitorData", () => { |
| 111 | test.provider( |
| 112 | "reads the trailing hour of events (name injected)", |
| 113 | (_stack) => |
| 114 | Effect.gen(function* () { |
| 115 | const response = (yield* untilGranted(getJson("/data"))) as TagResponse; |
| 116 | yield* Effect.logInfo(`/data response: ${JSON.stringify(response)}`); |
| 117 | expect(response.error ?? response.tag).toBe("ok"); |
| 118 | // Ingested events surface asynchronously (minutes) — a zero count |
| 119 | // still proves the grant + monitor-name injection round-tripped. |
| 120 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 121 | }), |
| 122 | { timeout: 120_000 }, |
| 123 | ); |
| 124 | }); |
| 125 | }, |
| 126 | ); |