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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
#!/usr/bin/env node
// Native touch events through Chromium's DevTools protocol, without npm.
// PARDES_CHROME overrides the Chromium executable.
import childProcess from "node:child_process";
import fs from "node:fs";
import fsp from "node:fs/promises";
import http from "node:http";
import net from "node:net";
import os from "node:os";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
const VIEWPORT = { width: 1120, height: 720 };
let scriptPath;
let server;
let chrome;
let client;
let userDataDir;
let chromeStderr = "";
const browserLog = [];
async function main() {
const args = process.argv.slice(2);
const update = args.includes("--update");
const positional = args.filter((arg) => arg !== "--update");
if (positional.length !== 2) {
console.error("usage: node test/web_snapshot.mjs <zig-out/web> <test.mjs|script.snap> [--update]");
process.exitCode = 2;
return;
}
const webDir = path.resolve(positional[0]);
scriptPath = path.resolve(positional[1]);
const scenarioModule = scriptPath.endsWith(".mjs");
const goldenPath = scriptPath.replace(/\.snap$/, ".golden");
try {
if (!scenarioModule && goldenPath === scriptPath) throw new Error(`not a test module or .snap path: ${scriptPath}`);
if (scenarioModule && update) throw new Error("semantic browser tests have no golden to update");
const served = await serveDirectory(webDir);
server = served.server;
const debugPort = await unusedPort();
const chromePath = await findChrome();
userDataDir = await fsp.mkdtemp(path.join(os.tmpdir(), "pardes-web-snap-"));
chrome = childProcess.spawn(chromePath, [
"--headless=new",
"--no-sandbox",
"--disable-dev-shm-usage",
"--enable-unsafe-swiftshader",
"--use-angle=swiftshader",
"--no-first-run",
"--no-default-browser-check",
`--remote-debugging-address=127.0.0.1`,
`--remote-debugging-port=${debugPort}`,
`--user-data-dir=${userDataDir}`,
"about:blank",
], { stdio: ["ignore", "ignore", "pipe"] });
chrome.on("error", (error) => { chromeStderr += `\n${error.message}`; });
chrome.stderr.setEncoding("utf8");
chrome.stderr.on("data", (chunk) => {
chromeStderr = (chromeStderr + chunk).slice(-16_384);
});
await waitForDevTools(debugPort, chrome);
const targetResponse = await fetch(
`http://127.0.0.1:${debugPort}/json/new?${encodeURIComponent(`http://127.0.0.1:${served.port}/index.html`)}`,
{ method: "PUT", signal: AbortSignal.timeout(10_000) },
);
if (!targetResponse.ok) throw new Error(`create Chrome target: HTTP ${targetResponse.status}`);
const target = await targetResponse.json();
client = await Cdp.connect(target.webSocketDebuggerUrl, browserLog);
await client.send("Runtime.enable");
await client.send("Page.enable");
await client.send("Log.enable");
await client.send("Emulation.setDeviceMetricsOverride", {
...VIEWPORT,
screenWidth: VIEWPORT.width,
screenHeight: VIEWPORT.height,
deviceScaleFactor: 1,
mobile: false,
});
await client.send("Emulation.setTouchEmulationEnabled", { enabled: true, maxTouchPoints: 1 });
await waitForRuntime(20_000);
if (scenarioModule) {
const scenario = await import(pathToFileURL(scriptPath));
await scenario.default({ grid, touch: dispatchTouch, wait: waitUntil, stable: waitStable, delay, evaluate,
mouse: (event) => client.send("Input.dispatchMouseEvent", event) });
console.error(`web test ok: ${path.relative(process.cwd(), scriptPath)}`);
return;
}
const output = await runScript(await fsp.readFile(scriptPath, "utf8"));
if (update) {
await fsp.writeFile(goldenPath, output);
console.error(`updated ${path.relative(process.cwd(), goldenPath)}`);
} else {
const golden = await fsp.readFile(goldenPath, "utf8").catch(() => null);
if (golden === null) throw new Error(`missing ${goldenPath}; run with --update`);
if (golden !== output) {
const actualPath = goldenPath.replace(/\.golden$/, ".actual");
await fsp.writeFile(actualPath, output);
throw new Error(`snapshot mismatch; actual written to ${actualPath}`);
}
console.error(`web snapshot ok: ${path.relative(process.cwd(), scriptPath)}`);
}
} catch (error) {
console.error(error?.stack ?? error);
if (scenarioModule && client) {
const frame = await grid().catch(() => null);
if (frame) console.error(formatSnapshot("failure", frame));
}
if (browserLog.length) console.error(`browser log:\n${browserLog.join("\n")}`);
if (chromeStderr.trim()) console.error(`chromium stderr (tail):\n${chromeStderr.trim()}`);
process.exitCode = 1;
} finally {
client?.close();
if (chrome) await stopChrome(chrome);
if (server) {
const closed = new Promise((resolve) => server.close(resolve));
server.closeAllConnections();
await closed;
}
if (userDataDir) await fsp.rm(userDataDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
}
async function runScript(source) {
let output = "";
let activeTouch = null;
const lines = source.split("\n");
for (let index = 0; index < lines.length; index += 1) {
const line = lines[index].trim();
if (!line || line.startsWith("#")) continue;
const tokens = line.split(/\s+/);
const command = tokens.shift();
try {
if (command === "wait") {
const timeout = integer(tokens.shift(), "wait timeout");
const needle = tokens.join(" ");
if (!needle) throw new Error("wait needs text");
await waitUntil(async () => (await grid()).text.includes(needle), timeout, `screen containing ${JSON.stringify(needle)}`);
} else if (command === "stable") {
const quiet = integer(tokens.shift(), "stable quiet time");
const timeout = integer(tokens.shift(), "stable timeout");
await waitStable(quiet, timeout);
} else if (command === "snap") {
const label = tokens.join(" ");
if (!label) throw new Error("snap needs a label");
output += formatSnapshot(label, await grid());
} else if (command === "snapstyle") {
const label = tokens.join(" ");
if (!label) throw new Error("snapstyle needs a label");
output += formatStyleSnapshot(label, await grid());
} else if (command === "touch") {
const phase = tokens.shift();
const id = integer(tokens.shift(), "touch id");
const col = number(tokens.shift(), "touch col");
const row = number(tokens.shift(), "touch row");
if (tokens.length || !["down", "move", "up"].includes(phase)) throw new Error("bad touch command");
activeTouch = await dispatchTouch(phase, id, col, row, activeTouch);
await delay(40);
} else if (command === "swipe") {
const id = integer(tokens.shift(), "swipe id");
const x0 = number(tokens.shift(), "swipe start col");
const y0 = number(tokens.shift(), "swipe start row");
const x1 = number(tokens.shift(), "swipe end col");
const y1 = number(tokens.shift(), "swipe end row");
const steps = integer(tokens.shift(), "swipe steps");
if (tokens.length || steps < 1) throw new Error("bad swipe command");
activeTouch = await dispatchTouch("down", id, x0, y0, activeTouch);
for (let step = 1; step <= steps; step += 1) {
const t = step / steps;
activeTouch = await dispatchTouch("move", id, x0 + (x1 - x0) * t, y0 + (y1 - y0) * t, activeTouch);
await delay(20);
}
activeTouch = await dispatchTouch("up", id, x1, y1, activeTouch);
await delay(80);
} else {
throw new Error(`unknown command ${command}`);
}
} catch (error) {
const screen = await grid().catch(() => null);
const detail = screen ? `\ncurrent screen:\n${screen.lines.map((row) => `|${row}`).join("\n")}` : "";
throw new Error(`${path.basename(scriptPath)}:${index + 1}: ${line}\n${error.message}${detail}`, { cause: error });
}
}
if (activeTouch) throw new Error("script ended with a finger still down");
return output;
}
async function dispatchTouch(phase, id, col, row, active) {
const point = await evaluate(`pardes.test.cellPoint(${JSON.stringify(col)}, ${JSON.stringify(row)})`);
const touch = { x: point.x, y: point.y, id, radiusX: 1, radiusY: 1, force: 1 };
if (phase === "down") {
if (active) throw new Error("only one finger is supported by this web test");
await client.send("Input.dispatchTouchEvent", { type: "touchStart", touchPoints: [touch] });
return touch;
}
if (!active || active.id !== id) throw new Error(`finger ${id} is not down`);
if (phase === "move") {
await client.send("Input.dispatchTouchEvent", { type: "touchMove", touchPoints: [touch] });
return touch;
}
// Chrome's touchEnd carries no points; a final move gives SDL the scripted
// release coordinate before the lift.
await client.send("Input.dispatchTouchEvent", { type: "touchMove", touchPoints: [touch] });
await client.send("Input.dispatchTouchEvent", { type: "touchEnd", touchPoints: [] });
return null;
}
async function grid() {
return evaluate(`(() => {
const frame = pardes.test.grid();
const { cols, rows, cursorX, cursorY } = frame;
const lines = [];
const styleRows = [];
for (let y = 0; y < rows; y++) {
let line = "";
const styles = [];
for (let x = 0; x < cols; x++) {
const cell = frame.cells[y * cols + x];
line += cell.text || " ";
styles.push([cell.fg, cell.bg, cell.attrs]);
}
lines.push(line.replace(/ +$/, ""));
styleRows.push(styles);
}
return {
cols, rows, lines, styleRows,
cursorX, cursorY,
text: lines.join("\\n"),
};
})()`);
}
function formatSnapshot(label, state) {
const cursorX = state.cursorX < 0 ? 0 : state.cursorX;
const cursorY = state.cursorY < 0 ? 0 : state.cursorY;
let out = `== snap ${label} grid=${state.cols}x${state.rows} cursor=${cursorX},${cursorY}\n`;
for (const line of state.lines) out += `|${line}\n`;
return out;
}
function formatStyleSnapshot(label, state) {
let out = `== style ${label}\n`;
for (let y = 0; y < state.rows; y++) {
const styles = state.styleRows[y];
out += `|${y}:`;
let runStart = 0;
let runKey = styleKey(styles[0]);
for (let x = 1; x <= state.cols; x++) {
const key = x === state.cols ? null : styleKey(styles[x]);
if (key === runKey) continue;
out += ` ${runStart}-${x - 1} ${runKey}`;
runStart = x;
runKey = key;
}
out += "\n";
}
return out;
}
function styleKey([fg, bg, attrs]) {
return `${colorKey(fg)},${colorKey(bg)},${attrsKey(attrs)}`;
}
function colorKey(color) {
if (color === 0x01000000) return "d";
if ((color & 0xff000000) === 0x02000000) return `p${color & 0xff}`;
return `#${(color & 0xffffff).toString(16).padStart(6, "0")}`;
}
function attrsKey(attrs) {
let out = "";
if (attrs & (1 << 0)) out += "b";
if (attrs & (1 << 1)) out += "f";
if (attrs & (1 << 2)) out += "i";
if (attrs & (1 << 3)) out += "k";
if (attrs & (1 << 4)) out += "r";
if (attrs & (1 << 5)) out += "v";
if (attrs & (1 << 6)) out += "x";
out += ["", "u", "U", "~", ".", "-"][(attrs >> 8) & 0x7] ?? "?";
return out;
}
async function waitStable(quiet, timeout) {
const deadline = Date.now() + timeout;
let previous = JSON.stringify(await grid());
while (Date.now() < deadline) {
await delay(Math.min(quiet, deadline - Date.now()));
const current = JSON.stringify(await grid());
if (current === previous && Date.now() <= deadline) return;
previous = current;
}
throw new Error(`screen did not stabilize within ${timeout}ms`);
}
async function waitForRuntime(timeout) {
await waitUntil(async () => {
try {
return await evaluate(`(() => {
const app = globalThis.pardes;
if (!app?.test?.grid()?.cols || document.querySelector("canvas")) return false;
const want = app.runtime.gridSize();
const frame = app.test.grid();
const tagline = document.querySelector(".pardes-cell.is-tagline");
const body = document.querySelector(".pardes-cell:not(.is-tagline)");
if (!tagline || !body) return false;
const tagRect = tagline.getBoundingClientRect();
const bodyRect = body.getBoundingClientRect();
return frame.cols === want.cols && frame.rows === want.rows &&
document.querySelectorAll(".pardes-row").length === want.rows &&
tagRect.width === bodyRect.width && tagRect.height === bodyRect.height &&
parseFloat(getComputedStyle(tagline).fontSize) < parseFloat(getComputedStyle(body).fontSize);
})()`);
} catch (error) {
if (client.error) throw error;
return false;
}
}, timeout, "Pardes web runtime");
}
async function waitUntil(predicate, timeout, description) {
const deadline = Date.now() + timeout;
while (Date.now() < deadline) {
if (await predicate()) return;
if (chrome && (chrome.exitCode !== null || chrome.signalCode !== null))
throw new Error(`Chromium exited with ${chrome.exitCode ?? chrome.signalCode}`);
await delay(50);
}
throw new Error(`timed out waiting for ${description}`);
}
async function evaluate(expression) {
const response = await client.send("Runtime.evaluate", {
expression,
returnByValue: true,
awaitPromise: true,
});
if (response.exceptionDetails) throw new Error(response.exceptionDetails.text);
return response.result.value;
}
export class Cdp {
static async connect(url, log, timeout = 10_000) {
const socket = new WebSocket(url);
const client = new Cdp(socket, log, timeout);
try {
await new Promise((resolve, reject) => {
const finish = (error) => {
clearTimeout(timer);
socket.removeEventListener("open", opened);
socket.removeEventListener("close", failed);
socket.removeEventListener("error", failed);
if (error) reject(error); else resolve();
};
const opened = () => finish();
const failed = () => finish(client.error ?? new Error(`connect DevTools websocket ${url}`));
const timer = setTimeout(() => finish(new Error(`DevTools connection timed out after ${timeout}ms`)), timeout);
socket.addEventListener("open", opened, { once: true });
socket.addEventListener("close", failed, { once: true });
socket.addEventListener("error", failed, { once: true });
});
return client;
} catch (error) {
client.close();
throw error;
}
}
constructor(socket, log, timeout = 10_000) {
this.socket = socket;
this.timeout = timeout;
this.nextId = 1;
this.pending = new Map();
this.error = null;
socket.addEventListener("close", (event) => this.fail(new Error(`DevTools connection closed (${event.code ?? "unknown"})`)));
socket.addEventListener("error", () => this.fail(new Error("DevTools connection error")));
socket.addEventListener("message", (event) => {
if (this.error) return;
try {
const message = JSON.parse(event.data);
if (message.id) {
const pending = this.pending.get(message.id);
if (!pending) return;
this.pending.delete(message.id);
clearTimeout(pending.timer);
if (message.error) pending.reject(new Error(`${pending.method}: ${message.error.message}`));
else pending.resolve(message.result ?? {});
return;
}
if (message.method === "Runtime.exceptionThrown")
log.push(`exception: ${message.params.exceptionDetails.text}`);
else if (message.method === "Runtime.consoleAPICalled")
log.push(`console ${message.params.type}: ${message.params.args.map((arg) => arg.value ?? arg.description).join(" ")}`);
else if (message.method === "Log.entryAdded")
log.push(`${message.params.entry.level}: ${message.params.entry.text}`);
} catch (error) {
this.fail(new Error("invalid DevTools message", { cause: error }));
}
});
}
send(method, params = {}) {
if (this.error) return Promise.reject(this.error);
if (this.socket.readyState !== WebSocket.OPEN) {
this.fail(new Error("DevTools connection is not open"));
return Promise.reject(this.error);
}
const id = this.nextId++;
return new Promise((resolve, reject) => {
const timer = setTimeout(() => this.fail(new Error(`${method} timed out after ${this.timeout}ms`)), this.timeout);
this.pending.set(id, { resolve, reject, method, timer });
try {
this.socket.send(JSON.stringify({ id, method, params }));
} catch (error) {
this.fail(new Error(`send DevTools command ${method}`, { cause: error }));
}
});
}
fail(error) {
if (this.error) return;
this.error = error;
for (const pending of this.pending.values()) {
clearTimeout(pending.timer);
pending.reject(error);
}
this.pending.clear();
this.socket.close();
}
close() {
this.fail(new Error("DevTools client closed"));
}
}
export async function stopChrome(child, timeout = 2_000) {
if (!child.pid || child.exitCode !== null || child.signalCode !== null) return;
await new Promise((resolve) => {
const timer = setTimeout(() => child.kill("SIGKILL"), timeout);
child.once("exit", () => {
clearTimeout(timer);
resolve();
});
child.kill("SIGTERM");
});
}
async function serveDirectory(root) {
const canonicalRoot = await fsp.realpath(root);
const server = http.createServer(async (request, response) => {
try {
const url = new URL(request.url, "http://localhost");
const relative = decodeURIComponent(url.pathname === "/" ? "/index.html" : url.pathname);
const filename = path.resolve(canonicalRoot, `.${relative}`);
if (filename !== canonicalRoot && !filename.startsWith(`${canonicalRoot}${path.sep}`)) {
response.writeHead(403).end();
return;
}
const contents = await fsp.readFile(filename);
const contentType = filename.endsWith(".wasm") ? "application/wasm"
: filename.endsWith(".js") || filename.endsWith(".mjs") ? "text/javascript; charset=utf-8"
: filename.endsWith(".css") ? "text/css; charset=utf-8"
: filename.endsWith(".ttf") ? "font/ttf"
: "text/html; charset=utf-8";
response.writeHead(200, { "content-type": contentType, "cache-control": "no-store" });
response.end(contents);
} catch {
response.writeHead(404).end();
}
});
await new Promise((resolve, reject) => {
server.once("error", reject);
server.listen(0, "127.0.0.1", resolve);
});
return { server, port: server.address().port };
}
async function unusedPort() {
const probe = net.createServer();
await new Promise((resolve, reject) => {
probe.once("error", reject);
probe.listen(0, "127.0.0.1", resolve);
});
const port = probe.address().port;
await new Promise((resolve) => probe.close(resolve));
return port;
}
async function waitForDevTools(port, child) {
const deadline = Date.now() + 10_000;
while (Date.now() < deadline) {
if (!child.pid || child.exitCode !== null || child.signalCode !== null)
throw new Error(`Chromium exited with ${child.exitCode ?? child.signalCode}`);
try {
const response = await fetch(`http://127.0.0.1:${port}/json/version`, { signal: AbortSignal.timeout(500) });
if (response.ok) return;
} catch {}
await delay(50);
}
throw new Error("Chromium DevTools endpoint did not start");
}
async function findChrome() {
const candidates = [];
if (process.env.PARDES_CHROME) candidates.push(process.env.PARDES_CHROME);
for (const command of ["chromium", "chromium-browser", "google-chrome", "google-chrome-stable"]) {
const found = childProcess.spawnSync("which", [command], { encoding: "utf8" });
if (found.status === 0 && found.stdout.trim()) candidates.push(found.stdout.trim());
}
// macOS installs a browser as an application BUNDLE, whose executable is
// buried inside it and never on PATH, so the lookup above finds nothing on
// a Mac that has Chrome sitting in /Applications.
if (process.platform === "darwin")
for (const app of ["Google Chrome", "Chromium", "Google Chrome Canary"])
candidates.push(`/Applications/${app}.app/Contents/MacOS/${app}`);
const cache = process.env.PLAYWRIGHT_BROWSERS_PATH || path.join(os.homedir(), ".cache", "ms-playwright");
for (const entry of await fsp.readdir(cache).catch(() => [])) {
if (entry.startsWith("chromium-")) {
candidates.push(path.join(cache, entry, "chrome-linux64", "chrome"));
candidates.push(path.join(cache, entry, "chrome-linux", "chrome"));
} else if (entry.startsWith("chromium_headless_shell-")) {
candidates.push(path.join(cache, entry, "chrome-headless-shell-linux64", "chrome-headless-shell"));
}
}
for (const candidate of candidates) {
try {
await fsp.access(candidate, fs.constants.X_OK);
return candidate;
} catch {}
}
throw new Error("no Chromium found; set PARDES_CHROME=/path/to/chrome");
}
function integer(value, label) {
const parsed = Number.parseInt(value, 10);
if (!Number.isSafeInteger(parsed) || String(parsed) !== value) throw new Error(`bad ${label}: ${value}`);
return parsed;
}
function number(value, label) {
const parsed = Number(value);
if (!Number.isFinite(parsed)) throw new Error(`bad ${label}: ${value}`);
return parsed;
}
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) await main();
|