|
| 1 | +import fs from 'node:fs/promises'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | +import pixelmatch from 'pixelmatch'; |
| 5 | +import { chromium } from 'playwright'; |
| 6 | +import { PNG } from 'pngjs'; |
| 7 | +import { expectMismatch, ignore, skip } from './file-lists.js'; |
| 8 | +import { pathToPosix, printReport } from './lib.js'; |
| 9 | +import { |
| 10 | + readReport, |
| 11 | + readVersion, |
| 12 | + REGRESSION_DIFFS_PATH, |
| 13 | + REGRESSION_FIXTURES_PATH, |
| 14 | + REGRESSION_OPTIMIZED_PATH, |
| 15 | + writeReport, |
| 16 | +} from './regression-io.js'; |
| 17 | + |
| 18 | +const NAVIGATION_TIMEOUT_MS = 0; |
| 19 | +const WIDTH = 960; |
| 20 | +const HEIGHT = 720; |
| 21 | + |
| 22 | +/** @type {import('playwright').PageScreenshotOptions} */ |
| 23 | +const screenshotOptions = { |
| 24 | + omitBackground: true, |
| 25 | + clip: { x: 0, y: 0, width: WIDTH, height: HEIGHT }, |
| 26 | + animations: 'disabled', |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * @param {ReadonlyArray<string>} list |
| 31 | + * @returns {Promise<Omit<import('./regression-io.js').TestReport, 'metrics' | 'checksums'>>} |
| 32 | + */ |
| 33 | +const runTests = async (list) => { |
| 34 | + const version = await readVersion(); |
| 35 | + const listCopy = [...list]; |
| 36 | + |
| 37 | + /** @type {Omit<import('./regression-io.js').TestReport, 'metrics' | 'checksums'>} */ |
| 38 | + const report = { |
| 39 | + version, |
| 40 | + files: { |
| 41 | + toMatch: listCopy.length - expectMismatch.length - ignore.length, |
| 42 | + toMismatch: expectMismatch.length, |
| 43 | + toIgnore: ignore.length, |
| 44 | + toSkip: skip.length, |
| 45 | + }, |
| 46 | + results: { |
| 47 | + match: 0, |
| 48 | + expectMismatch: 0, |
| 49 | + ignored: 0, |
| 50 | + }, |
| 51 | + errors: { |
| 52 | + shouldHaveMatched: [], |
| 53 | + shouldHaveMismatched: [], |
| 54 | + }, |
| 55 | + }; |
| 56 | + |
| 57 | + const totalFiles = listCopy.length; |
| 58 | + let tested = 0; |
| 59 | + |
| 60 | + /** |
| 61 | + * @param {import('playwright').Page} page |
| 62 | + * @param {string} name |
| 63 | + */ |
| 64 | + const processFile = async (page, name) => { |
| 65 | + await page.goto(`file://${path.join(REGRESSION_FIXTURES_PATH, name)}`); |
| 66 | + const originalBuffer = await page.screenshot(screenshotOptions); |
| 67 | + |
| 68 | + await page.goto(`file://${path.join(REGRESSION_OPTIMIZED_PATH, name)}`); |
| 69 | + const optimizedBufferPromise = page.screenshot(screenshotOptions); |
| 70 | + |
| 71 | + const writeDiffs = process.env.NO_DIFF == null; |
| 72 | + const diff = writeDiffs ? new PNG({ width: WIDTH, height: HEIGHT }) : null; |
| 73 | + const originalPng = PNG.sync.read(originalBuffer); |
| 74 | + const optimizedPng = PNG.sync.read(await optimizedBufferPromise); |
| 75 | + const matched = pixelmatch( |
| 76 | + originalPng.data, |
| 77 | + optimizedPng.data, |
| 78 | + diff?.data, |
| 79 | + WIDTH, |
| 80 | + HEIGHT, |
| 81 | + ); |
| 82 | + |
| 83 | + // ignore small aliasing issues |
| 84 | + const isMatch = matched <= 4; |
| 85 | + const namePosix = pathToPosix(name); |
| 86 | + const expectedToMismatch = expectMismatch.includes(namePosix); |
| 87 | + |
| 88 | + if (isMatch) { |
| 89 | + if (expectedToMismatch) { |
| 90 | + report.errors.shouldHaveMismatched.push(namePosix); |
| 91 | + } else if (ignore.includes(namePosix)) { |
| 92 | + report.results.ignored++; |
| 93 | + } else { |
| 94 | + report.results.match++; |
| 95 | + } |
| 96 | + } else { |
| 97 | + if (expectedToMismatch) { |
| 98 | + report.results.expectMismatch++; |
| 99 | + } else if (!ignore.includes(namePosix)) { |
| 100 | + report.errors.shouldHaveMatched.push(namePosix); |
| 101 | + } |
| 102 | + |
| 103 | + if (diff) { |
| 104 | + const file = path.join(REGRESSION_DIFFS_PATH, `${name}.diff.png`); |
| 105 | + await fs.mkdir(path.dirname(file), { recursive: true }); |
| 106 | + await fs.writeFile(file, PNG.sync.write(diff)); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (process.stdout.isTTY) { |
| 111 | + process.stdout.clearLine(0); |
| 112 | + process.stdout.write( |
| 113 | + `\rCompared ${(++tested).toLocaleString()} of ${totalFiles.toLocaleString()}…`, |
| 114 | + ); |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + const worker = async () => { |
| 119 | + let item; |
| 120 | + const page = await context.newPage(); |
| 121 | + while ((item = listCopy.pop())) { |
| 122 | + await processFile(page, item); |
| 123 | + } |
| 124 | + await page.close(); |
| 125 | + }; |
| 126 | + |
| 127 | + const browser = await chromium.launch(); |
| 128 | + const context = await browser.newContext({ |
| 129 | + javaScriptEnabled: false, |
| 130 | + viewport: { width: WIDTH, height: HEIGHT }, |
| 131 | + }); |
| 132 | + context.setDefaultTimeout(NAVIGATION_TIMEOUT_MS); |
| 133 | + |
| 134 | + await Promise.all( |
| 135 | + Array.from(new Array(os.cpus().length * 2), () => worker()), |
| 136 | + ); |
| 137 | + await browser.close(); |
| 138 | + |
| 139 | + if (process.stdout.isTTY) { |
| 140 | + console.log(); |
| 141 | + } |
| 142 | + |
| 143 | + return report; |
| 144 | +}; |
| 145 | + |
| 146 | +(async () => { |
| 147 | + try { |
| 148 | + const filesPromise = fs.readdir(REGRESSION_FIXTURES_PATH, { |
| 149 | + recursive: true, |
| 150 | + }); |
| 151 | + const list = (await filesPromise).filter((name) => name.endsWith('.svg')); |
| 152 | + |
| 153 | + const report = await runTests(list); |
| 154 | + const metrics = await readReport(); |
| 155 | + const combinedReport = { |
| 156 | + ...report, |
| 157 | + ...metrics, |
| 158 | + }; |
| 159 | + |
| 160 | + printReport( |
| 161 | + /** @type {import('./regression-io.js').TestReport}*/ (combinedReport), |
| 162 | + ); |
| 163 | + await writeReport(combinedReport); |
| 164 | + |
| 165 | + const failed = |
| 166 | + report.results.match !== report.files.toMatch || |
| 167 | + report.results.expectMismatch !== report.files.toMismatch; |
| 168 | + |
| 169 | + if (failed) { |
| 170 | + process.exit(1); |
| 171 | + } |
| 172 | + } catch (error) { |
| 173 | + console.error(error); |
| 174 | + process.exit(1); |
| 175 | + } |
| 176 | +})(); |
0 commit comments