| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- 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 { TraceDrawer } from '../src/components/TraceDrawer';
- import { FilesPage, parseDeepLink } from '../src/pages/FilesPage';
- import type { ChatMessage } from '../src/api/types';
- function message(overrides: Partial<ChatMessage> = {}): 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(<App />);
- expect(html).toContain('海关问数 Agent');
- expect(html).toContain('对话问数');
- expect(html).toContain('数据处理');
- expect(html).toContain('文件');
- expect(html).toContain('系统');
- });
- it('用户消息渲染为气泡', () => {
- const html = renderToStaticMarkup(<ChatMessageView message={message({ role: 'user', content: '你好' })} />);
- expect(html).toContain('message-user');
- expect(html).toContain('你好');
- });
- it('助手消息带工具卡片', () => {
- const html = renderToStaticMarkup(
- <ChatMessageView
- message={message({
- tool_calls: [{ id: 'call_1', name: 'data_read', arguments: '{"object_id":"obj_1"}' }],
- tool_results: [{ call_id: 'call_1', tool: 'data_read', ok: true, rows: 12, duration_ms: 34, summary: '12 行' }],
- })}
- />,
- );
- expect(html).toContain('读取统计表');
- expect(html).toContain('12 行');
- });
- it('核验面板列出每个数字的状态', () => {
- const html = renderToStaticMarkup(
- <VerificationPanel
- verification={{
- verdict: 'flag',
- claims: [
- { raw: '400316.13', value: 400316.13, display: '400316.13', kind: 'money', status: 'verified' },
- { raw: '14.92万', value: 149200, display: '14.92万', kind: 'money', status: 'flagged', reason: '台账中无精确来源,但已明确标注为估算' },
- ],
- verified: 1,
- flagged: 1,
- unverified: 0,
- exempt: 0,
- total: 2,
- summary: '通过但需提示:共 2 个数字,已溯源 1 个,估算 1 个',
- chips: [],
- }}
- />,
- );
- expect(html).toContain('含估算数字');
- expect(html).toContain('已溯源');
- expect(html).toContain('估算');
- });
- it('溯源 chip 带 title 便于悬停查看来源', () => {
- const html = renderToStaticMarkup(
- <TraceChips
- 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: '实际金额(美元)' }]}
- />,
- );
- expect(html).toContain('chip-verified');
- expect(html).toContain('call_id=call_1');
- });
- it('Markdown 渲染粗体、行内代码、列表与表格', () => {
- const html = renderToStaticMarkup(
- <Markdownish
- text={['**结论**:金额 400316.13', '', '- 第一条', '1. 有序项', '', '| 类目 | 金额 |', '|---|---|', '| MLCC | 400316.13 |'].join('\n')}
- />,
- );
- expect(html).toContain('<strong>结论</strong>');
- expect(html).toContain('md-li');
- expect(html).toContain('md-table');
- expect(html).toContain('MLCC');
- });
- });
- describe('溯源抽屉与深链', () => {
- it('收起状态(chip 为 null)不渲染任何东西', () => {
- const html = renderToStaticMarkup(<TraceDrawer sessionId="s1" chip={null} onClose={() => {}} onOpenRow={() => {}} />);
- expect(html).toBe('');
- });
- it('打开时展示数字、状态与加载态', () => {
- const html = renderToStaticMarkup(
- <TraceDrawer
- sessionId="s1"
- 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: '实际金额(美元)' }}
- onClose={() => {}}
- 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(<FilesPage workspace={null} onToast={() => {}} onChanged={() => {}} />);
- expect(html).toContain('请先在左侧选择一个工作区');
- });
- });
|