Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

runtime.test.ts

125 lines4.3 KB

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// Load the runtime's module graph once at collection, outside the per-test timeout;
6// each case re-evaluates it from that cache after vi.resetModules().
7import "./runtime";
8​
9afterEach(() => {
10 vi.unstubAllEnvs();
11 vi.unstubAllGlobals();
12});
13​
14const answers = (...values: Array<boolean | null>) => {
15 let call = 0;
16 return () => values[Math.min(call++, values.length - 1)] ?? null;
17};
18​
19describe("web runtime relay telemetry policy", () => {
20 it.each([
21 {
22 surface: "desktop opt-out",
23 desktopBridge: { getRelayTelemetryEnabled: () => false },
24 exports: false,
25 },
26 {
27 surface: "desktop enabled",
28 desktopBridge: { getRelayTelemetryEnabled: () => true },
29 exports: true,
30 },
31 {
32 // The desktop host reads the login shell after the renderer has started.
33 surface: "desktop enabled once the login shell is read",
34 desktopBridge: { getRelayTelemetryEnabled: answers(null, true) },
35 exports: true,
36 },
37 {
38 surface: "desktop opt-out found in the login shell",
39 desktopBridge: { getRelayTelemetryEnabled: answers(null, false, true) },
40 exports: false,
41 },
42 {
43 surface: "browser before its server has answered",
44 desktopBridge: undefined,
45 exports: false,
46 },
47 {
48 surface: "browser whose server opted out",
49 desktopBridge: undefined,
50 server: false,
51 exports: false,
52 },
53 {
54 surface: "browser whose server allows it",
55 desktopBridge: undefined,
56 server: true,
57 exports: true,
58 },
59 {
60 // The hosted app is served by no T3 server, so nothing can answer for it.
61 surface: "hosted app",
62 desktopBridge: undefined,
63 location: new URL("https://app.example.test/"),
64 exports: true,
65 },
66 ])("applies $surface before the first relay span", async (surface) => {
67 const { desktopBridge, exports } = surface;
68 vi.resetModules();
69 vi.stubEnv("T3CODE_TELEMETRY_ENABLED", "false");
70 vi.stubEnv("T3CODE_OTEL_SDK_DISABLED", "true");
71 vi.stubEnv("OTEL_SDK_DISABLED", "true");
72 vi.stubEnv("VITE_RELAY_OTLP_TRACES_URL", "https://telemetry.example.test/v1/traces");
73 vi.stubEnv("VITE_RELAY_OTLP_TRACES_DATASET", "relay-test");
74 vi.stubEnv("VITE_RELAY_OTLP_TRACES_TOKEN", "test-token");
75 vi.stubEnv("VITE_HOSTED_APP_URL", "https://app.example.test");
76 vi.stubGlobal("window", {
77 desktopBridge,
78 location: "location" in surface ? surface.location : undefined,
79 });
80 const fetchFn = vi.fn<typeof fetch>(async () => new Response(null, { status: 202 }));
81 vi.stubGlobal("fetch", fetchFn);
82 // Re-import after setting runtime policy: static initialization creates the exporter layer.
83 const { runtime: freshRuntime } = await import("./runtime");
84 const { setDelegate } = await import("../observability/clientTracer");
85 if ("server" in surface) {
86 (await import("../cloud/publicConfig")).setServerRelayTelemetry(surface.server);
87 }
88 const userSpans: Array<string> = [];
89​
90 try {
91 expect(
92 await freshRuntime.runPromise(
93 Effect.succeed("relay operation completed").pipe(
94 Effect.withSpan("relay.first-operation"),
95 withRelayClientTracing,
96 ),
97 ),
98 ).toBe("relay operation completed");
99​
100 setDelegate(
101 Tracer.make({
102 span(options) {
103 userSpans.push(options.name);
104 return new Tracer.NativeSpan(options);
105 },
106 }),
107 );
108 await freshRuntime.runPromise(Effect.void.pipe(Effect.withSpan("user.work")));
109 expect(userSpans).toEqual(["user.work"]);
110 } finally {
111 setDelegate(null);
112 // Closing the runtime flushes the exporter without waiting for its batch timer.
113 await freshRuntime.dispose();
114 }
115​
116 if (exports) {
117 expect(fetchFn).toHaveBeenCalledOnce();
118 expect(String(fetchFn.mock.calls[0]?.[0])).toBe("https://telemetry.example.test/v1/traces");
119 const payload = new TextDecoder().decode(fetchFn.mock.calls[0]?.[1]?.body as Uint8Array);
120 expect(payload).toContain('"name":"relay.first-operation"');
121 } else {
122 expect(fetchFn).not.toHaveBeenCalled();
123 }
124 });
125});