import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import ts from 'typescript'; import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import { tableTextAlignment } from '../src/tableTextAlignment.ts'; import { sanitizeAnalysisDisplayText } from '../src/analysisDisplayText.ts'; import { analysisEvidenceParts } from '../src/analysisEvidenceRefs.ts'; const source = fs.readFileSync(new URL('../src/AiMarkdown.tsx', import.meta.url), 'utf8') .replace(/^import .*;$/gm, '').replace('export function AiMarkdown', 'function AiMarkdown'); const Markdown = new Function('React', 'tableTextAlignment', 'sanitizeAnalysisDisplayText', 'analysisEvidenceParts', ts.transpileModule(`${source}\nreturn AiMarkdown;`, { compilerOptions: { target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.React } }).outputText )(React, tableTextAlignment, sanitizeAnalysisDisplayText, analysisEvidenceParts); test('image syntax renders as a chart reference or caption without rewriting source or fetching URLs', () => { const answer = '前文。![趋势标题](chart:actual-chart) 后文。\n\n![未绑定标题](https://example.invalid/tracker.png)\n\n`![代码示例](chart:example)`'; const html = renderToStaticMarkup(React.createElement(Markdown, { content: answer, preserveSource: true, imageTargets: { 'chart:actual-chart': 'actual-chart' } })); assert.match(html, /href="#actual-chart">趋势标题<\/a>/); assert.match(html, /前文。/); assert.match(html, /后文。/); assert.match(html, /ai-markdown-image-caption[^>]*>未绑定标题<\/span>/); assert.match(html, /!\[代码示例\]\(chart:example\)<\/code>/); assert.doesNotMatch(html, / { const answer = '分析前文。\n\n![产品趋势](chart:verified)\n\n分析后文。\n\n```markdown\n![代码示例](chart:verified)\n```'; const source = 'data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%2F%3E'; const html = renderToStaticMarkup(React.createElement(Markdown, { content: answer, preserveSource: true, renderImage: (alt, target, key) => target === 'chart:verified' ? React.createElement('img', { key, src: source, alt }) : undefined, })); assert.match(html, /]+alt="产品趋势"/); assert.equal((html.match(/!\[代码示例\]\(chart:verified\)/); assert.ok(answer.includes('![产品趋势](chart:verified)')); }); test('original answers preserve wording, table headers, headings and escaped markup', () => { const answer = '## 模型原始回答\n\n数学说明保留。\n\n| 月份 | 原始值 |\n| --- | --- |\n| 8月 | 98.35% |\n\n'; const html = renderToStaticMarkup(React.createElement(Markdown, { content: answer, preserveSource: true })); assert.match(html, /

模型原始回答<\/h2>/); assert.match(html, /数学说明保留/); assert.match(html, /<script>alert\(1\)<\/script>/); assert.doesNotMatch(html, /| )/g) || []).length, 2); }); test('report layout preserves original Markdown instead of converting bold prose into chapters', async () => { const { buildAgentReportLayout } = await import('../src/agent/agentReportLayout.ts'); const text = '**原始小节**\n\n原始正文。\n\n1. 原始项目。'; const report = buildAgentReportLayout(text, []); assert.equal(report.sections.map(section => section.markdown).join('\n\n'), text); }); test('original Markdown rules and quotes render semantically without altering their words or tables', () => { const answer = '首句。\n---\n> **范围:**2026年10月\n> 保留