File size: 4,624 Bytes
ef987bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | const {chromium} = require('playwright');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const {pathToFileURL} = require('node:url');
(async () => {
const browser = await chromium.launch({headless: true, args: ['--no-sandbox']});
try {
const page = await browser.newPage({viewport: {width: 1100, height: 760}});
await page.route(/^https?:/, route => route.abort());
await page.goto(pathToFileURL(path.join(process.cwd(), 'report.html')).href);
for (const kind of ['annual', 'genre', 'tempo']) {
const chart = page.locator(`svg[data-chart="${kind}"]`);
assert(await chart.isVisible(), `${kind} chart is hidden`);
const failures = await chart.evaluate(svg => {
const bounds = svg.getBoundingClientRect();
const failures = [];
const color = document.createElement('canvas').getContext('2d');
const hasPaint = paint => {
if (paint === 'none') return false;
const gradientRef = /^url\(["']?#([^"')]+)["']?\)$/.exec(paint);
if (gradientRef) {
const gradient = document.getElementById(gradientRef[1]);
if (gradient?.matches('linearGradient, radialGradient')) {
const stops = [...gradient.querySelectorAll('stop')];
if (stops.length) return stops.some(stop => {
const style = getComputedStyle(stop);
return Number(style.stopOpacity) > 0 && hasPaint(style.stopColor);
});
}
return true; // Patterns and nested references can paint without a local stop.
}
color.fillStyle = '#000';
color.fillStyle = paint;
color.clearRect(0, 0, 1, 1);
color.fillRect(0, 0, 1, 1);
return color.getImageData(0, 0, 1, 1).data[3] > 0;
};
const painted = style =>
(Number(style.fillOpacity) > 0 && hasPaint(style.fill)) ||
(Number(style.strokeOpacity) > 0 && parseFloat(style.strokeWidth) > 0 && hasPaint(style.stroke));
for (const bar of svg.querySelectorAll('rect[data-key]')) {
const box = bar.getBoundingClientRect();
const label = [...svg.querySelectorAll('[data-value-label]')].find(element => element.getAttribute('data-value-label') === bar.dataset.key);
if (!label || !Number.isFinite(Number(label.textContent.trim())) || Math.abs(Number(label.textContent.trim()) - Number(bar.dataset.value)) > 0.0001) {
failures.push(`missing or incorrect numeric label ${bar.dataset.key}`);
} else {
const labelBox = label.getBoundingClientRect();
const glyphs = [label, ...label.querySelectorAll('text, tspan, textPath')];
const visibleInk = glyphs.some(node => {
const style = getComputedStyle(node);
return node.getClientRects().length && style.visibility === 'visible' && Number(style.opacity) > 0 && painted(style);
});
if (labelBox.width <= 0 || labelBox.height <= 0 || !visibleInk) failures.push(`invisible label ${bar.dataset.key}`);
for (let ancestor = label; ancestor; ancestor = ancestor.parentElement) {
const style = getComputedStyle(ancestor);
if (style.display === 'none' || style.visibility === 'hidden' || Number(style.opacity) === 0) failures.push(`hidden label ${bar.dataset.key}`);
}
}
if (Number(bar.dataset.value) === 0) continue;
let el = bar;
while (el) {
const style = getComputedStyle(el);
if (style.display === 'none' || style.visibility === 'hidden' || Number(style.opacity) === 0)
failures.push(`hidden ${bar.dataset.key}`);
el = el.parentElement;
}
if (box.width <= 0 || box.height <= 0 || !painted(getComputedStyle(bar))) failures.push(`unpainted ${bar.dataset.key}`);
if (box.left < bounds.left - 1 || box.right > bounds.right + 1 || box.top < bounds.top - 1 || box.bottom > bounds.bottom + 1)
failures.push(`clipped ${bar.dataset.key}`);
}
return failures;
});
assert.deepEqual(failures, [], `${kind} plot must paint visible data bars inside its own viewport`);
}
fs.mkdirSync('/logs/verifier/visual-audit', {recursive: true});
await page.screenshot({path: `/logs/verifier/visual-audit/${process.env.SPOTIFY_SCREENSHOT || 'spotify-report'}.png`, fullPage: true});
} finally {
await browser.close();
}
})().catch(error => {console.error(error); process.exit(1)});
|