summaryrefslogtreecommitdiff
path: root/tools/chrome.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'tools/chrome.mjs')
-rw-r--r--tools/chrome.mjs119
1 files changed, 119 insertions, 0 deletions
diff --git a/tools/chrome.mjs b/tools/chrome.mjs
new file mode 100644
index 0000000..e3361e9
--- /dev/null
+++ b/tools/chrome.mjs
@@ -0,0 +1,119 @@
+// Headless Chrome (software Canvas2D, no GPU needed) on a built web page,
+// for tests: serves `dir` (plus `extra` files by URL path), opens it at
+// `width` x `height`, waits for `window.sketch`, and hands `run` an
+// `evaluate(expression)` that returns the value. Uncaught page errors fail.
+import {spawn} from 'node:child_process';
+import {createServer} from 'node:http';
+import {mkdtemp, readFile, rm} from 'node:fs/promises';
+import {tmpdir} from 'node:os';
+import {extname, join} from 'node:path';
+import assert from 'node:assert/strict';
+
+export async function withPage({dir, extra = {}, width, height, chromePath}, run) {
+ const types = {'.html': 'text/html', '.js': 'text/javascript', '.wasm': 'application/wasm', '.png': 'image/png'};
+ const server = createServer(async (request, response) => {
+ const path = new URL(request.url, 'http://x').pathname;
+ try {
+ const body = extra[path] ? await readFile(extra[path]) : await readFile(join(dir, path === '/' ? 'index.html' : path));
+ response.writeHead(200, {'content-type': types[extname(path)] ?? 'text/html'});
+ response.end(body);
+ } catch {
+ response.writeHead(404);
+ response.end();
+ }
+ });
+ await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
+ const url = `http://127.0.0.1:${server.address().port}/`;
+ const profile = await mkdtemp(join(tmpdir(), 'sketchbook-chrome-'));
+ const chrome = spawn(chromePath || process.env.CHROME || 'google-chrome-stable', ['--headless=new', '--no-sandbox', '--disable-gpu', '--no-first-run',
+ '--disable-background-networking', '--remote-debugging-port=0', `--user-data-dir=${profile}`, 'about:blank'], {stdio: ['ignore', 'ignore', 'pipe']});
+ const sleep = ms => new Promise(r => setTimeout(r, ms));
+ let socket, stderr = '', sequence = 0;
+ const pending = new Map(), errors = [];
+ try {
+ chrome.stderr.on('data', b => stderr += b);
+ let endpoint;
+ for (let i = 0; i < 100 && !endpoint; i++) {
+ endpoint = stderr.match(/DevTools listening on (ws:\/\/\S+)/)?.[1];
+ if (chrome.exitCode !== null) throw new Error(stderr);
+ if (!endpoint) await sleep(100);
+ }
+ assert.ok(endpoint, 'Chrome did not start');
+ const tabs = await (await fetch(`${endpoint.replace(/^ws:/, 'http:').split('/devtools/')[0]}/json/list`)).json();
+ socket = new WebSocket(tabs.find(t => t.type === 'page').webSocketDebuggerUrl);
+ await new Promise((resolve, reject) => { socket.onopen = resolve; socket.onerror = reject; });
+ socket.onmessage = e => {
+ const m = JSON.parse(e.data);
+ if (m.id) { const p = pending.get(m.id); pending.delete(m.id); m.error ? p.reject(new Error(JSON.stringify(m.error))) : p.resolve(m.result); }
+ else if (m.method === 'Runtime.exceptionThrown') errors.push(m.params.exceptionDetails);
+ };
+ const send = (method, params = {}) => new Promise((resolve, reject) => { const id = ++sequence; pending.set(id, {resolve, reject}); socket.send(JSON.stringify({id, method, params})); });
+ const evaluate = async expression => {
+ const r = await send('Runtime.evaluate', {expression, returnByValue: true, awaitPromise: true});
+ if (r.exceptionDetails) throw new Error(JSON.stringify(r.exceptionDetails));
+ return r.result.value;
+ };
+ await send('Runtime.enable');
+ await send('Emulation.setDeviceMetricsOverride', {width, height, deviceScaleFactor: 1, mobile: false});
+ await send('Page.navigate', {url});
+ let ready = false;
+ for (let i = 0; i < 100 && !ready; i++) { ready = await evaluate(`!!window.sketch`); if (!ready) await sleep(100); }
+ assert.ok(ready, 'the sketch did not start');
+ await run(evaluate);
+ assert.deepEqual(errors, []);
+ } finally {
+ socket?.close();
+ chrome.kill('SIGTERM');
+ await new Promise(r => chrome.exitCode !== null ? r() : chrome.once('exit', r));
+ await rm(profile, {recursive: true, force: true, maxRetries: 10, retryDelay: 100});
+ server.close();
+ }
+}
+
+// Page code comparing the canvas with `native.png`: pixels away from edges
+// (neither picture changes by more than 8 within a pixel) must agree within
+// `tolerance`; pixels next to edges are only counted, by how far apart.
+export const compareScript = (width, height, tolerance, points) => `(async () => {
+ const W = ${width}, H = ${height};
+ const canvas = document.querySelector('canvas');
+ if (canvas.width !== W || canvas.height !== H) return {error: 'canvas is ' + canvas.width + 'x' + canvas.height};
+ const web = canvas.getContext('2d').getImageData(0, 0, W, H).data;
+ const image = new Image();
+ image.src = 'native.png';
+ await image.decode();
+ const scratch = new OffscreenCanvas(W, H).getContext('2d');
+ scratch.drawImage(image, 0, 0);
+ const native = scratch.getImageData(0, 0, W, H).data;
+ const at = (d, x, y) => [0, 1, 2].map(c => d[(y * W + x) * 4 + c]);
+ const edge = (x, y) => {
+ for (let dy = -1; dy <= 1; dy++) for (let dx = -1; dx <= 1; dx++) {
+ const nx = x + dx, ny = y + dy;
+ if (nx < 0 || ny < 0 || nx >= W || ny >= H) continue;
+ for (const d of [native, web]) {
+ const a = at(d, x, y), b = at(d, nx, ny);
+ if (a.some((v, c) => Math.abs(v - b[c]) > 8)) return true;
+ }
+ }
+ return false;
+ };
+ let compared = 0, differing = 0, worst = 0, first = null, edges = 0, edgeOver16 = 0, edgeOver48 = 0, edgeSum = 0;
+ for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
+ const a = at(native, x, y), b = at(web, x, y);
+ const d = Math.max(...a.map((v, c) => Math.abs(v - b[c])));
+ if (edge(x, y)) { edges++; edgeSum += d; if (d > 16) edgeOver16++; if (d > 48) edgeOver48++; continue; }
+ compared++;
+ worst = Math.max(worst, d);
+ if (d > ${tolerance}) { differing++; first ??= {x, y, native: a, web: b}; }
+ }
+ const sampled = {};
+ for (const [name, [x, y]] of Object.entries(${JSON.stringify(points)})) sampled[name] = {native: at(native, x, y), web: at(web, x, y)};
+ return {compared, edges, differing, worst, first, sampled, edgeOver16, edgeOver48, edgeMean: edges ? edgeSum / edges : 0};
+})()`;
+
+export function report(result, tolerance) {
+ assert.ok(!result.error, result.error);
+ console.log(`compared ${result.compared} pixels away from edges: ${result.differing} differ by more than ${tolerance}, worst ${result.worst}`);
+ console.log(`next to edges ${result.edges}: mean difference ${result.edgeMean.toFixed(1)}, ${result.edgeOver16} over 16, ${result.edgeOver48} over 48`);
+ for (const [name, {native, web}] of Object.entries(result.sampled)) console.log(` ${name.padEnd(8)} native ${native.join(',').padEnd(12)} web ${web.join(',')}`);
+ assert.equal(result.differing, 0, `first difference: ${JSON.stringify(result.first)}`);
+}