Open History to see the last commit
1import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
2import { act } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
5import { userEvent } from "vite-plus/test/browser";
6
7const { state, updateSettings } = vi.hoisted(() => ({
8 state: { observability: {} as typeof DEFAULT_SERVER_SETTINGS.observability },
9 updateSettings: vi.fn(async () => ({ _tag: "Success" as const, value: undefined })),
10}));
11
12vi.mock(import("@effect/atom-react"), async (original) => ({
13 ...(await original()),
14 useAtomValue: (() => true) as never,
15}));
16vi.mock(import("../../state/use-atom-command"), async (original) => ({
17 ...(await original()),
18 useAtomCommand: ((command: unknown) =>
19 command === "updateSettings" ? updateSettings : vi.fn()) as never,
20}));
21vi.mock(import("../../state/server"), async (original) => {
22 const actual = await original();
23 return {
24 ...actual,
25 serverEnvironment: {
26 ...actual.serverEnvironment,
27 updateSettings: "updateSettings",
28 } as unknown as typeof actual.serverEnvironment,
29 };
30});
31vi.mock(import("./SettingsScopeContext"), async (original) => ({
32 ...(await original()),
33 useSettingsScope: (() => ({
34 scope: { kind: "environment" },
35 environment: {
36 environmentId: "env",
37 label: "MAC1",
38 serverConfig: { environment: { capabilities: {} } },
39 },
40 })) as never,
41}));
42vi.mock(import("./useScopedSettings"), async (original) => ({
43 ...(await original()),
44 useScopedSettings: ((select: (settings: typeof state) => unknown) => select(state)) as never,
45}));
46
47import { TelemetryExportSettings } from "./TelemetryExportSettings";
48
49let root: Root;
50let container: HTMLDivElement;
51beforeEach(() => {
52 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
53 container = document.createElement("div");
54 document.body.append(container);
55 root = createRoot(container);
56});
57
58afterEach(async () => {
59 await act(async () => root.unmount());
60 container.remove();
61 updateSettings.mockClear();
62 vi.unstubAllGlobals();
63});
64
65const field = (id: string) => container.querySelector<HTMLInputElement>(`#${id}`)!;
66async function replace(id: string, value: string) {
67 await userEvent.clear(field(id));
68 await userEvent.type(field(id), value);
69}
70
71describe("TelemetryExportSettings", () => {
72 it("does not resend a field the user restored after another client changed it", async () => {
73 state.observability = {
74 ...DEFAULT_SERVER_SETTINGS.observability,
75 otlpTracesUrl: "http://old/v1/traces",
76 };
77 await act(async () => root.render(<TelemetryExportSettings />));
78
79 await replace("otlpTracesUrl", "http://draft/v1/traces");
80 await replace("otlpTracesUrl", "http://old/v1/traces");
81 state.observability = { ...state.observability, otlpTracesUrl: "http://other/v1/traces" };
82 await act(async () => root.render(<TelemetryExportSettings />));
83 await replace("otlpMetricsUrl", "http://new/v1/metrics");
84 const save = [...container.querySelectorAll("button")].find(
85 (button) => button.textContent === "Save",
86 )!;
87 await userEvent.click(save);
88
89 expect(updateSettings).toHaveBeenCalledExactlyOnceWith({
90 environmentId: "env",
91 input: { patch: { observability: { otlpMetricsUrl: "http://new/v1/metrics" } } },
92 });
93 expect(container.textContent).toContain("Saved. Restart the server to apply.");
94 });
95});