@ai-ecoverse/slicc-shared-web 1.29.9 → 1.29.11

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
@@ -229,7 +229,7 @@ The diff-coverage hook, `slicc-unit-cover`, runs only the unit tests that cover
229
229
 
230
230
  - **Which directories are source:** `src/` by default, anywhere in the path, as before. A repo whose code lives elsewhere lists its directories in `package.json`: `"slicc": { "unitSources": ["extension", "six", "scripts"] }`, repo-relative, without `.` or `..` segments. `mcr.config.mjs` then collects coverage only from those directories, and the hook treats a staged file outside them as no code. A malformed list fails the hook.
231
231
  - **What counts as changed:** the staged files (`git diff --cached --name-only`), and nothing else. `test/unit/` is gitignored, and repos that keep their unit tests in a store outside git sync the whole store into it before a commit, which brings in tests for code that is on other branches; so a unit test that is new or edited runs only when its imports reach a staged file, like any other. One that reaches nothing staged adds no coverage to the staged lines.
232
- - **A unit test for code this tree doesn't have:** a test whose imports reach a staged file but also a repo file that doesn't exist and isn't a staged deletion (a store test for a module on another branch) is skipped, and the hook names it in one line before the count.
232
+ - **A unit test for code this tree doesn't have:** a test whose imports reach a staged file but also a code module under the sources that doesn't exist, isn't gitignored and isn't a staged deletion (a store test for a module on another branch) is skipped, and the hook names it in one line before the count. A missing `new URL(…, import.meta.url)` target, a missing non-code file such as `.wasm` or `.json`, and a missing file outside the sources or gitignored (a build output such as `dist/` or a bundled worker) don't skip the test.
233
233
  - **Which tests run:** every `*.test.*` file in `test/unit/` that is changed, or whose static imports reach a changed file. The import graph is parsed with [acorn](https://github.com/acornjs/acorn), TypeScript after Node's type stripping, or amaro's transform where stripping isn't enough. It follows `import`, `export … from`, `import()`, `require()` and `new URL(…, import.meta.url)` with literal paths, relative or through the `paths` of `tsconfig.json` (and the files it extends). A path may leave out its extension or name `index`, and `.js` also finds `.ts`. Type-only imports don't count, and nothing is followed into `node_modules/`.
234
234
  - **Barrels are followed by name:** a test that imports `{ a, b }` from a module follows only what provides `a` and `b`: `export { a } from`, `export { default as a } from`, an imported binding exported again, and `export * from`, by the target's own exports. `const { a, b } = await import('…')` with a literal path is followed by name too. A name the module declares itself follows all of that module's imports. A namespace import (`import * as`), any other `import()` (bound to a namespace, with `.then`, or destructured with a computed key or a rest element), a `require()`, and an `export *` that leads outside the repo or into CommonJS (any `.cjs` or `.cts` file) all count as the whole module.
235
235
  - **No code staged** (only docs, YAML, JSON or lockfiles): no unit tests and no diff coverage; the hook passes.
@@ -237,7 +237,7 @@ The diff-coverage hook, `slicc-unit-cover`, runs only the unit tests that cover
237
237
  - **A staged source file missing from the lcov:** the hook fails and names it, so no file passes diff coverage just because nothing measured it. `.jsx` and `.tsx` files are not transformed for coverage, so staging one under the sources fails the hook too.
238
238
  - **A file that won't parse:** one warning line, then every unit test.
239
239
 
240
- The hook passes the list to the repo's own `test:unit` in `SLICC_UNIT_TESTS` (one path per line). A selection of more than `SLICC_UNIT_BATCH` files (60 by default) runs in batches of that size, one `test:unit` run each with its own coverage in `coverage/unit/batch-N/` (the shared `mcr.config.mjs` writes to `SLICC_UNIT_COVERAGE_DIR` when it's set), and `slicc-diff-cover` gets every batch's `lcov.info`; diff-cover counts a line covered when any batch covered it. The hook ends with one verdict line over all batches, `slicc-unit-cover: PASS …` or `slicc-unit-cover: FAIL …` (the first failing batch stops the run and fails it; diff coverage then doesn't run). That keeps the coverage merge, which grows with the number of test files, small enough for a memory-capped hook on a widely imported file. A `--require` preload, which `slicc-unit-cover` and the shared `mcr.config.mjs` add to `NODE_OPTIONS`, makes the `node --test` process run the files on that list that its own patterns match, and none when the list is empty. Without the variable, `npm run test:unit` runs everything as before. To force a full run in the hook:
240
+ The hook passes the list to the repo's own `test:unit` in `SLICC_UNIT_TESTS` (one path per line). A selection of more than `SLICC_UNIT_BATCH` files (60 by default) runs in batches of that size, one `test:unit` run each with its own coverage in `coverage/unit/batch-N/` (the shared `mcr.config.mjs` writes to `SLICC_UNIT_COVERAGE_DIR` when it's set), and `slicc-diff-cover` gets every batch's `lcov.info`; diff-cover counts a line covered when any batch covered it. The hook ends with one verdict line over all batches, `slicc-unit-cover: PASS …` or `slicc-unit-cover: FAIL …` (the first failing batch stops the run and fails it; diff coverage then doesn't run). That keeps the coverage merge, which grows with the number of test files, small enough for a memory-capped hook on a widely imported file. A `--require` preload, which `slicc-unit-cover` and the shared `mcr.config.mjs` add to `NODE_OPTIONS`, makes the `node --test` process run the files on that list that its own patterns match, and none when the list is empty. Without the variable, `npm run test:unit` runs everything as before. In the hook the same preload bounds each test file's process (with the default `--test-isolation=process`; `none` runs no such processes, so nothing is bounded): every test gets `--test-timeout` (`SLICC_UNIT_TIMEOUT` ms, 60000 by default, `0` turns all of these bounds off), a file still running `SLICC_UNIT_TIMEOUT` ms after its tests ended (its top-level `after()` hooks get that long too) is ended, and a file whose event loop is blocked, or that has no test running and nothing finishing, for `SLICC_UNIT_TIMEOUT` + 5 s (one that hangs at top level, or a `before()` hook running that long) is killed; a test's own longer `timeout` is honoured. The FAIL verdict names each test that timed out and each file that was ended (`… was still running 60000ms after its tests ended, open handles kept it alive (TCPServerWrap)`) or killed, and such a file fails even when its tests passed. To force a full run in the hook:
241
241
 
242
242
  ```sh
243
243
  SLICC_UNIT_ALL=1 git commit
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { spawnSync } from 'node:child_process';
3
- import { existsSync, readFileSync } from 'node:fs';
3
+ import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
4
+ import { tmpdir } from 'node:os';
4
5
  import { join } from 'node:path';
5
6
  import { fileURLToPath } from 'node:url';
6
7
  import { code, ParseError, select } from '../unit/graph.mjs';
@@ -69,8 +70,19 @@ const batches = [];
69
70
  if (tests === undefined || !(size > 0) || tests.length <= size) batches.push(tests);
70
71
  else for (let at = 0; at < tests.length; at += size) batches.push(tests.slice(at, at + size));
71
72
  const reports = [];
73
+ const scratch = mkdtempSync(join(tmpdir(), 'slicc-unit-cover-'));
74
+ const report = join(scratch, 'report');
75
+ process.on('exit', () => rmSync(scratch, { recursive: true, force: true }));
76
+ const why = () => {
77
+ const lines = existsSync(report) ? readFileSync(report, 'utf8').trim().split('\n') : [];
78
+ return lines.length > 0 ? `: ${lines.join('; ')}` : '';
79
+ };
72
80
  for (const [index, batch] of batches.entries()) {
73
- const env = { ...process.env };
81
+ const env = {
82
+ ...process.env,
83
+ SLICC_UNIT_TIMEOUT: process.env.SLICC_UNIT_TIMEOUT ?? '60000',
84
+ SLICC_UNIT_REPORT: report,
85
+ };
74
86
  if (batch === undefined) delete env.SLICC_UNIT_TESTS;
75
87
  else {
76
88
  env.SLICC_UNIT_TESTS = batch.join('\n');
@@ -84,7 +96,7 @@ for (const [index, batch] of batches.entries()) {
84
96
  const status = run('npm', ['run', '--silent', 'test:unit'], env);
85
97
  if (status !== 0) {
86
98
  const which = batches.length > 1 ? `batch ${index + 1} of ${batches.length}` : 'the unit tests';
87
- verdict(false, `${which} failed (status ${status})`);
99
+ verdict(false, `${which} failed (status ${status})${why()}`);
88
100
  }
89
101
  }
90
102
  const local = join(root, 'node_modules', '.bin', 'slicc-diff-cover');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-ecoverse/slicc-shared-web",
3
- "version": "1.29.9",
3
+ "version": "1.29.11",
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",
package/unit/exports.mjs CHANGED
@@ -59,7 +59,9 @@ function dynamics(ast) {
59
59
  continue;
60
60
  }
61
61
  const value = dynamic(node);
62
- if (value !== undefined) found.push({ spec: value, names: [ALL] });
62
+ if (value !== undefined) {
63
+ found.push({ spec: value, names: [ALL], asset: node.type === 'NewExpression' });
64
+ }
63
65
  for (const key of Object.keys(node)) {
64
66
  const child = node[key];
65
67
  if (Array.isArray(child)) {
package/unit/graph.mjs CHANGED
@@ -1,7 +1,9 @@
1
+ import { spawnSync } from 'node:child_process';
1
2
  import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
2
3
  import { dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path';
3
4
  import { parse } from 'acorn';
4
5
  import { ALL, BODY, describe } from './exports.mjs';
6
+ import { inSources, unitSources } from './sources.mjs';
5
7
  import { compile } from './typescript.mjs';
6
8
 
7
9
  export const code = /\.(m|c)?(j|t)sx?$/;
@@ -226,12 +228,12 @@ function reacher(graph) {
226
228
  const absent = [];
227
229
  const seen = new Set();
228
230
  const queue = [[start, BODY]];
229
- const go = (file, spec, name) => {
231
+ const go = (file, spec, name, asset) => {
230
232
  const found = find(file, spec);
231
233
  if (found?.hit) queue.push([found.hit, name]);
232
234
  else if (found) {
233
235
  for (const path of found.missing) touched.add(path);
234
- if (found.missing.length > 0) absent.push(found.missing);
236
+ if (found.missing.length > 0 && !asset) absent.push(found.missing);
235
237
  }
236
238
  };
237
239
  while (queue.length > 0) {
@@ -242,9 +244,9 @@ function reacher(graph) {
242
244
  touched.add(file);
243
245
  const info = graph.load(file);
244
246
  if (!info) continue;
245
- for (const [next, wanted, spec] of step(info, name, file, context)) {
247
+ for (const [next, wanted, spec, asset] of step(info, name, file, context)) {
246
248
  if (spec === undefined) queue.push([next, wanted]);
247
- else go(next, spec, wanted);
249
+ else go(next, spec, wanted, asset);
248
250
  }
249
251
  }
250
252
  return { touched, absent };
@@ -264,7 +266,9 @@ function step(info, name, file, context) {
264
266
  if (name === BODY) {
265
267
  return [
266
268
  ...info.imports.flatMap(({ spec, names }) => names.map((wanted) => [file, wanted, spec])),
267
- ...info.dynamic.flatMap(({ spec, names }) => names.map((wanted) => [file, wanted, spec])),
269
+ ...info.dynamic.flatMap(({ spec, names, asset }) =>
270
+ names.map((wanted) => [file, wanted, spec, asset])
271
+ ),
268
272
  ];
269
273
  }
270
274
  if (info.whole) return [[file, BODY]];
@@ -282,15 +286,29 @@ function step(info, name, file, context) {
282
286
  return starred(file, info, name, context);
283
287
  }
284
288
 
289
+ function ignored(root) {
290
+ const memo = new Map();
291
+ return (path) => {
292
+ if (!memo.has(path)) {
293
+ const run = spawnSync('git', ['check-ignore', '-q', '--', path], { cwd: root });
294
+ memo.set(path, run.status === 0);
295
+ }
296
+ return memo.get(path);
297
+ };
298
+ }
299
+
285
300
  export function select({ root, changed }) {
286
301
  const all = walk(join(root, 'test', 'unit')).filter((file) => tests.test(file));
287
302
  const staged = new Set(changed.map((file) => resolve(root, file)));
288
303
  const reach = reacher(modules(root));
289
304
  const posix = (file) => relative(root, file).split(sep).join('/');
305
+ const sources = unitSources(root);
306
+ const built = ignored(root);
290
307
  const lacks = ([target, ...rest]) =>
291
- !relative(root, target).startsWith('..') &&
292
- !target.includes(`${sep}node_modules${sep}`) &&
308
+ (code.test(target) || extname(target) === '') &&
309
+ inSources(relative(root, target), root, sources) &&
293
310
  !existsSync(target) &&
311
+ !built(target) &&
294
312
  ![target, ...rest].some((path) => staged.has(path));
295
313
  const reached = [];
296
314
  const elsewhere = [];
package/unit/guard.cjs ADDED
@@ -0,0 +1,60 @@
1
+ 'use strict';
2
+ const { appendFileSync } = require('node:fs');
3
+ const { relative } = require('node:path');
4
+
5
+ const report = process.env.SLICC_UNIT_REPORT;
6
+ const runner = 'SLICC_UNIT_RUNNER';
7
+ const ms = Number(process.env.SLICC_UNIT_TIMEOUT);
8
+ const stall = ms + 5000;
9
+
10
+ const watchdog = `const { appendFileSync } = require('node:fs');
11
+ const { workerData: { beat, stall, report, line } } = require('node:worker_threads');
12
+ let last = Atomics.load(beat, 0);
13
+ while (Atomics.wait(beat, 0, last, stall) !== 'timed-out') last = Atomics.load(beat, 0);
14
+ appendFileSync(report, line);
15
+ process.kill(process.pid, 'SIGKILL');`;
16
+
17
+ if (report && process.execArgv.includes('--test') && !process.env.NODE_TEST_CONTEXT) {
18
+ process.env[runner] = String(process.pid);
19
+ if (ms > 0) process.execArgv.push(`--test-timeout=${ms}`);
20
+ } else if (
21
+ report &&
22
+ process.env.NODE_TEST_CONTEXT &&
23
+ require('node:worker_threads').isMainThread &&
24
+ process.ppid === Number(process.env[runner])
25
+ ) {
26
+ const file = relative(process.cwd(), process.argv[1] ?? '');
27
+ const note = (line) => appendFileSync(report, `${file}${line}\n`);
28
+ const beat = new Int32Array(new SharedArrayBuffer(4));
29
+ const alive = () => Atomics.notify(beat, 0, Atomics.add(beat, 0, 1) + 1);
30
+ const { after, afterEach, beforeEach } = require('node:test');
31
+ let running = 0;
32
+ beforeEach(() => {
33
+ running++;
34
+ alive();
35
+ });
36
+ afterEach((t) => {
37
+ running--;
38
+ alive();
39
+ if (t.error?.failureType === 'testTimeoutFailure') note(`: "${t.fullName}" ${t.error.message}`);
40
+ });
41
+ if (ms > 0) {
42
+ const { Worker } = require('node:worker_threads');
43
+ const line = `${file} was killed after ${stall}ms with no test running and nothing finishing, or its event loop blocked\n`;
44
+ new Worker(watchdog, { eval: true, workerData: { beat, stall, report, line } }).unref();
45
+ setInterval(() => running > 0 && alive(), 1000).unref();
46
+ after(() => {
47
+ alive();
48
+ setTimeout(() => {
49
+ let stdio = 3;
50
+ const open = process
51
+ .getActiveResourcesInfo()
52
+ .filter((name) => !(/^(Pipe|TTY)Wrap$/.test(name) && stdio-- > 0));
53
+ note(
54
+ ` was still running ${ms}ms after its tests ended, open handles kept it alive (${open.join(', ')})`
55
+ );
56
+ process.exit(1);
57
+ }, ms).unref();
58
+ });
59
+ }
60
+ }
package/unit/select.cjs CHANGED
@@ -1,4 +1,5 @@
1
1
  'use strict';
2
+ require('./guard.cjs');
2
3
  const { isAbsolute, join, matchesGlob, normalize, relative } = require('node:path');
3
4
 
4
5
  const key = Symbol.for('slicc.unit.select');