Files
pulse/scripts/ci/screenshots.js
T
jaredandClaude Opus 5.5 74eb525413
CI / UI screenshots (before/after) (push) Skipped
CI / Security (npm audit) (push) Successful in 42s
CI / Test (jest) (push) Successful in 45s
CI / Integration (api) (push) Successful in 2m21s
CI / Scan (secrets, workflows, shell) (push) Successful in 2m35s
CI / Lint (eslint) (push) Successful in 20s
CI / Lint (eslint) (pull_request) Successful in 14s
CI / Test (jest) (pull_request) Successful in 24s
CI / Security (npm audit) (pull_request) Successful in 25s
CI / UI (playwright) (push) Successful in 2m20s
CI / Deploy (push) Skipped
CI / Deploy worker (push) Skipped
CI / Notify (push) Skipped
CI / Integration (api) (pull_request) Successful in 1m45s
CI / Scan (secrets, workflows, shell) (pull_request) Successful in 2m1s
CI / UI screenshots (before/after) (pull_request) Successful in 13s
CI / UI (playwright) (pull_request) Successful in 1m52s
CI / Deploy (pull_request) Skipped
CI / Deploy worker (pull_request) Skipped
CI / Notify (pull_request) Skipped
CI phase 5: stricter eslint, design system integrity check, PR screenshots
- eslint: errors for ==, unused or shadowed variables, eval and friends,
  prefer-const, curly, throw literals, useless catch/escape, constant
  conditions (loops allowed), unused expressions (short-circuit allowed);
  --max-warnings 0. Fixed the few violations (two unused requires in
  server.js, a shadowed mock argument in a test).
- Scan job: scripts/ci/check-web-template.sh fails if public/web_template
  differs from upstream at the commit in its VERSION (change it upstream,
  then scripts/sync-web-template.sh).
- New job UI screenshots (before/after), PRs only, when public/, views/
  or the render/nav code changed: runs the base branch and the PR side by
  side with the same seeded data and uploads one before/after image per
  page, desktop and phone (scripts/ci/screenshots.js). Not required.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 20:04:02 -05:00

127 lines
5.3 KiB
JavaScript

'use strict';
/* eslint-env node, browser */
// Before/after screenshots for UI pull requests (#45 phase 5).
//
// node scripts/ci/screenshots.js <out-dir> before=<url> after=<url>
//
// 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 (<page>-<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 = `<!doctype html><meta charset="utf-8"><style>
body{margin:0;background:#0b0e14;color:#ddd;font:16px monospace}
.row{display:flex;gap:16px;padding:16px;align-items:flex-start}
.col{flex:1;min-width:0} h2{margin:0 0 8px;font-size:16px}
.b h2{color:#ff7878}.a h2{color:#78ffa0} img{width:100%;border:1px solid #333}
</style><div class="row">
<div class="col b"><h2>BEFORE (base) ${name}</h2><img src="${b64(before)}"></div>
<div class="col a"><h2>AFTER (this PR) ${name}</h2><img src="${b64(after)}"></div>
</div>`;
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 <out-dir> before=<url> after=<url>');
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); });