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 Cause from "effect/Cause"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Exit from "effect/Exit"; |
| 5 | import * as Option from "effect/Option"; |
| 6 | import * as Tracer from "effect/Tracer"; |
| 7 | |
| 8 | import { |
| 9 | nonInterferingTracer, |
| 10 | RelayClientTracer, |
| 11 | withLocalTracing, |
| 12 | withRelayClientTracing, |
| 13 | } from "./relayTracing.ts"; |
| 14 | |
| 15 | function collectingTracer(spans: Array<string>): Tracer.Tracer { |
| 16 | return Tracer.make({ |
| 17 | span: (options) => { |
| 18 | const span = new Tracer.NativeSpan(options); |
| 19 | const end = span.end.bind(span); |
| 20 | span.end = (endTime, exit) => { |
| 21 | end(endTime, exit); |
| 22 | spans.push(span.name); |
| 23 | }; |
| 24 | return span; |
| 25 | }, |
| 26 | }); |
| 27 | } |
| 28 | |
| 29 | describe("withRelayClientTracing", () => { |
| 30 | it.effect("uses the product tracer only for relay operations", () => |
| 31 | Effect.gen(function* () { |
| 32 | const userSpans: Array<string> = []; |
| 33 | const productSpans: Array<string> = []; |
| 34 | const userTracer = collectingTracer(userSpans); |
| 35 | const productTracer = collectingTracer(productSpans); |
| 36 | |
| 37 | yield* Effect.void.pipe(Effect.withSpan("user.operation"), Effect.withTracer(userTracer)); |
| 38 | yield* Effect.void.pipe( |
| 39 | Effect.withSpan("relay.operation"), |
| 40 | withRelayClientTracing, |
| 41 | Effect.provideService(RelayClientTracer, Option.some(productTracer)), |
| 42 | Effect.withTracer(userTracer), |
| 43 | ); |
| 44 | |
| 45 | expect(userSpans).toEqual(["user.operation"]); |
| 46 | expect(productSpans).toEqual(["relay.operation"]); |
| 47 | }), |
| 48 | ); |
| 49 | }); |
| 50 | |
| 51 | describe("nonInterferingTracer", () => { |
| 52 | it.effect("exports failures without their messages", () => |
| 53 | Effect.gen(function* () { |
| 54 | const exits: Array<Exit.Exit<unknown, unknown>> = []; |
| 55 | const delegate = Tracer.make({ |
| 56 | span: (options) => { |
| 57 | const span = new Tracer.NativeSpan(options); |
| 58 | const end = span.end.bind(span); |
| 59 | span.end = (endTime, exit) => { |
| 60 | end(endTime, exit); |
| 61 | exits.push(exit); |
| 62 | }; |
| 63 | return span; |
| 64 | }, |
| 65 | }); |
| 66 | const failure = Object.assign( |
| 67 | new Error("studio-mac could not establish a WebSocket connection.", { |
| 68 | cause: new Error("connect ECONNREFUSED studio-mac.local"), |
| 69 | }), |
| 70 | { name: "ConnectionTransientError" }, |
| 71 | ); |
| 72 | |
| 73 | yield* Effect.fail(failure).pipe( |
| 74 | Effect.withSpan("relay.connection.attempt"), |
| 75 | Effect.withTracer(nonInterferingTracer(delegate)), |
| 76 | Effect.ignore, |
| 77 | ); |
| 78 | |
| 79 | const exit = exits[0]; |
| 80 | if (exit === undefined || !Exit.isFailure(exit)) throw new Error("expected a failed span"); |
| 81 | const exported = Cause.prettyErrors(exit.cause); |
| 82 | expect(exported.map((error) => error.name)).toEqual(["ConnectionTransientError"]); |
| 83 | const text = exported.map((error) => `${error.message}\n${error.stack}`).join("\n"); |
| 84 | expect(text).toContain("Caused by: Error"); |
| 85 | expect(text).not.toContain("studio-mac"); |
| 86 | }), |
| 87 | ); |
| 88 | }); |
| 89 | |
| 90 | describe("withLocalTracing", () => { |
| 91 | it.effect("keeps local work inside a relay span off the product tracer", () => |
| 92 | Effect.gen(function* () { |
| 93 | const localSpans: Array<string> = []; |
| 94 | const productSpans: Array<string> = []; |
| 95 | const localTracer = collectingTracer(localSpans); |
| 96 | const productTracer = collectingTracer(productSpans); |
| 97 | |
| 98 | yield* Effect.void.pipe( |
| 99 | Effect.withSpan("relay.connection.nested"), |
| 100 | Effect.andThen( |
| 101 | Effect.void.pipe( |
| 102 | Effect.withSpan("sql.execute"), |
| 103 | Effect.withSpan("ServerSecretStore.get"), |
| 104 | withLocalTracing, |
| 105 | ), |
| 106 | ), |
| 107 | Effect.withSpan("environment.orchestration.threadSnapshot"), |
| 108 | withRelayClientTracing, |
| 109 | Effect.provideService(RelayClientTracer, Option.some(productTracer)), |
| 110 | Effect.withTracer(localTracer), |
| 111 | ); |
| 112 | |
| 113 | expect(productSpans).toEqual([ |
| 114 | "relay.connection.nested", |
| 115 | "environment.orchestration.threadSnapshot", |
| 116 | ]); |
| 117 | expect(localSpans).toEqual(["sql.execute", "ServerSecretStore.get"]); |
| 118 | }), |
| 119 | ); |
| 120 | }); |