Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from "@effect/vitest";
2import { withRelayClientTracing } from "@t3tools/shared/relayTracing";
3import * as Effect from "effect/Effect";
4import * as Tracer from "effect/Tracer";
5
6import * as ClientTracer from "../observability/clientTracer";
7import { runtime } from "./runtime";
8
9afterEach(() => {
10 vi.unstubAllEnvs();
11 vi.unstubAllGlobals();
12});
13
14describe("web runtime", () => {
15 it("routes client spans to the exporter client tracing configured", async () => {
16 const exported: Array<string> = [];
17 ClientTracer.setDelegate(
18 Tracer.make({
19 span(options) {
20 exported.push(options.name);
21 return new Tracer.NativeSpan(options);
22 },
23 }),
24 );
25
26 try {
27 await runtime.runPromise(Effect.void.pipe(Effect.withSpan("client.work")));
28 } finally {
29 ClientTracer.setDelegate(null);
30 }
31
32 expect(exported).toEqual(["client.work"]);
33 });
34});
35
36describe("web runtime relay telemetry policy", () => {
37 it.each([
38 {
39 surface: "desktop opt-out",
40 desktopBridge: { getRelayTelemetryEnabled: () => false },
41 exports: false,
42 },
43 {
44 surface: "desktop enabled",
45 desktopBridge: { getRelayTelemetryEnabled: () => true },
46 exports: true,
47 },
48 { surface: "older desktop bridge", desktopBridge: {}, exports: true },
49 {
50 surface: "ordinary browser ignores host opt-out",
51 desktopBridge: undefined,
52 exports: true,
53 },
54 ])("applies $surface before the first relay span", async ({ desktopBridge, exports }) => {
55 vi.resetModules();
56 vi.stubEnv("T3CODE_TELEMETRY_ENABLED", "false");
57 vi.stubEnv("T3CODE_OTEL_SDK_DISABLED", "true");
58 vi.stubEnv("OTEL_SDK_DISABLED", "true");
59 vi.stubEnv("VITE_RELAY_OTLP_TRACES_URL", "https://telemetry.example.test/v1/traces");
60 vi.stubEnv("VITE_RELAY_OTLP_TRACES_DATASET", "relay-test");
61 vi.stubEnv("VITE_RELAY_OTLP_TRACES_TOKEN", "test-token");
62 vi.stubGlobal("window", { desktopBridge });
63 const fetchFn = vi.fn<typeof fetch>(async () => new Response(null, { status: 202 }));
64 vi.stubGlobal("fetch", fetchFn);
65 // Re-import after setting runtime policy: static initialization creates the exporter layer.
66 const { runtime: freshRuntime } = await import("./runtime");
67 const { setDelegate } = await import("../observability/clientTracer");
68 const userSpans: Array<string> = [];
69
70 try {
71 expect(
72 await freshRuntime.runPromise(
73 Effect.succeed("relay operation completed").pipe(
74 Effect.withSpan("relay.first-operation"),
75 withRelayClientTracing,
76 ),
77 ),
78 ).toBe("relay operation completed");
79
80 setDelegate(
81 Tracer.make({
82 span(options) {
83 userSpans.push(options.name);
84 return new Tracer.NativeSpan(options);
85 },
86 }),
87 );
88 await freshRuntime.runPromise(Effect.void.pipe(Effect.withSpan("user.work")));
89 expect(userSpans).toEqual(["user.work"]);
90 } finally {
91 setDelegate(null);
92 // Closing the runtime flushes the exporter without waiting for its batch timer.
93 await freshRuntime.dispose();
94 }
95
96 if (exports) {
97 expect(fetchFn).toHaveBeenCalledOnce();
98 expect(String(fetchFn.mock.calls[0]?.[0])).toBe("https://telemetry.example.test/v1/traces");
99 const payload = new TextDecoder().decode(fetchFn.mock.calls[0]?.[1]?.body as Uint8Array);
100 expect(payload).toContain('"name":"relay.first-operation"');
101 } else {
102 expect(fetchFn).not.toHaveBeenCalled();
103 }
104 });
105});