@ai-ecoverse/slicc-shared-web 2.7.1 → 2.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -265,7 +265,7 @@ SLICC_UNIT_ALL=1 git commit
265
265
 
266
266
  ## Integration-test harness
267
267
 
268
- `harness/` drives Chromium over raw CDP on `--remote-debugging-pipe`, as its only client, from `node --test`. On CI (`CI` set, as GitHub Actions does) Chrome runs headed, since headless Chrome behaves slightly differently; the pipeline starts Xvfb when the runner has no display. On a developer machine (no `CI`) it runs headless, so a test run doesn't open windows. `SLICC_HEADLESS=1` or `0`, or `launch({ headless })`, overrides either way, and a headed run on Linux without a display needs `xvfb-run -a`. Every page, service worker, shared worker and dedicated worker, nested ones included, starts paused, gets coverage (and the CPU profiler, when profiling is on) switched on in the same tick, and only then runs. Each test gets its own browser context and leaves `artifacts/<suite>/<test>/` behind: a screenshot per tab and a `console.log` of every target. Lines logged before the first test, such as an extension service worker's at startup, go into the first test's `console.log`. The global teardown merges the raw coverage into `coverage/` (console table, lcov, V8 HTML) and writes `artifacts/hotspots.md`.
268
+ `harness/` drives Chromium over raw CDP on `--remote-debugging-pipe`, as its only client, from `node --test`. On CI (`CI` set, as GitHub Actions does) Chrome runs headed, since headless Chrome behaves slightly differently; the pipeline starts Xvfb when the runner has no display. On a developer machine (no `CI`) it runs headless, so a test run doesn't open windows. `SLICC_HEADLESS=1` or `0`, or `launch({ headless })`, overrides either way. On Linux, a headed Chrome on CI (or with no `DISPLAY`) gets an Xvfb display of its own, started with `-displayfd` so it picks a free number and stopped with the browser: browsers launched side by side, as `node --test` runs files with `--test-concurrency`, never share a screen, so none takes another's window focus (four on one display broke `:focus-visible` checks in slicc-spectrum#572). This needs `Xvfb` on `PATH`; without it, Chrome uses `DISPLAY`, and on a Linux desktop without `CI` it always does. Every page, service worker, shared worker and dedicated worker, nested ones included, starts paused, gets coverage (and the CPU profiler, when profiling is on) switched on in the same tick, and only then runs. Each test gets its own browser context and leaves `artifacts/<suite>/<test>/` behind: a screenshot per tab and a `console.log` of every target. Lines logged before the first test, such as an extension service worker's at startup, go into the first test's `console.log`. The global teardown merges the raw coverage into `coverage/` (console table, lcov, V8 HTML) and writes `artifacts/hotspots.md`.
269
269
 
270
270
  CPU profiling is off by default. V8's sampler interrupts each page and worker thread every 100 µs with a signal, and the renderer SIGSEGVs in slicc-bios#114 all fault inside a signal handler walking the interrupted thread's frames, which points at it. Turn it on with `profile: true` or `SLICC_PROFILE=1` to get one CPU profile per target and snapshot in the test's artifact directory, and the 15 frames with the most self time in the repo's own scripts in `hotspots.md`. Without it, `hotspots.md` says that profiling is off.
271
271
 
@@ -8,6 +8,7 @@ import { promisify } from 'node:util';
8
8
  import { executablePath, explicit } from './browser.mjs';
9
9
  import { cdn } from './cdn.mjs';
10
10
  import { CRASHED, connect, pipe, SEND_TIMEOUT } from './cdp.mjs';
11
+ import { screenFor } from './display.mjs';
11
12
  import { crashpad } from './dumps.mjs';
12
13
  import {
13
14
  builtinWanted,
@@ -137,6 +138,12 @@ export function died(child) {
137
138
 
138
139
  export const WAITS = { close: 10000, exit: 10000, kill: 5000 };
139
140
 
141
+ export async function discard(error, closing, dirs) {
142
+ await Promise.all(closing);
143
+ await Promise.all(dirs.map((dir) => rm(dir, { recursive: true, force: true })));
144
+ throw error;
145
+ }
146
+
140
147
  export async function stop(cdp, child, waits = WAITS) {
141
148
  const gone = () => child.exitCode !== null || child.signalCode !== null;
142
149
  const exited = new Promise((resolve) => child.once('exit', resolve));
@@ -205,15 +212,15 @@ export function watchdog(pid) {
205
212
  return dog;
206
213
  }
207
214
 
208
- export function track(child, paths) {
209
- const dog = watchdog(child.pid);
210
- const entry = { children: [child, dog], paths };
215
+ export function track(child, paths, others = []) {
216
+ const dogs = [child, ...others].map((tracked) => watchdog(tracked.pid));
217
+ const entry = { children: [child, ...others, ...dogs], paths };
211
218
  if (running.size === 0) hook();
212
219
  running.add(entry);
213
220
  return () => {
214
221
  running.delete(entry);
215
222
  if (running.size === 0) unhook();
216
- kill(dog);
223
+ for (const dog of dogs) kill(dog);
217
224
  };
218
225
  }
219
226
 
@@ -335,19 +342,16 @@ export async function launch({
335
342
  const userData = await mkdtemp(join(tmpdir(), PREFIX));
336
343
  const loaded = [...found.values()].map((root) => root.slice(0, -1));
337
344
  const dumps = await crashpad();
345
+ const scrap = (error, close) =>
346
+ discard(error, [server.close(), close?.()], [userData, dumps.dir]);
347
+ const screen = await screenFor(!headless || realistic, dumps.env).catch((error) => scrap(error));
338
348
  const { child, exited, stderr } = await start(
339
349
  userData,
340
350
  commandLine(userData, loaded, args, realistic, builtin, headless),
341
351
  browser,
342
- dumps.env
343
- ).catch(async (error) => {
344
- await server.close();
345
- await Promise.all(
346
- [userData, dumps.dir].map((dir) => rm(dir, { recursive: true, force: true }))
347
- );
348
- throw error;
349
- });
350
- const untrack = track(child, [userData, dumps.dir]);
352
+ screen.env
353
+ ).catch((error) => scrap(error, screen.close));
354
+ const untrack = track(child, [userData, dumps.dir], [screen.child].filter(Boolean));
351
355
  const cdp = await connect(pipe(child.stdio[3], child.stdio[4]), {
352
356
  exited,
353
357
  tail: () => stderr.tail().map((line) => `stderr: ${line}`),
@@ -381,6 +385,7 @@ export async function launch({
381
385
  return own;
382
386
  })().catch(async (error) => {
383
387
  await stop(cdp, child);
388
+ await screen.close();
384
389
  untrack();
385
390
  await rm(userData, { recursive: true, force: true });
386
391
  await Promise.all([dumps.remove(), server.close()]);
@@ -480,6 +485,7 @@ export async function launch({
480
485
  },
481
486
  async close() {
482
487
  await stop(cdp, child);
488
+ await screen.close();
483
489
  await rm(userData, { recursive: true, force: true });
484
490
  untrack();
485
491
  closed = true;
@@ -0,0 +1,71 @@
1
+ import { spawn, spawnSync } from 'node:child_process';
2
+ import { env as inherited, platform as running } from 'node:process';
3
+ import { bounded, TIMED_OUT } from './util.mjs';
4
+
5
+ const ci = (env) => Boolean(env.CI) && env.CI !== 'false' && env.CI !== '0';
6
+ const installed = () =>
7
+ spawnSync('sh', ['-c', 'command -v Xvfb'], { stdio: 'ignore' }).status === 0;
8
+
9
+ export function wantsOwnDisplay({
10
+ headed,
11
+ env = inherited,
12
+ platform = running,
13
+ has = installed,
14
+ } = {}) {
15
+ if (!headed || platform !== 'linux') return false;
16
+ if (env.DISPLAY && !ci(env)) return false;
17
+ return has();
18
+ }
19
+
20
+ function stopper(xvfb) {
21
+ const exited = new Promise((resolve) => {
22
+ if (xvfb.exitCode !== null || xvfb.signalCode !== null) resolve();
23
+ else xvfb.once('exit', resolve);
24
+ });
25
+ return async () => {
26
+ for (const signal of ['SIGTERM', 'SIGKILL']) {
27
+ try {
28
+ process.kill(-xvfb.pid, signal);
29
+ } catch {}
30
+ if ((await bounded(exited, 'Xvfb exit', 3000)) !== TIMED_OUT) return;
31
+ }
32
+ };
33
+ }
34
+
35
+ export async function ownDisplay(env = inherited) {
36
+ const xvfb = spawn(
37
+ 'Xvfb',
38
+ ['-displayfd', '3', '-screen', '0', '1920x1080x24', '-nolisten', 'tcp'],
39
+ { detached: true, stdio: ['ignore', 'ignore', 'pipe', 'pipe'] }
40
+ );
41
+ const close = stopper(xvfb);
42
+ let said = '';
43
+ xvfb.stderr.on('data', (chunk) => {
44
+ said = `${said}${chunk}`.slice(-2000);
45
+ });
46
+ const number = new Promise((resolve, reject) => {
47
+ let out = '';
48
+ xvfb.stdio[3].on('data', (chunk) => {
49
+ out += chunk;
50
+ const found = /^(\d+)\n/.exec(out);
51
+ if (found) resolve(found[1]);
52
+ });
53
+ xvfb.once('error', reject);
54
+ xvfb.once('exit', (code, signal) => reject(new Error(`Xvfb exited with ${signal ?? code}`)));
55
+ });
56
+ number.catch(() => {});
57
+ const display = await bounded(number, 'Xvfb display', 10000).catch(async (error) => {
58
+ await close();
59
+ throw new Error(`${error.message}\n${said}`);
60
+ });
61
+ if (display === TIMED_OUT) {
62
+ await close();
63
+ throw new Error(`Xvfb named no display within 10 s\n${said}`);
64
+ }
65
+ return { env: { ...env, DISPLAY: `:${display}` }, child: xvfb, close };
66
+ }
67
+
68
+ export async function screenFor(headed, env = inherited) {
69
+ if (!wantsOwnDisplay({ headed, env })) return { env, child: null, close: async () => {} };
70
+ return ownDisplay(env);
71
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-ecoverse/slicc-shared-web",
3
- "version": "2.7.1",
3
+ "version": "2.7.2",
4
4
  "description": "Shared Renovate, Biome, TypeScript, lefthook and GitHub Actions configuration, plus the CDP integration-test harness, for SLICC's web repos",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",