|
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | +import userEvent from "@testing-library/user-event"; |
| 3 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 4 | +import { ThemeProvider } from "@mui/material/styles"; |
| 5 | +import mockTheme from "../../../__mocks__/themeMock"; |
| 6 | +import ServerSelectSetting from "../ServerSelectSetting"; |
| 7 | +import useRemoteSettingsStore from "../../../stores/RemoteSettingStore"; |
| 8 | + |
| 9 | +jest.mock("../../../stores/RemoteSettingStore"); |
| 10 | + |
| 11 | +const fetchSettings = jest.fn().mockResolvedValue([]); |
| 12 | +const updateSettings = jest.fn().mockResolvedValue(undefined); |
| 13 | + |
| 14 | +const mockStore = useRemoteSettingsStore as unknown as jest.Mock; |
| 15 | + |
| 16 | +const OPTIONS = [ |
| 17 | + { value: "tools", label: "Tools (JSON tool calls)" }, |
| 18 | + { value: "codeact", label: "CodeAct (JavaScript actions)" } |
| 19 | +] as const; |
| 20 | + |
| 21 | +const setStoredValue = (value: string | undefined) => { |
| 22 | + const state = { |
| 23 | + settings: |
| 24 | + value === undefined |
| 25 | + ? [] |
| 26 | + : [{ env_var: "NODETOOL_AGENT_EXECUTION_MODE", value }], |
| 27 | + fetchSettings, |
| 28 | + updateSettings |
| 29 | + }; |
| 30 | + mockStore.mockImplementation((selector: (s: typeof state) => unknown) => |
| 31 | + selector(state) |
| 32 | + ); |
| 33 | +}; |
| 34 | + |
| 35 | +const renderSetting = () => |
| 36 | + render( |
| 37 | + <QueryClientProvider client={new QueryClient()}> |
| 38 | + <ThemeProvider theme={mockTheme}> |
| 39 | + <ServerSelectSetting |
| 40 | + envVar="NODETOOL_AGENT_EXECUTION_MODE" |
| 41 | + label="Agent Execution Mode" |
| 42 | + defaultValue="tools" |
| 43 | + options={OPTIONS} |
| 44 | + description="How agent steps act on their toolbelt." |
| 45 | + /> |
| 46 | + </ThemeProvider> |
| 47 | + </QueryClientProvider> |
| 48 | + ); |
| 49 | + |
| 50 | +describe("ServerSelectSetting", () => { |
| 51 | + beforeEach(() => { |
| 52 | + jest.clearAllMocks(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("falls back to the default when the setting is unset", () => { |
| 56 | + setStoredValue(undefined); |
| 57 | + renderSetting(); |
| 58 | + expect(screen.getByRole("combobox")).toHaveTextContent( |
| 59 | + "Tools (JSON tool calls)" |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it("shows the stored value", () => { |
| 64 | + setStoredValue("codeact"); |
| 65 | + renderSetting(); |
| 66 | + expect(screen.getByRole("combobox")).toHaveTextContent( |
| 67 | + "CodeAct (JavaScript actions)" |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it("writes the picked value back to the server setting", async () => { |
| 72 | + setStoredValue("tools"); |
| 73 | + renderSetting(); |
| 74 | + |
| 75 | + await userEvent.click(screen.getByRole("combobox")); |
| 76 | + await userEvent.click( |
| 77 | + screen.getByRole("option", { name: "CodeAct (JavaScript actions)" }) |
| 78 | + ); |
| 79 | + |
| 80 | + expect(updateSettings).toHaveBeenCalledWith({ |
| 81 | + NODETOOL_AGENT_EXECUTION_MODE: "codeact" |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it("does not write when the value is unchanged", async () => { |
| 86 | + setStoredValue("codeact"); |
| 87 | + renderSetting(); |
| 88 | + |
| 89 | + await userEvent.click(screen.getByRole("combobox")); |
| 90 | + await userEvent.click( |
| 91 | + screen.getByRole("option", { name: "CodeAct (JavaScript actions)" }) |
| 92 | + ); |
| 93 | + |
| 94 | + expect(updateSettings).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | +}); |
0 commit comments