render.test.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { describe, expect, it } from 'vitest';
  2. import { renderToStaticMarkup } from 'react-dom/server';
  3. import { ChatMessageView, Markdownish, TraceChips, VerificationPanel } from '../src/components/ChatMessageView';
  4. import App from '../src/App';
  5. import { WorkspaceCreateDialog } from '../src/components/WorkspaceCreateDialog';
  6. import { TraceDrawer } from '../src/components/TraceDrawer';
  7. import { FilesPage, parseDeepLink } from '../src/pages/FilesPage';
  8. import type { ChatMessage } from '../src/api/types';
  9. function message(overrides: Partial<ChatMessage> = {}): ChatMessage {
  10. return {
  11. id: 'msg_1',
  12. session_id: 'sess_1',
  13. seq: 1,
  14. role: 'assistant',
  15. content: '2025 年 1 月 MLCC 进口额为 400316.13 美元。',
  16. tool_calls: [],
  17. tool_results: [],
  18. verification: null,
  19. created_at: '2026-11-17T10:00:00+08:00',
  20. ...overrides,
  21. };
  22. }
  23. describe('组件渲染(服务端渲染,不需要浏览器)', () => {
  24. it('App 外壳能渲染出导航与品牌', () => {
  25. const html = renderToStaticMarkup(<App />);
  26. expect(html).toContain('海关问数 Agent');
  27. expect(html).toContain('对话问数');
  28. expect(html).toContain('数据处理');
  29. expect(html).toContain('文件');
  30. expect(html).toContain('系统');
  31. });
  32. it('用户消息渲染为气泡', () => {
  33. const html = renderToStaticMarkup(<ChatMessageView message={message({ role: 'user', content: '你好' })} />);
  34. expect(html).toContain('message-user');
  35. expect(html).toContain('你好');
  36. });
  37. it('助手消息带工具卡片', () => {
  38. const html = renderToStaticMarkup(
  39. <ChatMessageView
  40. message={message({
  41. tool_calls: [{ id: 'call_1', name: 'data_read', arguments: '{"object_id":"obj_1"}' }],
  42. tool_results: [{ call_id: 'call_1', tool: 'data_read', ok: true, rows: 12, duration_ms: 34, summary: '12 行' }],
  43. })}
  44. />,
  45. );
  46. expect(html).toContain('读取统计表');
  47. expect(html).toContain('12 行');
  48. });
  49. it('核验面板列出每个数字的状态', () => {
  50. const html = renderToStaticMarkup(
  51. <VerificationPanel
  52. verification={{
  53. verdict: 'flag',
  54. claims: [
  55. { raw: '400316.13', value: 400316.13, display: '400316.13', kind: 'money', severity: 'strict', status: 'verified' },
  56. { raw: '14.92万', value: 149200, display: '14.92万', kind: 'money', severity: 'strict', status: 'flagged', reason: '台账中无精确来源,但已明确标注为估算' },
  57. ],
  58. verified: 1,
  59. flagged: 1,
  60. unverified: 0,
  61. exempt: 0,
  62. reference: 0,
  63. total: 2,
  64. strictTotal: 2,
  65. strictClaims: [],
  66. summary: '通过但需提示:共 2 个数字,必审 2 个,已溯源 1 个,估算 1 个',
  67. chips: [],
  68. }}
  69. />,
  70. );
  71. expect(html).toContain('含估算数字');
  72. expect(html).toContain('已溯源');
  73. expect(html).toContain('估算');
  74. });
  75. it('溯源 chip 带 title 便于悬停查看来源', () => {
  76. const html = renderToStaticMarkup(
  77. <TraceChips
  78. chips={[{ value: '400316.13', display: '400316.13', status: 'verified', call_id: 'call_1', object_id: 'obj_1', table: '1.0-3.0_MLCC', row_index: 0, field: '实际金额(美元)' }]}
  79. />,
  80. );
  81. expect(html).toContain('chip-verified');
  82. expect(html).toContain('call_id=call_1');
  83. });
  84. it('Markdown 渲染粗体、行内代码、列表与表格', () => {
  85. const html = renderToStaticMarkup(
  86. <Markdownish
  87. text={['**结论**:金额 400316.13', '', '- 第一条', '1. 有序项', '', '| 类目 | 金额 |', '|---|---|', '| MLCC | 400316.13 |'].join('\n')}
  88. />,
  89. );
  90. expect(html).toContain('<strong>结论</strong>');
  91. expect(html).toContain('md-li');
  92. expect(html).toContain('md-table');
  93. expect(html).toContain('MLCC');
  94. });
  95. });
  96. describe('溯源抽屉与深链', () => {
  97. it('收起状态(chip 为 null)不渲染任何东西', () => {
  98. const html = renderToStaticMarkup(<TraceDrawer sessionId="s1" chip={null} onClose={() => {}} onOpenRow={() => {}} />);
  99. expect(html).toBe('');
  100. });
  101. it('打开时展示数字、状态与加载态', () => {
  102. const html = renderToStaticMarkup(
  103. <TraceDrawer
  104. sessionId="s1"
  105. chip={{ value: '400316.13', display: '400316.13', status: 'verified', call_id: 'call_1', object_id: 'obj_1', table: '1.0-3.0_MLCC', row_index: 0, field: '实际金额(美元)' }}
  106. onClose={() => {}}
  107. onOpenRow={() => {}}
  108. />,
  109. );
  110. expect(html).toContain('数字溯源');
  111. expect(html).toContain('400316.13');
  112. expect(html).toContain('已溯源');
  113. expect(html).toContain('实际金额(美元)');
  114. expect(html).toContain('drawer');
  115. });
  116. it('parseDeepLink 解析从 chip 跳过来的地址', () => {
  117. expect(parseDeepLink('#/files?object=obj_ab12&table=1.0-3.0_MLCC&row=7')).toEqual({
  118. objectId: 'obj_ab12',
  119. table: '1.0-3.0_MLCC',
  120. row: 7,
  121. });
  122. expect(parseDeepLink('#/files')).toBeNull();
  123. expect(parseDeepLink('#/files?object=obj_x&table=raw')).toEqual({ objectId: 'obj_x', table: 'raw', row: null });
  124. expect(parseDeepLink('#/files?table=raw')).toBeNull();
  125. expect(parseDeepLink('#/files?object=obj_x&row=abc')).toEqual({ objectId: 'obj_x', table: 'raw', row: null });
  126. });
  127. it('文件页在没有工作区时给出提示(不崩)', () => {
  128. const html = renderToStaticMarkup(<FilesPage workspace={null} onToast={() => {}} onChanged={() => {}} />);
  129. expect(html).toContain('请先在左侧选择一个工作区');
  130. });
  131. });
  132. it('截断查询在工具卡片外层可见,不需要展开参数', () => {
  133. const html=renderToStaticMarkup(<ChatMessageView message={message({
  134. tool_calls:[{id:'bounded',name:'data_read',arguments:'{}'}],
  135. tool_results:[{call_id:'bounded',tool:'data_read',ok:true,rows:5,duration_ms:2,summary:'5行',truncated:true,warning:'仅返回部分结果,不能作为全量合计'}],
  136. })}/>);
  137. expect(html).toContain('仅返回部分结果,不能作为全量合计');
  138. expect(html).toContain('role="status"');
  139. });
  140. it('工作区创建使用应用内表单,空白名和提交中阻止重复提交', () => {
  141. const render=(open:boolean,name:string,pending:boolean)=>renderToStaticMarkup(<WorkspaceCreateDialog open={open} name={name} pending={pending} onNameChange={()=>{}} onSubmit={()=>{}} onClose={()=>{}}/>);
  142. expect(render(false,'test',false)).toBe('');
  143. const ready=render(true,'界面回归',false);
  144. expect(ready).toContain('role="dialog"');
  145. expect(ready).toContain('工作区名称');
  146. expect(render(true,' ',false)).toMatch(/type="submit"[^>]*disabled/);
  147. expect(render(true,'test',true)).toContain('创建中');
  148. });