补齐Settings Hook请求与凭据测试
This commit is contained in:
1 parent
82d76f9fcc
commit
a9d840a8c5
2 files changed
+141
No files matched your search
@@ -647,3 +647,4 @@ npm --prefix frontend run build
|
||||
| 2026-08-15 | 完成测试覆盖率提升切片“React 通知与自动化异常组件分支覆盖” | 扩展静态渲染测试覆盖通知渠道空状态/停用/测试中状态,以及自动化运行异常和延迟任务的人工处理按钮;前端 202 个测试、类型检查、注释门禁通过,全源语句覆盖率提升至 23.00%;复杂表单交互与页面 Hook 仍需后续使用可控测试替身继续补齐,不降低门禁或伪造 100% | 下一步:补齐 React 页面/Hook 行为测试,并继续覆盖 Browser 非账号错误分支 |
|
||||
| 2026-08-15 | 完成测试覆盖率提升切片“React SMTP 设置组件展示分支覆盖” | 新增 SMTP 设置面板静态渲染测试,覆盖密码显隐、保存中禁用状态、TLS/SSL 配置及发件人字段回填;前端 203 个测试、类型检查、注释门禁通过,全源语句覆盖率提升至 23.18%;表单事件回调和页面 Hook 仍需后续使用可控测试替身继续补齐,不降低门禁或伪造 100% | 下一步:补齐 React 页面/Hook 行为测试,并继续覆盖 Browser 非账号错误分支 |
|
||||
| 2026-08-15 | 完成测试覆盖率提升切片“React Hook 可控运行时与请求分支覆盖” | 引入 `@testing-library/react` 与 `jsdom` 测试运行时;新增账号任务 Hook 的加载、保存、执行、失败重试和禁用账号门禁测试;新增仪表盘 Hook 的并行加载、刷新、概览失败和非法日期范围测试;前端 209 个测试、类型检查、注释门禁通过,全源语句覆盖率提升至 26.45%;真实账号、外部平台和未注入的生产页面组合仍列为后续切片,不降低门禁或伪造 100% | 下一步:继续补齐 Accounts/Cards/Chat/Items/Notifications/Orders/Settings/Rules Hook 与页面行为测试 |
|
||||
| 2026-08-15 | 完成测试覆盖率提升切片“React Settings Hook 请求与凭据行为覆盖” | 新增系统设置 Hook 的成功加载/保存、模型发现失败、凭据前端校验、后端拒绝和初始读取失败测试;前端 213 个测试、类型检查、注释门禁通过,全源语句覆盖率提升至 28.96%;真实重新登录和浏览器页面重载仍只验证可控响应与调度,不伪造真实会话 | 下一步:继续补齐 Accounts/Cards/Chat/Items/Notifications/Orders/Rules Hook 与页面行为测试 |
|
||||
@@ -0,0 +1,140 @@
|
||||
// @vitest-environment jsdom
|
||||
import { act, renderHook, waitFor } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
import type { OperationResponse, SystemSettings } from '../../../types';
|
||||
import { fetchAIModels, getSystemSettings, updateLoginCredentials, updateSystemSettings, verifySession } from './api';
|
||||
import { useSettings } from './hooks';
|
||||
|
||||
vi.mock('./api', /* settingsApiMockFactory 提供设置 Hook 的确定性 API 替身。 */ () => ({
|
||||
fetchAIModels: vi.fn(),
|
||||
getSystemSettings: vi.fn(),
|
||||
updateLoginCredentials: vi.fn(),
|
||||
updateSystemSettings: vi.fn(),
|
||||
verifySession: vi.fn(),
|
||||
}));
|
||||
|
||||
// fetchModelsMock 是模型发现请求的可控替身。
|
||||
const fetchModelsMock = vi.mocked(fetchAIModels);
|
||||
// getSettingsMock 是系统设置读取请求的可控替身。
|
||||
const getSettingsMock = vi.mocked(getSystemSettings);
|
||||
// updateCredentialsMock 是登录凭据更新请求的可控替身。
|
||||
const updateCredentialsMock = vi.mocked(updateLoginCredentials);
|
||||
// updateSettingsMock 是系统设置保存请求的可控替身。
|
||||
const updateSettingsMock = vi.mocked(updateSystemSettings);
|
||||
// verifySessionMock 是会话校验请求的可控替身。
|
||||
const verifySessionMock = vi.mocked(verifySession);
|
||||
|
||||
// settingsFixture 是覆盖 AI、验证码和日志字段的系统配置。
|
||||
const settingsFixture: SystemSettings = { ai_api_url: 'https://ai.example.com', ai_api_key: 'secret', ai_model: '', 'captcha.remote_service_url': 'https://captcha.example.com', log_level: 'info' };
|
||||
// noopReload 是设置凭据成功后定时重载页面的浏览器行为替身。
|
||||
const noopReload = vi.fn();
|
||||
// renderSettingsHook 是渲染系统设置 Hook 的具名回调。
|
||||
const renderSettingsHook = () => useSettings();
|
||||
|
||||
describe('useSettings', /* 当前回调处理系统设置、模型和凭据请求状态。 */ () => {
|
||||
beforeEach(/* 当前回调重置设置 API 替身和浏览器副作用。 */ () => {
|
||||
vi.clearAllMocks();
|
||||
getSettingsMock.mockResolvedValue(settingsFixture);
|
||||
verifySessionMock.mockResolvedValue({ authenticated: true, username: 'admin' });
|
||||
fetchModelsMock.mockResolvedValue(['model-a', 'model-b']);
|
||||
updateSettingsMock.mockResolvedValue({ success: true });
|
||||
updateCredentialsMock.mockResolvedValue({ success: true, message: '凭据已更新' });
|
||||
vi.spyOn(window, 'alert').mockImplementation(
|
||||
// alertImplementation 屏蔽设置保存成功时的浏览器弹窗。
|
||||
() => undefined,
|
||||
);
|
||||
Object.defineProperty(window, 'location', { configurable: true, value: { ...window.location, reload: noopReload } });
|
||||
});
|
||||
|
||||
test('加载设置和模型后可以保存配置', /* 当前回调验证系统设置成功加载和保存路径。 */ async () => {
|
||||
// hook 是系统设置 Hook 的渲染结果。
|
||||
const hook = renderHook(renderSettingsHook);
|
||||
await waitFor(
|
||||
// statusAssertion 等待系统设置加载成功。
|
||||
() => expect(hook.result.current.requestStatus).toBe('success'),
|
||||
);
|
||||
expect(hook.result.current.settings).toMatchObject({ ...settingsFixture, ai_model: 'model-a' });
|
||||
expect(hook.result.current.aiModels).toEqual(['model-a', 'model-b']);
|
||||
expect(hook.result.current.credentials.new_username).toBe('admin');
|
||||
|
||||
await act(
|
||||
// saveAction 执行系统设置保存动作。
|
||||
async () => hook.result.current.handleSave(),
|
||||
);
|
||||
expect(updateSettingsMock).toHaveBeenCalledWith(expect.objectContaining({ ai_api_url: settingsFixture.ai_api_url }), expect.objectContaining({ signal: expect.any(AbortSignal) }));
|
||||
expect(window.alert).toHaveBeenCalledWith('系统配置已保存');
|
||||
});
|
||||
|
||||
test('模型发现失败时清空列表并展示错误', /* 当前回调验证模型接口错误路径。 */ async () => {
|
||||
fetchModelsMock.mockRejectedValue(new Error('模型服务不可用'));
|
||||
// hook 是模型发现失败场景下的系统设置 Hook 渲染结果。
|
||||
const hook = renderHook(renderSettingsHook);
|
||||
await waitFor(
|
||||
// statusAssertion 等待初始系统设置请求完成。
|
||||
() => expect(hook.result.current.requestStatus).toBe('success'),
|
||||
);
|
||||
await act(
|
||||
// loadModelsAction 触发失败的模型发现请求。
|
||||
async () => hook.result.current.loadAIModels(settingsFixture, true),
|
||||
);
|
||||
expect(hook.result.current.aiModels).toEqual([]);
|
||||
expect(hook.result.current.modelDropdownOpen).toBe(false);
|
||||
expect(hook.result.current.modelError).toBe('模型服务不可用');
|
||||
});
|
||||
|
||||
test('凭据校验失败和服务拒绝都写入错误提示', /* 当前回调验证登录凭据校验和后端拒绝路径。 */ async () => {
|
||||
// hook 是凭据表单测试使用的系统设置 Hook 渲染结果。
|
||||
const hook = renderHook(renderSettingsHook);
|
||||
await waitFor(
|
||||
// statusAssertion 等待凭据测试使用的系统设置请求完成。
|
||||
() => expect(hook.result.current.requestStatus).toBe('success'),
|
||||
);
|
||||
// invalidEvent 是触发前端凭据校验的最小表单事件。
|
||||
const invalidEvent = { preventDefault: vi.fn() } as unknown as React.FormEvent;
|
||||
await act(
|
||||
// invalidAction 提交非法凭据表单并触发前端校验。
|
||||
async () => hook.result.current.handleCredentialsSave(invalidEvent),
|
||||
);
|
||||
expect(hook.result.current.credentialsMessage?.type).toBe('error');
|
||||
expect(updateCredentialsMock).not.toHaveBeenCalled();
|
||||
|
||||
await act(
|
||||
// invalidCredentials 设置密码确认不一致的草稿。
|
||||
() => hook.result.current.setCredentials({ new_username: 'new-admin', current_password: 'old-password', new_password: 'new-password', confirm_password: 'wrong-password' }),
|
||||
);
|
||||
// validEvent 是触发后端凭据拒绝的表单事件。
|
||||
const validEvent = { preventDefault: vi.fn() } as unknown as React.FormEvent;
|
||||
await act(
|
||||
// mismatchAction 提交确认密码不一致的表单。
|
||||
async () => hook.result.current.handleCredentialsSave(validEvent),
|
||||
);
|
||||
expect(updateCredentialsMock).not.toHaveBeenCalled();
|
||||
|
||||
await act(
|
||||
// validCredentials 设置可以提交的凭据草稿。
|
||||
() => hook.result.current.setCredentials({ new_username: 'new-admin', current_password: 'old-password', new_password: 'new-password', confirm_password: 'new-password' }),
|
||||
);
|
||||
// rejectedResponse 是后端拒绝凭据更新的统一响应。
|
||||
const rejectedResponse: OperationResponse = { success: false, message: '当前密码错误' };
|
||||
updateCredentialsMock.mockResolvedValueOnce(rejectedResponse);
|
||||
// rejectedEvent 是触发后端拒绝分支的表单事件。
|
||||
const rejectedEvent = { preventDefault: vi.fn() } as unknown as React.FormEvent;
|
||||
await act(
|
||||
// rejectedAction 提交合法表单并验证后端拒绝提示。
|
||||
async () => hook.result.current.handleCredentialsSave(rejectedEvent),
|
||||
);
|
||||
expect(hook.result.current.credentialsMessage).toEqual({ type: 'error', text: '当前密码错误' });
|
||||
});
|
||||
|
||||
test('设置读取失败时展示加载错误', /* 当前回调验证系统设置初始请求失败路径。 */ async () => {
|
||||
getSettingsMock.mockRejectedValueOnce(new Error('设置服务失败'));
|
||||
// hook 是设置读取失败场景下的系统设置 Hook 渲染结果。
|
||||
const hook = renderHook(renderSettingsHook);
|
||||
await waitFor(
|
||||
// errorAssertion 等待设置读取错误状态写入。
|
||||
() => expect(hook.result.current.requestStatus).toBe('error'),
|
||||
);
|
||||
expect(hook.result.current.loadError).toBe('设置服务失败');
|
||||
expect(hook.result.current.settings).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user