补齐Accounts Hook账号与运行状态测试

This commit is contained in:
Christ committed 2026-08-15 10:24:21 +08:00
1 parent f4f51a8d3c
commit eb9dbd27cc
2 files changed
+84

No files matched your search

@@ -650,3 +650,4 @@ npm --prefix frontend run build
| 2026-08-15 | 完成测试覆盖率提升切片“React Settings Hook 请求与凭据行为覆盖” | 新增系统设置 Hook 的成功加载/保存、模型发现失败、凭据前端校验、后端拒绝和初始读取失败测试;前端 213 个测试、类型检查、注释门禁通过,全源语句覆盖率提升至 28.96%;真实重新登录和浏览器页面重载仍只验证可控响应与调度,不伪造真实会话 | 下一步:继续补齐 Accounts/Cards/Chat/Items/Notifications/Orders/Rules Hook 与页面行为测试 |
| 2026-08-15 | 完成测试覆盖率提升切片“React Orders Hook 查询与导入行为覆盖” | 新增订单查询 Hook 的分页加载、辅助数据映射、账号/商品名称解析测试;新增订单导入 Hook 的成功刷新关闭、文件格式校验和服务失败状态测试;前端 216 个测试、类型检查、注释门禁通过,全源语句覆盖率提升至 31.69%;真实订单平台数据仍不触网,仅验证可控接口契约与状态机 | 下一步:继续补齐 Accounts/Cards/Chat/Notifications/Rules Hook 与页面行为测试 |
| 2026-08-15 | 完成测试覆盖率提升切片“React Notifications Hook 渠道与 SMTP 行为覆盖” | 新增通知 Hook 的管理员/普通用户加载、渠道新建、启用切换、测试通知、SMTP 保存、表单校验、删除和失败提示测试;前端 219 个测试、类型检查、注释门禁通过,全源语句覆盖率提升至 34.81%;真实推送服务和账号绑定数据仍不触网,仅验证可控接口契约与状态机 | 下一步:继续补齐 Accounts/Cards/Chat/Rules Hook 与页面行为测试 |
| 2026-08-15 | 完成测试覆盖率提升切片“React Accounts Hook 账号与运行状态覆盖” | 新增账号数据 Hook 的账号详情/AI 配置并行加载、AI 失败隔离、账号详情失败和运行状态轮询测试;前端 221 个测试、类型检查、注释门禁通过,全源语句覆盖率提升至 36.02%;真实账号凭证、远端资料和平台运行状态仍不触网,仅验证可控接口契约与生命周期清理 | 下一步:继续补齐 Cards/Chat/Items/Rules Hook 与页面行为测试 |
@@ -0,0 +1,83 @@
// @vitest-environment jsdom
import { renderHook, waitFor } from '@testing-library/react';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import type { AccountDetail } from '../../../types';
import { getAccountDetails, getAccountRuntimeStatuses, getAllAISettings } from './api';
import { useAccountsData } from './hooks';
vi.mock('./api', /* accountsApiMockFactory 提供账号列表 Hook 的确定性 API 替身。 */ () => ({
getAccountDetails: vi.fn(),
getAccountRuntimeStatuses: vi.fn(),
getAllAISettings: vi.fn(),
}));
// getDetailsMock 是账号详情请求的可控替身。
const getDetailsMock = vi.mocked(getAccountDetails);
// getRuntimeMock 是账号运行状态轮询请求的可控替身。
const getRuntimeMock = vi.mocked(getAccountRuntimeStatuses);
// getAISettingsMock 是账号 AI 配置请求的可控替身。
const getAISettingsMock = vi.mocked(getAllAISettings);
// accountFixture 是账号列表 Hook 测试使用的基础账号对象。
const accountFixture: AccountDetail = { id: 'account-1', enabled: true, auto_confirm: false, remark: '测试账号' };
describe('useAccountsData', /* 当前回调处理账号详情、AI 配置和运行状态轮询。 */ () => {
beforeEach(/* 当前回调重置账号 API 替身和日志输出。 */ () => {
vi.clearAllMocks();
getDetailsMock.mockResolvedValue([accountFixture]);
getAISettingsMock.mockResolvedValue({ 'account-1': { ai_enabled: true, max_discount_percent: 20, max_discount_amount: 30, max_bargain_rounds: 4, custom_prompts: '提示词' } } as never);
getRuntimeMock.mockResolvedValue({ 'account-1': { state: 'online', connected: true, failures: 0, message: '在线', updated_at: '2026-08-15T00:00:00Z' } });
vi.spyOn(console, 'error').mockImplementation(
// errorImplementation 屏蔽轮询失败测试中的日志输出。
() => undefined,
);
});
test('加载账号并合并 AI 与运行状态', /* 当前回调验证账号列表成功加载路径。 */ async () => {
// hook 是账号数据 Hook 的渲染结果。
const hook = renderHook(
// accountsHookFactory 创建账号数据 Hook。
() => useAccountsData(),
);
await waitFor(
// loadingAssertion 等待账号详情和 AI 配置请求完成。
() => expect(hook.result.current.loading).toBe(false),
);
await waitFor(
// runtimeAssertion 等待首次运行状态轮询完成。
() => expect(getRuntimeMock).toHaveBeenCalled(),
);
expect(hook.result.current.accounts[0]).toMatchObject({ ai_enabled: true, max_discount_percent: 20, runtime_state: 'online', runtime_connected: true });
hook.unmount();
});
test('AI 配置失败不阻断账号列表,详情失败则结束加载', /* 当前回调验证两类并行请求的错误隔离。 */ async () => {
getAISettingsMock.mockRejectedValueOnce(new Error('AI 服务失败'));
// aiHook 是 AI 配置失败场景的账号数据 Hook 渲染结果。
const aiHook = renderHook(
// aiHookFactory 创建 AI 配置失败场景的 Hook。
() => useAccountsData(),
);
await waitFor(
// loadingAssertion 等待 AI 失败但账号详情成功的结果。
() => expect(aiHook.result.current.loading).toBe(false),
);
expect(aiHook.result.current.accounts).toHaveLength(1);
expect(aiHook.result.current.accounts[0].ai_enabled).toBe(false);
aiHook.unmount();
getDetailsMock.mockRejectedValueOnce(new Error('账号服务失败'));
// detailsHook 是账号详情失败场景的账号数据 Hook 渲染结果。
const detailsHook = renderHook(
// detailsHookFactory 创建账号详情失败场景的 Hook。
() => useAccountsData(),
);
await waitFor(
// loadingAssertion 等待账号详情失败状态收束。
() => expect(detailsHook.result.current.loading).toBe(false),
);
expect(detailsHook.result.current.accounts).toEqual([]);
expect(console.error).toHaveBeenCalled();
detailsHook.unmount();
});
});