* fix(webui): allow custom context window token budgets * fix(i18n): translate context window hints in all locales * fix(webui): accept context window K and M shorthand * fix(webui): shorten context window hints * refactor(webui): simplify context window validation
1627 lines
58 KiB
TypeScript
1627 lines
58 KiB
TypeScript
import { fireEvent, screen, waitFor, within } from "@testing-library/react";
|
||
import { expect, it, vi } from "vitest";
|
||
import type { SettingsPayload } from "@/lib/types";
|
||
import { requestMutationMock, jsonResponse, settingsPayload, renderSettingsView, openPopover, installSettingsViewTestHooks } from "@/tests/settings-test-utils";
|
||
|
||
|
||
function settingsPayloadWithBackup(): {
|
||
payload: SettingsPayload;
|
||
backupPreset: SettingsPayload["model_presets"][number];
|
||
} {
|
||
const base = settingsPayload();
|
||
const backupPreset = {
|
||
...base.model_presets[0],
|
||
name: "backup",
|
||
label: "Backup",
|
||
active: false,
|
||
model: "anthropic/claude-sonnet-4",
|
||
provider: "anthropic",
|
||
resolved_provider: "anthropic",
|
||
};
|
||
return {
|
||
backupPreset,
|
||
payload: {
|
||
...base,
|
||
model_presets: [base.model_presets[0], backupPreset],
|
||
model_call_order: ["primary", "backup"],
|
||
providers: [
|
||
{
|
||
name: "openai",
|
||
label: "OpenAI",
|
||
configured: true,
|
||
},
|
||
{
|
||
name: "anthropic",
|
||
label: "Anthropic",
|
||
configured: true,
|
||
},
|
||
],
|
||
},
|
||
};
|
||
}
|
||
|
||
function autoDynamicProviderPayload(
|
||
options: {
|
||
configured: boolean;
|
||
hasApiKey: boolean;
|
||
apiBase: string | null;
|
||
apiKeyHint: string | null;
|
||
},
|
||
): SettingsPayload {
|
||
const base = settingsPayload();
|
||
return {
|
||
...base,
|
||
agent: {
|
||
...base.agent,
|
||
model: "companyProxy/gpt-4o",
|
||
provider: "companyProxy",
|
||
resolved_provider: "companyProxy",
|
||
has_api_key: options.hasApiKey,
|
||
},
|
||
model_presets: [
|
||
{
|
||
...base.model_presets[0],
|
||
model: "companyProxy/gpt-4o",
|
||
provider: "auto",
|
||
resolved_provider: "companyProxy",
|
||
},
|
||
],
|
||
providers: [
|
||
{
|
||
name: "companyProxy",
|
||
label: "Company Proxy",
|
||
configured: options.configured,
|
||
auth_type: "api_key",
|
||
api_key_required: false,
|
||
api_key_hint: options.apiKeyHint,
|
||
api_base: options.apiBase,
|
||
default_api_base: null,
|
||
},
|
||
],
|
||
};
|
||
}
|
||
|
||
async function togglePresetEditor(name = "primary") {
|
||
const row = await screen.findByTestId(`model-call-order-row-${name}`);
|
||
fireEvent.click(within(row).getAllByRole("button")[0]);
|
||
}
|
||
|
||
describe("Settings models", () => {
|
||
installSettingsViewTestHooks();
|
||
|
||
it.each(["manual", "poll", "direct"])("reauthenticates from the catalog and preserves the preset draft (%s)", async (mode) => {
|
||
const payload = settingsPayload();
|
||
payload.providers = [{
|
||
name: "openai_codex", label: "OpenAI Codex", configured: true,
|
||
auth_type: "oauth", model_catalog: "hybrid", oauth_login_supported: true,
|
||
}];
|
||
let recovered = false;
|
||
const signedIn = () => {
|
||
recovered = true;
|
||
return { ...payload, providers: payload.providers.map((row) => ({ ...row })) };
|
||
};
|
||
requestMutationMock.mockImplementation(async (action, args) => {
|
||
if (action === "settings.provider.oauth_login") {
|
||
return mode === "direct" ? signedIn() : {
|
||
status: "authorization_required", provider: "openai_codex", flow_id: "flow-test",
|
||
authorization_url: "https://example.com/authorize", expires_in: 600,
|
||
completion_input: "callback_url",
|
||
};
|
||
}
|
||
if (action === "settings.provider.oauth_complete") {
|
||
return mode === "manual" && !args.authorization_response
|
||
? { status: "pending", provider: "openai_codex", flow_id: "flow-test" }
|
||
: signedIn();
|
||
}
|
||
throw new Error(`Unexpected mutation: ${action}`);
|
||
});
|
||
vi.stubGlobal("fetch", vi.fn(async (input: RequestInfo | URL) => {
|
||
if (String(input).includes("/provider-models?")) return jsonResponse({
|
||
provider: "openai_codex", label: "OpenAI Codex", status: "available", catalog_kind: "hybrid",
|
||
source: recovered ? "remote" : "fallback", error_kind: recovered ? null : "auth_required",
|
||
models: [{ id: recovered ? "openai-codex/new-model" : "openai-codex/offline-model" }], model_count: 1,
|
||
});
|
||
return jsonResponse(payload);
|
||
}));
|
||
renderSettingsView({ initialSection: "models" });
|
||
fireEvent.click(await screen.findByRole("button", { name: "New model preset" }));
|
||
fireEvent.change(screen.getByRole("textbox", { name: "Preset name" }), {
|
||
target: { value: "My unsaved preset" },
|
||
});
|
||
await openPopover(screen.getByRole("button", { name: "Select model" }));
|
||
fireEvent.click(await screen.findByRole("button", { name: "Sign in again" }));
|
||
if (mode === "manual") {
|
||
fireEvent.change(await screen.findByRole("textbox", { name: "Full callback URL" }), {
|
||
target: { value: "http://localhost:1455/auth/callback?code=fixture&state=test" },
|
||
});
|
||
fireEvent.click(screen.getByRole("button", { name: "Finish sign-in" }));
|
||
}
|
||
await waitFor(() => expect(recovered).toBe(true), { timeout: 3000 });
|
||
expect(screen.getByRole("textbox", { name: "Preset name" })).toHaveValue("My unsaved preset");
|
||
await openPopover(screen.getByRole("button", { name: "Select model" }));
|
||
expect(await screen.findByRole("option", { name: /openai-codex\/new-model/ })).toBeVisible();
|
||
expect(screen.queryByText("Authorization expired. Please sign in again.")).not.toBeInTheDocument();
|
||
expect(requestMutationMock.mock.calls.every(([action]) => action.startsWith("settings.provider.oauth_"))).toBe(true);
|
||
});
|
||
|
||
it("uses the preset name as the canonical identity", async () => {
|
||
const payload = settingsPayload();
|
||
payload.model_presets[0] = {
|
||
...payload.model_presets[0],
|
||
name: "openai",
|
||
label: "minimax",
|
||
};
|
||
payload.model_call_order = ["openai"];
|
||
payload.agent.model_preset = "openai";
|
||
vi.stubGlobal("fetch", vi.fn(() => new Promise<Response>(() => {})));
|
||
|
||
renderSettingsView({ initialSection: "models", initialSettings: payload });
|
||
|
||
await togglePresetEditor("openai");
|
||
|
||
const editor = screen.getByTestId("model-preset-editor");
|
||
expect(within(editor).getByText("Preset name")).toBeInTheDocument();
|
||
expect(within(editor).getByRole("textbox", { name: "Preset name" })).toHaveValue(
|
||
"openai",
|
||
);
|
||
});
|
||
|
||
it("edits a legacy case-conflicting preset without treating its own name as a rename", async () => {
|
||
const payload = settingsPayload();
|
||
const primary = {
|
||
...payload.model_presets[0],
|
||
name: "Fast",
|
||
label: "Fast",
|
||
active: true,
|
||
};
|
||
const legacyConflict = {
|
||
...primary,
|
||
name: "fast",
|
||
label: "fast",
|
||
active: false,
|
||
};
|
||
const legacyPayload: SettingsPayload = {
|
||
...payload,
|
||
agent: { ...payload.agent, model_preset: "Fast" },
|
||
model_presets: [primary, legacyConflict],
|
||
model_call_order: ["Fast", "fast"],
|
||
};
|
||
vi.stubGlobal("fetch", vi.fn(() => new Promise<Response>(() => {})));
|
||
requestMutationMock.mockResolvedValueOnce(legacyPayload);
|
||
|
||
renderSettingsView({ initialSection: "models", initialSettings: legacyPayload });
|
||
await togglePresetEditor("Fast");
|
||
fireEvent.click(screen.getByRole("button", { name: /Advanced options/ }));
|
||
fireEvent.change(screen.getByLabelText("Temperature"), {
|
||
target: { value: "0.4" },
|
||
});
|
||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||
|
||
await waitFor(() => {
|
||
expect(requestMutationMock).toHaveBeenCalledWith(
|
||
"settings.model_configuration.update",
|
||
{ name: "Fast", temperature: 0.4 },
|
||
20_000,
|
||
);
|
||
});
|
||
});
|
||
|
||
it("renames an existing preset without losing the editor selection", async () => {
|
||
const payload = settingsPayload();
|
||
const renamedPayload: SettingsPayload = {
|
||
...payload,
|
||
agent: { ...payload.agent, model_preset: "Codex" },
|
||
model_presets: payload.model_presets.map((preset) => ({
|
||
...preset,
|
||
name: "Codex",
|
||
label: "Codex",
|
||
})),
|
||
model_call_order: ["Codex"],
|
||
};
|
||
vi.stubGlobal("fetch", vi.fn(() => new Promise<Response>(() => {})));
|
||
requestMutationMock.mockResolvedValueOnce(renamedPayload);
|
||
|
||
renderSettingsView({ initialSection: "models", initialSettings: payload });
|
||
await togglePresetEditor();
|
||
|
||
const nameInput = screen.getByRole("textbox", { name: "Preset name" });
|
||
fireEvent.change(nameInput, { target: { value: "Codex" } });
|
||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||
|
||
await waitFor(() => {
|
||
expect(requestMutationMock).toHaveBeenCalledWith(
|
||
"settings.model_configuration.update",
|
||
{ name: "primary", new_name: "Codex" },
|
||
20_000,
|
||
);
|
||
});
|
||
expect(await screen.findByTestId("model-call-order-row-Codex")).toBeInTheDocument();
|
||
expect(screen.getByRole("textbox", { name: "Preset name" })).toHaveValue("Codex");
|
||
});
|
||
|
||
it("maps a server-side name conflict back to the preset name field", async () => {
|
||
const payload = settingsPayload();
|
||
vi.stubGlobal("fetch", vi.fn(() => new Promise<Response>(() => {})));
|
||
requestMutationMock.mockRejectedValueOnce({ status: 409 });
|
||
|
||
renderSettingsView({ initialSection: "models", initialSettings: payload });
|
||
await togglePresetEditor();
|
||
|
||
const nameInput = screen.getByRole("textbox", { name: "Preset name" });
|
||
fireEvent.change(nameInput, { target: { value: "Codex" } });
|
||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||
|
||
expect(await screen.findByRole("alert")).toHaveTextContent(
|
||
"A preset with this name already exists.",
|
||
);
|
||
expect(nameInput).toHaveAttribute("aria-invalid", "true");
|
||
expect(nameInput).toHaveFocus();
|
||
});
|
||
|
||
it("keeps generation parameters collapsed until advanced options are opened", async () => {
|
||
vi.stubGlobal(
|
||
"fetch",
|
||
vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings") return jsonResponse(settingsPayload());
|
||
if (url === "/api/settings/cli-apps") {
|
||
return jsonResponse({ apps: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/mcp-presets") {
|
||
return jsonResponse({ presets: [], installed_count: 0 });
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
}),
|
||
);
|
||
|
||
renderSettingsView({ initialSection: "models" });
|
||
|
||
await togglePresetEditor();
|
||
const advanced = await screen.findByRole("button", { name: /Advanced options/ });
|
||
|
||
fireEvent.click(advanced);
|
||
|
||
expect(await screen.findByText("Context window")).toBeInTheDocument();
|
||
expect(screen.getByText("Temperature")).toBeInTheDocument();
|
||
expect(screen.getByRole("textbox", { name: "Context window" })).toHaveValue("200k");
|
||
const reasoningEffort = screen.getByLabelText("Reasoning effort");
|
||
expect(reasoningEffort).toHaveProperty("type", "text");
|
||
fireEvent.change(reasoningEffort, { target: { value: "provider-native-mode" } });
|
||
expect(reasoningEffort).toHaveValue("provider-native-mode");
|
||
});
|
||
|
||
it.each([["128000", 128000], ["131072", 131072], ["272k", 272000], ["256K", 256000], ["1.5m", 1500000], ["1.001k", 1001]] as const)("saves a custom context budget (%s)", async (input, tokens) => {
|
||
const payload = settingsPayload();
|
||
vi.stubGlobal("fetch", vi.fn(() => new Promise<Response>(() => {})));
|
||
requestMutationMock.mockResolvedValue({
|
||
...payload,
|
||
model_presets: payload.model_presets.map((preset) => ({ ...preset, context_window_tokens: tokens })),
|
||
});
|
||
renderSettingsView({ initialSection: "models", initialSettings: payload });
|
||
await togglePresetEditor();
|
||
fireEvent.click(screen.getByRole("button", { name: /Advanced options/ }));
|
||
fireEvent.change(screen.getByRole("textbox", { name: "Context window" }), {
|
||
target: { value: input },
|
||
});
|
||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||
await waitFor(() => expect(requestMutationMock).toHaveBeenCalledWith(
|
||
"settings.model_configuration.update", expect.objectContaining({ context_window_tokens: tokens }), expect.any(Number),
|
||
));
|
||
expect(screen.getByRole("textbox", { name: "Context window" })).toHaveValue(input);
|
||
});
|
||
|
||
it.each(["", "0", "-1", "1.5", "abc", "2ki", "0.0001k", "9007199254740992"])("keeps invalid context input unsaved (%s)", async (value) => {
|
||
vi.stubGlobal("fetch", vi.fn(() => new Promise<Response>(() => {})));
|
||
renderSettingsView({ initialSection: "models", initialSettings: settingsPayload() });
|
||
await togglePresetEditor();
|
||
fireEvent.click(screen.getByRole("button", { name: /Advanced options/ }));
|
||
const input = screen.getByRole("textbox", { name: "Context window" });
|
||
fireEvent.change(input, { target: { value } });
|
||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||
expect(input).toHaveAttribute("aria-invalid", "true");
|
||
expect(input).toHaveAccessibleDescription("Enter a positive token count, such as 200k, 1m, or 131072.");
|
||
await new Promise((resolve) => setTimeout(resolve, 750));
|
||
expect(requestMutationMock).not.toHaveBeenCalled();
|
||
expect(input).toHaveValue(value);
|
||
});
|
||
|
||
it("opens the preset editor in a dialog and protects the primary preset", async () => {
|
||
vi.stubGlobal(
|
||
"fetch",
|
||
vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings") return jsonResponse(settingsPayload());
|
||
if (url === "/api/settings/cli-apps") {
|
||
return jsonResponse({ apps: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/mcp-presets") {
|
||
return jsonResponse({ presets: [], installed_count: 0 });
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
}),
|
||
);
|
||
|
||
renderSettingsView({ initialSection: "models" });
|
||
|
||
const row = await screen.findByTestId("model-call-order-row-primary");
|
||
const trigger = within(row).getAllByRole("button")[0];
|
||
expect(trigger).toHaveAttribute("aria-haspopup", "dialog");
|
||
|
||
fireEvent.click(trigger);
|
||
|
||
const editor = screen.getByTestId("model-preset-editor");
|
||
expect(trigger).toHaveAttribute("aria-pressed", "true");
|
||
expect(row.closest('[role="listitem"]')).toBeInTheDocument();
|
||
expect(row.closest('[role="list"]')).toBeInTheDocument();
|
||
expect(screen.getByRole("dialog", { name: "primary" })).toContainElement(editor);
|
||
expect(within(editor).getByRole("textbox", { name: "Preset name" })).toHaveValue(
|
||
"primary",
|
||
);
|
||
const deleteButton = within(editor).getByRole("button", { name: "Delete" });
|
||
expect(deleteButton).toBeDisabled();
|
||
expect(deleteButton).toHaveAttribute("aria-describedby", "model-preset-delete-hint");
|
||
expect(
|
||
within(editor).getByText("Choose another primary preset before deleting this one."),
|
||
).toBeInTheDocument();
|
||
|
||
fireEvent.click(screen.getByRole("button", { name: "Close", exact: true }));
|
||
});
|
||
|
||
it("drags model presets to reorder and saves the model call order immediately", async () => {
|
||
const { payload, backupPreset } = settingsPayloadWithBackup();
|
||
const updatedPayload: SettingsPayload = {
|
||
...payload,
|
||
agent: {
|
||
...payload.agent,
|
||
model: backupPreset.model,
|
||
provider: backupPreset.provider,
|
||
resolved_provider: backupPreset.resolved_provider,
|
||
model_preset: backupPreset.name,
|
||
},
|
||
model_presets: payload.model_presets.map((preset) => ({
|
||
...preset,
|
||
active: preset.name === backupPreset.name,
|
||
})),
|
||
model_call_order: ["backup", "primary"],
|
||
};
|
||
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings") return jsonResponse(payload);
|
||
if (url === "/api/settings/cli-apps") {
|
||
return jsonResponse({ apps: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/mcp-presets") {
|
||
return jsonResponse({ presets: [], installed_count: 0 });
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
});
|
||
vi.stubGlobal("fetch", fetchMock);
|
||
requestMutationMock.mockResolvedValueOnce(updatedPayload);
|
||
|
||
renderSettingsView({ initialSection: "models", initialSettings: payload });
|
||
|
||
await waitFor(() =>
|
||
expect(fetchMock).toHaveBeenCalledWith(
|
||
"/api/settings",
|
||
expect.objectContaining({
|
||
headers: { Authorization: "Bearer tok" },
|
||
}),
|
||
),
|
||
);
|
||
await togglePresetEditor();
|
||
fireEvent.click(screen.getByRole("button", { name: /Advanced options/ }));
|
||
fireEvent.change(screen.getByLabelText("Temperature"), {
|
||
target: { value: "0.4" },
|
||
});
|
||
fireEvent.click(screen.getByRole("button", { name: "Close", exact: true }));
|
||
const backupRow = screen.getByTestId("model-call-order-row-backup");
|
||
fireEvent.pointerDown(backupRow, { button: 0, clientY: 100, pointerId: 1 });
|
||
fireEvent.pointerMove(backupRow, { clientY: 40, pointerId: 1, buttons: 1 });
|
||
fireEvent.pointerUp(backupRow, { pointerId: 1 });
|
||
fireEvent.click(backupRow);
|
||
|
||
await waitFor(() => {
|
||
expect(requestMutationMock).toHaveBeenCalledWith(
|
||
"settings.model_call_order.update",
|
||
{ order: ["backup", "primary"] },
|
||
20_000,
|
||
);
|
||
});
|
||
|
||
fireEvent.click(within(screen.getByTestId("model-call-order-row-primary")).getByRole("button"));
|
||
fireEvent.click(screen.getByRole("button", { name: /Advanced options/ }));
|
||
expect(screen.getByLabelText("Temperature")).toHaveValue(0.4);
|
||
expect(screen.getByRole("button", { name: "Save" })).toBeEnabled();
|
||
});
|
||
|
||
it("keeps repeated fallback preset rows stable when changing the primary preset", async () => {
|
||
const { payload, backupPreset } = settingsPayloadWithBackup();
|
||
const initialPayload: SettingsPayload = {
|
||
...payload,
|
||
agent: {
|
||
...payload.agent,
|
||
model: backupPreset.model,
|
||
provider: backupPreset.provider,
|
||
resolved_provider: backupPreset.resolved_provider,
|
||
model_preset: backupPreset.name,
|
||
},
|
||
model_presets: payload.model_presets.map((preset) => ({
|
||
...preset,
|
||
active: preset.name === backupPreset.name,
|
||
})),
|
||
model_call_order: ["backup", "primary", "backup"],
|
||
};
|
||
const updatedPayload: SettingsPayload = {
|
||
...initialPayload,
|
||
agent: {
|
||
...payload.agent,
|
||
model_preset: "primary",
|
||
},
|
||
model_presets: payload.model_presets.map((preset) => ({
|
||
...preset,
|
||
active: preset.name === "primary",
|
||
})),
|
||
model_call_order: ["primary", "backup", "backup"],
|
||
};
|
||
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings") return jsonResponse(initialPayload);
|
||
if (url === "/api/settings/cli-apps") {
|
||
return jsonResponse({ apps: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/mcp-presets") {
|
||
return jsonResponse({ presets: [], installed_count: 0 });
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
});
|
||
vi.stubGlobal("fetch", fetchMock);
|
||
requestMutationMock.mockResolvedValueOnce(updatedPayload);
|
||
|
||
renderSettingsView({ initialSection: "models", initialSettings: initialPayload });
|
||
|
||
const primaryRow = screen.getByTestId("model-call-order-row-primary");
|
||
const backupRows = screen.getAllByTestId("model-call-order-row-backup");
|
||
const secondBackupRow = backupRows[1];
|
||
const secondBackupTrigger = within(secondBackupRow).getAllByRole("button")[0];
|
||
fireEvent.click(secondBackupTrigger);
|
||
expect(screen.getAllByTestId("model-preset-editor")).toHaveLength(1);
|
||
expect(screen.getByRole("dialog", { name: "backup" })).toContainElement(screen.getByTestId("model-preset-editor"));
|
||
expect(screen.getByRole("button", { name: "Delete", exact: true })).toBeDisabled();
|
||
fireEvent.click(screen.getByRole("button", { name: "Close", exact: true }));
|
||
|
||
fireEvent.pointerDown(primaryRow, { button: 0, clientY: 100, pointerId: 1 });
|
||
fireEvent.pointerMove(primaryRow, { clientY: 40, pointerId: 1, buttons: 1 });
|
||
fireEvent.pointerUp(primaryRow, { pointerId: 1 });
|
||
|
||
await waitFor(() =>
|
||
expect(
|
||
screen
|
||
.getAllByTestId(/^model-call-order-row-/)
|
||
.map((row) => row.getAttribute("data-testid")),
|
||
).toEqual([
|
||
"model-call-order-row-primary",
|
||
"model-call-order-row-backup",
|
||
"model-call-order-row-backup",
|
||
]),
|
||
);
|
||
});
|
||
|
||
it("restores the model call order when immediate persistence fails", async () => {
|
||
const { payload } = settingsPayloadWithBackup();
|
||
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings") return jsonResponse(payload);
|
||
if (url === "/api/settings/cli-apps") {
|
||
return jsonResponse({ apps: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/mcp-presets") {
|
||
return jsonResponse({ presets: [], installed_count: 0 });
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
});
|
||
vi.stubGlobal("fetch", fetchMock);
|
||
requestMutationMock.mockRejectedValueOnce(new Error("Order update failed"));
|
||
|
||
renderSettingsView({ initialSection: "models", initialSettings: payload });
|
||
|
||
await waitFor(() =>
|
||
expect(fetchMock).toHaveBeenCalledWith(
|
||
"/api/settings",
|
||
expect.objectContaining({
|
||
headers: { Authorization: "Bearer tok" },
|
||
}),
|
||
),
|
||
);
|
||
fireEvent.keyDown(screen.getByTestId("model-call-order-row-backup"), {
|
||
key: "ArrowUp",
|
||
});
|
||
|
||
expect(await screen.findByText("Order update failed")).toBeInTheDocument();
|
||
expect(
|
||
screen
|
||
.getAllByTestId(/^model-call-order-row-/)
|
||
.map((row) => row.getAttribute("data-testid")),
|
||
).toEqual([
|
||
"model-call-order-row-primary",
|
||
"model-call-order-row-backup",
|
||
]);
|
||
});
|
||
|
||
it("shows presets outside the call order in the unified list and adds them directly", async () => {
|
||
const { payload } = settingsPayloadWithBackup();
|
||
const codexPreset = {
|
||
...payload.model_presets[0],
|
||
name: "codex",
|
||
label: "Codex",
|
||
active: false,
|
||
model: "openai-codex/gpt-5.5",
|
||
provider: "openai",
|
||
resolved_provider: "openai",
|
||
};
|
||
const payloadWithCodex: SettingsPayload = {
|
||
...payload,
|
||
model_presets: [...payload.model_presets, codexPreset],
|
||
};
|
||
const orderedPayload: SettingsPayload = {
|
||
...payloadWithCodex,
|
||
model_call_order: ["primary", "backup", "codex"],
|
||
};
|
||
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings") return jsonResponse(payloadWithCodex);
|
||
if (url === "/api/settings/cli-apps") {
|
||
return jsonResponse({ apps: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/mcp-presets") {
|
||
return jsonResponse({ presets: [], installed_count: 0 });
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
});
|
||
vi.stubGlobal("fetch", fetchMock);
|
||
requestMutationMock.mockResolvedValueOnce(orderedPayload);
|
||
|
||
renderSettingsView({ initialSection: "models", initialSettings: payloadWithCodex });
|
||
|
||
const codexRow = await screen.findByTestId("model-call-order-row-codex");
|
||
expect(codexRow).toHaveTextContent("codex");
|
||
expect(codexRow).toHaveAttribute("tabindex", "-1");
|
||
|
||
const enableSwitch = within(codexRow).getByRole("switch", { name: "Enable preset" });
|
||
expect(enableSwitch).not.toBeChecked();
|
||
expect(enableSwitch).toHaveClass("h-5", "w-9", "bg-muted-foreground/25");
|
||
fireEvent.click(enableSwitch);
|
||
|
||
await waitFor(() => {
|
||
expect(requestMutationMock).toHaveBeenCalledWith(
|
||
"settings.model_call_order.update",
|
||
{ order: ["primary", "backup", "codex"] },
|
||
20_000,
|
||
);
|
||
});
|
||
const enabledCodexRow = await screen.findByTestId("model-call-order-row-codex");
|
||
expect(enabledCodexRow).toHaveAttribute("tabindex", "0");
|
||
expect(
|
||
within(enabledCodexRow).getByRole("switch", { name: "Disable preset" }),
|
||
).toBeChecked();
|
||
expect(within(enabledCodexRow).getByRole("switch")).toHaveClass("h-5", "w-9", "bg-foreground");
|
||
});
|
||
|
||
it("does not allow disabling the last preset", () => {
|
||
renderSettingsView({ initialSection: "models", initialSettings: settingsPayload() });
|
||
const toggle = screen.getByRole("switch", { name: "Disable preset" });
|
||
expect(toggle).toBeChecked();
|
||
expect(toggle).toBeDisabled();
|
||
fireEvent.click(toggle);
|
||
expect(requestMutationMock).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it("appends a new model preset to the call order immediately", async () => {
|
||
const { payload } = settingsPayloadWithBackup();
|
||
const writerPreset = {
|
||
...payload.model_presets[0],
|
||
name: "Writer",
|
||
label: "Writer",
|
||
active: false,
|
||
model: "openai/gpt-4o-mini",
|
||
provider: "openai",
|
||
resolved_provider: "openai",
|
||
};
|
||
const createdPayload: SettingsPayload = {
|
||
...payload,
|
||
model_presets: [...payload.model_presets, writerPreset],
|
||
created_model_preset: writerPreset.name,
|
||
};
|
||
const orderedPayload: SettingsPayload = {
|
||
...createdPayload,
|
||
model_call_order: ["primary", "backup", "Writer"],
|
||
created_model_preset: undefined,
|
||
};
|
||
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings") return jsonResponse(payload);
|
||
if (url === "/api/settings/cli-apps") {
|
||
return jsonResponse({ apps: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/mcp-presets") {
|
||
return jsonResponse({ presets: [], installed_count: 0 });
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
});
|
||
vi.stubGlobal("fetch", fetchMock);
|
||
requestMutationMock
|
||
.mockResolvedValueOnce(createdPayload)
|
||
.mockResolvedValueOnce(orderedPayload);
|
||
|
||
renderSettingsView({ initialSection: "models", initialSettings: payload });
|
||
|
||
await waitFor(() =>
|
||
expect(fetchMock).toHaveBeenCalledWith(
|
||
"/api/settings",
|
||
expect.objectContaining({
|
||
headers: { Authorization: "Bearer tok" },
|
||
}),
|
||
),
|
||
);
|
||
fireEvent.click(screen.getByRole("button", { name: "New model preset" }));
|
||
expect(screen.getByRole("dialog", { name: "New model preset" })).toBeInTheDocument();
|
||
expect(screen.getByRole("button", { name: "Save" })).toBeDisabled();
|
||
fireEvent.change(screen.getByRole("textbox", { name: "Preset name" }), {
|
||
target: { value: "Writer" },
|
||
});
|
||
await openPopover(screen.getByRole("button", { name: "Select model" }));
|
||
const modelSearch = await screen.findByRole("combobox", {
|
||
name: "Search or type model ID",
|
||
});
|
||
fireEvent.change(modelSearch, {
|
||
target: { value: "openai/gpt-4o-mini" },
|
||
});
|
||
fireEvent.keyDown(modelSearch, { key: "Enter" });
|
||
const saveButton = screen.getByRole("button", { name: "Save" });
|
||
expect(saveButton).toBeEnabled();
|
||
fireEvent.click(saveButton);
|
||
|
||
await waitFor(() => {
|
||
expect(requestMutationMock).toHaveBeenLastCalledWith(
|
||
"settings.model_call_order.update",
|
||
{ order: ["primary", "backup", "Writer"] },
|
||
20_000,
|
||
);
|
||
});
|
||
const writerRow = await screen.findByTestId("model-call-order-row-Writer");
|
||
fireEvent.click(screen.getByRole("button", { name: "Close", exact: true }));
|
||
expect(within(writerRow).getByRole("switch", { name: "Disable preset" })).toBeChecked();
|
||
expect(screen.getAllByText("Writer").length).toBeGreaterThan(0);
|
||
});
|
||
|
||
it("shows an inline error when a new preset name already exists", async () => {
|
||
const { payload } = settingsPayloadWithBackup();
|
||
vi.stubGlobal(
|
||
"fetch",
|
||
vi.fn(async () => ({ ok: false, status: 404, json: async () => ({}) }) as Response),
|
||
);
|
||
|
||
renderSettingsView({ initialSection: "models", initialSettings: payload });
|
||
|
||
fireEvent.click(screen.getByRole("button", { name: "New model preset" }));
|
||
const nameInput = screen.getByRole("textbox", { name: "Preset name" });
|
||
fireEvent.change(nameInput, { target: { value: "PRIMARY" } });
|
||
await openPopover(screen.getByRole("button", { name: "Select model" }));
|
||
const modelSearch = await screen.findByRole("combobox", {
|
||
name: "Search or type model ID",
|
||
});
|
||
fireEvent.change(modelSearch, { target: { value: "openai/gpt-4o-mini" } });
|
||
fireEvent.keyDown(modelSearch, { key: "Enter" });
|
||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||
|
||
expect(requestMutationMock).not.toHaveBeenCalled();
|
||
expect(nameInput).toHaveAttribute("aria-invalid", "true");
|
||
expect(nameInput).toHaveAttribute("aria-describedby", "model-preset-name-error");
|
||
expect(screen.getByRole("alert")).toHaveTextContent(
|
||
"A preset with this name already exists.",
|
||
);
|
||
expect(nameInput.parentElement).toHaveClass(
|
||
"animate-[preset-name-shake_180ms_ease-in-out]",
|
||
);
|
||
|
||
fireEvent.change(nameInput, { target: { value: "Writer" } });
|
||
expect(nameInput).toHaveAttribute("aria-invalid", "false");
|
||
});
|
||
|
||
it("converts legacy model settings into presets before editing call order", async () => {
|
||
const migratedPayload = settingsPayload();
|
||
const defaultPreset = {
|
||
...migratedPayload.model_presets[0],
|
||
name: "default",
|
||
label: "Default",
|
||
is_default: true,
|
||
};
|
||
const legacyPayload: SettingsPayload = {
|
||
...migratedPayload,
|
||
agent: {
|
||
...migratedPayload.agent,
|
||
model_preset: "default",
|
||
},
|
||
model_presets: [defaultPreset],
|
||
model_call_order: [],
|
||
model_call_order_editable: false,
|
||
model_configuration_migratable: true,
|
||
};
|
||
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings") return jsonResponse(legacyPayload);
|
||
if (url === "/api/settings/cli-apps") {
|
||
return jsonResponse({ apps: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/mcp-presets") {
|
||
return jsonResponse({ presets: [], installed_count: 0 });
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
});
|
||
vi.stubGlobal("fetch", fetchMock);
|
||
requestMutationMock.mockResolvedValueOnce(migratedPayload);
|
||
|
||
renderSettingsView({ initialSection: "models", initialSettings: legacyPayload });
|
||
|
||
fireEvent.click(
|
||
await screen.findByRole("button", { name: "Convert to presets" }),
|
||
);
|
||
|
||
await waitFor(() =>
|
||
expect(requestMutationMock).toHaveBeenCalledWith(
|
||
"settings.model_configuration.migrate",
|
||
{},
|
||
20_000,
|
||
),
|
||
);
|
||
});
|
||
|
||
it("starts fresh users with an empty preset list instead of legacy conversion", async () => {
|
||
const base = settingsPayload();
|
||
const freshPayload: SettingsPayload = {
|
||
...base,
|
||
agent: {
|
||
...base.agent,
|
||
model: "anthropic/claude-opus-4-5",
|
||
provider: "auto",
|
||
resolved_provider: null,
|
||
has_api_key: false,
|
||
model_preset: "default",
|
||
},
|
||
model_presets: [
|
||
{
|
||
...base.model_presets[0],
|
||
name: "default",
|
||
label: "Default",
|
||
active: true,
|
||
is_default: true,
|
||
model: "anthropic/claude-opus-4-5",
|
||
provider: "auto",
|
||
resolved_provider: null,
|
||
},
|
||
],
|
||
model_call_order: [],
|
||
model_call_order_editable: false,
|
||
model_configuration_migratable: false,
|
||
providers: [],
|
||
};
|
||
vi.stubGlobal("fetch", vi.fn(() => new Promise<Response>(() => {})));
|
||
|
||
renderSettingsView({ initialSection: "models", initialSettings: freshPayload });
|
||
|
||
expect(
|
||
await screen.findByRole("button", { name: "New model preset" }),
|
||
).toBeInTheDocument();
|
||
});
|
||
|
||
it("does not expose the synthetic default configuration as a WebUI preset", async () => {
|
||
const base = settingsPayload();
|
||
const payload: SettingsPayload = {
|
||
...base,
|
||
agent: {
|
||
...base.agent,
|
||
model: "MiniMax-M3",
|
||
provider: "minimax_anthropic",
|
||
resolved_provider: "minimax_anthropic",
|
||
model_preset: "fast",
|
||
},
|
||
model_presets: [
|
||
{
|
||
...base.model_presets[0],
|
||
name: "default",
|
||
label: "Default",
|
||
active: false,
|
||
is_default: true,
|
||
model: "openai-codex/gpt-5.5",
|
||
provider: "openai_codex",
|
||
resolved_provider: "openai_codex",
|
||
},
|
||
{
|
||
...base.model_presets[0],
|
||
name: "fast",
|
||
label: "fast",
|
||
active: true,
|
||
is_default: false,
|
||
model: "MiniMax-M3",
|
||
provider: "minimax_anthropic",
|
||
resolved_provider: "minimax_anthropic",
|
||
},
|
||
],
|
||
model_call_order: ["fast"],
|
||
providers: [
|
||
{
|
||
name: "openai_codex",
|
||
label: "OpenAI Codex",
|
||
configured: true,
|
||
auth_type: "oauth",
|
||
api_key_required: false,
|
||
api_key_hint: null,
|
||
api_base: null,
|
||
default_api_base: null,
|
||
oauth_account: "acct-test",
|
||
oauth_expires_at: null,
|
||
oauth_login_supported: true,
|
||
},
|
||
{
|
||
name: "minimax_anthropic",
|
||
label: "MiniMax (Anthropic)",
|
||
configured: true,
|
||
auth_type: "api_key",
|
||
api_key_required: true,
|
||
api_key_hint: "sk-...",
|
||
api_base: "https://api.minimax.io/anthropic",
|
||
default_api_base: "https://api.minimax.io/anthropic",
|
||
},
|
||
],
|
||
};
|
||
vi.stubGlobal("fetch", vi.fn(() => new Promise<Response>(() => {})));
|
||
|
||
renderSettingsView({ initialSection: "models", initialSettings: payload });
|
||
|
||
expect(screen.getAllByText("fast").length).toBeGreaterThan(0);
|
||
});
|
||
|
||
it("does not expose the synthetic default preset in the overview summary", async () => {
|
||
const base = settingsPayload();
|
||
const payload: SettingsPayload = {
|
||
...base,
|
||
agent: {
|
||
...base.agent,
|
||
model_preset: "default",
|
||
},
|
||
model_presets: [
|
||
{
|
||
...base.model_presets[0],
|
||
name: "default",
|
||
label: "Default",
|
||
is_default: true,
|
||
},
|
||
],
|
||
model_call_order: [],
|
||
model_call_order_editable: false,
|
||
};
|
||
vi.stubGlobal("fetch", vi.fn(() => new Promise<Response>(() => {})));
|
||
|
||
renderSettingsView({ initialSection: "overview", initialSettings: payload });
|
||
|
||
expect(await screen.findByText("openai/gpt-4o")).toBeInTheDocument();
|
||
expect(screen.getByText("openai")).toBeInTheDocument();
|
||
});
|
||
|
||
it("uses the resolved provider row for auto dynamic providers without api keys", async () => {
|
||
vi.stubGlobal("fetch", vi.fn(() => new Promise<Response>(() => {})));
|
||
|
||
renderSettingsView({
|
||
initialSection: "models",
|
||
initialSettings: autoDynamicProviderPayload({
|
||
configured: true,
|
||
hasApiKey: false,
|
||
apiBase: "https://proxy.example.test/v1",
|
||
apiKeyHint: null,
|
||
}),
|
||
});
|
||
|
||
expect(screen.getAllByText("Company Proxy").length).toBeGreaterThan(0);
|
||
});
|
||
|
||
it("does not treat auto dynamic provider api keys as configured without apiBase", async () => {
|
||
vi.stubGlobal("fetch", vi.fn(() => new Promise<Response>(() => {})));
|
||
|
||
renderSettingsView({
|
||
initialSection: "models",
|
||
initialSettings: autoDynamicProviderPayload({
|
||
configured: false,
|
||
hasApiKey: true,
|
||
apiBase: null,
|
||
apiKeyHint: "sk-...",
|
||
}),
|
||
});
|
||
|
||
expect(screen.getByText("Provider setup required")).toBeInTheDocument();
|
||
});
|
||
|
||
it("marks the current model as unconfigured when its provider needs setup", async () => {
|
||
const payload: SettingsPayload = {
|
||
...settingsPayload(),
|
||
agent: {
|
||
...settingsPayload().agent,
|
||
model: "openai-codex/gpt-5.1-codex",
|
||
provider: "openai_codex",
|
||
resolved_provider: "openai_codex",
|
||
has_api_key: false,
|
||
},
|
||
model_presets: [
|
||
{
|
||
...settingsPayload().model_presets[0],
|
||
model: "openai-codex/gpt-5.1-codex",
|
||
provider: "openai_codex",
|
||
},
|
||
],
|
||
providers: [
|
||
{
|
||
name: "openai_codex",
|
||
label: "OpenAI Codex",
|
||
configured: false,
|
||
auth_type: "oauth",
|
||
api_key_required: false,
|
||
api_key_hint: null,
|
||
api_base: null,
|
||
default_api_base: null,
|
||
oauth_account: null,
|
||
oauth_expires_at: null,
|
||
oauth_login_supported: true,
|
||
},
|
||
],
|
||
};
|
||
vi.stubGlobal(
|
||
"fetch",
|
||
vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings") return jsonResponse(payload);
|
||
if (url === "/api/settings/cli-apps") {
|
||
return jsonResponse({ apps: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/mcp-presets") {
|
||
return jsonResponse({ presets: [], installed_count: 0 });
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
}),
|
||
);
|
||
|
||
renderSettingsView({ initialSection: "models" });
|
||
|
||
expect(await screen.findByText("Provider setup required")).toBeInTheDocument();
|
||
await togglePresetEditor();
|
||
fireEvent.click(screen.getByRole("button", { name: "Sign in required" }));
|
||
expect(await screen.findByRole("tooltip")).toHaveTextContent(/Sign in before saving/);
|
||
expect(await screen.findByRole("button", { name: "Sign in" })).toBeInTheDocument();
|
||
});
|
||
|
||
it("keeps unsigned OAuth providers out of the active provider picker", async () => {
|
||
const payload: SettingsPayload = {
|
||
...settingsPayload(),
|
||
agent: {
|
||
...settingsPayload().agent,
|
||
model: "deepseek-chat",
|
||
provider: "deepseek",
|
||
resolved_provider: "deepseek",
|
||
},
|
||
model_presets: [
|
||
{
|
||
...settingsPayload().model_presets[0],
|
||
model: "deepseek-chat",
|
||
provider: "deepseek",
|
||
},
|
||
],
|
||
providers: [
|
||
{
|
||
name: "deepseek",
|
||
label: "DeepSeek",
|
||
configured: true,
|
||
auth_type: "api_key",
|
||
api_key_required: true,
|
||
api_key_hint: "sk-...",
|
||
api_base: "https://api.deepseek.com",
|
||
default_api_base: "https://api.deepseek.com",
|
||
},
|
||
{
|
||
name: "openai_codex",
|
||
label: "OpenAI Codex",
|
||
configured: false,
|
||
auth_type: "oauth",
|
||
api_key_required: false,
|
||
api_key_hint: null,
|
||
api_base: null,
|
||
default_api_base: null,
|
||
oauth_account: null,
|
||
oauth_expires_at: null,
|
||
oauth_login_supported: true,
|
||
},
|
||
{
|
||
name: "github_copilot",
|
||
label: "GitHub Copilot",
|
||
configured: false,
|
||
auth_type: "oauth",
|
||
api_key_required: false,
|
||
api_key_hint: null,
|
||
api_base: null,
|
||
default_api_base: "https://api.githubcopilot.com",
|
||
oauth_account: null,
|
||
oauth_expires_at: null,
|
||
oauth_login_supported: true,
|
||
},
|
||
],
|
||
};
|
||
vi.stubGlobal(
|
||
"fetch",
|
||
vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings") return jsonResponse(payload);
|
||
if (url === "/api/settings/cli-apps") {
|
||
return jsonResponse({ apps: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/mcp-presets") {
|
||
return jsonResponse({ presets: [], installed_count: 0 });
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
}),
|
||
);
|
||
|
||
renderSettingsView({ initialSection: "models" });
|
||
|
||
await togglePresetEditor();
|
||
const providerPicker = await screen.findByRole("combobox", { name: /DeepSeek/ });
|
||
fireEvent.keyDown(providerPicker, { key: "ArrowDown" });
|
||
|
||
expect(await screen.findByRole("option", { name: /DeepSeek/ })).toBeInTheDocument();
|
||
});
|
||
|
||
it("does not fetch model lists for unsigned OAuth providers", async () => {
|
||
const payload: SettingsPayload = {
|
||
...settingsPayload(),
|
||
agent: {
|
||
...settingsPayload().agent,
|
||
model: "",
|
||
provider: "openai_codex",
|
||
resolved_provider: "openai_codex",
|
||
},
|
||
model_presets: [
|
||
{
|
||
...settingsPayload().model_presets[0],
|
||
model: "",
|
||
provider: "openai_codex",
|
||
},
|
||
],
|
||
providers: [
|
||
{
|
||
name: "openai_codex",
|
||
label: "OpenAI Codex",
|
||
configured: false,
|
||
auth_type: "oauth",
|
||
api_key_required: false,
|
||
api_key_hint: null,
|
||
api_base: null,
|
||
default_api_base: null,
|
||
oauth_account: null,
|
||
oauth_expires_at: null,
|
||
oauth_login_supported: true,
|
||
},
|
||
{
|
||
name: "github_copilot",
|
||
label: "GitHub Copilot",
|
||
configured: false,
|
||
auth_type: "oauth",
|
||
api_key_required: false,
|
||
api_key_hint: null,
|
||
api_base: null,
|
||
default_api_base: "https://api.githubcopilot.com",
|
||
oauth_account: null,
|
||
oauth_expires_at: null,
|
||
oauth_login_supported: true,
|
||
},
|
||
],
|
||
};
|
||
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings") return jsonResponse(payload);
|
||
if (url === "/api/settings/cli-apps") {
|
||
return jsonResponse({ apps: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/mcp-presets") {
|
||
return jsonResponse({ presets: [], installed_count: 0 });
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
});
|
||
vi.stubGlobal("fetch", fetchMock);
|
||
|
||
renderSettingsView({ initialSection: "models" });
|
||
|
||
await togglePresetEditor();
|
||
await openPopover(await screen.findByRole("button", { name: /Select model/i }));
|
||
expect(
|
||
await screen.findByText("Configure this provider before loading models."),
|
||
).toBeInTheDocument();
|
||
expect(
|
||
fetchMock.mock.calls.some(([input]) =>
|
||
String(input).startsWith("/api/settings/provider-models"),
|
||
),
|
||
).toBe(false);
|
||
});
|
||
|
||
it("prefills manual model ids for configured OAuth providers", async () => {
|
||
const payload: SettingsPayload = {
|
||
...settingsPayload(),
|
||
agent: {
|
||
...settingsPayload().agent,
|
||
model: "open-codex/gpt-5.5",
|
||
provider: "openai_codex",
|
||
resolved_provider: "openai_codex",
|
||
},
|
||
model_presets: [
|
||
{
|
||
...settingsPayload().model_presets[0],
|
||
model: "open-codex/gpt-5.5",
|
||
provider: "openai_codex",
|
||
},
|
||
],
|
||
providers: [
|
||
{
|
||
name: "openai_codex",
|
||
label: "OpenAI Codex",
|
||
configured: true,
|
||
auth_type: "oauth",
|
||
api_key_required: false,
|
||
api_key_hint: null,
|
||
api_base: null,
|
||
default_api_base: null,
|
||
oauth_account: "acct-test",
|
||
oauth_expires_at: null,
|
||
oauth_login_supported: true,
|
||
},
|
||
],
|
||
};
|
||
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings") return jsonResponse(payload);
|
||
if (url === "/api/settings/cli-apps") {
|
||
return jsonResponse({ apps: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/mcp-presets") {
|
||
return jsonResponse({ presets: [], installed_count: 0 });
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
});
|
||
vi.stubGlobal("fetch", fetchMock);
|
||
|
||
renderSettingsView({ initialSection: "models" });
|
||
|
||
await togglePresetEditor();
|
||
const modelButtons = await screen.findAllByRole("button", { name: /open-codex\/gpt-5\.5/i });
|
||
await openPopover(modelButtons[modelButtons.length - 1]);
|
||
const input = (await screen.findByPlaceholderText("Search or type model ID")) as HTMLInputElement;
|
||
expect(input.value).toBe("open-codex/gpt-5.5");
|
||
|
||
fireEvent.change(input, { target: { value: "openai-codex/gpt-5.5" } });
|
||
expect(await screen.findByText("“openai-codex/gpt-5.5”")).toBeInTheDocument();
|
||
expect(
|
||
fetchMock.mock.calls.some(([input]) =>
|
||
String(input).startsWith("/api/settings/provider-models"),
|
||
),
|
||
).toBe(false);
|
||
});
|
||
|
||
it("defers the OrcaRouter catalog until the user searches", async () => {
|
||
const base = settingsPayload();
|
||
const payload: SettingsPayload = {
|
||
...base,
|
||
agent: {
|
||
...base.agent,
|
||
model: "orcarouter/auto",
|
||
provider: "orcarouter",
|
||
resolved_provider: "orcarouter",
|
||
},
|
||
model_presets: [
|
||
{
|
||
...base.model_presets[0],
|
||
model: "orcarouter/auto",
|
||
provider: "orcarouter",
|
||
resolved_provider: "orcarouter",
|
||
},
|
||
],
|
||
providers: [
|
||
{
|
||
name: "orcarouter",
|
||
label: "OrcaRouter",
|
||
configured: true,
|
||
auth_type: "api_key",
|
||
api_key_required: true,
|
||
api_key_hint: "sk-o••••test",
|
||
api_base: null,
|
||
default_api_base: "https://api.orcarouter.ai/v1",
|
||
model_catalog: "catalog",
|
||
},
|
||
],
|
||
};
|
||
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings") return jsonResponse(payload);
|
||
if (url === "/api/settings/cli-apps") {
|
||
return jsonResponse({ apps: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/mcp-presets") {
|
||
return jsonResponse({ presets: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/provider-models?provider=orcarouter") {
|
||
return jsonResponse({
|
||
provider: "orcarouter",
|
||
label: "OrcaRouter",
|
||
status: "available",
|
||
catalog_kind: "catalog",
|
||
models: [
|
||
{ id: "orcarouter/auto", owned_by: "orcarouter" },
|
||
{ id: "anthropic/claude-sonnet-4.6", owned_by: "anthropic" },
|
||
],
|
||
model_count: 2,
|
||
fetched_at: 1,
|
||
});
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
});
|
||
vi.stubGlobal("fetch", fetchMock);
|
||
|
||
renderSettingsView({ initialSection: "models" });
|
||
|
||
await togglePresetEditor();
|
||
const modelButtons = await screen.findAllByRole("button", { name: /orcarouter\/auto/i });
|
||
await openPopover(modelButtons[modelButtons.length - 1]);
|
||
expect(await screen.findByText("Search this provider’s model catalog.")).toBeInTheDocument();
|
||
expect(
|
||
fetchMock.mock.calls.some(([input]) =>
|
||
String(input).startsWith("/api/settings/provider-models"),
|
||
),
|
||
).toBe(false);
|
||
|
||
fireEvent.change(screen.getByPlaceholderText("Search or type model ID"), {
|
||
target: { value: "cl" },
|
||
});
|
||
|
||
await screen.findByText("anthropic/claude-sonnet-4.6");
|
||
expect(fetchMock).toHaveBeenCalledWith(
|
||
"/api/settings/provider-models?provider=orcarouter",
|
||
expect.objectContaining({
|
||
headers: { Authorization: "Bearer tok" },
|
||
}),
|
||
);
|
||
});
|
||
|
||
it("loads curated models for configured OAuth providers", async () => {
|
||
const base = settingsPayload();
|
||
const payload: SettingsPayload = {
|
||
...base,
|
||
agent: {
|
||
...base.agent,
|
||
model: "openai-codex/gpt-5.5",
|
||
provider: "openai_codex",
|
||
resolved_provider: "openai_codex",
|
||
},
|
||
model_presets: [
|
||
{
|
||
...base.model_presets[0],
|
||
model: "openai-codex/gpt-5.5",
|
||
provider: "openai_codex",
|
||
},
|
||
],
|
||
providers: [
|
||
{
|
||
name: "openai_codex",
|
||
label: "OpenAI Codex",
|
||
configured: true,
|
||
auth_type: "oauth",
|
||
api_key_required: false,
|
||
api_key_hint: null,
|
||
api_base: null,
|
||
default_api_base: "https://chatgpt.com/backend-api",
|
||
model_catalog: "builtin",
|
||
oauth_account: "acct-test",
|
||
oauth_expires_at: null,
|
||
oauth_login_supported: true,
|
||
},
|
||
],
|
||
};
|
||
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings/provider-models?provider=openai_codex") {
|
||
return jsonResponse({
|
||
provider: "openai_codex",
|
||
label: "OpenAI Codex",
|
||
status: "available",
|
||
catalog_kind: "builtin",
|
||
models: [
|
||
{
|
||
id: "openai-codex/gpt-5.6-sol",
|
||
label: "GPT-5.6-Sol",
|
||
description: "Latest frontier agentic coding model.",
|
||
owned_by: "OpenAI Codex",
|
||
context_window: 372000,
|
||
},
|
||
],
|
||
model_count: 1,
|
||
fetched_at: 1,
|
||
});
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
});
|
||
vi.stubGlobal("fetch", fetchMock);
|
||
|
||
renderSettingsView({ initialSection: "models", initialSettings: payload });
|
||
|
||
await togglePresetEditor();
|
||
const modelButtons = await screen.findAllByRole("button", {
|
||
name: /openai-codex\/gpt-5\.5/i,
|
||
});
|
||
await openPopover(modelButtons[modelButtons.length - 1]);
|
||
|
||
expect(await screen.findByText("GPT-5.6-Sol")).toBeInTheDocument();
|
||
expect(screen.getByText(/Latest frontier agentic coding model\./)).toBeInTheDocument();
|
||
expect(fetchMock).toHaveBeenCalledWith(
|
||
"/api/settings/provider-models?provider=openai_codex",
|
||
expect.objectContaining({ headers: { Authorization: "Bearer tok" } }),
|
||
);
|
||
});
|
||
|
||
it("loads hybrid online models for configured OAuth providers", async () => {
|
||
const base = settingsPayload();
|
||
const payload: SettingsPayload = {
|
||
...base,
|
||
agent: {
|
||
...base.agent,
|
||
model: "xai-grok/grok-4.5",
|
||
provider: "xai_grok",
|
||
resolved_provider: "xai_grok",
|
||
},
|
||
model_presets: [
|
||
{
|
||
...base.model_presets[0],
|
||
model: "xai-grok/grok-4.5",
|
||
provider: "xai_grok",
|
||
},
|
||
],
|
||
providers: [
|
||
{
|
||
name: "xai_grok",
|
||
label: "xAI Grok",
|
||
configured: true,
|
||
auth_type: "oauth",
|
||
api_key_required: false,
|
||
api_key_hint: null,
|
||
api_base: null,
|
||
default_api_base: "https://cli-chat-proxy.grok.com/v1",
|
||
model_catalog: "hybrid",
|
||
oauth_account: "acct-test",
|
||
oauth_expires_at: null,
|
||
oauth_login_supported: true,
|
||
},
|
||
],
|
||
};
|
||
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings/provider-models?provider=xai_grok") {
|
||
return jsonResponse({
|
||
provider: "xai_grok",
|
||
label: "xAI Grok",
|
||
status: "available",
|
||
catalog_kind: "hybrid",
|
||
source: "remote",
|
||
models: [
|
||
{
|
||
id: "xai-grok/grok-4.6",
|
||
label: "Grok 4.6",
|
||
description: "Latest frontier model",
|
||
owned_by: "xAI",
|
||
context_window: 500_000,
|
||
},
|
||
{
|
||
id: "xai-grok/grok-4.5",
|
||
label: "Grok 4.5",
|
||
owned_by: "xAI",
|
||
context_window: 500_000,
|
||
},
|
||
],
|
||
model_count: 2,
|
||
fetched_at: 1,
|
||
});
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
});
|
||
vi.stubGlobal("fetch", fetchMock);
|
||
|
||
renderSettingsView({ initialSection: "models", initialSettings: payload });
|
||
|
||
await togglePresetEditor();
|
||
const modelButtons = await screen.findAllByRole("button", {
|
||
name: /xai-grok\/grok-4\.5/i,
|
||
});
|
||
await openPopover(modelButtons[modelButtons.length - 1]);
|
||
|
||
expect(await screen.findByText("Grok 4.6")).toBeInTheDocument();
|
||
expect(screen.getByText(/Latest frontier model/)).toBeInTheDocument();
|
||
expect(fetchMock).toHaveBeenCalledWith(
|
||
"/api/settings/provider-models?provider=xai_grok",
|
||
expect.objectContaining({ headers: { Authorization: "Bearer tok" } }),
|
||
);
|
||
fireEvent.keyDown(document.activeElement!, { key: "Escape" });
|
||
await waitFor(() => expect(modelButtons[modelButtons.length - 1]).toHaveFocus());
|
||
fireEvent.click(screen.getByRole("button", { name: "Close", exact: true }));
|
||
await waitFor(() => expect(within(screen.getByTestId("model-call-order-row-primary")).getAllByRole("button")[0]).toBeVisible());
|
||
});
|
||
|
||
it("creates presets in a dialog and can cancel", async () => {
|
||
const payload = settingsPayload();
|
||
payload.providers = [{ name: "openai", label: "OpenAI", configured: true }];
|
||
vi.stubGlobal(
|
||
"fetch",
|
||
vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings") return jsonResponse(payload);
|
||
if (url === "/api/settings/cli-apps") {
|
||
return jsonResponse({ apps: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/mcp-presets") {
|
||
return jsonResponse({ presets: [], installed_count: 0 });
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
}),
|
||
);
|
||
|
||
renderSettingsView({ initialSection: "models" });
|
||
|
||
const createButton = await screen.findByRole("button", { name: "New model preset" });
|
||
const previousPointerEvents = document.body.style.pointerEvents;
|
||
expect(createButton).toHaveClass("w-full");
|
||
fireEvent.click(createButton);
|
||
|
||
expect(screen.getByRole("dialog", { name: "New model preset" })).toBeInTheDocument();
|
||
expect(screen.getByRole("dialog")).toContainElement(screen.getByTestId("model-preset-editor"));
|
||
expect(screen.getByRole("textbox", { name: "Preset name" })).toHaveValue("");
|
||
fireEvent.click(screen.getByRole("button", { name: /Advanced options/ }));
|
||
expect(screen.getByText("Temperature")).toBeInTheDocument();
|
||
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
|
||
|
||
await waitFor(() => expect(document.body.style.pointerEvents).toBe(previousPointerEvents));
|
||
|
||
fireEvent.click(screen.getByRole("button", { name: "New model preset" }));
|
||
const nameInput = await screen.findByRole("textbox", { name: "Preset name" });
|
||
expect(nameInput).toHaveValue("");
|
||
expect(nameInput).toHaveAttribute("placeholder", "e.g. Fast writing");
|
||
|
||
await openPopover(screen.getByRole("button", { name: "Select model" }));
|
||
const modelSearch = await screen.findByRole("combobox", {
|
||
name: "Search or type model ID",
|
||
});
|
||
fireEvent.change(modelSearch, { target: { value: "openai/gpt-4o-mini" } });
|
||
fireEvent.keyDown(modelSearch, { key: "Enter" });
|
||
|
||
expect(nameInput).toHaveValue("gpt-4o-mini");
|
||
expect(screen.getByRole("button", { name: "Save" })).toBeEnabled();
|
||
|
||
fireEvent.change(nameInput, { target: { value: "Writer" } });
|
||
await openPopover(screen.getByRole("button", { name: /openai\/gpt-4o-mini/ }));
|
||
const nextModelSearch = await screen.findByRole("combobox", {
|
||
name: "Search or type model ID",
|
||
});
|
||
fireEvent.change(nextModelSearch, { target: { value: "openai/gpt-4.1-mini" } });
|
||
fireEvent.keyDown(nextModelSearch, { key: "Enter" });
|
||
expect(nameInput).toHaveValue("Writer");
|
||
});
|
||
|
||
it("loads provider models and lets users choose one without typing the id manually", async () => {
|
||
const payload: SettingsPayload = {
|
||
...settingsPayload(),
|
||
agent: {
|
||
...settingsPayload().agent,
|
||
model: "deepseek-chat",
|
||
provider: "deepseek",
|
||
resolved_provider: "deepseek",
|
||
},
|
||
model_presets: [
|
||
{
|
||
...settingsPayload().model_presets[0],
|
||
model: "deepseek-chat",
|
||
provider: "deepseek",
|
||
},
|
||
],
|
||
providers: [
|
||
{
|
||
name: "deepseek",
|
||
label: "DeepSeek",
|
||
configured: true,
|
||
auth_type: "api_key",
|
||
api_key_required: true,
|
||
api_key_hint: "sk-...",
|
||
api_base: "https://api.deepseek.com",
|
||
default_api_base: "https://api.deepseek.com",
|
||
},
|
||
],
|
||
};
|
||
const updatedPayload: SettingsPayload = {
|
||
...payload,
|
||
agent: {
|
||
...payload.agent,
|
||
model: "deepseek-reasoner",
|
||
temperature: 0.4,
|
||
},
|
||
model_presets: [
|
||
{
|
||
...payload.model_presets[0],
|
||
model: "deepseek-reasoner",
|
||
temperature: 0.4,
|
||
reasoning_effort: "provider-native-mode",
|
||
},
|
||
],
|
||
};
|
||
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
|
||
const url = String(input);
|
||
if (url === "/api/settings") return jsonResponse(payload);
|
||
if (url === "/api/settings/cli-apps") {
|
||
return jsonResponse({ apps: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/mcp-presets") {
|
||
return jsonResponse({ presets: [], installed_count: 0 });
|
||
}
|
||
if (url === "/api/settings/provider-models?provider=deepseek") {
|
||
return jsonResponse({
|
||
provider: "deepseek",
|
||
label: "DeepSeek",
|
||
status: "available",
|
||
catalog_kind: "official",
|
||
models: [
|
||
{ id: "deepseek-chat", owned_by: "deepseek", context_window: 65536 },
|
||
{ id: "deepseek-reasoner", owned_by: "deepseek", context_window: 65536 },
|
||
],
|
||
model_count: 2,
|
||
fetched_at: 1,
|
||
});
|
||
}
|
||
return { ok: false, status: 404, json: async () => ({}) } as Response;
|
||
});
|
||
vi.stubGlobal("fetch", fetchMock);
|
||
requestMutationMock.mockResolvedValueOnce(updatedPayload);
|
||
|
||
renderSettingsView({ initialSection: "models" });
|
||
|
||
await togglePresetEditor();
|
||
const modelButtons = await screen.findAllByRole("button", { name: /deepseek-chat/i });
|
||
await openPopover(modelButtons[modelButtons.length - 1]);
|
||
await screen.findByText("deepseek-reasoner");
|
||
fireEvent.click(screen.getAllByText("deepseek-reasoner")[0]);
|
||
fireEvent.click(screen.getByRole("button", { name: /Advanced options/ }));
|
||
fireEvent.change(screen.getByLabelText("Temperature"), {
|
||
target: { value: "0.4" },
|
||
});
|
||
fireEvent.change(screen.getByLabelText("Reasoning effort"), {
|
||
target: { value: "provider-native-mode" },
|
||
});
|
||
fireEvent.click(screen.getByRole("button", { name: "Save" }));
|
||
|
||
await waitFor(() =>
|
||
expect(fetchMock).toHaveBeenCalledWith(
|
||
"/api/settings/provider-models?provider=deepseek",
|
||
expect.objectContaining({
|
||
headers: { Authorization: "Bearer tok" },
|
||
}),
|
||
),
|
||
);
|
||
await waitFor(() => {
|
||
const saveCall = requestMutationMock.mock.calls.find(([action]) =>
|
||
action === "settings.model_configuration.update",
|
||
);
|
||
expect(saveCall).toBeDefined();
|
||
expect(saveCall?.[1]).toEqual({
|
||
name: "primary",
|
||
model: "deepseek-reasoner",
|
||
reasoning_effort: "provider-native-mode",
|
||
temperature: 0.4,
|
||
});
|
||
});
|
||
});
|
||
});
|