@ai-ecoverse/slicc-shared-web 1.5.1 → 1.7.0

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
@@ -63,8 +63,16 @@ test('boots', async (t) => {
63
63
  | `extensions` | Unpacked extension directories to load. Their scripts are covered and profiled like the repo's own, and tests then run in the default browser context, closing their tabs when they end, because Chrome does not run extensions in the per-test contexts | none |
64
64
  | `args` | Extra Chromium flags, such as `--host-resolver-rules` or `--ignore-certificate-errors` | none |
65
65
  | `timeout` | How long `page.until` polls and `page.evaluate` waits, in ms | `30000` |
66
+ | `stallAfter` | How long one evaluation may run before the harness probes the test's targets, in ms | `30000` |
66
67
 
67
- `chrome.page(t)` opens a tab and returns the page API: `goto(path)`, `reload()`, `evaluate(fn, ...args)`, `until(fn, ...args)`, `press(key, ...modifiers)` (a key name like `Enter` or `F5`, or a single character typed with its US-layout key code), `type(text)`, `insert(text)`, `enter()`, `init(fn)` (runs in every new document), `expose(name, handler)` (a binding the page calls with a JSON string), `screenshot(file)`, `tab()` (another tab in the same context), `close()`, `errors`, `responses` and `dir` (the test's artifact directory). `goto` and `reload` wait until the new document is complete and no longer `about:blank`, and retry the navigation once if that never happens. `until` polls until the function returns a truthy value (not only strict `true`) and resolves with that value; the timeout error names the last result. `within(ms, fn, ...args)` is `until` with its own deadline, for a stage that should take a known time. Both stop at the deadline even if one evaluation never answers. Every CDP call has a deadline: `connect(url).send(method, params, sessionId, ms)` rejects with `<method>: no answer in <ms> ms` (60 s unless given), so a stuck page fails its test instead of hanging the run. Downloads of intercepted URLs time out after 60 s and are tried up to three times; a `4xx` is not retried. `chrome.requests` lists the paths the server saw in this test, `chrome.overrides` is a `Map` of paths to bodies served instead of files, and `chrome.cdn` takes `status` (answer every intercepted request with it), `corrupt` (flip the last byte) and lists the intercepted `requests`. All of them reset with each test. `serve(options)` starts the same server on its own, for an `npm start`.
68
+ `chrome.page(t)` opens a tab and returns the page API: `goto(path)`, `reload()`, `evaluate(fn, ...args)`, `until(fn, ...args)`, `press(key, ...modifiers)` (a key name like `Enter` or `F5`, or a single character typed with its US-layout key code), `type(text)`, `insert(text)`, `enter()`, `init(fn)` (runs in every new document), `expose(name, handler)` (a binding the page calls with a JSON string), `screenshot(file)`, `tab()` (another tab in the same context), `close()`, `errors`, `responses` and `dir` (the test's artifact directory). `goto` and `reload` wait until the new document is complete and no longer `about:blank`, and retry the navigation once if that never happens. `until` polls until the function returns a truthy value (not only strict `true`) and resolves with that value; the timeout error names the last result. `within(ms, fn, ...args)` is `until` with its own deadline, for a stage that should take a known time. Both stop at the deadline even if one evaluation never answers. Every CDP call has a deadline: `connect(url).send(method, params, sessionId, ms)` rejects with `<method>: no answer in <ms> ms` (60 s unless given), so a stuck page fails its test instead of hanging the run. When one evaluation runs past `stallAfter`, the harness probes once, without interrupting the wait:
69
+ - it saves `stall-<n>.png` (a screenshot of the page, if the renderer still answers) in the test's artifact directory;
70
+ - it saves a CPU profile sample of every page and worker in the test, including workers started by other workers, as `stall-<n>-<target>-<session>.cpuprofile`;
71
+ - it adds a line per target to `console.log`, saying whether that target is running or paused;
72
+ - on the first stall, it records a 10 s browser trace as `stall-1-trace.json` (open it in DevTools' Performance panel or Perfetto). The browser collects it with V8 CPU samples per thread, so it shows what a page's main thread is doing even when that thread no longer answers CDP. It also logs each process's CPU time over the window;
73
+ - it adds a line for every debugger pause still held in the browser, in any target, with its id, URL, start time, reason and top call frames.
74
+
75
+ A pause in one target blocks every page in the same renderer process, so those pause lines name the culprit even when it isn't the page under test. The harness resumes its own coverage pauses (`beforeunload`, worker exits) within 10 s, even when collecting coverage fails. Downloads of intercepted URLs time out after 60 s and are tried up to three times; a `4xx` is not retried. `chrome.requests` lists the paths the server saw in this test, `chrome.overrides` is a `Map` of paths to bodies served instead of files, and `chrome.cdn` takes `status` (answer every intercepted request with it), `corrupt` (flip the last byte) and lists the intercepted `requests`. All of them reset with each test. `serve(options)` starts the same server on its own, for an `npm start`.
68
76
 
69
77
  Why it works the way it does:
70
78
 
@@ -1,5 +1,5 @@
1
1
  import { spawn } from 'node:child_process';
2
- import { mkdir, mkdtemp, rm } from 'node:fs/promises';
2
+ import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
3
3
  import { tmpdir } from 'node:os';
4
4
  import { basename, join } from 'node:path';
5
5
  import { chromium } from 'playwright-core';
@@ -75,6 +75,7 @@ export async function launch({
75
75
  extensions = [],
76
76
  args = [],
77
77
  timeout,
78
+ stallAfter,
78
79
  } = {}) {
79
80
  const server = await serve({ roots, aliases, isolated });
80
81
  const found = await unpacked(extensions);
@@ -105,6 +106,7 @@ export async function launch({
105
106
  }
106
107
 
107
108
  async function finish(pages, browserContextId, dir, before) {
109
+ await bounded(Promise.all(pages.map((opened) => opened.probed())), 'probes', 30000);
108
110
  const shots = pages.map((opened, i) =>
109
111
  opened.screenshot(new URL(`tab-${i + 1}.png`, dir)).catch(ignore)
110
112
  );
@@ -136,7 +138,13 @@ export async function launch({
136
138
  const pages = [];
137
139
  t.after(() => finish(pages, browserContextId, dir, before));
138
140
  const tab = async () => {
139
- const opened = page(cdp, await record.open(browserContextId), server, timeout);
141
+ const opened = page(cdp, await record.open(browserContextId), server, timeout, {
142
+ stallAfter,
143
+ stall: async (tag, png) => {
144
+ if (png) await writeFile(new URL(`${tag}.png`, dir), png).catch(ignore);
145
+ await bounded(record.stall(dir, tag), tag, 45000);
146
+ },
147
+ });
140
148
  pages.push(opened);
141
149
  return Object.assign(opened, { tab, dir });
142
150
  };
package/harness/page.mjs CHANGED
@@ -84,7 +84,17 @@ export function decoded({ value, unserializableValue }) {
84
84
  return specials[unserializableValue];
85
85
  }
86
86
 
87
- export function page(cdp, { sessionId, targetId }, server, timeout = 30000) {
87
+ export const STALL_AFTER = 30000;
88
+
89
+ export function page(
90
+ cdp,
91
+ { sessionId, targetId },
92
+ server,
93
+ timeout = 30000,
94
+ { stall, stallAfter = STALL_AFTER } = {}
95
+ ) {
96
+ let stalls = 0;
97
+ const probes = new Set();
88
98
  const send = (method, params, ms) => cdp.send(method, params, sessionId, ms);
89
99
  const errors = [];
90
100
  const responses = [];
@@ -99,15 +109,34 @@ export function page(cdp, { sessionId, targetId }, server, timeout = 30000) {
99
109
  if (method === 'Network.responseReceived') responses.push(params.response);
100
110
  });
101
111
 
112
+ async function capture() {
113
+ stalls += 1;
114
+ const tag = `stall-${stalls}`;
115
+ const shot = await send('Page.captureScreenshot', { format: 'png' }, 10000).catch(() => null);
116
+ await stall(tag, shot?.data ? Buffer.from(shot.data, 'base64') : null);
117
+ }
118
+
119
+ function probe() {
120
+ const running = capture().catch(() => undefined);
121
+ probes.add(running);
122
+ void running.then(() => probes.delete(running));
123
+ }
124
+
102
125
  async function run(fn, args, ms) {
103
126
  const expression = `(${fn})(...${JSON.stringify(args)})`;
104
- const { result, exceptionDetails } = await send(
105
- 'Runtime.evaluate',
106
- { expression, awaitPromise: true, returnByValue: true },
107
- ms
108
- );
109
- if (exceptionDetails) throw new Error(exceptionDetails.exception?.description);
110
- return decoded(result);
127
+ const at = Math.min(stallAfter, Math.max(ms - 1000, ms / 2));
128
+ const timer = stall ? setTimeout(probe, at) : null;
129
+ try {
130
+ const { result, exceptionDetails } = await send(
131
+ 'Runtime.evaluate',
132
+ { expression, awaitPromise: true, returnByValue: true },
133
+ ms
134
+ );
135
+ if (exceptionDetails) throw new Error(exceptionDetails.exception?.description);
136
+ return decoded(result);
137
+ } finally {
138
+ clearTimeout(timer);
139
+ }
111
140
  }
112
141
 
113
142
  function evaluate(fn, ...args) {
@@ -161,6 +190,7 @@ export function page(cdp, { sessionId, targetId }, server, timeout = 30000) {
161
190
  evaluate,
162
191
  until,
163
192
  within,
193
+ probed: () => Promise.all(probes),
164
194
  press,
165
195
  goto: (path) => navigate(`location.assign(${JSON.stringify(new URL(path, server.url).href)})`),
166
196
  reload: () => navigate('location.reload()'),
@@ -65,8 +65,113 @@ async function entry(server, script) {
65
65
  };
66
66
  }
67
67
 
68
+ function frames(params) {
69
+ return (params.callFrames ?? [])
70
+ .slice(0, 3)
71
+ .map(
72
+ (frame) =>
73
+ `${frame.functionName || '(anonymous)'} ${pathOf(frame.url) || frame.url}:${frame.location.lineNumber + 1}`
74
+ )
75
+ .join(' < ');
76
+ }
77
+
78
+ async function sample(cdp, pauses, [sessionId, target], dir, tag) {
79
+ const send = (method) => cdp.send(method, {}, sessionId, 8000);
80
+ const stopped = await send('Profiler.stop').catch((error) => ({ error }));
81
+ send('Profiler.start').catch(ignore);
82
+ const pause = pauses.get(sessionId);
83
+ const state = pause ? `paused (${pause.reason})` : 'running';
84
+ if (!stopped.profile) return `${label(target)}: ${state}, no profile (${stopped.error?.message})`;
85
+ const name = `${tag}-${label(target)}-${sessionId.slice(0, 6)}.cpuprofile`;
86
+ await writeFile(new URL(name, dir), JSON.stringify(stopped.profile)).catch(ignore);
87
+ return `${label(target)}: ${state}, profile ${name}`;
88
+ }
89
+
90
+ function held(pauses) {
91
+ return [...pauses.values()].map(
92
+ (pause) =>
93
+ `paused ${pause.type} ${pause.targetId} ${pause.url} since ${pause.since}: ${pause.reason}`
94
+ );
95
+ }
96
+
97
+ function pauseOf(target = {}, params) {
98
+ return {
99
+ type: target.type ?? 'unknown',
100
+ targetId: target.targetId ?? '?',
101
+ url: target.url ?? '?',
102
+ since: new Date().toISOString(),
103
+ reason: `${params.reason}: ${frames(params)}`,
104
+ };
105
+ }
106
+
107
+ const TRACED = [
108
+ 'toplevel',
109
+ 'devtools.timeline',
110
+ 'disabled-by-default-devtools.timeline',
111
+ 'v8.execute',
112
+ 'disabled-by-default-v8.cpu_profiler',
113
+ ];
114
+
115
+ async function cpuTimes(cdp) {
116
+ const { processInfo = [] } = await cdp
117
+ .send('SystemInfo.getProcessInfo', {}, undefined, 5000)
118
+ .catch(() => ({}));
119
+ return new Map(processInfo.map((info) => [`${info.type} ${info.id}`, info.cpuTime]));
120
+ }
121
+
122
+ export async function trace(cdp, dir, tag, ms = 10000) {
123
+ const events = [];
124
+ const done = Promise.withResolvers();
125
+ const off = cdp.on(({ method, params }) => {
126
+ if (method === 'Tracing.dataCollected') events.push(...params.value);
127
+ if (method === 'Tracing.tracingComplete') done.resolve();
128
+ });
129
+ try {
130
+ const before = await cpuTimes(cdp);
131
+ await cdp.send(
132
+ 'Tracing.start',
133
+ { traceConfig: { includedCategories: TRACED }, transferMode: 'ReportEvents' },
134
+ undefined,
135
+ 10000
136
+ );
137
+ await sleep(ms);
138
+ await cdp.send('Tracing.end', {}, undefined, 10000);
139
+ await bounded(done.promise, 'trace', 20000);
140
+ const after = await cpuTimes(cdp);
141
+ await writeFile(new URL(`${tag}-trace.json`, dir), JSON.stringify({ traceEvents: events }));
142
+ const busy = [...after]
143
+ .map(([name, time]) => [name, time - (before.get(name) ?? 0)])
144
+ .filter(([, used]) => used > 0.05)
145
+ .map(([name, used]) => `${name} ${used.toFixed(1)}s`);
146
+ return [
147
+ `trace ${tag}-trace.json (${events.length} events)`,
148
+ `cpu over ${ms / 1000}s: ${busy.join(', ') || 'none'}`,
149
+ ];
150
+ } catch (error) {
151
+ return [`trace failed: ${error.message}`];
152
+ } finally {
153
+ off();
154
+ }
155
+ }
156
+
157
+ function claim(run) {
158
+ if (!run || run.traced) return false;
159
+ run.traced = true;
160
+ return true;
161
+ }
162
+
163
+ function contextOf(sessions, parents, sessionId) {
164
+ for (let id = sessionId; id; id = parents.get(id)) {
165
+ const context = sessions.get(id)?.browserContextId;
166
+ if (context) return context;
167
+ }
168
+ return undefined;
169
+ }
170
+
68
171
  export function recorder(cdp, server, { coverage = ['/'], exits = new Map() } = {}) {
69
172
  const sessions = new Map();
173
+ const pauses = new Map();
174
+ const parents = new Map();
70
175
  const tabs = new Map();
71
176
  const ours = covered(server, coverage);
72
177
  let run = null;
@@ -151,21 +256,45 @@ export function recorder(cdp, server, { coverage = ['/'], exits = new Map() } =
151
256
  run.console.push(`${label(target)}: ${line}`);
152
257
  }
153
258
 
259
+ async function capture(sessionId, target) {
260
+ if (target?.type === 'worker') return dump(sessionId, target, false);
261
+ const { result } = await cdp
262
+ .send('Runtime.evaluate', { expression: 'location.href', returnByValue: true }, sessionId)
263
+ .catch(() => ({ result: {} }));
264
+ if (result?.value && result.value !== 'about:blank') await checkpoint();
265
+ }
266
+
154
267
  async function paused(sessionId) {
155
- const target = sessions.get(sessionId);
156
- if (target?.type === 'worker') await dump(sessionId, target, false);
157
- else {
158
- const { result } = await cdp
159
- .send('Runtime.evaluate', { expression: 'location.href', returnByValue: true }, sessionId)
160
- .catch(() => ({ result: {} }));
161
- if (result?.value && result.value !== 'about:blank') await checkpoint();
268
+ try {
269
+ await bounded(capture(sessionId, sessions.get(sessionId)).catch(ignore), 'pause', 10000);
270
+ } finally {
271
+ await cdp.send('Debugger.resume', {}, sessionId, 10000).catch(ignore);
162
272
  }
163
- await cdp.send('Debugger.resume', {}, sessionId).catch(ignore);
273
+ }
274
+
275
+ async function stall(dir, tag) {
276
+ const first = claim(run);
277
+ const current = [...sessions].filter(
278
+ ([sessionId]) => contextOf(sessions, parents, sessionId) === run?.context
279
+ );
280
+ const [lines, traced] = await Promise.all([
281
+ Promise.all(current.map((entry) => sample(cdp, pauses, entry, dir, tag))),
282
+ first ? trace(cdp, dir, tag) : [],
283
+ ]);
284
+ const all = [...lines, ...traced, ...held(pauses)];
285
+ for (const line of all) run?.console.push(`${tag}: ${line}`);
286
+ return all;
164
287
  }
165
288
 
166
289
  cdp.on(async ({ method, params, sessionId }) => {
167
- if (method === 'Target.attachedToTarget') await attach(params);
168
- if (method === 'Target.detachedFromTarget') sessions.delete(params.sessionId);
290
+ if (method === 'Target.attachedToTarget') {
291
+ if (sessionId) parents.set(params.sessionId, sessionId);
292
+ await attach(params);
293
+ }
294
+ if (method === 'Target.detachedFromTarget') {
295
+ sessions.delete(params.sessionId);
296
+ pauses.delete(params.sessionId);
297
+ }
169
298
  if (method === 'Runtime.consoleAPICalled') {
170
299
  const text = params.args.map((arg) => arg.value ?? arg.description).join(' ');
171
300
  log(sessionId, `${params.type} ${text}`);
@@ -174,7 +303,11 @@ export function recorder(cdp, server, { coverage = ['/'], exits = new Map() } =
174
303
  const { exception, text } = params.exceptionDetails;
175
304
  log(sessionId, `uncaught ${exception?.description ?? text}`);
176
305
  }
177
- if (method === 'Debugger.paused') await paused(sessionId);
306
+ if (method === 'Debugger.paused') {
307
+ pauses.set(sessionId, pauseOf(sessions.get(sessionId), params));
308
+ await paused(sessionId);
309
+ }
310
+ if (method === 'Debugger.resumed') pauses.delete(sessionId);
178
311
  });
179
312
 
180
313
  async function open(browserContextId) {
@@ -211,5 +344,5 @@ export function recorder(cdp, server, { coverage = ['/'], exits = new Map() } =
211
344
  ]);
212
345
  }
213
346
 
214
- return { open, begin, end };
347
+ return { open, begin, end, stall };
215
348
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-ecoverse/slicc-shared-web",
3
- "version": "1.5.1",
3
+ "version": "1.7.0",
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",