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 { buildAgentReportLayout, agentReportPrimary, agentReportSupplementary } from '../src/agent/agentReportLayout.ts'; import { stripDuplicateFirstMarkdownH1 } from '../src/analysisReportTitle.ts'; import { tableTextAlignment } from '../src/tableTextAlignment.ts'; import { sanitizeAnalysisDisplayText } from '../src/analysisDisplayText.ts'; import { analysisEvidenceParts } from '../src/analysisEvidenceRefs.ts'; function load(file, exported, bindings) { const source=fs.readFileSync(new URL(file,import.meta.url),'utf8').replace(/^import .*;\n/gm,'').replaceAll('export function ','function ').replaceAll('export async function ','async function '); return new Function(...Object.keys(bindings),ts.transpileModule(source+'\nreturn '+exported+';', {compilerOptions:{target:ts.ScriptTarget.ES2022,jsx:ts.JsxEmit.React}}).outputText.replace(/^export\s*\{\s*\};?$/gm,''))(...Object.values(bindings)); } const AiMarkdown=load('../src/AiMarkdown.tsx','AiMarkdown',{React,tableTextAlignment,sanitizeAnalysisDisplayText,analysisEvidenceParts}); const figures=[{id:'trend',title:'月度趋势',type:'line',unit:'ratio',metric:'composite_rate',sectionKey:'trend',scope:{},categories:[],series:[],evidenceRefs:[]}, {id:'scrap',title:'报废结构',type:'bar',unit:'kg',metric:'scrap_weight',sectionKey:'category',scope:{},categories:[],series:[],evidenceRefs:[]}]; let images=[]; const Empty=()=>null; const Document=load('../src/agent/AgentDeliverable.tsx','AgentDeliverable',{ React,useLayoutEffect:()=>{},useRef:value=>({current:value}),useState:value=>[value,()=>{}], Alert:Empty,Spin:Empty,DownloadOutlined:Empty,Button:Empty,Modal:({children})=>React.createElement('main',null,children), AnalysisMemberListReport:Empty,AnalysisRecordListReport:Empty,AiMarkdown, ReportVisualFigure:({visual,image})=>image?.svg?React.createElement('figure',{'data-chart':visual.id},visual.title):React.createElement('table',{'data-fallback':visual.id},React.createElement('tbody',null,React.createElement('tr',null,React.createElement('td',null,visual.title)))), reportVisualImageSource:image=>image?.svg||'',reportVisuals:()=>figures, useReportVisualImages:()=>({authorized:true,loading:false,images}),analysisSnapshotResult:turn=>turn.result, reportSnapshotIdentity:()=>'',analysisScopeText:()=>'',stripDuplicateFirstMarkdownH1,buildAgentReportLayout,agentReportPrimary,agentReportSupplementary, captureReportLongImage:()=>{},downloadAuthorizedReport:()=>{},documentCss:'', }); const answer='# 原始报告\n\n原始导读。\n\n## 月度趋势\n\n原始分析。\n\n| 月份 | 值 |\n|---|---|\n| 10月 | 95.46% |\n\n## 报废大类\n\n原始说明。'; function render() {return renderToStaticMarkup(React.createElement(Document,{turn:{status:'COMPLETED',question:'生成报告',result:{modelAnswer:answer,answer:'此替代回答不应显示'}},onClose:()=>{}}));} test('every report figure precedes the original detail table, appears once, and leaves the source intact',()=>{ images=figures.map(v=>({id:v.id,svg:'data:image/svg+xml,test'})); const html=render(); for(const id of ['trend','scrap']) { assert.equal((html.match(new RegExp('data-chart="'+id+'"','g'))||[]).length,1); assert.ok(html.indexOf('data-chart="'+id+'"')')); } assert.ok(html.indexOf('原始分析。'){ images=[{id:'trend',svg:'data:image/svg+xml,test'},{id:'scrap',failed:true}]; const html=render(); assert.equal((html.match(/data-chart=/g)||[]).length,1); assert.doesNotMatch(html,/data-fallback|重试|暂不可用/); assert.ok(html.indexOf('data-chart="trend"')')); assert.match(html,/原始说明。/); }); test('no image yields no empty gallery and all original data remains accessible',()=>{ images=[];const html=render();assert.doesNotMatch(html,/agent-document-overview/);assert.match(html,/95.46%/); assert.equal((html.match(/data-fallback=/g)||[]).length,0); }); test('the real report figure quietly omits unavailable images',()=>{ const Figure=load('../src/AnalysisReportVisuals.tsx','ReportVisualFigure',{React,visualPeriodSummary:()=>'',visualScopeSummary:()=>''}); for(const image of [undefined,{id:'trend',failed:true}]) { assert.equal(renderToStaticMarkup(React.createElement(Figure,{visual:{...figures[0],chartable:true},image,quiet:true})), ''); } const image={id:'trend',svg:'data:image/svg+xml;charset=utf-8,%3Csvg%2F%3E'}; const html=renderToStaticMarkup(React.createElement(Figure,{visual:{...figures[0],chartable:true,coverage:{truncated:false}},image,quiet:true})); assert.match(html,/
{ const authored=load('../src/agent/AgentDeliverable.tsx','authoredReport',{analysisSnapshotResult:turn=>turn.result,stripDuplicateFirstMarkdownH1}); for(const question of ['2026年8月综合合格率是多少?','一句话回答,不生成报告。','对比9月10月,简短回答,不需要生成报告。','沿用上面,只回答这两项。']) { assert.equal(authored({status:'COMPLETED',question,result:{modelAnswer:answer,reportMarkdown:answer}}),null); } assert.ok(authored({status:'COMPLETED',question:'生成亮片报废结构专题报告',result:{modelAnswer:answer}})); assert.ok(authored({status:'COMPLETED',question:'生成质量异常诊断简报',result:{modelAnswer:answer}})); });