File size: 8,406 Bytes
689359a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | 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();
});
});
|