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
| 1 | import { render } from "../../../test/render"; |
| 2 | import { DEFAULT_SERVER_SETTINGS, type ServerObservability } from "@t3tools/contracts"; |
| 3 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 4 | import { userEvent } from "vite-plus/test/browser"; |
| 5 | |
| 6 | const { 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 | |
| 22 | vi.mock(import("@effect/atom-react"), async (original) => ({ |
| 23 | ...(await original()), |
| 24 | useAtomValue: (() => true) as never, |
| 25 | })); |
| 26 | vi.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 | })); |
| 35 | vi.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 | }); |
| 46 | vi.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 | })); |
| 57 | vi.mock(import("./useScopedSettings"), async (original) => ({ |
| 58 | ...(await original()), |
| 59 | useScopedSettings: ((select: (settings: typeof state) => unknown) => select(state)) as never, |
| 60 | })); |
| 61 | |
| 62 | import { TelemetryExportSettings } from "./TelemetryExportSettings"; |
| 63 | |
| 64 | function setup() { |
| 65 | updateSettings.mockClear(); |
| 66 | checkOtlpEndpoint.mockReset(); |
| 67 | state.serverConfig = { environment: { capabilities: {} } }; |
| 68 | } |
| 69 | |
| 70 | describe("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 | |
| 143 | it("can test a corrected URL and reports only the latest attempt when a URL is reused", async () => { |
| 144 | setup(); |
| 145 | state.observability = { ...DEFAULT_SERVER_SETTINGS.observability, otlpTracesUrl: "" }; |
| 146 | state.serverConfig = { environment: { capabilities: { otlpEndpointCheck: true } } }; |
| 147 | const first = Promise.withResolvers<Awaited<ReturnType<typeof checkOtlpEndpoint>>>(); |
| 148 | const corrected = Promise.withResolvers<Awaited<ReturnType<typeof checkOtlpEndpoint>>>(); |
| 149 | const latest = Promise.withResolvers<Awaited<ReturnType<typeof checkOtlpEndpoint>>>(); |
| 150 | checkOtlpEndpoint |
| 151 | .mockReturnValueOnce(first.promise) |
| 152 | .mockReturnValueOnce(corrected.promise) |
| 153 | .mockReturnValueOnce(latest.promise); |
| 154 | await using screen = await render(<TelemetryExportSettings />); |
| 155 | |
| 156 | const traces = screen.getByLabelText("Traces endpoint"); |
| 157 | const testTraces = screen.getByRole("button", { name: "Test" }).first(); |
| 158 | await userEvent.fill(traces, "http://first/v1/traces"); |
| 159 | await userEvent.click(testTraces); |
| 160 | await expect.element(testTraces).toBeDisabled(); |
| 161 | |
| 162 | await userEvent.fill(traces, "http://corrected/v1/traces"); |
| 163 | await expect.element(testTraces).toBeEnabled(); |
| 164 | await userEvent.click(testTraces); |
| 165 | await expect.element(testTraces).toBeDisabled(); |
| 166 | |
| 167 | await userEvent.fill(traces, "http://first/v1/traces"); |
| 168 | await userEvent.click(testTraces); |
| 169 | await expect.element(testTraces).toBeDisabled(); |
| 170 | |
| 171 | corrected.resolve({ _tag: "Success", value: { _tag: "Accepted", latencyMs: 22 } }); |
| 172 | first.resolve({ _tag: "Success", value: { _tag: "Accepted", latencyMs: 11 } }); |
| 173 | latest.resolve({ _tag: "Success", value: { _tag: "Accepted", latencyMs: 33 } }); |
| 174 | |
| 175 | await expect.element(screen.getByText("Connected · 33 ms")).toBeInTheDocument(); |
| 176 | }); |
| 177 | }); |