'use strict'; /* eslint-env node, browser */ // Before/after screenshots for UI pull requests (#45 phase 5). // // node scripts/ci/screenshots.js before= after= // // Seeds the same demo data into each running Pulse stack (scripts/ci/ui-stack.sh), // captures every page on desktop and on a phone, then composes one side-by-side // image per page and viewport (-.png), rendered in the browser so // no image tooling is needed. Pages that look identical are listed in unchanged.txt. const fs = require('fs'); const path = require('path'); const { chromium, devices } = require('@playwright/test'); const SSO = { 'Remote-User': 'ci.screens', 'Remote-Name': 'CI Screens', 'Remote-Email': 'ci@example.invalid', 'Remote-Groups': 'admin,employee', }; const PAGES = ['/', '/workers', '/workflows', '/executions', '/quick', '/scheduler']; const VIEWPORTS = { desktop: { viewport: { width: 1440, height: 900 } }, phone: { ...devices['Pixel 7'] }, }; const isLocal = url => /^(https?|wss?):\/\/(127\.0\.0\.1|localhost)(:\d+)?\//.test(url); async function api(base, method, url, body) { const res = await fetch(base + url, { method, headers: { ...SSO, ...(body ? { 'Content-Type': 'application/json' } : {}) }, body: body ? JSON.stringify(body) : undefined, }); return res.ok ? res.json().catch(() => ({})) : null; } // The same data on both sides, so differences come from the code, not the data. async function seed(base) { const workers = (await api(base, 'GET', '/api/workers')) || []; for (const w of workers) { await api(base, 'POST', `/api/workers/${w.id}/command`, { command: `echo hello from ${w.name}` }); } if (workers[0]) await api(base, 'POST', `/api/workers/${workers[0].id}/command`, { command: 'echo oops >&2; exit 2' }); await api(base, 'POST', '/api/workflows', { name: 'Node health check', description: 'Uptime and disk on every worker', definition: { steps: [ { id: 'uptime', name: 'Uptime', type: 'execute', command: 'uptime', targets: ['all'] }, { id: 'disk', name: 'Disk', type: 'execute', command: 'df -h / | tail -1', targets: ['all'] }, ] }, }); await new Promise(r => setTimeout(r, 3000)); // let the commands finish } async function capture(browser, label, base, dir) { const shots = {}; for (const [vp, opts] of Object.entries(VIEWPORTS)) { // reducedMotion instead of injected CSS: Pulse's CSP rightly blocks inline styles. const ctx = await browser.newContext({ ...opts, extraHTTPHeaders: SSO, reducedMotion: 'reduce' }); await ctx.route(u => !isLocal(u.href), r => r.abort()); await ctx.addInitScript(() => { try { localStorage.setItem('lt_theme', 'dark'); sessionStorage.setItem('lt_boot', '1'); } catch (e) { /* ignore */ } }); const page = await ctx.newPage(); for (const p of PAGES) { await page.goto(base + p); await page.waitForLoadState('networkidle'); await page.waitForTimeout(500); const name = (p === '/' ? 'dashboard' : p.slice(1)) + '-' + vp; const file = path.join(dir, `${label}-${name}.png`); await page.screenshot({ path: file, fullPage: true }); shots[name] = file; } await ctx.close(); } return shots; } async function compose(browser, name, before, after, out) { const b64 = f => 'data:image/png;base64,' + fs.readFileSync(f).toString('base64'); const html = `

BEFORE (base) ${name}

AFTER (this PR) ${name}

`; const page = await browser.newPage({ viewport: { width: 1600, height: 800 } }); await page.setContent(html); await page.screenshot({ path: out, fullPage: true }); await page.close(); } (async () => { const [outDir, ...targets] = process.argv.slice(2); const urls = Object.fromEntries(targets.map(t => t.split('='))); if (!outDir || !urls.before || !urls.after) { console.error('usage: screenshots.js before= after='); process.exit(2); } const raw = path.join(outDir, 'raw'); fs.mkdirSync(raw, { recursive: true }); await Promise.all([seed(urls.before), seed(urls.after)]); const browser = await chromium.launch(); try { const before = await capture(browser, 'before', urls.before, raw); const after = await capture(browser, 'after', urls.after, raw); const unchanged = []; for (const name of Object.keys(after)) { if (before[name] && fs.readFileSync(before[name]).equals(fs.readFileSync(after[name]))) { unchanged.push(name); continue; } await compose(browser, name, before[name], after[name], path.join(outDir, `${name}.png`)); console.log(`changed: ${name}`); } fs.writeFileSync(path.join(outDir, 'unchanged.txt'), unchanged.join('\n') + '\n'); console.log(`unchanged: ${unchanged.length ? unchanged.join(', ') : 'none'}`); } finally { await browser.close(); } })().catch(e => { console.error(e); process.exit(1); });