|
| 1 | +/** |
| 2 | + * Renders every route on two deployments and diffs what came out. |
| 3 | + * |
| 4 | + * Useful when a change should not alter the site — a build tool swap, a |
| 5 | + * dependency bump — where "it still builds" says nothing about the result. |
| 6 | + * Asset filenames are normalised, since content hashes legitimately differ. |
| 7 | + * |
| 8 | + * Usage: node scripts/compare.mjs <baseUrlA> <baseUrlB> |
| 9 | + * e.g. node scripts/compare.mjs https://gpu.rocks http://localhost:4173 |
| 10 | + */ |
| 11 | +import { launch, ROUTES } from './browser.mjs'; |
| 12 | + |
| 13 | +const [a, b] = process.argv.slice(2).map(url => url && url.replace(/\/$/, '')); |
| 14 | +if (!a || !b) { |
| 15 | + console.error('Usage: node scripts/compare.mjs <baseUrlA> <baseUrlB>'); |
| 16 | + process.exit(2); |
| 17 | +} |
| 18 | + |
| 19 | +const browser = await launch(); |
| 20 | + |
| 21 | +// strip the content hash bundlers add, so image-1a2b3c4d.png === image.abcd1234.png |
| 22 | +const normaliseAsset = src => src.split('/').pop() |
| 23 | + .replace(/[-.][A-Za-z0-9_-]{8,}\.(png|jpe?g|gif|svg|webp)$/i, '.$1'); |
| 24 | + |
| 25 | +async function snapshot(base, route) { |
| 26 | + const page = await browser.newPage(); |
| 27 | + await page.goto(base + route, { waitUntil: 'networkidle2', timeout: 60000 }); |
| 28 | + await new Promise(resolve => setTimeout(resolve, 2500)); |
| 29 | + const data = await page.evaluate(() => ({ |
| 30 | + text: document.body.innerText.replace(/\s+/g, ' ').trim(), |
| 31 | + links: Array.from(document.querySelectorAll('a')).map(el => el.getAttribute('href')).filter(Boolean).sort(), |
| 32 | + images: Array.from(document.images).map(img => img.src), |
| 33 | + })); |
| 34 | + await page.close(); |
| 35 | + return { |
| 36 | + ...data, |
| 37 | + // data: URIs mean the bundler inlined a small asset; count them rather than |
| 38 | + // comparing their contents |
| 39 | + images: data.images.map(src => src.startsWith('data:') ? '<inlined>' : normaliseAsset(src)).sort(), |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +let differences = 0; |
| 44 | +for (const route of ROUTES) { |
| 45 | + const [x, y] = [await snapshot(a, route), await snapshot(b, route)]; |
| 46 | + const compare = (name, left, right) => { |
| 47 | + const same = JSON.stringify(left) === JSON.stringify(right); |
| 48 | + if (!same) differences++; |
| 49 | + return { name, same, left, right }; |
| 50 | + }; |
| 51 | + const results = [ |
| 52 | + compare('text', x.text, y.text), |
| 53 | + compare('links', x.links, y.links), |
| 54 | + compare('images', x.images, y.images), |
| 55 | + ]; |
| 56 | + |
| 57 | + console.log(route); |
| 58 | + for (const r of results) { |
| 59 | + const size = Array.isArray(r.left) ? `${r.left.length} vs ${r.right.length}` : `${r.left.length} vs ${r.right.length} chars`; |
| 60 | + console.log(` ${r.name}: ${r.same ? 'identical' : 'DIFFERS'} (${size})`); |
| 61 | + if (r.same) continue; |
| 62 | + if (Array.isArray(r.left)) { |
| 63 | + const onlyA = r.left.filter(v => !r.right.includes(v)); |
| 64 | + const onlyB = r.right.filter(v => !r.left.includes(v)); |
| 65 | + if (onlyA.length) console.log(` only in A: ${onlyA.slice(0, 8).join(', ')}`); |
| 66 | + if (onlyB.length) console.log(` only in B: ${onlyB.slice(0, 8).join(', ')}`); |
| 67 | + } else { |
| 68 | + const words = (p, q) => p.split(' ').filter(w => !q.includes(w)).slice(0, 20).join(' '); |
| 69 | + console.log(` only in A: "${words(r.left, r.right).slice(0, 150)}"`); |
| 70 | + console.log(` only in B: "${words(r.right, r.left).slice(0, 150)}"`); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +await browser.close(); |
| 76 | +console.log(differences ? `\n${differences} difference(s)` : '\nA and B render identically'); |
| 77 | +process.exit(differences ? 1 : 0); |
0 commit comments