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
6afterEach(() => {
7 vi.unstubAllEnvs();
8 vi.unstubAllGlobals();
9});
10
11const answers = (...values: Array<boolean | null>) => {
12 let call = 0;
13 return () => values[Math.min(call++, values.length - 1)] ?? null;
14};
15
16describe("web runtime relay telemetry policy", () => {
17 it.each([
18 {
19 surface: "desktop opt-out",
20 desktopBridge: { getRelayTelemetryEnabled: () => false },
21 exports: false,
22 },
23 {
24 surface: "desktop enabled",
25 desktopBridge: { getRelayTelemetryEnabled: () => true },
26 exports: true,
27 },
28 {
29 // The desktop host reads the login shell after the renderer has started.
30 surface: "desktop enabled once the login shell is read",
31 desktopBridge: { getRelayTelemetryEnabled: answers(null, true) },
32 exports: true,
33 },
34 {
35 surface: "desktop opt-out found in the login shell",
36 desktopBridge: { getRelayTelemetryEnabled: answers(null, false, true) },
37 exports: false,
38 },
39 {
40 surface: "ordinary browser ignores host opt-out",
41 desktopBridge: undefined,
42 exports: true,
43 },
44 ])("applies $surface before the first relay span", async ({ desktopBridge, exports }) => {
45 vi.resetModules();
46 vi.stubEnv("T3CODE_TELEMETRY_ENABLED", "false");
47 vi.stubEnv("T3CODE_OTEL_SDK_DISABLED", "true");
48 vi.stubEnv("OTEL_SDK_DISABLED", "true");
49 vi.stubEnv("VITE_RELAY_OTLP_TRACES_URL", "https://telemetry.example.test/v1/traces");
50 vi.stubEnv("VITE_RELAY_OTLP_TRACES_DATASET", "relay-test");
51 vi.stubEnv("VITE_RELAY_OTLP_TRACES_TOKEN", "test-token");
52 vi.stubGlobal("window", { desktopBridge });
53 const fetchFn = vi.fn<typeof fetch>(async () => new Response(null, { status: 202 }));
54 vi.stubGlobal("fetch", fetchFn);
55 // Re-import after setting runtime policy: static initialization creates the exporter layer.
56 const { runtime: freshRuntime } = await import("./runtime");
57 const { setDelegate } = await import("../observability/clientTracer");
58 const userSpans: Array<string> = [];
59
60 try {
61 expect(
62 await freshRuntime.runPromise(
63 Effect.succeed("relay operation completed").pipe(
64 Effect.withSpan("relay.first-operation"),
65 withRelayClientTracing,
66 ),
67 ),
68 ).toBe("relay operation completed");
69
70 setDelegate(
71 Tracer.make({
72 span(options) {
73 userSpans.push(options.name);
74 return new Tracer.NativeSpan(options);
75 },
76 }),
77 );
78 await freshRuntime.runPromise(Effect.void.pipe(Effect.withSpan("user.work")));
79 expect(userSpans).toEqual(["user.work"]);
80 } finally {
81 setDelegate(null);
82 // Closing the runtime flushes the exporter without waiting for its batch timer.
83 await freshRuntime.dispose();
84 }
85
86 if (exports) {
87 expect(fetchFn).toHaveBeenCalledOnce();
88 expect(String(fetchFn.mock.calls[0]?.[0])).toBe("https://telemetry.example.test/v1/traces");
89 const payload = new TextDecoder().decode(fetchFn.mock.calls[0]?.[1]?.body as Uint8Array);
90 expect(payload).toContain('"name":"relay.first-operation"');
91 } else {
92 expect(fetchFn).not.toHaveBeenCalled();
93 }
94 });
95});