修复聊天状态同步与验证码验证链路

This commit is contained in:
Christ committed 2026-08-18 01:15:48 +08:00
1 parent 3fdace9492
commit 55904cc409
38 files changed
+884 -139

No files matched your search

+7 -1
View File
@@ -438,7 +438,9 @@ describe('useChat', /* 当前回调处理聊天加载、分页、发送和实时
// unknownMessage 是不在当前联系人列表中的实时消息。
const unknownMessage = { ...messageFixture, chat_id: 'chat-unknown', message_key: 'message-unknown', content: '未知会话消息' };
getSessionPageMock.mockResolvedValueOnce({ sessions: [sessionFixture], has_more: false, next_cursor: undefined });
// unknownSession 是刷新接口返回的新会话,必须自动出现在联系人列表中。
const unknownSession = { ...sessionFixture, chat_id: 'chat-unknown', buyer_id: 'buyer-unknown', last_message: '未知会话消息', last_message_at: 9 };
getSessionPageMock.mockResolvedValueOnce({ sessions: [unknownSession, sessionFixture], has_more: false, next_cursor: undefined });
await act(
// unknownMessageAction 触发未知会话的联系人刷新。
() => latestSocket?.onmessage?.({ data: JSON.stringify({ message: unknownMessage }) }),
@@ -447,6 +449,10 @@ describe('useChat', /* 当前回调处理聊天加载、分页、发送和实时
// reloadAssertion 等待未知会话触发联系人刷新。
() => expect(getSessionPageMock).toHaveBeenCalledWith('account-1', undefined, expect.objectContaining({ signal: expect.any(AbortSignal) }), true),
);
await waitFor(
// unknownSessionAssertion 等待实时事件关联的新会话写入联系人列表。
() => expect(hook.result.current.activeSessions).toContainEqual(unknownSession),
);
await act(
// emptyMessageAction 触发无消息载荷并保持状态稳定。
() => latestSocket?.onmessage?.({ data: JSON.stringify({}) }),
+6 -3
View File
@@ -4,6 +4,7 @@ import {
Search, Send, Smile, UserRound, Wifi, WifiOff,
} from 'lucide-react';
import { useChat } from '../hooks';
import { unreadBadgeClassName, unreadBadgeLabel } from '../state';
// Chat 展示实时会话、消息分页和消息发送界面。
const Chat: React.FC = () => {
@@ -38,6 +39,8 @@ const Chat: React.FC = () => {
const active = account.id === activeAccountID;
// unread unread,负责当前功能中的对应处理。
const unread = unreadForAccount(account.id);
// unreadLabel 保存账号未读徽标的展示文本。
const unreadLabel = unreadBadgeLabel(unread);
// online 响应当前用户操作(line)。
const online = account.runtime_state === 'online';
return (
@@ -45,7 +48,7 @@ const Chat: React.FC = () => {
className={`relative flex h-11 shrink-0 items-center gap-2 border-b-2 px-3 text-sm font-extrabold transition-colors ${active ? 'border-sky-500 text-sky-700' : 'border-transparent text-slate-500 hover:text-slate-900'}`}>
<span className={`h-2 w-2 rounded-full ${online ? 'bg-emerald-500' : 'bg-slate-300'}`} />
<span className="max-w-36 truncate">{account.nickname || account.remark || account.id}</span>
{unread > 0 && <span className="min-w-5 rounded-full bg-red-500 px-1.5 py-0.5 text-[10px] text-white">{unread > 99 ? '99+' : unread}</span>}
{unread > 0 && <span aria-label={`未读消息 ${unreadLabel} 条`} className={unreadBadgeClassName(unread)}>{unreadLabel}</span>}
</button>
);
})}
@@ -91,7 +94,7 @@ const Chat: React.FC = () => {
</div>
<div className="mt-1 flex items-center gap-2">
<span className="truncate text-xs text-slate-500">{session.last_message || '暂无消息'}</span>
{session.unread_count > 0 && <span className="ml-auto min-w-5 shrink-0 rounded-full bg-red-500 px-1.5 py-0.5 text-center text-[10px] font-bold text-white">{session.unread_count}</span>}
{session.unread_count > 0 && <span aria-label={`未读消息 ${unreadBadgeLabel(session.unread_count)} 条`} className={`ml-auto ${unreadBadgeClassName(session.unread_count)}`}>{unreadBadgeLabel(session.unread_count)}</span>}
</div>
{session.item_title && <div className="mt-1.5 truncate text-[10px] font-medium text-sky-700">商品 · {session.item_title}</div>}
</div>
@@ -160,7 +163,7 @@ const Chat: React.FC = () => {
)}
<div className="mt-1 flex items-center gap-1 text-[10px] text-slate-400">
{messageTime(message.sent_at)}
{outgoing && (message.status === 'failed' ? <AlertCircle className="h-3 w-3 text-red-500" /> : message.status === 'sent' ? <CheckCheck className="h-3 w-3 text-sky-500" /> : <Check className="h-3 w-3" />)}
{outgoing && (message.status === 'failed' ? <AlertCircle className="h-3 w-3 text-red-500" aria-label="发送失败" /> : message.read_status === 2 ? <CheckCheck className="h-3 w-3 text-sky-500" aria-label="对方已读" /> : message.status === 'sent' ? <Check className="h-3 w-3 text-sky-500" aria-label="已发送未读" /> : <Check className="h-3 w-3" aria-label="发送中" />)}
</div>
</div>
{outgoing && <div className="h-9 w-9 shrink-0 overflow-hidden rounded-full bg-sky-100 ring-2 ring-white">
+13 -1
View File
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest';
import type { ChatMessage, ChatSession } from '../../../shared/api-contract';
import { collectChatReadReceipts, filterChatSessions, formatClock, isChatAbortError, isCurrentChatRequest, mergeLiveMessage, mergeOlderMessages, messageTime } from './state';
import { collectChatReadReceipts, filterChatSessions, formatClock, isChatAbortError, isCurrentChatRequest, mergeLiveMessage, mergeOlderMessages, messageTime, unreadBadgeClassName, unreadBadgeLabel } from './state';
// sessionFixture 是覆盖搜索、未读筛选和联系人隔离的最小会话数据。
const sessionFixture: ChatSession[] = [
@@ -62,3 +62,15 @@ test('Chat 状态工具覆盖追加消息、搜索字段和时间格式化',
expect(formatClock(Date.now() - 86_400_000)).toMatch(/\d{2}\/\d{2}/);
expect(messageTime(Date.now())).toMatch(/\d{2}\/\d{2}/);
});
test('Chat 未读徽标为单数字保留正圆尺寸,多数字才横向扩展',
// 未读徽标测试验证账号页签和会话列表共享的形状边界。
() => {
expect(unreadBadgeLabel(1)).toBe('1');
expect(unreadBadgeLabel(99)).toBe('99');
expect(unreadBadgeLabel(100)).toBe('99+');
expect(unreadBadgeClassName(1)).toContain('w-5');
expect(unreadBadgeClassName(1)).not.toContain('min-w-5');
expect(unreadBadgeClassName(10)).toContain('min-w-5');
expect(unreadBadgeClassName(99)).toContain('min-w-5');
});
+16
View File
@@ -1,6 +1,22 @@
import type { ChatMessage, ChatSession } from '../../../shared/api-contract';
import type { ChatReadReceipt } from './types';
/** 将未读数规范为徽标可展示的文本,超过两位数时统一显示 99+。 */
export const unreadBadgeLabel = (count: number): string => {
// normalized 保存排除 NaN、负数与小数后的未读数量。
const normalized = Number.isFinite(count) ? Math.max(0, Math.floor(count)) : 0;
return normalized > 99 ? '99+' : String(normalized);
};
/** 根据未读文本长度返回稳定徽标尺寸,单数字必须为正圆,双数字及 99+ 才可横向扩展。 */
export const unreadBadgeClassName = (count: number): string => {
// normalized 保存用于区分单数字与多数字徽标的规范未读数量。
const normalized = Number.isFinite(count) ? Math.max(0, Math.floor(count)) : 0;
return normalized < 10
? 'inline-flex h-5 w-5 shrink-0 items-center justify-center rounded-full bg-red-500 text-[10px] font-bold leading-none text-white'
: 'inline-flex h-5 min-w-5 shrink-0 items-center justify-center rounded-full bg-red-500 px-1.5 text-[10px] font-bold leading-none text-white';
};
/** 将可确认的普通入站消息转换为平台已读回执。 */
export const collectChatReadReceipts = (messages: ChatMessage[], chatID: string): ChatReadReceipt[] => messages
.filter(/* currentMessage 只保留平台可接受的普通入站消息。 */ currentMessage => (
+3
View File
@@ -11,6 +11,9 @@ export default {
"./index.html",
"./*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
// 阶段七后的页面和共享 UI 位于 app/shared,必须参与生产 utility 扫描。
"./app/**/*.{js,ts,jsx,tsx}",
"./shared/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {
+17
View File
@@ -0,0 +1,17 @@
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { expect, test } from 'vitest';
// tailwindConfigPath 指向生产构建读取的 Tailwind 配置,测试以文本方式审计其静态扫描范围。
const tailwindConfigPath = resolve(__dirname, 'tailwind.config.js');
/** verifyTailwindFeatureSourceGlobs 验证阶段七迁移后的 app/shared 源码不会被 Tailwind 生产构建遗漏。 */
function verifyTailwindFeatureSourceGlobs(): void {
// configSource 是当前 Tailwind 配置原文,避免 TypeScript 为 JavaScript 配置要求声明文件。
const configSource = readFileSync(tailwindConfigPath, 'utf8');
expect(configSource).toContain('"./app/**/*.{js,ts,jsx,tsx}"');
expect(configSource).toContain('"./shared/**/*.{js,ts,jsx,tsx}"');
}
test('Tailwind scans feature and shared UI source trees', verifyTailwindFeatureSourceGlobs);