149 lines
4.5 KiB
TypeScript
149 lines
4.5 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { render, screen } from '@testing-library/react';
|
|
import { MemoryRouter, Route, Routes } from 'react-router-dom';
|
|
import { Settings } from '@/pages/Settings';
|
|
import { useGatewayStore } from '@/stores/gateway';
|
|
import { useSettingsStore } from '@/stores/settings';
|
|
import { useYinianStore } from '@/stores/yinian';
|
|
import i18n from '@/i18n';
|
|
|
|
const hostApiFetchMock = vi.fn();
|
|
|
|
vi.mock('@/components/settings/ProvidersSettings', () => ({
|
|
ProvidersSettings: () => <div data-testid="settings-advanced-model-config-panel">模型配置设置</div>,
|
|
}));
|
|
|
|
vi.mock('@/components/settings/UpdateSettings', () => ({
|
|
UpdateSettings: () => null,
|
|
}));
|
|
|
|
vi.mock('@/pages/Channels', () => ({
|
|
Channels: () => null,
|
|
}));
|
|
|
|
vi.mock('@/pages/YinianSkills', () => ({
|
|
YinianSkills: () => null,
|
|
}));
|
|
|
|
vi.mock('@/lib/host-api', () => ({
|
|
hostApiFetch: (...args: unknown[]) => hostApiFetchMock(...args),
|
|
}));
|
|
|
|
vi.mock('@/lib/api-client', () => ({
|
|
getGatewayWsDiagnosticEnabled: () => false,
|
|
invokeIpc: vi.fn().mockResolvedValue({ success: true, command: 'agent' }),
|
|
setGatewayWsDiagnosticEnabled: vi.fn(),
|
|
toUserMessage: (error: unknown) => String(error),
|
|
}));
|
|
|
|
vi.mock('@/lib/telemetry', () => ({
|
|
clearUiTelemetry: vi.fn(),
|
|
getUiTelemetrySnapshot: vi.fn(() => []),
|
|
subscribeUiTelemetry: vi.fn(() => () => undefined),
|
|
trackUiEvent: vi.fn(),
|
|
}));
|
|
|
|
const hotel = {
|
|
id: 'workspace-1',
|
|
name: '智念空间',
|
|
city: '杭州',
|
|
role: 'manager' as const,
|
|
permissions: [],
|
|
ota: [],
|
|
};
|
|
|
|
function renderRuntimeSettings() {
|
|
return render(
|
|
<MemoryRouter initialEntries={['/settings/runtime']}>
|
|
<Routes>
|
|
<Route path="/settings/*" element={<Settings />} />
|
|
</Routes>
|
|
</MemoryRouter>,
|
|
);
|
|
}
|
|
|
|
describe('Settings advanced model config', () => {
|
|
beforeEach(async () => {
|
|
vi.clearAllMocks();
|
|
await i18n.changeLanguage('zh');
|
|
hostApiFetchMock.mockImplementation((path: string) => {
|
|
if (path.startsWith('/api/diagnostics/model-config')) {
|
|
return Promise.resolve({
|
|
capturedAt: 1,
|
|
ok: true,
|
|
model: { primary: null, fallbacks: [], providerKey: null, modelId: null },
|
|
runtime: { heartbeatEvery: null, heartbeatDisabled: false },
|
|
providers: [],
|
|
authProfiles: { path: '', exists: false, providers: [] },
|
|
checks: [],
|
|
paths: { openclawConfig: '', authProfiles: '' },
|
|
});
|
|
}
|
|
if (path.startsWith('/api/diagnostics/office-runtime')) {
|
|
return Promise.resolve({
|
|
capturedAt: 1,
|
|
ok: true,
|
|
repairAttempted: false,
|
|
python: { executable: null, packages: [] },
|
|
node: { modules: [] },
|
|
dotnet: { available: false, version: null },
|
|
checks: [],
|
|
});
|
|
}
|
|
return Promise.resolve({});
|
|
});
|
|
useSettingsStore.setState({
|
|
devModeUnlocked: false,
|
|
gatewayAutoStart: false,
|
|
telemetryEnabled: true,
|
|
proxyEnabled: false,
|
|
proxyServer: '',
|
|
proxyHttpServer: '',
|
|
proxyHttpsServer: '',
|
|
proxyAllServer: '',
|
|
proxyBypassRules: '<local>',
|
|
});
|
|
useGatewayStore.setState({
|
|
status: { state: 'stopped', port: 18789, gatewayReady: false },
|
|
});
|
|
useYinianStore.setState({
|
|
status: 'authenticated',
|
|
session: {
|
|
authenticated: true,
|
|
user: { id: 'user-1', name: '王管理员' },
|
|
hotels: [hotel],
|
|
currentHotelId: hotel.id,
|
|
accessTokenExpiresAt: 100,
|
|
},
|
|
config: {
|
|
serverTime: 1,
|
|
user: { id: 'user-1', name: '王管理员' },
|
|
hotel,
|
|
hotels: [hotel],
|
|
entitlements: [],
|
|
notificationChannels: [],
|
|
featureFlags: {},
|
|
uiPolicy: { defaultPage: 'today', showAdvancedSettings: false },
|
|
},
|
|
error: null,
|
|
});
|
|
});
|
|
|
|
it('keeps model configuration settings inside advanced mode', async () => {
|
|
useSettingsStore.setState({ devModeUnlocked: true });
|
|
|
|
renderRuntimeSettings();
|
|
|
|
expect(await screen.findByTestId('settings-model-config-section')).toBeVisible();
|
|
expect(screen.getByTestId('settings-advanced-model-config-panel')).toHaveTextContent('模型配置设置');
|
|
expect(screen.getByTestId('settings-model-diagnostics')).toBeVisible();
|
|
});
|
|
|
|
it('keeps model configuration settings hidden until advanced mode is opened', () => {
|
|
renderRuntimeSettings();
|
|
|
|
expect(screen.queryByTestId('settings-model-config-section')).not.toBeInTheDocument();
|
|
expect(screen.queryByTestId('settings-advanced-model-config-panel')).not.toBeInTheDocument();
|
|
});
|
|
});
|