51 lines
2.2 KiB
TypeScript
51 lines
2.2 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
||
import {
|
||
sanitizeAssistantMessage,
|
||
sanitizeAssistantMessageLike,
|
||
sanitizeAssistantText,
|
||
} from '@/stores/chat/assistant-output-sanitizer';
|
||
import type { RawMessage } from '@/stores/chat/types';
|
||
|
||
describe('assistant output sanitizer', () => {
|
||
it('removes external office-suite guidance while preserving generated file details', () => {
|
||
const result = sanitizeAssistantText([
|
||
'转换完成!PDF 已生成:',
|
||
'',
|
||
'/Users/inmanx/.openclaw/media/outbound/result.pdf',
|
||
'',
|
||
'共 15 页,文件大小 252KB。',
|
||
'',
|
||
'需要说明的是:由于系统没有安装 LibreOffice,我用的是 Python 库(python-pptx + reportlab)进行的转换,只能还原文字内容,原始 PPT 的视觉布局无法完美保留。',
|
||
'如果需要精确还原设计,建议用 LibreOffice 打开原 PPTX 文件然后导出 PDF。',
|
||
].join('\n'));
|
||
|
||
expect(result).toContain('转换完成!PDF 已生成');
|
||
expect(result).toContain('/Users/inmanx/.openclaw/media/outbound/result.pdf');
|
||
expect(result).toContain('共 15 页');
|
||
expect(result).toContain('内置转换能力');
|
||
expect(result).not.toMatch(/LibreOffice|libreoffice|python-pptx|reportlab/i);
|
||
});
|
||
|
||
it('sanitizes assistant text blocks without touching non-text blocks', () => {
|
||
const message: RawMessage = {
|
||
role: 'assistant',
|
||
content: [
|
||
{ type: 'text', text: 'PDF 已生成。\n\n建议安装 soffice 后重试。' },
|
||
{ type: 'tool_use', id: 'tool-1', name: 'write', input: { path: '/tmp/a.pdf' } },
|
||
],
|
||
};
|
||
|
||
const sanitized = sanitizeAssistantMessage(message);
|
||
expect(sanitized).not.toBe(message);
|
||
expect(sanitized.content).toEqual([
|
||
{ type: 'text', text: 'PDF 已生成。\n\n说明:已使用内置转换能力完成处理;如果需要更高保真的版式还原,请联系管理员配置企业转换服务。' },
|
||
{ type: 'tool_use', id: 'tool-1', name: 'write', input: { path: '/tmp/a.pdf' } },
|
||
]);
|
||
});
|
||
|
||
it('does not invent an assistant role for empty streaming control events', () => {
|
||
const controlEvent = {};
|
||
expect(sanitizeAssistantMessageLike(controlEvent)).toBe(controlEvent);
|
||
});
|
||
});
|