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

TelemetryExportSettings.browser.test.tsx

142 lines5.2 KB

1import { render } from "../../../test/render";
2import { DEFAULT_SERVER_SETTINGS, type ServerObservability } from "@t3tools/contracts";
3import { describe, expect, it, vi } from "vite-plus/test";
4import { userEvent } from "vite-plus/test/browser";
5​
6const { state, updateSettings, checkOtlpEndpoint, checkOtlpEndpointCommand } = vi.hoisted(() => ({
7 state: {
8 observability: {} as typeof DEFAULT_SERVER_SETTINGS.observability,
9 serverConfig: { environment: { capabilities: {} } } as {
10 environment: { capabilities: { otlpEndpointCheck?: boolean } };
11 observability?: ServerObservability;
12 },
13 },
14 updateSettings: vi.fn(async () => ({ _tag: "Success" as const, value: undefined })),
15 checkOtlpEndpoint: vi.fn(async (_request: unknown) => ({
16 _tag: "Success" as const,
17 value: { _tag: "Accepted" as const, latencyMs: 12 },
18 })),
19 checkOtlpEndpointCommand: { permissionAtom: () => null },
20}));
21​
22vi.mock(import("@effect/atom-react"), async (original) => ({
23 ...(await original()),
24 useAtomValue: (() => true) as never,
25}));
26vi.mock(import("../../state/use-atom-command"), async (original) => ({
27 ...(await original()),
28 useAtomCommand: ((command: unknown) =>
29 command === "updateSettings"
30 ? updateSettings
31 : command === checkOtlpEndpointCommand
32 ? checkOtlpEndpoint
33 : vi.fn()) as never,
34}));
35vi.mock(import("../../state/server"), async (original) => {
36 const actual = await original();
37 return {
38 ...actual,
39 serverEnvironment: {
40 ...actual.serverEnvironment,
41 updateSettings: "updateSettings",
42 checkOtlpEndpoint: checkOtlpEndpointCommand,
43 } as unknown as typeof actual.serverEnvironment,
44 };
45});
46vi.mock(import("./SettingsScopeContext"), async (original) => ({
47 ...(await original()),
48 useSettingsScope: (() => ({
49 scope: { kind: "environment" },
50 environment: {
51 environmentId: "env",
52 label: "MAC1",
53 serverConfig: state.serverConfig,
54 },
55 })) as never,
56}));
57vi.mock(import("./useScopedSettings"), async (original) => ({
58 ...(await original()),
59 useScopedSettings: ((select: (settings: typeof state) => unknown) => select(state)) as never,
60}));
61​
62import { TelemetryExportSettings } from "./TelemetryExportSettings";
63​
64function setup() {
65 updateSettings.mockClear();
66 checkOtlpEndpoint.mockClear();
67 state.serverConfig = { environment: { capabilities: {} } };
69​
70describe("TelemetryExportSettings", () => {
71 it("does not resend a field the user restored after another client changed it", async () => {
72 setup();
73 state.observability = {
74 ...DEFAULT_SERVER_SETTINGS.observability,
75 otlpTracesUrl: "http://old/v1/traces",
76 };
77 await using screen = await render(<TelemetryExportSettings />);
78​
79 const { container } = screen;
80 const field = (id: string) => container.querySelector<HTMLInputElement>(`#${id}`)!;
81 async function replace(id: string, value: string) {
82 await userEvent.clear(field(id));
83 await userEvent.type(field(id), value);
84 }
85 await replace("otlpTracesUrl", "http://draft/v1/traces");
86 await replace("otlpTracesUrl", "http://old/v1/traces");
87 state.observability = { ...state.observability, otlpTracesUrl: "http://other/v1/traces" };
88 await screen.rerender(<TelemetryExportSettings />);
89 await replace("otlpMetricsUrl", "http://new/v1/metrics");
90 const save = [...container.querySelectorAll("button")].find(
91 (button) => button.textContent === "Save",
92 )!;
93 await userEvent.click(save);
94​
95 expect(updateSettings).toHaveBeenCalledExactlyOnceWith({
96 environmentId: "env",
97 input: { patch: { observability: { otlpMetricsUrl: "http://new/v1/metrics" } } },
98 });
99 expect(container.textContent).toContain("Saved. Restart the server to apply.");
100 });
101​
102 it("reports and tests the endpoint the environment set over an empty setting", async () => {
103 setup();
104 state.observability = { ...DEFAULT_SERVER_SETTINGS.observability, otlpTracesUrl: "" };
105 state.serverConfig = {
106 environment: { capabilities: { otlpEndpointCheck: true } },
107 observability: {
108 logsDirectoryPath: "/tmp/logs",
109 localTracingEnabled: true,
110 otlpTracesUrl: "http://collector/v1/traces",
111 otlpTracesSource: "environment",
112 otlpTracesVariable: "OTEL_EXPORTER_OTLP_ENDPOINT",
113 otlpTracesEnabled: true,
114 otlpMetricsEnabled: false,
115 otlpLogsEnabled: false,
116 },
117 };
118 await using screen = await render(<TelemetryExportSettings />);
119​
120 await expect
121 .element(screen.getByText("Set by environment · Connected · 12 ms"))
122 .toBeInTheDocument();
123 await expect
124 .element(
125 screen.getByText("Running: http://collector/v1/traces from OTEL_EXPORTER_OTLP_ENDPOINT"),
126 )
127 .toBeInTheDocument();
128 expect(checkOtlpEndpoint).toHaveBeenCalledExactlyOnceWith({
129 environmentId: "env",
130 input: { signal: "traces", url: "http://collector/v1/traces" },
131 });
132 expect(screen.container.querySelector<HTMLInputElement>("#otlpTracesUrl")!.value).toBe("");
133​
134 await userEvent.click(screen.getByRole("button", { name: "Test" }).first());
135​
136 expect(checkOtlpEndpoint).toHaveBeenCalledTimes(2);
137 expect(checkOtlpEndpoint).toHaveBeenLastCalledWith({
138 environmentId: "env",
139 input: { signal: "traces", url: "http://collector/v1/traces" },
140 });
141 });
142});