Files

112 lines
4.8 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
// Before/after screenshots for UI pull requests.
//
// node tests/ui/screenshots.js <out-dir> before=<url> after=<url>
//
// Both instances run tests/ui/stack.sh, so they hold the same seed data and
// differences come from the code. Captures each page on desktop (dark and
// light theme) and on a phone, then composes one side-by-side image per page
// and view (<page>-<view>.png), rendered in the browser so no image tooling is
// needed. Views that look identical are listed in unchanged.txt.
const fs = require('fs');
const path = require('path');
const { chromium, devices } = require('@playwright/test');
const { SSO, isLocal } = require('./fixtures');
// The day after the seed's timestamps, so "1d ago" reads the same on both sides.
const NOW = new Date('2026-11-21T09:00:00Z');
const PAGES = {
dashboard: '/',
'ticket-markdown': '/ticket/100000001',
'ticket-alert': '/ticket/100000002',
'ticket-plain': '/ticket/100000003',
'ticket-create': '/ticket/create',
};
const VIEWS = {
dark: { theme: 'dark', opts: { viewport: { width: 1280, height: 900 } } },
light: { theme: 'light', opts: { viewport: { width: 1280, height: 900 } } },
phone: { theme: 'dark', opts: { ...devices['Pixel 7'] } },
};
async function capture(browser, label, base, dir) {
const shots = {};
for (const [view, { theme, opts }] of Object.entries(VIEWS)) {
const ctx = await browser.newContext({ ...opts, colorScheme: theme, extraHTTPHeaders: SSO, reducedMotion: 'reduce', timezoneId: 'UTC' });
await ctx.route(u => !isLocal(u.href), r => r.abort());
await ctx.addInitScript(t => {
try { localStorage.setItem('lt_theme', t); } catch (e) { /* ignore */ }
const get = Storage.prototype.getItem;
Storage.prototype.getItem = function (k) {
return String(k).startsWith('lt_booted_') ? '1' : get.call(this, k);
};
}, theme);
const page = await ctx.newPage();
await page.clock.setFixedTime(NOW);
for (const [name, p] of Object.entries(PAGES)) {
await page.goto(base + p);
await page.waitForLoadState('networkidle');
// Every image settled: loaded, or (avatars without an lldap photo)
// swapped for the initials fallback.
await page.waitForFunction(() => [...document.images].every(i => i.complete
&& (i.naturalWidth > 0 || !i.closest('.lt-avatar') || i.classList.contains('lt-avatar-img-err'))), null, { timeout: 5000 })
.catch(() => {});
await page.evaluate(t => document.documentElement.setAttribute('data-theme', t), theme);
await page.waitForTimeout(500);
const key = `${name}-${view}`;
const file = path.join(dir, `${label}-${key}.png`);
await page.screenshot({ path: file, fullPage: true });
shots[key] = 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 });
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); });