-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathregression.js
147 lines (138 loc) · 4.16 KB
/
regression.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* @typedef {import('playwright').Page} Page
* @typedef {import('playwright').PageScreenshotOptions} PageScreenshotOptions
*/
import fs from 'node:fs/promises';
import http from 'http';
import os from 'os';
import path from 'path';
import pixelmatch from 'pixelmatch';
import { chromium } from 'playwright';
import { PNG } from 'pngjs';
import { fileURLToPath } from 'url';
import { optimize } from '../lib/svgo.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const PORT = 5001;
const WIDTH = 960;
const HEIGHT = 720;
/** @type {PageScreenshotOptions} */
const screenshotOptions = {
omitBackground: true,
clip: { x: 0, y: 0, width: WIDTH, height: HEIGHT },
animations: 'disabled',
};
/**
* @param {string[]} list
* @returns {Promise<boolean>}
*/
const runTests = async (list) => {
let mismatched = 0;
let passed = 0;
console.info('Start browser…');
/**
* @param {Page} page
* @param {string} name
*/
const processFile = async (page, name) => {
await page.goto(`http://localhost:${PORT}/original/${name}`);
const originalBuffer = await page.screenshot(screenshotOptions);
await page.goto(`http://localhost:${PORT}/optimized/${name}`);
const optimizedBufferPromise = page.screenshot(screenshotOptions);
const writeDiffs = process.env.NO_DIFF == null;
const diff = writeDiffs && new PNG({ width: WIDTH, height: HEIGHT });
const originalPng = PNG.sync.read(originalBuffer);
const optimizedPng = PNG.sync.read(await optimizedBufferPromise);
const matched = pixelmatch(
originalPng.data,
optimizedPng.data,
diff ? diff.data : null,
WIDTH,
HEIGHT,
);
// ignore small aliasing issues
if (matched <= 4) {
passed++;
} else {
mismatched++;
console.error(`${name} is mismatched`);
if (diff) {
const file = path.join(
__dirname,
'regression-diffs',
`${name}.diff.png`,
);
await fs.mkdir(path.dirname(file), { recursive: true });
await fs.writeFile(file, PNG.sync.write(diff));
}
}
};
const worker = async () => {
let item;
const page = await context.newPage();
while ((item = list.pop())) {
await processFile(page, item);
}
await page.close();
};
const browser = await chromium.launch();
const context = await browser.newContext({
javaScriptEnabled: false,
viewport: { width: WIDTH, height: HEIGHT },
});
await Promise.all(
Array.from(new Array(os.cpus().length * 2), () => worker()),
);
await browser.close();
console.info(`Mismatched: ${mismatched}`);
console.info(`Passed: ${passed}`);
return mismatched === 0;
};
(async () => {
try {
const start = process.hrtime.bigint();
const fixturesDir = path.join(__dirname, 'regression-fixtures');
const filesPromise = fs.readdir(fixturesDir, { recursive: true });
const server = http.createServer(async (req, res) => {
const name = req.url.slice(req.url.indexOf('/', 1));
let file;
try {
file = await fs.readFile(path.join(fixturesDir, name), 'utf-8');
} catch {
res.statusCode = 404;
res.end();
return;
}
if (req.url.startsWith('/original/')) {
res.setHeader('Content-Type', 'image/svg+xml');
res.end(file);
return;
}
if (req.url.startsWith('/optimized/')) {
const optimized = optimize(file, {
floatPrecision: 4,
});
res.setHeader('Content-Type', 'image/svg+xml');
res.end(optimized.data);
return;
}
throw new Error(`unknown path ${req.url}`);
});
await new Promise((resolve) => {
server.listen(PORT, resolve);
});
const list = (await filesPromise).filter((name) => name.endsWith('.svg'));
const passed = await runTests(list);
server.close();
const end = process.hrtime.bigint();
const diff = (end - start) / BigInt(1e6);
if (passed) {
console.info(`Regression tests successfully completed in ${diff}ms`);
} else {
console.error(`Regression tests failed in ${diff}ms`);
process.exit(1);
}
} catch (error) {
console.error(error);
process.exit(1);
}
})();