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); const rows = page.locator('#survival-rates tr[data-group]'); assert(await rows.count(), 'visible report needs survival rows'); const roles = await rows.first().evaluate(row => { const table = row.closest('table'); const header = table.tHead?.rows[table.tHead.rows.length - 1] ?? table.rows[0]; return Array.from(header.cells, cell => { const text = cell.innerText.trim(); if (/surviv(?:ed|ors?)|生还|幸存|存活/i.test(text) && !/rate|%|率|\b(?:non[- ]?|not |did not )surviv|未(?:生还|幸存|存活)/i.test(text)) return 'survived'; if (/^(?:total|total (?:passenger count|passengers|people|group size|size|count|records)|all passengers)$|总数|总计|总人数|样本数/i.test(text)) return 'total'; if (/^(?:n|passengers|乘客数)$/i.test(text)) return 'total-fallback'; return null; }); }); const numbers = text => (text.match(/\d[\d,]*(?:\.\d+)?\s*%?/g) ?? []).map(value => { const number = parseFloat(value.replaceAll(',', '')); return value.includes('%') ? number / 100 : number; }); for (const row of await rows.all()) { assert(await row.isVisible(), 'survival row is hidden'); const text = await row.innerText(); const category = await row.getAttribute('data-category'); assert(text.includes(category), 'category exists only in hidden metadata'); const rate = Number(await row.getAttribute('data-rate')); const values = numbers(text); const cells = await row.locator('td, th').all(); for (const role of ['survived', 'total']) { const expected = Number(await row.getAttribute('data-' + role)); const exact = roles.indexOf(role); const column = exact >= 0 || role !== 'total' ? exact : roles.indexOf('total-fallback'); const visibleValues = column >= 0 ? (cells[column] && await cells[column].isVisible() ? numbers(await cells[column].innerText()) : []) : values; assert(visibleValues.includes(expected), `${role} count is wrong or missing for ${category}`); } assert(values.some(value => Math.abs(value - rate) <= 0.0006), 'visible rate disagrees with metadata'); } assert(await page.locator('#survival-chart').isVisible(), 'chart is hidden'); for (const bar of await page.locator('#survival-chart rect[data-rate]').all()) { const rate = Number(await bar.getAttribute('data-rate')); if (rate > 0) { const painted = await bar.evaluate(node => { const style = getComputedStyle(node); const context = document.createElement('canvas').getContext('2d'); const hasInk = paint => { if (paint === 'none') return false; if (paint.startsWith('url(')) return true; context.fillStyle = '#000'; context.fillStyle = paint; context.clearRect(0, 0, 1, 1); context.fillRect(0, 0, 1, 1); return context.getImageData(0, 0, 1, 1).data[3] > 0; }; return ((+style.fillOpacity > 0 && hasInk(style.fill)) || (+style.strokeOpacity > 0 && parseFloat(style.strokeWidth) > 0 && hasInk(style.stroke))) && (() => { for (let ancestor = node; ancestor; ancestor = ancestor.parentElement) { if (+getComputedStyle(ancestor).opacity === 0) return false; } return true; })(); }); assert(await bar.isVisible() && painted, 'positive-rate bar is hidden'); } } fs.mkdirSync('/logs/verifier/visual-audit', {recursive: true}); await page.screenshot({path: `/logs/verifier/visual-audit/${process.env.TITANIC_SCREENSHOT || 'titanic-report'}.png`, fullPage: true}); } finally { await browser.close(); } })().catch(error => {console.error(error); process.exit(1)});