import { describe, expect, it } from 'vitest'; import { renderToStaticMarkup } from 'react-dom/server'; import { ChatMessageView, Markdownish, TraceChips, VerificationPanel } from '../src/components/ChatMessageView'; import App from '../src/App'; import { WorkspaceCreateDialog } from '../src/components/WorkspaceCreateDialog'; import { TraceDrawer } from '../src/components/TraceDrawer'; import { FilesPage, parseDeepLink } from '../src/pages/FilesPage'; import type { ChatMessage } from '../src/api/types'; function message(overrides: Partial = {}): ChatMessage { return { id: 'msg_1', session_id: 'sess_1', seq: 1, role: 'assistant', content: '2025 年 1 月 MLCC 进口额为 400316.13 美元。', tool_calls: [], tool_results: [], verification: null, created_at: '2026-11-17T10:00:00+08:00', ...overrides, }; } describe('组件渲染(服务端渲染,不需要浏览器)', () => { it('App 外壳能渲染出导航与品牌', () => { const html = renderToStaticMarkup(); expect(html).toContain('海关问数 Agent'); expect(html).toContain('对话问数'); expect(html).toContain('数据处理'); expect(html).toContain('文件'); expect(html).toContain('系统'); }); it('用户消息渲染为气泡', () => { const html = renderToStaticMarkup(); expect(html).toContain('message-user'); expect(html).toContain('你好'); }); it('助手消息带工具卡片', () => { const html = renderToStaticMarkup( , ); expect(html).toContain('读取统计表'); expect(html).toContain('12 行'); }); it('核验面板列出每个数字的状态', () => { const html = renderToStaticMarkup( , ); expect(html).toContain('含估算数字'); expect(html).toContain('已溯源'); expect(html).toContain('估算'); }); it('溯源 chip 带 title 便于悬停查看来源', () => { const html = renderToStaticMarkup( , ); expect(html).toContain('chip-verified'); expect(html).toContain('call_id=call_1'); }); it('Markdown 渲染粗体、行内代码、列表与表格', () => { const html = renderToStaticMarkup( , ); expect(html).toContain('结论'); expect(html).toContain('md-li'); expect(html).toContain('md-table'); expect(html).toContain('MLCC'); }); }); describe('溯源抽屉与深链', () => { it('收起状态(chip 为 null)不渲染任何东西', () => { const html = renderToStaticMarkup( {}} onOpenRow={() => {}} />); expect(html).toBe(''); }); it('打开时展示数字、状态与加载态', () => { const html = renderToStaticMarkup( {}} onOpenRow={() => {}} />, ); expect(html).toContain('数字溯源'); expect(html).toContain('400316.13'); expect(html).toContain('已溯源'); expect(html).toContain('实际金额(美元)'); expect(html).toContain('drawer'); }); it('parseDeepLink 解析从 chip 跳过来的地址', () => { expect(parseDeepLink('#/files?object=obj_ab12&table=1.0-3.0_MLCC&row=7')).toEqual({ objectId: 'obj_ab12', table: '1.0-3.0_MLCC', row: 7, }); expect(parseDeepLink('#/files')).toBeNull(); expect(parseDeepLink('#/files?object=obj_x&table=raw')).toEqual({ objectId: 'obj_x', table: 'raw', row: null }); expect(parseDeepLink('#/files?table=raw')).toBeNull(); expect(parseDeepLink('#/files?object=obj_x&row=abc')).toEqual({ objectId: 'obj_x', table: 'raw', row: null }); }); it('文件页在没有工作区时给出提示(不崩)', () => { const html = renderToStaticMarkup( {}} onChanged={() => {}} />); expect(html).toContain('请先在左侧选择一个工作区'); }); }); it('截断查询在工具卡片外层可见,不需要展开参数', () => { const html=renderToStaticMarkup(); expect(html).toContain('仅返回部分结果,不能作为全量合计'); expect(html).toContain('role="status"'); }); it('工作区创建使用应用内表单,空白名和提交中阻止重复提交', () => { const render=(open:boolean,name:string,pending:boolean)=>renderToStaticMarkup({}} onSubmit={()=>{}} onClose={()=>{}}/>); expect(render(false,'test',false)).toBe(''); const ready=render(true,'界面回归',false); expect(ready).toContain('role="dialog"'); expect(ready).toContain('工作区名称'); expect(render(true,' ',false)).toMatch(/type="submit"[^>]*disabled/); expect(render(true,'test',true)).toContain('创建中'); });