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('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> 保留