补齐React Chat Hook加载与发送测试

This commit is contained in:
Christ committed 2026-08-15 10:33:17 +08:00
1 parent 53dc0d95fe
commit 121fbe7f48
2 files changed
+154

No files matched your search

@@ -652,3 +652,4 @@ npm --prefix frontend run build
| 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 与页面行为测试 |
| 2026-08-15 | 完成测试覆盖率提升切片“React Cards Hook 库存与批量操作覆盖” | 新增卡密库存 Hook 的成功/失败加载测试;新增批量操作 Hook 的追加预览、追加成功、批量创建成功和追加失败测试;前端 223 个测试、类型检查、注释门禁通过,全源语句覆盖率提升至 38.19%;真实卡密库存和外部 API 卡密仍不触网,仅验证可控接口契约与状态机 | 下一步:继续补齐 Chat/Items/Rules Hook 与页面行为测试 |
| 2026-08-15 | 完成测试覆盖率提升切片“React Chat Hook 加载、分页与发送覆盖” | 新增聊天 Hook 的账号/运行状态加载、会话与消息分页、已读标记、文字/图片发送、发送失败重试和 WebSocket 清理测试;前端 225 个测试、类型检查、注释门禁通过,全源语句覆盖率提升至 44.11%;真实聊天账号、外部平台消息和 WebSocket 服务仍不触网,仅验证可控接口契约与状态机 | 下一步:继续补齐 Items/Rules Hook、账号子模块 Hook 与页面行为测试 |
+153
View File
@@ -0,0 +1,153 @@
// @vitest-environment jsdom
import { act, renderHook, waitFor } from '@testing-library/react';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import type { AccountDetail, ChatMessage, ChatSession } from '../../../types';
import { getAccountDetails, getAccountRuntimeStatuses, getChatMessagePage, getChatSessionPage, markChatRead, sendChatImage, sendChatMessage } from './api';
import { useChat } from './hooks';
vi.mock('./api', /* chatApiMockFactory 提供聊天 Hook 的确定性 API 替身。 */ () => ({
getAccountDetails: vi.fn(),
getAccountRuntimeStatuses: vi.fn(),
getChatMessagePage: vi.fn(),
getChatSessionPage: vi.fn(),
markChatRead: vi.fn(),
sendChatImage: vi.fn(),
sendChatMessage: vi.fn(),
}));
// getDetailsMock 是聊天账号详情请求的可控替身。
const getDetailsMock = vi.mocked(getAccountDetails);
// getRuntimeMock 是聊天账号运行状态请求的可控替身。
const getRuntimeMock = vi.mocked(getAccountRuntimeStatuses);
// getMessagePageMock 是聊天消息分页请求的可控替身。
const getMessagePageMock = vi.mocked(getChatMessagePage);
// getSessionPageMock 是聊天会话分页请求的可控替身。
const getSessionPageMock = vi.mocked(getChatSessionPage);
// markReadMock 是聊天已读请求的可控替身。
const markReadMock = vi.mocked(markChatRead);
// sendImageMock 是聊天图片发送请求的可控替身。
const sendImageMock = vi.mocked(sendChatImage);
// sendMessageMock 是聊天文字发送请求的可控替身。
const sendMessageMock = vi.mocked(sendChatMessage);
// accountFixture 是聊天 Hook 使用的启用账号对象。
const accountFixture: AccountDetail = { id: 'account-1', enabled: true, auto_confirm: false, nickname: '测试账号' };
// sessionFixture 是聊天会话列表中的当前会话。
const sessionFixture: ChatSession = { account_id: 'account-1', chat_id: 'chat-1', buyer_id: 'buyer-1', buyer_name: '买家', item_title: '商品', last_message: '你好', last_message_at: 1, unread_count: 1 };
// messageFixture 是当前会话中的历史消息。
const messageFixture = { id: 1, account_id: 'account-1', chat_id: 'chat-1', message_key: 'message-1', direction: 'incoming', sender_id: 'buyer-1', sender_name: '买家', message_type: 'text', content: '你好', status: 'received', sent_at: 1 } as never as ChatMessage;
// sentMessageFixture 是文字发送成功后返回的消息。
const sentMessageFixture = { ...messageFixture, id: 2, message_key: 'message-2', direction: 'outgoing', content: '回复内容' } as ChatMessage;
describe('useChat', /* 当前回调处理聊天加载、分页、发送和实时连接状态。 */ () => {
beforeEach(/* 当前回调重置聊天 API 替身和 WebSocket。 */ () => {
vi.clearAllMocks();
getDetailsMock.mockResolvedValue([accountFixture]);
getRuntimeMock.mockResolvedValue({ 'account-1': { state: 'online', connected: true, failures: 0, updated_at: '2026-08-15T00:00:00Z' } });
getSessionPageMock.mockResolvedValue({ sessions: [sessionFixture], has_more: true, next_cursor: 2 });
getMessagePageMock.mockResolvedValue({ messages: [messageFixture], has_more: true, next_cursor: 2, session: sessionFixture });
markReadMock.mockResolvedValue({ success: true });
sendMessageMock.mockResolvedValue({ message: sentMessageFixture });
sendImageMock.mockResolvedValue({ message: sentMessageFixture });
// websocketFactory 是不连接真实服务器的 WebSocket 构造替身。
const websocketFactory = vi.fn(
// websocketConstructor 创建不连接真实服务器的 WebSocket 实例。
function websocketConstructor() {
return { close: vi.fn(), onopen: null, onmessage: null, onclose: null, onerror: null };
},
);
vi.stubGlobal('WebSocket', websocketFactory);
// localStorageStub 是聊天 Hook 记忆账号选择所需的浏览器存储替身。
Object.defineProperty(window, 'localStorage', { configurable: true, value: { getItem: vi.fn().mockReturnValue(''), setItem: vi.fn(), removeItem: vi.fn() } });
});
test('加载账号、会话和消息后可以发送文字与图片', /* 当前回调验证聊天 Hook 成功加载和发送路径。 */ async () => {
// hook 是聊天 Hook 的渲染结果。
const hook = renderHook(
// chatHookFactory 创建聊天 Hook。
() => useChat(),
);
await waitFor(
// loadingAssertion 等待账号和会话加载完成。
() => expect(hook.result.current.loading).toBe(false),
);
await waitFor(
// activeChatAssertion 等待默认会话被选中。
() => expect(hook.result.current.activeChatID).toBe('chat-1'),
);
await waitFor(
// messagesAssertion 等待当前会话消息加载完成。
() => expect(hook.result.current.messagesLoading).toBe(false),
);
expect(hook.result.current.accounts[0]).toMatchObject(accountFixture);
expect(hook.result.current.activeChatID).toBe('chat-1');
expect(hook.result.current.messages).toEqual([messageFixture]);
expect(markReadMock).toHaveBeenCalledWith('account-1', 'chat-1', expect.objectContaining({ signal: expect.any(AbortSignal) }));
await act(
// draftAction 写入文字消息草稿。
() => hook.result.current.setDraft('回复内容'),
);
await act(
// sendAction 提交文字消息。
async () => hook.result.current.handleSend(),
);
expect(sendMessageMock).toHaveBeenCalledWith(expect.objectContaining({ text: '回复内容', chat_id: 'chat-1' }), expect.objectContaining({ signal: expect.any(AbortSignal) }));
expect(hook.result.current.messages).toContainEqual(sentMessageFixture);
await act(
// imageAction 提交图片消息。
async () => hook.result.current.handleImage(new File(['image'], 'image.png', { type: 'image/png' })),
);
expect(sendImageMock).toHaveBeenCalledWith(expect.objectContaining({ chat_id: 'chat-1', image: expect.any(File) }), expect.objectContaining({ signal: expect.any(AbortSignal) }));
hook.unmount();
});
test('联系人分页和发送失败都提供可重试状态', /* 当前回调验证聊天分页和错误重试路径。 */ async () => {
// hook 是聊天失败场景的 Hook 渲染结果。
const hook = renderHook(
// failedChatHookFactory 创建聊天错误场景的 Hook。
() => useChat(),
);
await waitFor(
// loadingAssertion 等待错误场景的账号加载完成。
() => expect(hook.result.current.loading).toBe(false),
);
await waitFor(
// activeChatAssertion 等待错误场景的默认会话被选中。
() => expect(hook.result.current.activeChatID).toBe('chat-1'),
);
await waitFor(
// contactsAssertion 等待联系人分页标记生效。
() => expect(hook.result.current.hasMoreContacts).toBe(true),
);
await waitFor(
// messagesAssertion 等待错误场景的消息加载完成。
() => expect(hook.result.current.messagesLoading).toBe(false),
);
await act(
// contactsAction 请求更早的联系人。
async () => hook.result.current.loadMoreContacts(),
);
expect(getSessionPageMock).toHaveBeenCalledWith('account-1', undefined, expect.objectContaining({ signal: expect.any(AbortSignal) }), true);
sendMessageMock.mockRejectedValueOnce(new Error('发送失败'));
await act(
// draftAction 写入会失败的文字草稿。
() => hook.result.current.setDraft('失败消息'),
);
await act(
// failedSendAction 提交会失败的文字消息。
async () => hook.result.current.handleSend(),
);
expect(hook.result.current.error).toBe('发送失败');
expect(hook.result.current.retryAvailable).toBe(true);
sendMessageMock.mockResolvedValueOnce({ message: sentMessageFixture });
await act(
// retryAction 重试最近一次失败的文字消息。
async () => hook.result.current.retrySend(),
);
expect(sendMessageMock).toHaveBeenCalledTimes(2);
hook.unmount();
});
});