80 lines
4.0 KiB
JavaScript
80 lines
4.0 KiB
JavaScript
import { tableTextAlignment } from '../src/tableTextAlignment.ts';
|
|
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 { parseServerReadingSummary } from '../src/analysisReadingModel.ts';
|
|
import {
|
|
analysisMemberListFullyRead,
|
|
analysisMemberListPeriod,
|
|
parseAnalysisMemberList,
|
|
} from '../src/analysisMembersModel.ts';
|
|
|
|
const exportSource = fs.readFileSync(new URL('../src/AnalysisReportExport.tsx', import.meta.url), 'utf8');
|
|
const reportDocumentSource = exportSource.slice(exportSource.indexOf('function ReportDocument('), exportSource.indexOf('\nfunction reportHtmlFromElement('));
|
|
const node = (tag, props) => React.createElement(tag, props, props?.children);
|
|
const ReportDocument = new Function('tableTextAlignment', 'React', 'reportCss', 'record', 'text', 'buildAnalysisNarrative', 'analysisScopeText',
|
|
'parseServerReadingSummary', 'resolveAnalysisReportTitle', 'sanitizeAnalysisDisplayText', 'stripDuplicateFirstMarkdownH1', 'AnalysisReportNarrative',
|
|
'AnalysisLegacyResults', 'AnalysisRecordListReport', 'analysisMemberListFullyRead', 'analysisMemberListPeriod', 'parseAnalysisMemberList',
|
|
ts.transpileModule(`${reportDocumentSource}\nreturn ReportDocument;`, {
|
|
fileName: 'analysis-report-document.tsx',
|
|
compilerOptions: { target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.React },
|
|
}).outputText
|
|
)(tableTextAlignment, React, '', value => value && typeof value === 'object' && !Array.isArray(value) ? value : {},
|
|
value => value === null || value === undefined || value === '' ? '—' : String(value),
|
|
() => ({ period: '2026年9月' }), () => '', parseServerReadingSummary, () => '分析报告', value => value, value => value,
|
|
({ answer }) => node('div', { className: 'report-summary', children: answer }), () => null, () => null,
|
|
analysisMemberListFullyRead, analysisMemberListPeriod, parseAnalysisMemberList);
|
|
|
|
function memberListFixture() {
|
|
return {
|
|
schemaVersion: 1,
|
|
releaseId: 'release-2026-09',
|
|
period: { year: 2026, month: 9 },
|
|
fields: [{ key: 'member', label: '成员' }, { key: 'note', label: '备注' }],
|
|
rows: Array.from({ length: 181 }, (_, index) => ({
|
|
values: { member: index === 180 ? '最后一行成员181' : `成员${index + 1}`, note: index === 0 ? '<img src=x onerror=alert(1)>' : index % 3 ? `备注${index + 1}` : null },
|
|
stages: [index % 2 ? 'bright' : 'black'],
|
|
})),
|
|
totalCount: 181,
|
|
rowsReturned: 181,
|
|
queryComplete: true,
|
|
};
|
|
}
|
|
|
|
test('the report includes every member row beyond the former 150-row limit as a static escaped table', () => {
|
|
const html = renderToStaticMarkup(React.createElement(ReportDocument, {
|
|
turn: { id: 'turn-1', status: 'COMPLETED', result: { answer: '摘要内容', memberList: memberListFixture() } },
|
|
stamp: '2026-10-27 12:00:00', visualImages: [],
|
|
}));
|
|
|
|
assert.match(html, /<h2>成员清单<\/h2>/);
|
|
assert.match(html, /<th>成员<\/th>/);
|
|
assert.match(html, /<th style="text-align:left">备注<\/th>/);
|
|
assert.match(html, /<th>阶段<\/th>/);
|
|
assert.equal((html.match(/<tr/g) || []).length, 182);
|
|
assert.match(html, /最后一行成员181/);
|
|
assert.match(html, /黑片/);
|
|
assert.match(html, /亮片/);
|
|
assert.match(html, /—/);
|
|
assert.match(html, /已读取全部 181 条/);
|
|
assert.match(html, /<img src=x onerror=alert\(1\)>/);
|
|
assert.doesNotMatch(html, /<img|<a\b/);
|
|
assert.doesNotMatch(html, /分页|每页显示|下一页/);
|
|
});
|
|
|
|
test('the report accepts a root memberList and labels incomplete coverage', () => {
|
|
const source = memberListFixture();
|
|
source.rows = source.rows.slice(0, 160);
|
|
source.rowsReturned = 160;
|
|
source.queryComplete = false;
|
|
const html = renderToStaticMarkup(React.createElement(ReportDocument, {
|
|
turn: { id: 'turn-2', status: 'PARTIAL', answer: '摘要', memberList: source },
|
|
stamp: '2026-10-27 12:00:00', visualImages: [],
|
|
}));
|
|
assert.equal((html.match(/<tr/g) || []).length, 161);
|
|
assert.match(html, /读取未完成 160 \/ 181 条/);
|
|
});
|