const fs = require('fs'); const path = require('path'); const {pathToFileURL} = require('url'); const {chromium} = require('playwright'); function assert(condition, message) { if (!condition) throw new Error(message); } (async () => { const images = fs.readFileSync('train-images-idx3-ubyte'); const labels = fs.readFileSync('train-labels-idx1-ubyte'); const browser = await chromium.launch({headless: true}); try { const page = await browser.newPage({viewport: {width: 1100, height: 760}}); const errors = []; page.on('pageerror', error => errors.push(error.message)); await page.goto(pathToFileURL(path.resolve('report.html')).href); const previews = await page.locator('figure').evaluateAll(figures => figures.map(figure => { const visible = element => { for (let node = element; node; node = node.parentElement) { const style = getComputedStyle(node); if (style.display === 'none' || style.visibility === 'hidden' || Number(style.opacity) === 0) return false; } const rect = element.getBoundingClientRect(); return rect.width > 0 && rect.height > 0; }; return { text: figure.innerText, visible: visible(figure), pixels: [...figure.querySelectorAll('svg rect')].map(rect => { const box = rect.getBoundingClientRect(); return {x: box.x, y: box.y, width: box.width, height: box.height, fill: getComputedStyle(rect).fill, visible: visible(rect)}; }), }; })); assert(previews.length >= 2, 'show at least two sample figures'); for (const figure of previews.slice(0, 2)) { const match = /index\s+(\d+)\s*,\s*label\s+(\d+)/i.exec(figure.text); assert(match && figure.visible, 'sample index and label must be visible'); const index = Number(match[1]); assert(Number(match[2]) === labels[8 + index], 'visible sample label disagrees with IDX'); assert(figure.pixels.length === 64, 'sample needs all 64 pixels'); const first = figure.pixels[0]; for (const [offset, pixel] of figure.pixels.entries()) { const shade = 255 - images[16 + index * 64 + offset]; assert(pixel.visible && pixel.fill === `rgb(${shade}, ${shade}, ${shade})`, 'actual rendered sample color disagrees with IDX'); const near = (actual, expected) => Math.abs(actual - expected) <= 0.75; assert(near(pixel.width, first.width) && near(pixel.height, first.height) && near(pixel.x, first.x + offset % 8 * first.width) && near(pixel.y, first.y + Math.floor(offset / 8) * first.height), 'sample pixels must form the claimed row-major 8x8 image without overlaps or gaps'); } } assert(errors.length === 0, 'browser errors: ' + errors.join(' | ')); fs.mkdirSync('/logs/verifier/visual-audit', {recursive: true}); await page.screenshot({path: '/logs/verifier/visual-audit/tiny-mlp-report.png', fullPage: true}); } finally { await browser.close(); } console.log('Rendered IDX sample images and report passed'); })().catch(error => {console.error(error); process.exit(1);});