| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- // 测试JSON修复功能
- import { validateAndFixJSON } from './src/api/yjbdc/aiController.js';
- // 测试用例
- const testCases = [
- // 正常JSON
- {
- name: "正常JSON",
- input: '{"name":"test","value":123}'
- },
- // Markdown代码块包裹的JSON
- {
- name: "Markdown代码块",
- input: '```json\n{"name":"test","value":123}\n```'
- },
- // 带有语法错误的JSON
- {
- name: "语法错误JSON",
- input: '{name:"test",value:123}'
- },
- // 混合问题
- {
- name: "混合问题",
- input: '```json\n{name:"test",value:123}\n```'
- },
- // 非JSON内容中包含JSON对象
- {
- name: "非JSON内容中包含JSON对象",
- input: '这是一段文本,其中包含JSON:{"name":"test","value":123},后面还有其他内容'
- },
- // 阿里云通义千问可能返回的格式
- {
- name: "阿里云通义千问返回格式",
- input: '```json\n{\n "title": "测试文章",\n "content": "这是一篇测试文章",\n "author": "AI助手",\n "date": "2023-07-15"\n}\n```'
- }
- ];
- // 运行测试
- console.log("开始测试JSON修复功能...\n");
- testCases.forEach(testCase => {
- console.log(`测试用例: ${testCase.name}`);
- console.log(`输入: ${testCase.input}`);
-
- try {
- const fixed = validateAndFixJSON(testCase.input);
- console.log(`修复后: ${fixed}`);
-
- try {
- const parsed = JSON.parse(fixed);
- console.log(`解析结果: ${JSON.stringify(parsed, null, 2)}`);
- console.log("✅ 测试通过\n");
- } catch (error) {
- console.error(`❌ 修复后仍无法解析: ${error.message}\n`);
- }
- } catch (error) {
- console.error(`❌ 修复过程出错: ${error.message}\n`);
- }
- });
- console.log("测试完成");
|