// 把 sweep 产出的 hero 截图拼成 contact sheet,便于逐页人工/vision 过目。 // 用法:node scripts/acceptance/contact-sheet.mjs <截图目录> <输出目录> [前缀] import fs from 'fs'; import path from 'path'; import sharp from 'sharp'; const DIR = process.argv[2] || 'docs/acceptance/2026-10-21-full-qa/screenshots/public'; const OUT = process.argv[3] || 'docs/acceptance/2026-10-21-full-qa/contact-sheets'; const PREFIX = process.argv[4] || 'public'; fs.mkdirSync(OUT, { recursive: true }); const COLS = 6; const CELL_W = 360; const GAP = 8; const LABEL_H = 26; const escapeXml = (s) => s.replace(/[<>&'"]/g, (c) => ({ '<': '<', '>': '>', '&': '&', "'": ''', '"': '"' }[c])); async function buildSheet(groupKey, files) { const tiles = []; for (const file of files.sort()) { const src = await sharp(path.join(DIR, file)).resize({ width: CELL_W, withoutEnlargement: true }).jpeg({ quality: 74 }).toBuffer(); const meta = await sharp(src).metadata(); const label = Buffer.from( `` + `` + `${escapeXml(file.replace('__hero.jpg', ''))}` + '', ); const tile = await sharp({ create: { width: CELL_W, height: meta.height + LABEL_H, channels: 3, background: '#ffffff' } }) .composite([{ input: src, top: 0, left: 0 }, { input: label, top: meta.height, left: 0 }]) .jpeg({ quality: 76 }) .toBuffer(); tiles.push(tile); } const rows = []; for (let i = 0; i < tiles.length; i += COLS) rows.push(tiles.slice(i, i + COLS)); const rowHeights = []; for (const row of rows) { rowHeights.push(Math.max(...(await Promise.all(row.map(async (buf) => (await sharp(buf).metadata()).height))))); } const width = COLS * CELL_W + (COLS - 1) * GAP; const height = rowHeights.reduce((sum, h) => sum + h, 0) + (rows.length - 1) * GAP; const composites = []; let y = 0; for (let r = 0; r < rows.length; r += 1) { let x = 0; for (const tile of rows[r]) { composites.push({ input: tile, top: y, left: x }); x += CELL_W + GAP; } y += rowHeights[r] + GAP; } const out = path.join(OUT, `${PREFIX}-${groupKey}.jpg`); await sharp({ create: { width, height, channels: 3, background: '#ffffff' } }).composite(composites).jpeg({ quality: 78 }).toFile(out); return { out, tiles: tiles.length, width, height }; } const files = fs.readdirSync(DIR).filter((f) => f.endsWith('__hero.jpg')); const groups = new Map(); for (const file of files) { const m = file.match(/__(\d+)__([a-z]+)__([a-z]+)__hero\.jpg$/); if (!m) continue; const key = `${m[1]}-${m[2]}-${m[3]}`; if (!groups.has(key)) groups.set(key, []); groups.get(key).push(file); } const summary = []; for (const [key, groupFiles] of [...groups.entries()].sort()) { summary.push(await buildSheet(key, groupFiles)); console.log(`sheet ${key}: ${summary[summary.length - 1].tiles} tiles -> ${summary[summary.length - 1].out}`); } fs.writeFileSync(path.join(OUT, `${PREFIX}-index.json`), JSON.stringify(summary, null, 2));