summaryrefslogtreecommitdiff
path: root/tools/chrome.mjs
blob: e3361e998ba54c9bb5b0624af28e9b91605f0135 (plain) (blame)
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
// 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)}`);
}