@ai-ecoverse/slicc-shared-web 1.12.2 → 1.13.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,6 +63,7 @@ test('boots', async (t) => {
63
63
  | `exits` | `{ urlPath: [regex, ...] }`: a breakpoint on the first line matching each pattern in that worker script, where the harness takes the worker's coverage and profile before it closes itself | none |
64
64
  | `intercept` | Remote URL prefixes answered from a disk cache in `node_modules/.cache/`. Registry documents (anything on a `registry.*` host but a `/-/….tgz` tarball: packuments, abbreviated or full, and dist-tags) are fetched fresh once per test process and only served from the disk cache when the registry can't be reached (a `4xx` is passed on) | none |
65
65
  | `extensions` | Unpacked extension directories to load. They are loaded with `Extensions.loadUnpacked` (behind `--enable-unsafe-extension-debugging`) once the harness is attached, so their service workers start paused like every other target. 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 |
66
+ | `persistent` | Run every test in the default browser context, on the launch's on-disk `slicc-harness-*` profile, instead of a fresh off-the-record context per test. Off-the-record contexts keep OPFS in memory, with a far smaller real budget than `navigator.storage.estimate()` reports, so a suite that stores hundreds of MB there needs this. When a test ends, its tabs are closed and `Storage.clearDataForOrigin` (`storageTypes: 'all'`) clears the server's origin, every origin a page in the context showed (popups the app opened included) and every origin the harness's tabs got a response from, so nothing leaks into the next test. If clearing fails, the test fails | `false` |
66
67
  | `args` | Extra Chromium flags, such as `--host-resolver-rules` or `--ignore-certificate-errors` | none |
67
68
  | `timeout` | How long `page.until` polls and `page.evaluate` waits, in ms | `30000` |
68
69
  | `stallAfter` | How long one evaluation may run before the harness probes the test's targets, in ms | `30000` |
@@ -237,6 +237,7 @@ export async function launch({
237
237
  stallAfter,
238
238
  profile = inherited.SLICC_PROFILE === '1',
239
239
  realistic = false,
240
+ persistent = false,
240
241
  } = {}) {
241
242
  pruning ??= prune();
242
243
  await pruning;
@@ -244,7 +245,7 @@ export async function launch({
244
245
  const found = await unpacked(extensions);
245
246
  const fromExtension = extensionSource(found);
246
247
  const files = { ...server, source: (href) => server.source(href) ?? fromExtension(href) };
247
- const shared = extensions.length > 0;
248
+ const shared = persistent || extensions.length > 0;
248
249
  const userData = await mkdtemp(join(tmpdir(), PREFIX));
249
250
  const loaded = [...found.values()].map((root) => root.slice(0, -1));
250
251
  const dumps = await crashpad();
@@ -262,8 +263,12 @@ export async function launch({
262
263
  let crashes = 0;
263
264
  let open = 0;
264
265
  let closed = false;
265
- cdp.on(({ method }) => {
266
+ const visited = new Set();
267
+ cdp.on(({ method, params }) => {
266
268
  if (method === CRASHED || method === 'Target.targetCrashed') crashes += 1;
269
+ if (method === 'Target.targetCreated' || method === 'Target.targetInfoChanged') {
270
+ visited.add(params.targetInfo.url);
271
+ }
267
272
  });
268
273
  const record = recorder(cdp, files, {
269
274
  coverage,
@@ -290,6 +295,27 @@ export async function launch({
290
295
  await Promise.all(closing.map((sent) => sent.catch(ignore)));
291
296
  }
292
297
 
298
+ async function wipe(pages) {
299
+ const urls = [
300
+ server.url,
301
+ ...visited,
302
+ ...pages.flatMap(({ responses }) => responses.map(({ url }) => url)),
303
+ ];
304
+ const origins = new Set(
305
+ urls
306
+ .map((url) => URL.parse(url))
307
+ .filter((url) => url?.protocol === 'http:' || url?.protocol === 'https:')
308
+ .map(({ origin }) => origin)
309
+ );
310
+ const { sessionId, targetId } = await record.open();
311
+ const clearing = [...origins].map((origin) =>
312
+ cdp.send('Storage.clearDataForOrigin', { origin, storageTypes: 'all' }, sessionId, 120000)
313
+ );
314
+ await Promise.all(clearing).finally(() =>
315
+ cdp.send('Target.closeTarget', { targetId }).catch(ignore)
316
+ );
317
+ }
318
+
293
319
  async function finish(pages, browserContextId, dir, before, mark, seen) {
294
320
  const log = stderr.since(mark).map((line) => `${line}\n`);
295
321
  await writeFile(new URL('chrome-stderr.log', dir), log.join(''));
@@ -308,6 +334,7 @@ export async function launch({
308
334
  ? sweep(before)
309
335
  : cdp.send('Target.disposeBrowserContext', { browserContextId });
310
336
  await bounded(closing.catch(ignore), 'dispose');
337
+ if (persistent) await wipe(pages);
311
338
  }
312
339
 
313
340
  return {
@@ -325,6 +352,7 @@ export async function launch({
325
352
  const before = new Set(shared ? await tabs() : []);
326
353
  record.begin(browserContextId, dir, `${suite}-${slug(t.name)}`);
327
354
  server.requests.length = 0;
355
+ visited.clear();
328
356
  server.overrides.clear();
329
357
  server.overridden.clear();
330
358
  const pages = [];
@@ -14,6 +14,14 @@ export function flights(now = Date.now) {
14
14
  const { request, type = 'Other', requestId } = params;
15
15
  requests.set(requestId, { what: `${request.method} ${request.url} (${type})`, since: now() });
16
16
  }
17
+ if (method === 'Network.responseReceived') {
18
+ const flight = requests.get(params.requestId);
19
+ if (flight) flight.status = params.response.status;
20
+ }
21
+ if (method === 'Network.dataReceived') {
22
+ const flight = requests.get(params.requestId);
23
+ if (flight) flight.bytes = (flight.bytes ?? 0) + params.dataLength;
24
+ }
17
25
  if (done.has(method)) requests.delete(params.requestId);
18
26
  if (method === 'Page.frameStartedNavigating') {
19
27
  const what = `navigation to ${params.url} (${params.navigationType})`;
@@ -27,7 +35,10 @@ export function flights(now = Date.now) {
27
35
  return [...navigations.values(), ...requests.values()]
28
36
  .sort((a, b) => a.since - b.since)
29
37
  .slice(0, 20)
30
- .map(({ what, since }) => `pending ${what} for ${((at - since) / 1000).toFixed(1)}s`);
38
+ .map(({ what, since, status, bytes = 0 }) => {
39
+ const answer = status ? `, answered ${status} with ${bytes} bytes of body so far` : '';
40
+ return `pending ${what} for ${((at - since) / 1000).toFixed(1)}s${answer}`;
41
+ });
31
42
  }
32
43
 
33
44
  function loaded(targetId) {
package/harness/page.mjs CHANGED
@@ -87,6 +87,10 @@ export function decoded({ value, unserializableValue }) {
87
87
 
88
88
  export const STALL_AFTER = 30000;
89
89
 
90
+ export function unanswered(error) {
91
+ return /: no answer in \d+ ms$/.test(error.message);
92
+ }
93
+
90
94
  export function page(
91
95
  cdp,
92
96
  { sessionId, targetId },
@@ -152,13 +156,16 @@ export function page(
152
156
  async function within(ms, fn, ...args) {
153
157
  const deadline = Date.now() + ms;
154
158
  let last;
159
+ let answered = false;
155
160
  for (let left = ms; left > 0; left = deadline - Date.now()) {
156
161
  try {
157
162
  last = await run(fn, args, left);
163
+ answered = true;
158
164
  if (last) return last;
159
165
  } catch (error) {
160
166
  if (error.fatal) throw error;
161
- last = error.message;
167
+ if (!answered || !unanswered(error)) last = error.message;
168
+ answered ||= !unanswered(error);
162
169
  }
163
170
  await sleep(25);
164
171
  }
@@ -54,16 +54,23 @@ export async function serve({
54
54
  requests.push(pathname);
55
55
  if (overrides.has(pathname)) {
56
56
  overridden.add(pathname);
57
+ const body = overrides.get(pathname);
57
58
  return response
58
- .writeHead(200, { ...headers, 'content-type': type(pathname), 'cache-control': 'no-cache' })
59
- .end(overrides.get(pathname));
59
+ .writeHead(200, {
60
+ ...headers,
61
+ 'content-type': type(pathname),
62
+ 'content-length': Buffer.byteLength(body),
63
+ 'cache-control': 'no-cache',
64
+ })
65
+ .end(body);
60
66
  }
61
67
  const file = locate(pathname);
62
- const found = file && (await stat(file).catch(() => null))?.isFile();
63
- if (!found) return response.writeHead(404, headers).end();
68
+ const info = file ? await stat(file).catch(() => null) : null;
69
+ if (!info?.isFile()) return response.writeHead(404, headers).end();
64
70
  response.writeHead(200, {
65
71
  ...headers,
66
72
  'content-type': type(file),
73
+ 'content-length': info.size,
67
74
  'cache-control': 'no-cache',
68
75
  });
69
76
  createReadStream(file).pipe(response);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-ecoverse/slicc-shared-web",
3
- "version": "1.12.2",
3
+ "version": "1.13.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",