Files
quality_analyse/web/verification/analysis_report_export.test.mjs
lyun ff7bbe3e46 同步:完善质量分析工作台并更新主题字体
同步已上线的智能分析执行协议、来源查询、图表报告与管理页面配套代码。
补齐数据库迁移、运行依赖和回归测试,保留原有指标计算口径。
统一午夜黑及其他主题的控件细节、中文和业务数字字体。
排除本地业务数据、凭据、预览、发布与验收产物。

验证:生产构建通过,320项前端回归通过;本轮仅同步Git,不再次部署。
2026-10-30 22:40:58 +08:00

163 lines
8.7 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { downloadAuthorizedReport, longReportImageSize, reportDownloadName } from '../src/analysisReportFiles.ts';
import { resolveAnalysisReportTitle, stripDuplicateFirstMarkdownH1 } from '../src/analysisReportTitle.ts';
import { selectAnalysisReport } from '../src/analysisReportSummary.ts';
const exportSource = readFileSync(new URL('../src/AnalysisReportExport.tsx', import.meta.url), 'utf8');
test('report title prefers generated result and turn titles, then a legacy report H1', () => {
assert.equal(resolveAnalysisReportTitle('AI生成标题', 'turn标题', '# Markdown标题', '2026年9月', ''), 'AI生成标题');
assert.equal(resolveAnalysisReportTitle('', 'turn标题', '# Markdown标题', '2026年9月', ''), 'turn标题');
assert.equal(resolveAnalysisReportTitle('', '', '# 旧报告 AI 标题\n\n正文', '2026年9月', ''), '旧报告 AI 标题');
});
test('fallback title comes from the period and scope, never the user question', () => {
const question = '综合以上生成报告';
const title = resolveAnalysisReportTitle('', '', '', '2026年9月', '2026年9月 · 产品:新能源磁钢 · 黑片与亮片');
assert.equal(title, '2026年9月 · 新能源磁钢质量分析报告');
assert.notEqual(title, question);
});
test('a matching Markdown H1 is removed from the body without dropping detailed report content', () => {
const markdown = '# AI生成标题\n\n## 执行摘要\n综合合格率下降。\n\n## 重点产品\n完整投入、报废重量与率变化明细。';
const body = stripDuplicateFirstMarkdownH1(markdown, 'AI生成标题');
assert.equal(body.includes('# AI生成标题'), false);
assert.match(body, /^## 执行摘要/);
assert.match(body, /## 重点产品/);
assert.match(body, /完整投入、报废重量与率变化明细/);
});
test('the export has one detailed-report path and no summary mode selector', () => {
assert.doesNotMatch(exportSource, /Segmented|setMode|简版摘要/);
assert.match(exportSource, /<AnalysisReportNarrative[^>]*mode="full"/);
assert.equal((exportSource.match(/下载长图/g) || []).length, 3); // one action with its button/ready-state wording
});
test('download names stay concise and use a single report suffix', () => {
assert.equal(reportDownloadName('2026-08 → 2026-09'), '质量分析_2026-08至09_报告');
assert.equal(reportDownloadName(''), '质量分析_报告');
});
test('the complete long report retains its dimensions beyond the old canvas auto-scale limit', () => {
assert.deepEqual(longReportImageSize(900, 11912), { width: 900, height: 11912, pixelRatio: 1.5 });
assert.equal(longReportImageSize(900, 11912).height * 1.5, 17868);
});
test('very long reports fit one bounded canvas by scaling the entire image without dropping content', () => {
for (const [width, height] of [[900, 50000], [6000, 40000]]) {
const result = longReportImageSize(width, height);
assert.equal(result.width, width); assert.equal(result.height, height);
assert.ok(result.pixelRatio > 0);
assert.ok(width * result.pixelRatio <= 30000);
assert.ok(height * result.pixelRatio <= 30000);
assert.ok(width * height * result.pixelRatio ** 2 <= 48000001);
}
assert.deepEqual(longReportImageSize(900, 830.2), { width: 900, height: 831, pixelRatio: 1.5 });
assert.throws(() => longReportImageSize(900, NaN), /尺寸无效/);
});
const section = (id, kind, extra = {}) => ({ id, kind, title: id, markdown: id, narrative: id, tables: [], visuals: [], evidenceRefs: [], ...extra });
const model = sections => ({ sections, title: '质量报告', period: '2026年9月 对比8月', scope: '电机', evidenceLabels: { Q11111111: '依据1', Q22222222: '依据2' }, evidence: [{ ref: 'Q11111111', label: '依据1', context: '正式指标' }, { ref: 'Q22222222', label: '依据2', context: '细分数据' }] });
test('summary retains exact overview, stages, selected product inputs, chart, action and evidence', () => {
const detail = '黑片投入17,361.74→26,276.07 kg;亮片投入19,009.25→26,288.80 kg';
const source = model([
section('overall', 'overview', { evidenceRefs: ['Q11111111'], tables: ['| 正式值 | 95.13% |'] }),
section('focus', 'product', { focusProducts: [{ product: 'A', scrapDelta: '+758.76', rateChange: '下降1.95个百分点', basis: '按报废增量排序', detail, evidenceRefs: ['Q11111111'] }] }),
section('stages', 'stage', { visuals: [{ id: 'stage-chart', evidenceRefs: ['Q11111111'] }] }),
section('product-detail', 'product', { evidenceRefs: ['Q22222222'], tables: ['full table'], visuals: [{ id: 'focus-chart', evidenceRefs: ['Q11111111'] }, { id: 'unrelated-chart', evidenceRefs: ['Q22222222'] }] }),
section('defect-detail', 'defect', { evidenceRefs: ['Q22222222'] }),
section('scope-note', 'other', { narrative: '范围不含未分类;来源字段不代表正式总量。' }),
section('actions', 'action'),
]);
const summary = selectAnalysisReport(source, 'summary');
assert.deepEqual(summary.sections.map(item => item.id), ['overall','focus','stages','scope-note','actions']);
assert.equal(summary.sections[1].focusProducts[0].detail, detail);
assert.deepEqual(summary.sections[1].visuals.map(item => item.id), ['focus-chart']);
assert.deepEqual(summary.sections[0].tables, ['| 正式值 | 95.13% |']);
assert.equal(summary.sections[2].visuals[0].id, 'stage-chart');
assert.equal(summary.scope, source.scope);
assert.deepEqual(summary.evidence.map(item => item.ref), ['Q11111111']);
assert.equal(source.sections.length, 7);
assert.strictEqual(selectAnalysisReport(source, 'full'), source);
});
test('unrecognized historic answers keep their complete content and unavailable values', () => {
const source = model([section('旧格式', 'other', { narrative: '不可比;正式值—', tables: ['全部原始表格'] }), section('特别说明', 'other')]);
const summary = selectAnalysisReport(source, 'summary');
assert.deepEqual(summary.sections, source.sections);
const narrow = model([section('具体缺陷', 'defect', { evidenceRefs: ['Q22222222'] })]);
assert.deepEqual(selectAnalysisReport(narrow, 'summary').sections, narrow.sections);
});
function pendingAuthorization() {
let resolve, reject;
const promise = new Promise((yes, no) => { resolve = yes; reject = no; });
return { promise, resolve, reject };
}
for (const kind of ['HTML', '长图']) {
test(`${kind} download rechecks authorization before using the prepared file`, async () => {
const auth = pendingAuthorization();
const events = [];
const action = downloadAuthorizedReport({
authorize: () => { events.push('authorize'); return auth.promise; },
isCurrent: () => true,
download: () => events.push('download'),
onDenied: () => events.push('denied'),
});
assert.deepEqual(events, ['authorize']);
auth.resolve();
await action;
assert.deepEqual(events, ['authorize', 'download']);
});
test(`${kind} permission revoked after preparation clears files and reports denial`, async () => {
const auth = pendingAuthorization();
const files = new Set(['html-url', 'image-url']);
const revoked = [];
let shownError = '';
let downloaded = false;
const action = downloadAuthorizedReport({
authorize: () => auth.promise,
isCurrent: () => true,
download: () => { downloaded = true; },
onDenied: error => { for (const url of files) revoked.push(url); files.clear(); shownError = error.message; },
});
auth.reject(new Error('权限已撤销'));
await action;
assert.equal(downloaded, false);
assert.deepEqual(revoked, ['html-url', 'image-url']);
assert.equal(files.size, 0);
assert.equal(shownError, '权限已撤销');
});
for (const change of ['dialog closed', 'snapshot changed']) {
test(`${kind} is not downloaded or modified when ${change} during authorization`, async () => {
for (const outcome of ['allow', 'deny']) {
const auth = pendingAuthorization();
let current = true;
const events = [];
const action = downloadAuthorizedReport({
authorize: () => auth.promise,
isCurrent: () => current,
download: () => events.push('download'),
onDenied: () => events.push('denied'),
});
current = false;
if (outcome === 'allow') auth.resolve(); else auth.reject(new Error('denied'));
await action;
assert.deepEqual(events, []);
}
});
}
}
test('prepared export buttons route through the authorization gate', () => {
assert.match(exportSource, /downloadPrepared\(preparedHtml, 'html'\)/);
assert.match(exportSource, /downloadPrepared\(prepared, 'image'\)/);
assert.doesNotMatch(exportSource, /href=\{prepared(?:Html)?\.url\}/);
});