Files
nanobot/webui/src/tests/settings-test-utils.tsx
T
chengyongru 8ea683679d feat(webui): expand and organize settings with autosave (#5704)
* feat(webui): expand settings with autosave and live configuration

* feat(memory): add long-term memory and idle compaction switches

* refactor(webui): clarify settings hierarchy and align compact controls

* refactor(webui): keep only memory consolidation in memory settings

* refactor(webui): defer runtime changes until explicit restart

* refactor(webui): remove default workspace setting

* fix(webui): align model lists and remove preset counters

* fix(i18n): use natural Chinese labels for chat platforms

* fix(i18n): shorten chat settings navigation label

* fix(webui): match settings navigation icons to text size

* refactor(webui): remove redundant settings sidebar heading

* refactor(webui): use icon-only back navigation in settings

* fix(webui): remove back arrow hover hint

* refactor(webui): remove redundant overview details

* refactor(webui): hide secondary descriptions in settings rows

* style(webui): replace settings dividers with rounded highlights

* refactor(webui): remove redundant model list subtitles and badges

* fix(i18n): restore chat translation namespace boundaries

* fix(i18n): complete settings translations for all locales

* fix(webui): reveal settings only for enabled capabilities

* fix(webui): consolidate capabilities and isolate settings feedback

* fix(webui): clear restart notices when settings are reverted

* fix(webui): anchor restart controls in settings sidebar

* fix(webui): shorten sidebar restart label

* fix(webui): align settings spacing and control sizes

* refactor(webui): unify settings dialogs and interaction feedback

* fix(webui): keep settings option groups on one line without scrolling

* refactor(webui): align settings spacing and sidebar icon proportions

* style(webui): match main sidebar typography to settings

* style(webui): use monospace for sidebar session handles

* refactor(webui): remove heartbeat configuration from automations

* feat(webui): expose configuration actions beside capability toggles

* style(webui): remove ellipsis from configuration actions

* refactor(webui): remove redundant app configuration controls

* feat(webui): add compact sidebar navigation and shortcut hints

* fix(webui): synchronize npm dependency lockfile

* test(webui): align input radius expectation with pill styling

* feat(webui): show daily token usage and cache breakdown

* feat(webui): add model usage details and cache hit rates

* feat(webui): chart daily token usage by model

* refactor(webui): simplify usage details and model labels

* fix(webui): hide usage dialog date range

* refactor(webui): consolidate model usage into chart legends

* fix(webui): enforce restart choices and correct settings test route

* fix(webui): polish settings localization and controls

* fix(webui): avoid repeated usage legend labels

* fix(webui): skip empty usage columns in keyboard navigation

* test(webui): assert settings behavior positively

* feat(webui): refine sidebar navigation and add drag resizing

* style(webui): soften sidebar scroll edges

* style(webui): harmonize sidebar typography and text colors

* feat(webui): move channels to the main sidebar

* style(webui): remove dotted underlines from settings hints

* fix(webui): unify tooltip hover delay

* fix(webui): support Seatbelt sandbox settings

* fix(webui): remove unused appearance density preference

* fix(webui): align settings input values consistently

* style(webui): group compact settings controls before inputs

* feat(webui): edit multiline settings in compact dialogs

* feat(webui): add continuous settings scrolling with gentle snapping

* Revert "feat(webui): add continuous settings scrolling with gentle snapping"

This reverts commit f0978c261a53c844185eeab4d0c3190fbd196fe6.
2026-09-10 11:10:51 +08:00

197 lines
5.2 KiB
TypeScript

import { cleanup, render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, vi } from "vitest";
import { SettingsView } from "@/components/settings/SettingsView";
import { ClientProvider } from "@/providers/ClientProvider";
import type { SettingsPayload } from "@/lib/types";
export const requestMutationMock = vi.fn();
export function jsonResponse(body: unknown): Response {
return {
ok: true,
status: 200,
json: async () => body,
} as Response;
}
export function settingsPayload(): SettingsPayload {
return {
agent: {
model: "openai/gpt-4o",
provider: "auto",
resolved_provider: "openai",
has_api_key: true,
model_preset: "primary",
max_tokens: 8192,
context_window_tokens: 200000,
temperature: 0.1,
reasoning_effort: null,
timezone: "UTC",
tool_hint_max_length: 40,
},
model_presets: [{
name: "primary",
label: "Primary",
active: true,
is_default: false,
model: "openai/gpt-4o",
provider: "auto",
resolved_provider: "openai",
max_tokens: 8192,
context_window_tokens: 200000,
temperature: 0.1,
reasoning_effort: null,
}],
model_call_order: ["primary"],
model_call_order_editable: true,
providers: [],
web_search: {
provider: "duckduckgo",
api_key_hint: null,
base_url: null,
max_results: 5,
timeout: 30,
providers: [{ name: "duckduckgo", label: "DuckDuckGo", credential: "none" }],
},
web: {
enable: true,
proxy: null,
user_agent: null,
search: { max_results: 5, timeout: 30 },
fetch: { use_jina_reader: true },
},
api: {
host: "127.0.0.1",
port: 8900,
timeout: 120,
api_key_hint: null,
},
observability: {
provider: "langfuse",
configured: false,
base_url: "https://cloud.langfuse.com",
},
image_generation: {
enabled: false,
provider: "openrouter",
provider_configured: false,
model: "openai/gpt-5.4-image-2",
default_aspect_ratio: "1:1",
default_image_size: "1K",
max_images_per_turn: 4,
save_dir: "generated",
providers: [],
},
runtime: {
config_path: "/tmp/config.json",
workspace_path: "/tmp/workspace",
gateway_host: "127.0.0.1",
gateway_port: 18790,
heartbeat: {
enabled: true,
interval_s: 1800,
},
dream: {
schedule: "every 2h",
},
unified_session: false,
},
advanced: {
restrict_to_workspace: false,
webui_allow_local_service_access: true,
webui_default_access_mode: "default",
private_service_protection_enabled: true,
ssrf_whitelist_count: 0,
mcp_server_count: 0,
exec_enabled: true,
exec_sandbox: null,
exec_path_prepend_set: false,
exec_path_append_set: false,
},
requires_restart: false,
version: {
current: "0.2.2",
},
docs: {
version: "0.2.2",
base_url: "https://nanobot.wiki/docs/0.2.2",
chat_apps_url: "https://nanobot.wiki/docs/0.2.2/getting-started/chat-apps",
latest_url: "https://nanobot.wiki/docs/latest",
},
};
}
export function renderSettingsView(
options: {
initialSection?:
| "overview"
| "appearance"
| "apps"
| "channels"
| "automations"
| "advanced"
| "models"
| "image"
| "browser"
| "runtime";
initialSettings?: SettingsPayload;
showSidebar?: boolean;
onBackToChat?: () => void;
onSettingsChange?: (payload: SettingsPayload) => void;
onNativeEngineRestart?: () => Promise<string>;
onRestart?: () => void;
} = {},
) {
render(
<ClientProvider client={{ requestMutation: requestMutationMock } as never} token="tok">
<SettingsView
theme="light"
initialSection={options.initialSection ?? "apps"}
initialSettings={options.initialSettings}
showSidebar={options.showSidebar}
onToggleTheme={() => {}}
onBackToChat={options.onBackToChat ?? (() => {})}
onModelNameChange={() => {}}
onSettingsChange={options.onSettingsChange}
onNativeEngineRestart={options.onNativeEngineRestart}
onRestart={options.onRestart}
/>
</ClientProvider>,
);
}
export async function openPopover(trigger: HTMLElement) {
await userEvent.setup().click(trigger);
}
export function installSettingsViewTestHooks() {
beforeEach(() => {
requestMutationMock.mockReset().mockResolvedValue(settingsPayload());
vi.stubGlobal(
"matchMedia",
vi.fn((query: string) => ({
matches: query === "(min-width: 1280px)",
media: query,
onchange: null,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
addListener: vi.fn(),
removeListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
);
vi.stubGlobal(
"fetch",
vi.fn(() => new Promise<Response>(() => {})),
);
});
afterEach(() => {
cleanup();
localStorage.removeItem("nanobot-webui.settings-preferences");
vi.useRealTimers();
vi.unstubAllGlobals();
});
}