The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "@alchemy.run/test"; |
| 2 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 3 | import * as Context from "effect/Context"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as HttpClient from "effect/http/HttpClient"; |
| 6 | import type * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 7 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 8 | import * as HttpServerRequest from "effect/http/HttpServerRequest"; |
| 9 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 10 | import * as Redacted from "effect/Redacted"; |
| 11 | import { packEnvValue } from "@/RuntimeContext.ts"; |
| 12 | import * as Telemetry from "@/Telemetry.ts"; |
| 13 | |
| 14 | /** The backend's own OTLP destination, as a Worker would have it bound. */ |
| 15 | const backend = ConfigProvider.make((path) => |
| 16 | Effect.sync(() => { |
| 17 | switch (path.join("_")) { |
| 18 | case "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": |
| 19 | return ConfigProvider.makeValue("https://collector.example/v1/traces"); |
| 20 | case "OTEL_EXPORTER_OTLP_HEADERS": |
| 21 | return ConfigProvider.makeValue( |
| 22 | packEnvValue(Redacted.make("authorization=Bearer%20secret")), |
| 23 | ); |
| 24 | default: |
| 25 | return undefined; |
| 26 | } |
| 27 | }), |
| 28 | ); |
| 29 | |
| 30 | /** Post `body` to the relay, as a browser would, and record what it forwards. */ |
| 31 | const post = ( |
| 32 | relay: Effect.Effect< |
| 33 | HttpServerResponse.HttpServerResponse, |
| 34 | never, |
| 35 | HttpServerRequest.HttpServerRequest |
| 36 | >, |
| 37 | options: { path?: string; body?: string; headers?: Record<string, string>; status?: number } = {}, |
| 38 | ) => |
| 39 | Effect.gen(function* () { |
| 40 | const forwarded: Array<{ request: HttpClientRequest.HttpClientRequest; body: string }> = []; |
| 41 | const client = HttpClient.make((request) => |
| 42 | Effect.sync(() => { |
| 43 | const body = |
| 44 | request.body._tag === "Uint8Array" ? new TextDecoder().decode(request.body.body) : ""; |
| 45 | forwarded.push({ request, body }); |
| 46 | return HttpClientResponse.fromWeb( |
| 47 | request, |
| 48 | new Response(null, { status: options.status ?? 200 }), |
| 49 | ); |
| 50 | }), |
| 51 | ); |
| 52 | const body = options.body ?? '{"resourceSpans":[]}'; |
| 53 | const incoming = HttpServerRequest.fromWeb( |
| 54 | new Request(`https://app.example${options.path ?? "/api/telemetry/v1/traces"}`, { |
| 55 | method: "POST", |
| 56 | body, |
| 57 | headers: { |
| 58 | "content-type": "application/json", |
| 59 | "content-length": String(body.length), |
| 60 | ...options.headers, |
| 61 | }, |
| 62 | }), |
| 63 | ); |
| 64 | const response = yield* relay.pipe( |
| 65 | Effect.provideContext( |
| 66 | Context.make(HttpServerRequest.HttpServerRequest, incoming).pipe( |
| 67 | Context.add(HttpClient.HttpClient, client), |
| 68 | Context.add(ConfigProvider.ConfigProvider, backend), |
| 69 | ), |
| 70 | ), |
| 71 | ); |
| 72 | return { status: response.status, forwarded }; |
| 73 | }); |
| 74 | |
| 75 | describe("Telemetry.relay", { tags: ["unit", "local"] }, () => { |
| 76 | it.effect( |
| 77 | "forwards a client's batch to the backend's destination, with the backend's headers", |
| 78 | () => |
| 79 | Effect.gen(function* () { |
| 80 | const { status, forwarded } = yield* post(Telemetry.relay()); |
| 81 | expect(status).toBe(200); |
| 82 | expect(forwarded).toHaveLength(1); |
| 83 | expect(forwarded[0]!.request.url).toBe("https://collector.example/v1/traces"); |
| 84 | expect(forwarded[0]!.request.headers.authorization).toBe("Bearer secret"); |
| 85 | expect(forwarded[0]!.body).toBe('{"resourceSpans":[]}'); |
| 86 | }), |
| 87 | ); |
| 88 | |
| 89 | it.effect("routes a batch wherever `route` says", () => |
| 90 | Effect.gen(function* () { |
| 91 | const relay = Telemetry.relay({ |
| 92 | route: (signal, configured) => |
| 93 | Effect.succeed([ |
| 94 | ...configured.map((d) => ({ |
| 95 | ...d, |
| 96 | headers: { ...d.headers, "x-dataset": `browser-${signal}` }, |
| 97 | })), |
| 98 | { url: "https://other.example/v1/traces" }, |
| 99 | ]), |
| 100 | }); |
| 101 | const { forwarded } = yield* post(relay); |
| 102 | expect(forwarded.map((f) => f.request.url)).toEqual([ |
| 103 | "https://collector.example/v1/traces", |
| 104 | "https://other.example/v1/traces", |
| 105 | ]); |
| 106 | expect(forwarded[0]!.request.headers["x-dataset"]).toBe("browser-traces"); |
| 107 | expect(forwarded[0]!.request.headers.authorization).toBe("Bearer secret"); |
| 108 | }), |
| 109 | ); |
| 110 | |
| 111 | it.effect("refuses what it isn't configured to accept", () => |
| 112 | Effect.gen(function* () { |
| 113 | const tracesOnly = Telemetry.relay({ signals: ["traces"], maxBytes: 64 }); |
| 114 | expect((yield* post(tracesOnly, { path: "/api/telemetry/v1/logs" })).status).toBe(404); |
| 115 | expect((yield* post(tracesOnly, { path: "/api/telemetry/v1/nope" })).status).toBe(404); |
| 116 | const big = yield* post(tracesOnly, { body: "x".repeat(100) }); |
| 117 | expect(big.status).toBe(413); |
| 118 | expect(big.forwarded).toHaveLength(0); |
| 119 | |
| 120 | const signedIn = Telemetry.relay({ |
| 121 | authorize: Effect.map( |
| 122 | HttpServerRequest.HttpServerRequest, |
| 123 | (r) => r.headers.cookie === "session=ok", |
| 124 | ), |
| 125 | }); |
| 126 | expect((yield* post(signedIn)).status).toBe(403); |
| 127 | expect((yield* post(signedIn, { headers: { cookie: "session=ok" } })).status).toBe(200); |
| 128 | }), |
| 129 | ); |
| 130 | |
| 131 | it.effect("reports a batch no destination accepted", () => |
| 132 | Effect.gen(function* () { |
| 133 | const { status, forwarded } = yield* post(Telemetry.relay(), { status: 500 }); |
| 134 | expect(forwarded).toHaveLength(1); |
| 135 | expect(status).toBe(502); |
| 136 | }), |
| 137 | ); |
| 138 | }); |