// 2026-10-24 新增视觉基线的清单 + 拼版工具(只读 e2e/visual-snapshots,只在证据目录写产物) import fs from 'fs'; import path from 'path'; import crypto from 'crypto'; import sharp from 'sharp'; const ROOT = '/Users/zhangxiang/Codes/Novalon/novalon-website'; const EV = path.join(ROOT, 'docs/acceptance/2026-10-24-full-qa'); const SNAP = path.join(ROOT, 'e2e/visual-snapshots'); const OUT = path.join(EV, 'contact-sheets/baselines'); fs.mkdirSync(OUT, { recursive: true }); const PROJECTS = [ 'visual-chromium-desktop', 'visual-chromium-tablet', 'visual-chromium-mobile', 'visual-firefox-desktop', 'visual-webkit-desktop', ]; const ROUTES = [ ['not-found', '/_not-found'], ['cases', '/cases'], ['insights', '/insights'], ['news-digital-transformation', '/news/digital-transformation-solution'], ['privacy', '/privacy'], ['terms', '/terms'], ['product-bi', '/products/bi'], ['product-cms', '/products/cms'], ['product-crm', '/products/crm'], ['product-oa', '/products/oa'], ['product-sds', '/products/sds'], ['product-erp-upgrade-v3', '/products/erp-upgrade-v3'], ['service-consulting', '/services/consulting'], ['service-data', '/services/data'], ['service-solutions', '/services/solutions'], ['solution-education', '/solutions/education'], ['solution-finance', '/solutions/finance'], ['solution-healthcare', '/solutions/healthcare'], ['solution-logistics', '/solutions/logistics'], ['solution-retail', '/solutions/retail'], ]; const sha256 = (buf) => crypto.createHash('sha256').update(buf).digest('hex'); const esc = (s) => s.replace(/[<>&'"]/g, (c) => ({ '<': '<', '>': '>', '&': '&', "'": ''', '"': '"' }[c])); const manifest = []; for (const project of PROJECTS) { for (const [name, route] of ROUTES) { const file = path.join(SNAP, project, 'visual-regression.spec.ts', `${name}-fullpage-${project}.png`); if (!fs.existsSync(file)) { manifest.push({ project, name, route, file, exists: false }); continue; } const buf = fs.readFileSync(file); const meta = await sharp(buf).metadata(); const stats = await sharp(buf).stats(); const channels = stats.channels.slice(0, 3); const stdev = Math.max(...channels.map((c) => c.stdev)); const entropy = Math.max(...channels.map((c) => c.entropy)); manifest.push({ project, name, route, file: path.relative(ROOT, file), exists: true, width: meta.width, height: meta.height, bytes: buf.length, sha256: sha256(buf), maxChannelStdev: +stdev.toFixed(2), maxChannelEntropy: +entropy.toFixed(3), blankSuspicion: stdev < 6 || entropy < 1.2, }); } } fs.writeFileSync(path.join(EV, 'baseline-manifest.json'), JSON.stringify(manifest, null, 2)); console.log(`manifest=${manifest.length} missing=${manifest.filter((m) => !m.exists).length} blankSuspicion=${manifest.filter((m) => m.blankSuspicion).length}`); async function labeledTile(file, width, cropTop) { let src = await sharp(file).resize({ width, withoutEnlargement: true }).jpeg({ quality: 80 }).toBuffer(); let m = await sharp(src).metadata(); if (cropTop && m.height > cropTop) { src = await sharp(src).extract({ left: 0, top: 0, width: m.width, height: cropTop }).jpeg({ quality: 80 }).toBuffer(); m = await sharp(src).metadata(); } const label = Buffer.from( `` + `` + `${esc(path.basename(file).replace('-fullpage-' + path.basename(path.dirname(path.dirname(file))) + '.png', ''))}`, ); return { buf: await sharp({ create: { width: m.width, height: m.height + 24, channels: 3, background: '#fff' } }) .composite([{ input: src, top: 0, left: 0 }, { input: label, top: m.height, left: 0 }]) .jpeg({ quality: 82 }).toBuffer(), height: m.height + 24, }; } async function sheet(files, out, width, cols, cropTop) { const tiles = []; for (const f of files) tiles.push(await labeledTile(f, width, cropTop)); const rows = []; for (let i = 0; i < tiles.length; i += cols) rows.push(tiles.slice(i, i + cols)); const gap = 8; const heights = rows.map((r) => Math.max(...r.map((t) => t.height))); const w = cols * width + (cols - 1) * gap; const h = heights.reduce((a, b) => a + b, 0) + (rows.length - 1) * gap; const comps = []; let y = 0; rows.forEach((r, ri) => { let x = 0; r.forEach((t) => { comps.push({ input: t.buf, top: y, left: x }); x += width + gap; }); y += heights[ri] + gap; }); await sharp({ create: { width: w, height: h, channels: 3, background: '#fff' } }) .composite(comps).jpeg({ quality: 80 }).toFile(out); console.log(`sheet ${path.basename(out)} ${files.length} tiles ${w}x${h}`); } for (const project of PROJECTS) { const files = ROUTES.map(([name]) => path.join(SNAP, project, 'visual-regression.spec.ts', `${name}-fullpage-${project}.png`)).filter(fs.existsSync); await sheet(files, path.join(OUT, `${project}__top.jpg`), 300, 5, 900); for (let i = 0; i < files.length; i += 5) { await sheet(files.slice(i, i + 5), path.join(OUT, `${project}__full-${String(i / 5 + 1).padStart(2, '0')}.jpg`), 300, 5, 0); } }