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, 03:37 UTC
| 1 | import { describe, expect, it } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Option from "effect/Option"; |
| 4 | import * as Tracer from "effect/Tracer"; |
| 5 | import { HttpServerRequest } from "effect/http"; |
| 6 | import { RelayClientTracer } from "@t3tools/shared/relayTracing"; |
| 7 | |
| 8 | import { |
| 9 | traceAuthenticatedRelayRequest, |
| 10 | traceLocalHandlerWork, |
| 11 | traceRelayRequest, |
| 12 | } from "./traceRelayRequest.ts"; |
| 13 | |
| 14 | const AUTHENTICATION = { startTime: 1_000n, endTime: 2_000n }; |
| 15 | |
| 16 | describe("relay request tracing", () => { |
| 17 | it.effect("does not accept an unauthenticated request trace parent", () => |
| 18 | Effect.gen(function* () { |
| 19 | const spans: Array<Tracer.Span> = []; |
| 20 | const productTracer = Tracer.make({ |
| 21 | span: (options) => { |
| 22 | const span = new Tracer.NativeSpan(options); |
| 23 | spans.push(span); |
| 24 | return span; |
| 25 | }, |
| 26 | }); |
| 27 | const request = HttpServerRequest.fromWeb( |
| 28 | new Request("https://environment.example.test/api/t3-cloud/mint-credential", { |
| 29 | headers: { |
| 30 | traceparent: "00-0123456789abcdef0123456789abcdef-0123456789abcdef-01", |
| 31 | }, |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | yield* traceRelayRequest(Effect.void.pipe(Effect.withSpan("relay.mint.handler"))).pipe( |
| 36 | Effect.provideService(HttpServerRequest.HttpServerRequest, request), |
| 37 | Effect.provideService(RelayClientTracer, Option.some(productTracer)), |
| 38 | ); |
| 39 | |
| 40 | expect(spans).toHaveLength(1); |
| 41 | const span = spans[0]!; |
| 42 | expect(span.traceId).not.toBe("0123456789abcdef0123456789abcdef"); |
| 43 | expect(Option.isNone(span.parent)).toBe(true); |
| 44 | }), |
| 45 | ); |
| 46 | }); |
| 47 | |
| 48 | describe("relay request tracing boundary", () => { |
| 49 | it.effect("exports a T3 Connect handler span but not its local work", () => |
| 50 | Effect.gen(function* () { |
| 51 | const productSpans: Array<string> = []; |
| 52 | const localSpans: Array<string> = []; |
| 53 | const collect = (into: Array<string>) => |
| 54 | Tracer.make({ |
| 55 | span: (options) => { |
| 56 | into.push(options.name); |
| 57 | return new Tracer.NativeSpan(options); |
| 58 | }, |
| 59 | }); |
| 60 | const request = HttpServerRequest.fromWeb( |
| 61 | new Request("https://environment.example.test/api/orchestration/threads/thread-1"), |
| 62 | ); |
| 63 | |
| 64 | const handler = Effect.fn("environment.orchestration.threadSnapshot")(function* () { |
| 65 | yield* Effect.void.pipe( |
| 66 | Effect.withSpan("sql.execute"), |
| 67 | Effect.withSpan("ServerSecretStore.get"), |
| 68 | traceLocalHandlerWork, |
| 69 | ); |
| 70 | }); |
| 71 | |
| 72 | yield* traceAuthenticatedRelayRequest(handler(), AUTHENTICATION).pipe( |
| 73 | Effect.provideService(HttpServerRequest.HttpServerRequest, request), |
| 74 | Effect.provideService(RelayClientTracer, Option.some(collect(productSpans))), |
| 75 | Effect.withTracer(collect(localSpans)), |
| 76 | ); |
| 77 | |
| 78 | expect(productSpans).toEqual([ |
| 79 | "environment.relay.request", |
| 80 | "EnvironmentAuth.authenticateHttpRequest", |
| 81 | "environment.orchestration.threadSnapshot", |
| 82 | ]); |
| 83 | expect(localSpans).toEqual(["ServerSecretStore.get", "sql.execute"]); |
| 84 | }), |
| 85 | ); |
| 86 | }); |