Download frontend/src/components/admin/runtime-settings-form.test.tsx from new-arc/vector: direct link, hf CLI and curl.
- Browser
- Download file 8.41 kB
-
https://huggingface.co/spaces/new-arc/vector/resolve/main/frontend/src/components/admin/runtime-settings-form.test.tsx
- Command line
-
hf download hf://spaces/new-arc/vector/frontend/src/components/admin/runtime-settings-form.test.tsx
-
curl -L -o runtime-settings-form.test.tsx https://huggingface.co/spaces/new-arc/vector/resolve/main/frontend/src/components/admin/runtime-settings-form.test.tsx
8.41 kB
| import { fireEvent, render, screen, waitFor } from "@testing-library/react"; | |
| import userEvent from "@testing-library/user-event"; | |
| import { RuntimeSettingsForm, type RuntimeSetting } from "./runtime-settings-form"; | |
| const refresh = vi.fn(); | |
| vi.mock("next/navigation", () => ({ useRouter: () => ({ refresh }) })); | |
| const minimax = { | |
| label: "NVIDIA - MiniMax M3.1 Flash", | |
| value: "nvidia:minimaxai/minimax-m3.1-flash", | |
| }; | |
| function modelSetting(overrides: Partial<RuntimeSetting> = {}): RuntimeSetting { | |
| return { | |
| key: "LLM", | |
| label: "Language model", | |
| description: "Model used for clip selection.", | |
| input_type: "text", | |
| options: [minimax, { label: "OpenAI - GPT-4o", value: "openai:gpt-4o" }], | |
| source: "admin", | |
| configured: true, | |
| has_admin_value: true, | |
| has_env_value: false, | |
| prefer_admin_value: false, | |
| overridden_by_env: false, | |
| ...overrides, | |
| }; | |
| } | |
| const nvidiaKey = modelSetting({ | |
| key: "NVIDIA_API_KEY", | |
| label: "NVIDIA API key", | |
| description: "API key for NVIDIA models.", | |
| input_type: "password", | |
| options: [], | |
| }); | |
| describe("RuntimeSettingsForm model presets", () => { | |
| beforeEach(() => { | |
| refresh.mockReset(); | |
| vi.stubGlobal("fetch", vi.fn().mockResolvedValue(Response.json({ settings: [] }))); | |
| }); | |
| afterEach(() => vi.unstubAllGlobals()); | |
| it("leaves saved values hidden and only renders presets for nonempty options", () => { | |
| render(<RuntimeSettingsForm settings={[ | |
| modelSetting(), | |
| modelSetting({ key: "RERANK_LLM", label: "Reranking model" }), | |
| nvidiaKey, | |
| modelSetting({ key: "OLLAMA_BASE_URL", label: "Ollama URL", options: undefined }), | |
| ]} />); | |
| expect(screen.getAllByRole("combobox")).toHaveLength(2); | |
| expect(screen.getByRole("combobox", { name: "Choose model preset LLM" })).toHaveValue(""); | |
| expect(screen.getByRole("combobox", { name: "Choose model preset RERANK_LLM" })).toHaveValue(""); | |
| expect(screen.getByRole("textbox", { name: /Language model/ })).toHaveValue(""); | |
| expect(screen.getByRole("textbox", { name: /Reranking model/ })).toHaveValue(""); | |
| expect(screen.getByRole("textbox", { name: "Ollama URL" })).toHaveValue(""); | |
| expect(screen.getByLabelText("NVIDIA API key")).toHaveAttribute("type", "password"); | |
| expect(screen.getByLabelText("NVIDIA API key")).toHaveValue(""); | |
| expect(screen.getAllByPlaceholderText("Configured value is hidden")).toHaveLength(4); | |
| expect(screen.getByRole("button", { name: "Save settings" })).toBeDisabled(); | |
| expect(fetch).not.toHaveBeenCalled(); | |
| }); | |
| it.each(["LLM", "RERANK_LLM"])("populates %s and saves the actual preset and API key in one PATCH", async (key) => { | |
| const user = userEvent.setup(); | |
| const { container } = render(<RuntimeSettingsForm settings={[modelSetting({ key }), nvidiaKey]} />); | |
| const picker = screen.getByRole("combobox", { name: `Choose model preset ${key}` }); | |
| const input = screen.getByRole("textbox", { name: /Language model/ }); | |
| const secretInput = screen.getByLabelText("NVIDIA API key"); | |
| const testKey = "test-only-nvidia-key"; | |
| await user.selectOptions(picker, minimax.label); | |
| expect(input).toHaveValue(minimax.value); | |
| expect(picker).toHaveValue(minimax.value); | |
| expect(fetch).not.toHaveBeenCalled(); | |
| await user.type(secretInput, testKey); | |
| expect(secretInput).toHaveAttribute("type", "password"); | |
| expect(container.textContent).not.toContain(testKey); | |
| expect(fetch).not.toHaveBeenCalled(); | |
| await user.click(screen.getByRole("button", { name: "Save settings" })); | |
| await waitFor(() => expect(refresh).toHaveBeenCalledOnce()); | |
| expect(fetch).toHaveBeenCalledExactlyOnceWith("/api/admin/runtime-settings", { | |
| method: "PATCH", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ | |
| updates: { [key]: minimax.value, NVIDIA_API_KEY: testKey }, | |
| delete_keys: [], | |
| prefer_admin_values: {}, | |
| }), | |
| }); | |
| expect(input).toHaveValue(""); | |
| expect(picker).toHaveValue(""); | |
| expect(secretInput).toHaveValue(""); | |
| expect(container.innerHTML).not.toContain(testKey); | |
| expect(screen.getByText("Settings saved.")).toBeInTheDocument(); | |
| expect(screen.getByRole("button", { name: "Save settings" })).toBeDisabled(); | |
| }); | |
| it("keeps custom model input editable before and after a preset selection", async () => { | |
| const user = userEvent.setup(); | |
| render(<RuntimeSettingsForm settings={[modelSetting()]} />); | |
| const input = screen.getByRole("textbox", { name: /Language model/ }); | |
| const picker = screen.getByRole("combobox", { name: "Choose model preset LLM" }); | |
| await user.type(input, "ollama:custom-model"); | |
| expect(input).toHaveValue("ollama:custom-model"); | |
| expect(picker).toHaveValue(""); | |
| await user.selectOptions(picker, minimax.label); | |
| expect(input).toHaveValue(minimax.value); | |
| await user.clear(input); | |
| await user.type(input, " nvidia:custom/model "); | |
| expect(input).toHaveValue(" nvidia:custom/model "); | |
| expect(picker).toHaveValue(""); | |
| expect(fetch).not.toHaveBeenCalled(); | |
| await user.click(screen.getByRole("button", { name: "Save settings" })); | |
| expect(fetch).toHaveBeenCalledWith("/api/admin/runtime-settings", expect.objectContaining({ | |
| body: JSON.stringify({ updates: { LLM: "nvidia:custom/model" }, delete_keys: [], prefer_admin_values: {} }), | |
| })); | |
| await waitFor(() => expect(refresh).toHaveBeenCalledOnce()); | |
| }); | |
| it("preserves priority overrides and deletion choices when selecting a preset", async () => { | |
| render(<RuntimeSettingsForm settings={[modelSetting({ prefer_admin_value: true })]} />); | |
| const preferSaved = screen.getByRole("checkbox", { name: "Prefer saved" }); | |
| const clearSaved = screen.getByRole("checkbox", { name: "Clear saved" }); | |
| fireEvent.click(preferSaved); | |
| fireEvent.click(clearSaved); | |
| fireEvent.change(screen.getByRole("combobox"), { target: { value: minimax.value } }); | |
| expect(preferSaved).not.toBeChecked(); | |
| expect(clearSaved).toBeChecked(); | |
| expect(fetch).not.toHaveBeenCalled(); | |
| fireEvent.click(screen.getByRole("button", { name: "Save settings" })); | |
| expect(fetch).toHaveBeenCalledWith("/api/admin/runtime-settings", expect.objectContaining({ | |
| body: JSON.stringify({ | |
| updates: { LLM: minimax.value }, | |
| delete_keys: ["LLM"], | |
| prefer_admin_values: { LLM: false }, | |
| }), | |
| })); | |
| await waitFor(() => expect(refresh).toHaveBeenCalledOnce()); | |
| }); | |
| it("enables Prefer saved for a new preset without enabling Clear saved", async () => { | |
| render(<RuntimeSettingsForm settings={[modelSetting({ | |
| source: "environment", | |
| has_admin_value: false, | |
| has_env_value: true, | |
| })]} />); | |
| const preferSaved = screen.getByRole("checkbox", { name: "Prefer saved" }); | |
| const clearSaved = screen.getByRole("checkbox", { name: "Clear saved" }); | |
| expect(preferSaved).toBeDisabled(); | |
| expect(clearSaved).toBeDisabled(); | |
| fireEvent.change(screen.getByRole("combobox"), { target: { value: minimax.value } }); | |
| expect(preferSaved).toBeEnabled(); | |
| expect(clearSaved).toBeDisabled(); | |
| fireEvent.click(preferSaved); | |
| fireEvent.click(screen.getByRole("button", { name: "Save settings" })); | |
| expect(fetch).toHaveBeenCalledWith("/api/admin/runtime-settings", expect.objectContaining({ | |
| body: JSON.stringify({ | |
| updates: { LLM: minimax.value }, | |
| delete_keys: [], | |
| prefer_admin_values: { LLM: true }, | |
| }), | |
| })); | |
| await waitFor(() => expect(refresh).toHaveBeenCalledOnce()); | |
| }); | |
| it("retains the selected draft and reports a failed save without refreshing", async () => { | |
| vi.mocked(fetch).mockResolvedValue(Response.json({ detail: "Unable to save settings" }, { status: 500 })); | |
| render(<RuntimeSettingsForm settings={[modelSetting()]} />); | |
| fireEvent.change(screen.getByRole("combobox"), { target: { value: minimax.value } }); | |
| fireEvent.click(screen.getByRole("button", { name: "Save settings" })); | |
| expect(await screen.findByText("Unable to save settings")).toBeInTheDocument(); | |
| expect(screen.getByRole("textbox", { name: /Language model/ })).toHaveValue(minimax.value); | |
| expect(screen.getByRole("combobox")).toHaveValue(minimax.value); | |
| expect(screen.getByRole("button", { name: "Save settings" })).toBeEnabled(); | |
| expect(refresh).not.toHaveBeenCalled(); | |
| }); | |
| }); | |