@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 04:45 UTC

main
ts 98 lines 3.52 KB
Raw
1 import { afterEach, describe, expect, it, vi } from "@effect/vitest";
2 import { withRelayClientTracing } from "@t3tools/shared/relayTracing";
3 import * as Effect from "effect/Effect";
4 import * 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().
7 import "./runtime";
8
9 afterEach(() => {
10 vi.unstubAllEnvs();
11 vi.unstubAllGlobals();
12 });
13
14 const answers = (...values: Array<boolean | null>) => {
15 let call = 0;
16 return () => values[Math.min(call++, values.length - 1)] ?? null;
17 };
18
19 describe("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: "ordinary browser ignores host opt-out",
44 desktopBridge: undefined,
45 exports: true,
46 },
47 ])("applies $surface before the first relay span", async ({ desktopBridge, exports }) => {
48 vi.resetModules();
49 vi.stubEnv("T3CODE_TELEMETRY_ENABLED", "false");
50 vi.stubEnv("T3CODE_OTEL_SDK_DISABLED", "true");
51 vi.stubEnv("OTEL_SDK_DISABLED", "true");
52 vi.stubEnv("VITE_RELAY_OTLP_TRACES_URL", "https://telemetry.example.test/v1/traces");
53 vi.stubEnv("VITE_RELAY_OTLP_TRACES_DATASET", "relay-test");
54 vi.stubEnv("VITE_RELAY_OTLP_TRACES_TOKEN", "test-token");
55 vi.stubGlobal("window", { desktopBridge });
56 const fetchFn = vi.fn<typeof fetch>(async () => new Response(null, { status: 202 }));
57 vi.stubGlobal("fetch", fetchFn);
58 // Re-import after setting runtime policy: static initialization creates the exporter layer.
59 const { runtime: freshRuntime } = await import("./runtime");
60 const { setDelegate } = await import("../observability/clientTracer");
61 const userSpans: Array<string> = [];
62
63 try {
64 expect(
65 await freshRuntime.runPromise(
66 Effect.succeed("relay operation completed").pipe(
67 Effect.withSpan("relay.first-operation"),
68 withRelayClientTracing,
69 ),
70 ),
71 ).toBe("relay operation completed");
72
73 setDelegate(
74 Tracer.make({
75 span(options) {
76 userSpans.push(options.name);
77 return new Tracer.NativeSpan(options);
78 },
79 }),
80 );
81 await freshRuntime.runPromise(Effect.void.pipe(Effect.withSpan("user.work")));
82 expect(userSpans).toEqual(["user.work"]);
83 } finally {
84 setDelegate(null);
85 // Closing the runtime flushes the exporter without waiting for its batch timer.
86 await freshRuntime.dispose();
87 }
88
89 if (exports) {
90 expect(fetchFn).toHaveBeenCalledOnce();
91 expect(String(fetchFn.mock.calls[0]?.[0])).toBe("https://telemetry.example.test/v1/traces");
92 const payload = new TextDecoder().decode(fetchFn.mock.calls[0]?.[1]?.body as Uint8Array);
93 expect(payload).toContain('"name":"relay.first-operation"');
94 } else {
95 expect(fetchFn).not.toHaveBeenCalled();
96 }
97 });
98 });