The console gate had never looked at anything but a 1280 px desktop in light
mode, and I had recorded that as a gap twice. Probing it properly found two
real defects, both with numbers before and after.
Contrast. The page declares color-scheme: light dark, so Chromium paints a dark
canvas — but .ok and .warn were fixed colours chosen for white paper. The
status line, which is the only feedback the page gives ("token rejected" vs
"refreshed"), measured 2.82:1 against rgb(18,18,18). Below WCAG AA. The two
colours now have dark-scheme values; the same measurement reads 11.85:1.
Width. At a 375 px viewport the seven-column tables measured 501 px and pushed
the whole document to 520 px of sideways scroll — the token field and the
summary pills slid off screen, on the one device an operator reaches for when
they have blocked themselves. Each table now scrolls inside its own box; the
document measures 375 against a 375 viewport.
The gate covers both, so the gap cannot silently reopen: console-check.cjs runs
a second pass at 375x720 in dark mode and asserts the document does not
overflow, the status line clears 4.5:1 against the Canvas the browser actually
paints (computed, not eyeballed), the standing streak is still listed, the
token is still absent from the markup, and no javascript threw. Twelve
assertions pass on this commit.
Also fixed while here: SCREENSHOT without an extension made Chromium throw
"unsupported mime type", which reads like a page failure and is not one.
Gates on this commit: scripts/ci-gates.sh all — build 0 errors, unit 54/54,
integration 27/27, smoke 19/19, image built, compose-smoke 27/27; console gate
12/12 against the running image.
142 lines
7.0 KiB
JavaScript
142 lines
7.0 KiB
JavaScript
// The console is a page, and a page fails in ways curl cannot see: a typo in the
|
|
// inline script, a fetch that 401s, a table that renders nothing while the
|
|
// summary looks fine. This drives it in a real Chromium and fails the run when
|
|
// the page is wrong, or when the page throws.
|
|
//
|
|
// Run it through scripts/console-smoke.sh, which starts the stack and seeds the
|
|
// state the assertions look for. It needs playwright-core: point PLAYWRIGHT_CORE
|
|
// at it, or install it locally and it resolves by name.
|
|
|
|
const EDGE = process.env.EDGE_URL || 'http://127.0.0.1:8080';
|
|
const TOKEN = process.env.OPENWAF_ADMIN_TOKEN || '';
|
|
// A path without an extension makes Chromium throw "unsupported mime type",
|
|
// which reads like a page failure and is not one.
|
|
const SCREENSHOT = process.env.SCREENSHOT
|
|
? (/\.[a-z0-9]+$/i.test(process.env.SCREENSHOT) ? process.env.SCREENSHOT : process.env.SCREENSHOT + '.png')
|
|
: '';
|
|
const SEED_SOURCE = process.env.SEED_SOURCE || '';
|
|
const SEED_BLOCK = process.env.SEED_BLOCK || '';
|
|
|
|
let chromium;
|
|
try {
|
|
chromium = (process.env.PLAYWRIGHT_CORE
|
|
? require(process.env.PLAYWRIGHT_CORE)
|
|
: require('playwright-core')).chromium;
|
|
} catch (e) {
|
|
console.error('playwright-core is not resolvable. Set PLAYWRIGHT_CORE=/path/to/playwright-core,');
|
|
console.error('or install it. This gate does not skip quietly: a run that asserts nothing');
|
|
console.error('and reports success is worse than no gate at all.');
|
|
process.exit(2);
|
|
}
|
|
|
|
const failures = [];
|
|
const check = (name, ok, detail) => {
|
|
console.log(` ${ok ? 'ok ' : 'FAIL'} ${name}${detail ? ' — ' + detail : ''}`);
|
|
if (!ok) failures.push(name);
|
|
};
|
|
|
|
// WCAG relative luminance, so the assertion is a number and not an opinion.
|
|
const luminance = ([r, g, b]) => {
|
|
const f = (c) => { c /= 255; return c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4); };
|
|
return 0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b);
|
|
};
|
|
const contrast = (a, b) => {
|
|
const [x, y] = [luminance(a), luminance(b)].sort((p, q) => q - p);
|
|
return (x + 0.05) / (y + 0.05);
|
|
};
|
|
const rgb = (s) => s.match(/\d+/g).slice(0, 3).map(Number);
|
|
|
|
(async () => {
|
|
const browser = await chromium.launch({ headless: true });
|
|
const page = await browser.newPage({ viewport: { width: 1280, height: 1100 } });
|
|
const jsProblems = [];
|
|
page.on('pageerror', (e) => jsProblems.push('pageerror: ' + e.message));
|
|
page.on('console', (m) => { if (m.type() === 'error') jsProblems.push('console.error: ' + m.text()); });
|
|
|
|
await page.goto(`${EDGE}/admin`, { waitUntil: 'domcontentloaded' });
|
|
await page.fill('#token', TOKEN);
|
|
await page.click('#save');
|
|
await page.waitForFunction(
|
|
() => /refreshed/.test(document.getElementById('status').textContent),
|
|
null, { timeout: 15000 });
|
|
|
|
const summary = (await page.textContent('#summary')).replace(/\s+/g, ' ').trim();
|
|
const cells = (sel) => page.$$eval(sel, (rows) => rows.map((tr) => [...tr.children].map((td) => td.textContent)));
|
|
const streakRows = await cells('#streaks tbody tr');
|
|
const blockRows = await cells('#blocks tbody tr');
|
|
const obsRows = await page.$$eval('#obs tbody tr', (r) => r.length);
|
|
const countryRows = await page.$$eval('#countries tbody tr', (r) => r.length);
|
|
const html = await page.content();
|
|
if (SCREENSHOT) await page.screenshot({ path: SCREENSHOT, fullPage: true });
|
|
|
|
check('the page authenticates and reads the state', /mode Enforce/.test(summary), summary);
|
|
check('a standing streak is listed as current/threshold, not as a historical maximum',
|
|
streakRows.some((r) => r[0] === SEED_SOURCE && /2 \/ 3/.test(r[1] || '')),
|
|
JSON.stringify(streakRows.map((r) => [r[0], r[1]])));
|
|
check('the operator block is listed with its reason',
|
|
blockRows.some((r) => r[0] === SEED_BLOCK && /console gate/.test(r[2] || '')),
|
|
JSON.stringify(blockRows.map((r) => [r[0], r[2]])));
|
|
check('durable observations are listed', obsRows > 0, `${obsRows} rows`);
|
|
check('the per-country summary is listed', countryRows > 0, `${countryRows} rows`);
|
|
check('the token never appears in the markup', !html.includes(TOKEN));
|
|
check('the page threw no javascript', jsProblems.length === 0, jsProblems.join(' | '));
|
|
|
|
// A phone in dark mode is the case an operator actually reaches the console
|
|
// from — the address that got them blocked is the one they are typing about.
|
|
// Both halves of this used to be broken and measured: the page overflowed a
|
|
// 375 px viewport to 520 px of sideways scroll, and the status line sat on the
|
|
// dark canvas at 2.82:1.
|
|
{
|
|
const phone = await browser.newPage({ viewport: { width: 375, height: 720 }, colorScheme: 'dark' });
|
|
const phoneProblems = [];
|
|
phone.on('pageerror', (e) => phoneProblems.push('pageerror: ' + e.message));
|
|
phone.on('console', (m) => { if (m.type() === 'error') phoneProblems.push('console.error: ' + m.text()); });
|
|
|
|
await phone.goto(`${EDGE}/admin`, { waitUntil: 'domcontentloaded' });
|
|
await phone.fill('#token', TOKEN);
|
|
await phone.click('#save');
|
|
await phone.waitForFunction(
|
|
() => /refreshed/.test(document.getElementById('status').textContent),
|
|
null, { timeout: 15000 });
|
|
|
|
const m = await phone.evaluate(() => {
|
|
const probe = document.createElement('div');
|
|
probe.style.cssText = 'position:absolute;left:-9999px;background:Canvas;color:CanvasText';
|
|
document.body.appendChild(probe);
|
|
const cs = getComputedStyle(probe);
|
|
const canvas = cs.backgroundColor;
|
|
const statusColor = getComputedStyle(document.getElementById('status')).color;
|
|
probe.remove();
|
|
const streakRow = [...document.querySelectorAll('#streaks tbody tr')]
|
|
.map((tr) => [...tr.children].map((td) => td.textContent));
|
|
return {
|
|
canvas, statusColor,
|
|
scrollWidth: document.documentElement.scrollWidth,
|
|
innerWidth: window.innerWidth,
|
|
html: document.documentElement.outerHTML,
|
|
streakRow,
|
|
};
|
|
});
|
|
|
|
const ratio = contrast(rgb(m.statusColor), rgb(m.canvas));
|
|
check('the page fits a 375 px phone without sideways document scroll',
|
|
m.scrollWidth <= m.innerWidth, `scrollWidth ${m.scrollWidth} vs viewport ${m.innerWidth}`);
|
|
check('the status line is readable on the dark canvas (WCAG AA 4.5:1)',
|
|
ratio >= 4.5, `contrast ${ratio.toFixed(2)}:1, status ${m.statusColor} on ${m.canvas}`);
|
|
check('the standing streak is still listed at phone width',
|
|
m.streakRow.some((r) => r[0] === SEED_SOURCE && /2 \/ 3/.test(r[1] || '')),
|
|
JSON.stringify(m.streakRow.map((r) => [r[0], r[1]])));
|
|
check('the token is not in the markup at phone width either', !m.html.includes(TOKEN));
|
|
check('the page threw no javascript at phone width', phoneProblems.length === 0, phoneProblems.join(' | '));
|
|
if (SCREENSHOT) await phone.screenshot({ path: SCREENSHOT.replace(/(\.[a-z0-9]+)$/i, '.phone$1'), fullPage: true });
|
|
await phone.close();
|
|
}
|
|
|
|
await browser.close();
|
|
if (failures.length) {
|
|
console.error(`console check: ${failures.length} assertion(s) failed`);
|
|
process.exit(1);
|
|
}
|
|
console.log('console check: all assertions passed');
|
|
})().catch((e) => { console.error('console check failed to run:', e.message); process.exit(1); });
|