render.test.tsx 5.3 KB

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