response-cleaner.test.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**
  2. * LLM 响应清洗器 单元测试
  3. *
  4. * 测试 response-cleaner.ts 的所有边界情况。
  5. * 这是 LLM 调用后的最后一道防线,如果这里出问题,DB 会存垃圾。
  6. */
  7. import { describe, it, expect } from 'vitest';
  8. import { cleanLlmResponse, cleanLlmShortText, extractJsonFromResponse } from '@/services/llm/response-cleaner';
  9. describe('cleanLlmResponse - LLM 响应清洗', () => {
  10. describe('基础输入', () => {
  11. it('普通文本 → 原样返回', () => {
  12. const result = cleanLlmResponse('这是一段普通的正文内容。');
  13. expect(result).toBe('这是一段普通的正文内容。');
  14. });
  15. it('空字符串 → 空字符串', () => {
  16. expect(cleanLlmResponse('')).toBe('');
  17. });
  18. it('null/undefined → 不崩溃,原样返回', () => {
  19. expect(cleanLlmResponse(null as any)).toBe(null);
  20. expect(cleanLlmResponse(undefined as any)).toBe(undefined);
  21. });
  22. });
  23. describe('思考标签清除(关键功能)', () => {
  24. it('清除 <think> 成对标签及内容', () => {
  25. const input = '可见开头。<think>内部思考过程</think>可见结尾。';
  26. const result = cleanLlmResponse(input);
  27. expect(result).not.toContain('内部思考过程');
  28. expect(result).toContain('可见开头');
  29. expect(result).toContain('可见结尾');
  30. });
  31. it('清除 <thinking> 成对标签', () => {
  32. const input = '正文A<thinking>思考内容B</thinking>正文C';
  33. const result = cleanLlmResponse(input);
  34. expect(result).not.toContain('思考内容B');
  35. expect(result).toContain('正文A');
  36. expect(result).toContain('正文C');
  37. });
  38. it('清除跨多行的 think 块', () => {
  39. const input = '正文开始\n<think>\n思考第一行\n思考第二行\n更多思考\n</think>\n正文继续';
  40. const result = cleanLlmResponse(input);
  41. expect(result).not.toContain('思考第一行');
  42. expect(result).not.toContain('思考第二行');
  43. expect(result).toContain('正文开始');
  44. expect(result).toContain('正文继续');
  45. });
  46. it('清除多个并行的 think 块', () => {
  47. const input = 'A<think>X</think>B<think>Y</think>C';
  48. const result = cleanLlmResponse(input);
  49. expect(result).toBe('ABC');
  50. });
  51. it('清除未闭合的 think 标签', () => {
  52. // 没闭合的 think 应该把后面全部清掉(避免用户看到思考)
  53. const input = '正文开头。<think>思考开始但没结束';
  54. const result = cleanLlmResponse(input);
  55. expect(result).not.toContain('思考开始但没结束');
  56. expect(result).toContain('正文开头');
  57. });
  58. it('清除自闭合 think 标签', () => {
  59. const input = 'A<think/>B';
  60. const result = cleanLlmResponse(input);
  61. expect(result).toBe('AB');
  62. });
  63. it('清除孤立的闭标签', () => {
  64. const input = 'A</think>B';
  65. const result = cleanLlmResponse(input);
  66. expect(result).toBe('AB');
  67. });
  68. });
  69. describe('空白处理', () => {
  70. it('连续多个换行 → 压缩为 2 个', () => {
  71. const result = cleanLlmResponse('A\n\n\n\nB');
  72. expect(result).toBe('A\n\nB');
  73. });
  74. it('前后空白 → trim', () => {
  75. const result = cleanLlmResponse(' \n\n内容\n\n ');
  76. expect(result).toBe('内容');
  77. });
  78. });
  79. describe('真实 LLM 输出场景', () => {
  80. it('MiniMax 长思考+正文 → 只留正文', () => {
  81. const input = `<thinking>
  82. 让我想想怎么回答这个问题...
  83. 用户希望了解 AI 发展史,我应该包括:
  84. 1. 起源
  85. 2. 发展
  86. 3. 未来
  87. 我先写一个提纲。
  88. </thinking>
  89. # AI 发展史
  90. ## 第一章:起源
  91. AI 的起源可以追溯到 1950 年代...`;
  92. const result = cleanLlmResponse(input);
  93. expect(result).not.toContain('让我想想');
  94. expect(result).not.toContain('起源\n2');
  95. expect(result).toContain('AI 发展史');
  96. expect(result).toContain('AI 的起源');
  97. });
  98. it('Claude 风格的 thinking 块', () => {
  99. const input = '<antthinking>内部思考</antthinking>正文内容';
  100. const result = cleanLlmResponse(input);
  101. expect(result).toBe('正文内容');
  102. });
  103. it('纯思考标签无内容 → 返回空字符串(绝不能写进 DB)', () => {
  104. const result = cleanLlmResponse('<think>只有思考</think>');
  105. expect(result).toBe('');
  106. });
  107. it('双括号风格的 [think] 块', () => {
  108. const input = 'A[think]思考[/think]B';
  109. const result = cleanLlmResponse(input);
  110. expect(result).toBe('AB');
  111. });
  112. });
  113. });
  114. describe('extractJsonFromResponse - JSON 提取', () => {
  115. describe('合法 JSON', () => {
  116. it('纯 JSON 对象 → 解析', () => {
  117. expect(extractJsonFromResponse('{"title":"测试"}')).toEqual({ title: '测试' });
  118. });
  119. it('JSON 数组 → 解析', () => {
  120. expect(extractJsonFromResponse('[1,2,3]')).toEqual([1, 2, 3]);
  121. });
  122. it('代码块包裹 JSON → 提取解析', () => {
  123. const input = '这是返回\n```json\n{"title":"测试","count":5}\n```\n结束';
  124. expect(extractJsonFromResponse(input)).toEqual({ title: '测试', count: 5 });
  125. });
  126. it('无语言标记代码块 → 也解析', () => {
  127. const input = '```\n{"a":1}\n```';
  128. expect(extractJsonFromResponse(input)).toEqual({ a: 1 });
  129. });
  130. it('嵌套 JSON → 解析', () => {
  131. const json = '{"chapters":[{"number":1,"title":"测试","sections":[]}]}';
  132. expect(extractJsonFromResponse(json)).toEqual({
  133. chapters: [{ number: 1, title: '测试', sections: [] }]
  134. });
  135. });
  136. });
  137. describe('含 thinking 的 JSON', () => {
  138. it('LLM 返回 thinking + JSON 代码块 → 提取 JSON', () => {
  139. const input = `用户问题
  140. <thinking>让我想想大纲怎么写...</thinking>
  141. \`\`\`json
  142. {"chapters":[{"number":1,"title":"测试"}]}
  143. \`\`\``;
  144. expect(extractJsonFromResponse(input)).toEqual({ chapters: [{ number: 1, title: '测试' }] });
  145. });
  146. });
  147. describe('非法输入', () => {
  148. it('普通描述文字 → 返回 null', () => {
  149. expect(extractJsonFromResponse('不是 JSON,只是描述文字')).toBeNull();
  150. });
  151. it('破损 JSON → 返回 null(不能崩)', () => {
  152. expect(extractJsonFromResponse('{"title": 不完整的')).toBeNull();
  153. });
  154. it('空字符串 → 返回 null', () => {
  155. expect(extractJsonFromResponse('')).toBeNull();
  156. });
  157. it('null/undefined → 返回 null 不崩溃', () => {
  158. expect(extractJsonFromResponse(null as any)).toBeNull();
  159. expect(extractJsonFromResponse(undefined as any)).toBeNull();
  160. });
  161. it('纯思考标签 → 返回 null', () => {
  162. // 清理后是空字符串,parse 失败 → null
  163. expect(extractJsonFromResponse('<think>只有思考</think>')).toBeNull();
  164. });
  165. });
  166. describe('LLM 中文输出', () => {
  167. it('LLM 返回的 JSON 带中文 → 中文正确', () => {
  168. const json = JSON.stringify({ title: 'AI发展史', desc: '讲述AI的故事', tags: ['科技', '历史'] });
  169. const result = extractJsonFromResponse(json);
  170. expect(result).toEqual({
  171. title: 'AI发展史',
  172. desc: '讲述AI的故事',
  173. tags: ['科技', '历史']
  174. });
  175. });
  176. });
  177. });
  178. describe('cleanLlmShortText - 短文本清洗(标题/标签)', () => {
  179. describe('基础清洗', () => {
  180. it('去除首尾空白 → 不变', () => {
  181. expect(cleanLlmShortText(' 标题 ')).toBe('标题');
  182. });
  183. it('去除各种引号包裹', () => {
  184. expect(cleanLlmShortText('"已清洗"')).toBe('已清洗');
  185. expect(cleanLlmShortText('"又清洗"')).toBe('又清洗');
  186. });
  187. it('去除 markdown 粗体', () => {
  188. expect(cleanLlmShortText('**粗体标题**')).toBe('粗体标题');
  189. });
  190. it('去除 markdown 标题标记', () => {
  191. expect(cleanLlmShortText('## 二级标题')).toBe('二级标题');
  192. });
  193. it('去除 markdown 链接', () => {
  194. expect(cleanLlmShortText('[链接文字](http://url.com)')).toBe('链接文字');
  195. });
  196. it('去除 markdown 代码', () => {
  197. expect(cleanLlmShortText('`code`')).toBe('code');
  198. });
  199. it('去除换行 → 转空格', () => {
  200. expect(cleanLlmShortText('第一行\n第二行')).toBe('第一行 第二行');
  201. });
  202. it('去除"书名:"前缀', () => {
  203. expect(cleanLlmShortText('书名:AI发展史')).toBe('AI发展史');
  204. expect(cleanLlmShortText('书名: AI发展史')).toBe('AI发展史');
  205. });
  206. });
  207. describe('异常输入', () => {
  208. it('空字符串 → 返回空字符串', () => {
  209. expect(cleanLlmShortText('')).toBe('');
  210. });
  211. it('null/undefined 不崩溃', () => {
  212. expect(() => cleanLlmShortText(null as any)).not.toThrow();
  213. expect(() => cleanLlmShortText(undefined as any)).not.toThrow();
  214. expect(cleanLlmShortText(null as any)).toBe('');
  215. });
  216. it('纯思考 → 返回空字符串', () => {
  217. expect(cleanLlmShortText('<think>思考</think>')).toBe('');
  218. });
  219. });
  220. describe('截断保护', () => {
  221. it('超长文本 → 截断', () => {
  222. const longText = '一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十';
  223. const result = cleanLlmShortText(longText, { maxLength: 20 });
  224. expect(result.length).toBeLessThanOrEqual(20);
  225. });
  226. it('自定义 maxLength', () => {
  227. expect(cleanLlmShortText('十二个汉字', { maxLength: 6 })).toBe('十二个汉字'.substring(0, 6));
  228. });
  229. });
  230. });