// Capturas de pantalla y comprobación de scroll horizontal sobre dist/ (astro preview). // Uso: node scripts/capturas.mjs "/,/proyectos" import { chromium } from 'playwright'; import { spawn } from 'node:child_process'; import { setTimeout as sleep } from 'node:timers/promises'; import { mkdirSync } from 'node:fs'; const raiz = process.argv[2]; const salida = process.argv[3]; const rutas = (process.argv[4] || '/').split(','); mkdirSync(salida, { recursive: true }); const srv = spawn('npx', ['astro', 'preview', '--port', '4321'], { cwd: raiz, stdio: 'ignore' }); await sleep(2500); const browser = await chromium.launch(); try { for (const ruta of rutas) { for (const [ancho, alto] of [[320, 700], [360, 780], [768, 900], [1280, 900], [1920, 1000]]) { for (const esquema of ['light', 'dark']) { const ctx = await browser.newContext({ viewport: { width: ancho, height: alto }, colorScheme: esquema, deviceScaleFactor: 1 }); const page = await ctx.newPage(); const errores = []; page.on('console', (m) => { if (m.type() === 'error') errores.push(m.text()); }); page.on('requestfailed', (r) => errores.push('request failed: ' + r.url())); await page.goto('http://localhost:4321' + ruta, { waitUntil: 'networkidle' }); const sw = await page.evaluate(() => [document.documentElement.scrollWidth, document.documentElement.clientWidth]); const nombre = ruta.replace(/\//g, '_').replace(/^_$/, 'inicio').replace(/^_/, ''); if ([360, 1280].includes(ancho)) await page.screenshot({ path: `${salida}/${nombre}-${ancho}-${esquema}.png`, fullPage: true }); console.log(`${ruta} ${ancho}px ${esquema}: scrollWidth=${sw[0]} clientWidth=${sw[1]} ${sw[0] > sw[1] ? 'SCROLL HORIZONTAL' : 'ok'} ${errores.length ? 'ERRORES: ' + errores.join(' | ') : ''}`); await ctx.close(); } } } } finally { await browser.close(); srv.kill(); }