Repository navigation
Expand file tree
/
Copy pathruntime.test.ts
More file actions
79 lines (74 loc) · 2.8 KB
/
Copy pathruntime.test.ts
File metadata and controls
79 lines (74 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { afterEach, describe, expect, it, vi } from "@effect/vitest";
import { withRelayClientTracing } from "@t3tools/shared/relayTracing";
import * as Effect from "effect/Effect";
import * as Tracer from "effect/Tracer";
afterEach(() => {
vi.unstubAllEnvs();
vi.unstubAllGlobals();
});
describe("web runtime relay telemetry policy", () => {
it.each([
{
surface: "desktop opt-out",
desktopBridge: { getRelayTelemetryEnabled: () => false },
exports: false,
},
{
surface: "desktop enabled",
desktopBridge: { getRelayTelemetryEnabled: () => true },
exports: true,
},
{
surface: "ordinary browser ignores host opt-out",
desktopBridge: undefined,
exports: true,
},
])("applies $surface before the first relay span", async ({ desktopBridge, exports }) => {
vi.resetModules();
vi.stubEnv("T3CODE_TELEMETRY_ENABLED", "false");
vi.stubEnv("T3CODE_OTEL_SDK_DISABLED", "true");
vi.stubEnv("OTEL_SDK_DISABLED", "true");
vi.stubEnv("VITE_RELAY_OTLP_TRACES_URL", "https://telemetry.example.test/v1/traces");
vi.stubEnv("VITE_RELAY_OTLP_TRACES_DATASET", "relay-test");
vi.stubEnv("VITE_RELAY_OTLP_TRACES_TOKEN", "test-token");
vi.stubGlobal("window", { desktopBridge });
const fetchFn = vi.fn<typeof fetch>(async () => new Response(null, { status: 202 }));
vi.stubGlobal("fetch", fetchFn);
// Re-import after setting runtime policy: static initialization creates the exporter layer.
const { runtime: freshRuntime } = await import("./runtime");
const { setDelegate } = await import("../observability/clientTracer");
const userSpans: Array<string> = [];
try {
expect(
await freshRuntime.runPromise(
Effect.succeed("relay operation completed").pipe(
Effect.withSpan("relay.first-operation"),
withRelayClientTracing,
),
),
).toBe("relay operation completed");
setDelegate(
Tracer.make({
span(options) {
userSpans.push(options.name);
return new Tracer.NativeSpan(options);
},
}),
);
await freshRuntime.runPromise(Effect.void.pipe(Effect.withSpan("user.work")));
expect(userSpans).toEqual(["user.work"]);
} finally {
setDelegate(null);
// Closing the runtime flushes the exporter without waiting for its batch timer.
await freshRuntime.dispose();
}
if (exports) {
expect(fetchFn).toHaveBeenCalledOnce();
expect(String(fetchFn.mock.calls[0]?.[0])).toBe("https://telemetry.example.test/v1/traces");
const payload = new TextDecoder().decode(fetchFn.mock.calls[0]?.[1]?.body as Uint8Array);
expect(payload).toContain('"name":"relay.first-operation"');
} else {
expect(fetchFn).not.toHaveBeenCalled();
}
});
});