274 lines
13 KiB
TypeScript
274 lines
13 KiB
TypeScript
// @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;
|
|
|
|
// latestSocket 保存当前测试创建的 WebSocket 替身。
|
|
let latestSocket: {
|
|
// close 是关闭 WebSocket 的替身方法。
|
|
close: ReturnType<typeof vi.fn>;
|
|
// onopen 是 WebSocket 建立连接回调。
|
|
onopen: (() => void) | null;
|
|
// onmessage 是 WebSocket 消息回调。
|
|
onmessage: ((event: { /* data 是 WebSocket 事件数据。 */ data: string }) => void) | null;
|
|
// onclose 是 WebSocket 断开回调。
|
|
onclose: (() => void) | null;
|
|
// onerror 是 WebSocket 错误回调。
|
|
onerror: (() => void) | null;
|
|
} | null = null;
|
|
|
|
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() {
|
|
// socket 是当前 WebSocket 连接的可控替身。
|
|
const socket = { close: vi.fn(), onopen: null, onmessage: null, onclose: null, onerror: null };
|
|
latestSocket = socket;
|
|
return socket;
|
|
},
|
|
);
|
|
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();
|
|
});
|
|
|
|
test('WebSocket 连接和消息事件更新聊天状态', /* 当前回调验证实时连接和消息回调边界。 */ async () => {
|
|
// hook 是实时连接场景的聊天 Hook 渲染结果。
|
|
const hook = renderHook(
|
|
// socketHookFactory 创建实时连接场景的聊天 Hook。
|
|
() => useChat(),
|
|
);
|
|
await waitFor(
|
|
// loadingAssertion 等待聊天初始数据加载完成。
|
|
() => expect(hook.result.current.loading).toBe(false),
|
|
);
|
|
await waitFor(
|
|
// activeChatAssertion 等待实时消息目标会话选中。
|
|
() => expect(hook.result.current.activeChatID).toBe('chat-1'),
|
|
);
|
|
expect(latestSocket).not.toBeNull();
|
|
await act(
|
|
// openAction 触发 WebSocket 建立连接事件。
|
|
() => latestSocket?.onopen?.(),
|
|
);
|
|
expect(hook.result.current.liveState).toBe('online');
|
|
// incomingMessage 是实时 WebSocket 推送的入站消息。
|
|
const incomingMessage = { ...messageFixture, id: 3, message_key: 'message-3', sent_at: 3, content: '实时消息' };
|
|
await act(
|
|
// messageAction 触发合法的实时消息事件。
|
|
() => latestSocket?.onmessage?.({ data: JSON.stringify({ message: incomingMessage }) }),
|
|
);
|
|
expect(hook.result.current.messages).toContainEqual(incomingMessage);
|
|
expect(markReadMock).toHaveBeenCalledWith('account-1', 'chat-1');
|
|
await act(
|
|
// invalidMessageAction 触发非法消息帧并保持状态稳定。
|
|
() => latestSocket?.onmessage?.({ data: '{broken' }),
|
|
);
|
|
await act(
|
|
// closeAction 触发 WebSocket 断开事件。
|
|
() => latestSocket?.onclose?.(),
|
|
);
|
|
expect(hook.result.current.liveState).toBe('offline');
|
|
hook.unmount();
|
|
});
|
|
|
|
test('联系人、消息、历史分页和图片发送失败时保留可恢复错误', /* 当前回调验证聊天业务请求错误分支。 */ async () => {
|
|
// hook 是聊天请求错误场景的 Hook 渲染结果。
|
|
const hook = renderHook(
|
|
// errorHookFactory 创建聊天请求错误场景的 Hook。
|
|
() => useChat(),
|
|
);
|
|
await waitFor(
|
|
// loadingAssertion 等待聊天初始数据加载完成。
|
|
() => expect(hook.result.current.loading).toBe(false),
|
|
);
|
|
await waitFor(
|
|
// activeChatAssertion 等待默认会话选中。
|
|
() => expect(hook.result.current.activeChatID).toBe('chat-1'),
|
|
);
|
|
|
|
getSessionPageMock.mockRejectedValueOnce(new Error('联系人读取失败'));
|
|
await act(
|
|
// contactsErrorAction 触发联系人刷新错误。
|
|
async () => hook.result.current.reloadSessions('account-1'),
|
|
);
|
|
expect(hook.result.current.error).toBe('联系人读取失败');
|
|
|
|
getMessagePageMock.mockRejectedValueOnce(new Error('消息读取失败'));
|
|
await act(
|
|
// chatSwitchAction 切换到不存在会话以触发消息读取错误。
|
|
() => hook.result.current.setActiveChatID('chat-2'),
|
|
);
|
|
await waitFor(
|
|
// messageErrorAssertion 等待消息读取错误收口。
|
|
() => expect(hook.result.current.error).toBe('消息读取失败'),
|
|
);
|
|
|
|
await act(
|
|
// chatRestoreAction 恢复默认会话以继续验证历史分页。
|
|
() => hook.result.current.setActiveChatID('chat-1'),
|
|
);
|
|
await waitFor(
|
|
// restoredMessageAssertion 等待默认会话消息恢复。
|
|
() => expect(hook.result.current.messagesLoading).toBe(false),
|
|
);
|
|
getMessagePageMock.mockRejectedValueOnce(new Error('历史消息失败'));
|
|
await act(
|
|
// olderErrorAction 触发历史消息分页错误。
|
|
async () => hook.result.current.loadOlderMessages(),
|
|
);
|
|
expect(hook.result.current.error).toBe('历史消息失败');
|
|
|
|
sendImageMock.mockRejectedValueOnce(new Error('图片发送失败'));
|
|
await act(
|
|
// imageErrorAction 触发图片发送错误。
|
|
async () => hook.result.current.handleImage(new File(['image'], 'error.png', { type: 'image/png' })),
|
|
);
|
|
expect(hook.result.current.error).toBe('图片发送失败');
|
|
expect(hook.result.current.retryAvailable).toBe(true);
|
|
sendImageMock.mockResolvedValueOnce({ message: sentMessageFixture });
|
|
await act(
|
|
// imageRetryAction 重试最近一次图片发送。
|
|
async () => hook.result.current.retrySend(),
|
|
);
|
|
expect(sendImageMock).toHaveBeenCalledTimes(2);
|
|
hook.unmount();
|
|
});
|
|
});
|