Files
NianToB/tests/unit/assistant-output-sanitizer.test.ts
2026-05-12 19:44:44 +08:00

51 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
});
});