@ai-ecoverse/slicc-shared-web 2.1.1 → 2.2.1

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
@@ -9,7 +9,7 @@ Configuration shared by SLICC's web repos ([slicc-bios](https://github.com/ai-ec
9
9
  | `biome.json` | Formatter settings and slicc's strict linter: cognitive complexity ≤ 25, functions ≤ 150 lines, no unused variables or imports, no floating or misused promises, naming conventions, `import type`/`export type`. Test files under `test/` get slicc's test exemptions | `biome.json`: `{ "extends": ["@ai-ecoverse/slicc-shared-web/biome"] }` |
10
10
  | `tsconfig.json` | TypeScript base for Node's type stripping | `tsconfig.json`: `{ "extends": "@ai-ecoverse/slicc-shared-web/tsconfig.json", "include": ["src/**/*.ts"] }` |
11
11
  | `lefthook.yml` | Pre-commit hooks: Biome, typecheck, no comments, no unit tests in git, AGENTS.md, and diff coverage over [only the unit tests the change reaches](#which-unit-tests-a-commit-runs) | `lefthook.yml`: the [fail-closed snippet below](#hooks-that-fail-closed), and `prepare`: `slicc-install-hooks` ([one hooks directory per worktree](#one-hooks-directory-per-worktree)) |
12
- | `mcr.config.mjs` | Unit-test coverage for JS and TS sources in `src/` (TS types are stripped first), written to `coverage/unit/lcov.info`. Every source file is in it, tested or not, and paths are matched relative to the repo root, so a checkout under a dot-directory such as `~/.bb/` still reports untested files to diff coverage. With `SLICC_UNIT_TESTS` set, `node --test` runs only those files ([below](#which-unit-tests-a-commit-runs)) | `test:unit`: `mcr -c node_modules/@ai-ecoverse/slicc-shared-web/mcr.config.mjs node --test 'test/unit/**/*.test.mjs'` |
12
+ | `mcr.config.mjs` | Unit-test coverage for JS and TS sources (`.js`, `.mjs`, `.cjs`, `.ts`, `.mts`, `.cts`) in `src/` or the `slicc.unitSources` directories (TS types are stripped first), written to `coverage/unit/lcov.info`. Every source file is in it, tested or not, and paths are matched relative to the repo root, so a checkout under a dot-directory such as `~/.bb/` still reports untested files to diff coverage. With `SLICC_UNIT_TESTS` set, `node --test` runs only those files ([below](#which-unit-tests-a-commit-runs)) | `test:unit`: `mcr -c node_modules/@ai-ecoverse/slicc-shared-web/mcr.config.mjs node --test 'test/unit/**/*.test.mjs'` |
13
13
  | `bin/` | `slicc-lint` runs every check below plus Biome and, if the repo has one, `typecheck`. Every step prints its own `<step>: PASS …` line, and slicc-lint checks for it: a step that exits 0 without one fails the run with exit 70 (`slicc-lint: FAIL <step> (no PASS line from it)`), so a check that silently stops early can't pass. The last line, `slicc-lint: PASS`, comes only after every step has printed its PASS, and a gate can require it. `slicc-lint-comments` runs the [slicc no-comment checker](https://github.com/ai-ecoverse/slicc/tree/main/packages/dev-tools/no-comment) at a pinned commit, allowing AGENTS.md and [`# renovate:` annotations](#versions-pinned-in-workflow-files) in workflow and action files. `slicc-agents-md`, `slicc-no-unit-tests` and `slicc-diff-cover` complete the set, and `slicc-unit-cover` is the diff-coverage hook. `slicc-minidump <file.dmp>...` prints a crash dump's stack, described [below](#crash-dumps). `slicc-memory-peak` prints the job's [memory peak](#memory-peak). `slicc-install-hooks` installs the hooks into [this worktree's own directory](#one-hooks-directory-per-worktree) | `lint`: `slicc-lint`, and the hooks above |
14
14
  | `pnpmfile.mjs` | pnpm's counterpart to the Renovate preset: an `updateConfig` hook that adds `@ai-ecoverse/*` to `minimumReleaseAgeExclude`, so pnpm 12 doesn't hold back our own fresh releases (`ERR_PNPM_MINIMUM_RELEASE_AGE_VIOLATION`) while third-party packages keep its age check | in a repo with a pnpm lockfile, as a [config dependency](https://pnpm.io/config-dependencies) in `pnpm-workspace.yaml`: `configDependencies: { '@ai-ecoverse/slicc-shared-web': X.Y.Z }` plus `pnpmfile: node_modules/.pnpm-config/@ai-ecoverse/slicc-shared-web/pnpmfile.mjs` |
15
15
  | `harness/` | The CDP integration-test harness, described [below](#integration-test-harness) | `import { launch } from '@ai-ecoverse/slicc-shared-web/harness'`, and `--test-global-setup=node_modules/@ai-ecoverse/slicc-shared-web/harness/global.mjs` |
@@ -60,7 +60,7 @@ jobs:
60
60
 
61
61
  **Lint** fails the run, and nothing after it starts.
62
62
 
63
- **Deploy** runs the `deploy` command with every secret the caller passes as an environment variable. The command writes `url=<staging URL>` to `$GITHUB_OUTPUT`; the tests and the reviewers get it as `STAGING_URL`.
63
+ **Deploy** runs the `deploy` command with every secret the caller passes as an environment variable, except the pipeline's own `AWS_BEARER_TOKEN_BEDROCK`, `SLICC_ISSUES_TOKEN` and `github_token`. The command writes `url=<staging URL>` to `$GITHUB_OUTPUT`; the tests and the reviewers get it as `STAGING_URL`.
64
64
 
65
65
  **Integration tests** install Chrome for Testing with `slicc-cdp install-browser` (with `--with-deps` where the runner has root or passwordless sudo) (`--version` from the `chrome` input, stable without it), then split the files matching `test-glob` across `shards` jobs by their timings, with [split-tests-by-timings](https://github.com/marketplace/actions/split-tests-by-timings). The timings are the JUnit reports of the latest completed run of the same workflow; without one, the split is by count. Each shard runs the repo's own `test` command unchanged: `harness/shard.cjs`, preloaded through `NODE_OPTIONS`, makes the outermost `node --test` run only the shard's files that its own patterns match, writes `junit/<shard>.xml` next to the usual spec output, and then takes itself out of the environment, so a `node --test` that a test starts runs as it would without it. Files a `node --test` call would run that are not in `test-glob` (unit tests, say, whether in the same call or another) run on shard 0 only, so nothing is dropped; when `test-glob` matches nothing, shard 0 runs the command unchanged and the others run nothing. Each call keeps its own JUnit report (`junit/<shard>-<pid>.xml` for all but the last). Each shard uploads `coverage/`, `artifacts/` and `junit/` as `integration-<shard>`. `integration` passes only when every shard did, so it is the one check to require, under the caller's job name (`ci / integration`). A suite whose last step reads every test's artifacts, as this repo's `verify.mjs` does, sets `shards: 1`.
66
66
 
@@ -221,12 +221,14 @@ Without it, every worktree of a repo shares `.git/hooks/` with the main clone, a
221
221
 
222
222
  The diff-coverage hook, `slicc-unit-cover`, runs only the unit tests that cover the staged change, then `slicc-diff-cover` on their `coverage/unit/lcov.info`, so every staged line in `src/` still has to run:
223
223
 
224
+ - **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.
224
225
  - **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.
225
226
  - **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.
226
227
  - **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. 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/`.
227
228
  - **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.
228
229
  - **No code staged** (only docs, YAML, JSON or lockfiles): no unit tests and no diff coverage; the hook passes.
229
230
  - **A staged file no unit test reaches:** no tests run, its lines show as uncovered, and diff coverage fails as before.
231
+ - **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.
230
232
  - **A file that won't parse:** one warning line, then every unit test.
231
233
 
232
234
  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:
@@ -1,10 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
  import { spawnSync } from 'node:child_process';
3
- import { existsSync } from 'node:fs';
3
+ import { existsSync, readFileSync } from 'node:fs';
4
4
  import { join } from 'node:path';
5
5
  import { fileURLToPath } from 'node:url';
6
6
  import { code, ParseError, select } from '../unit/graph.mjs';
7
7
  import { inject } from '../unit/inject.mjs';
8
+ import { inSources } from '../unit/sources.mjs';
8
9
 
9
10
  const root = process.cwd();
10
11
  const say = (line) => process.stderr.write(`slicc-unit-cover: ${line}\n`);
@@ -47,10 +48,20 @@ function selection(staged) {
47
48
  const staged = (git('diff', '--cached', '--name-only', '--relative', '--no-renames', '-z') ?? '')
48
49
  .split('\0')
49
50
  .filter(Boolean);
50
- if (process.env.SLICC_UNIT_ALL !== '1' && !staged.some((file) => code.test(file))) {
51
+ if (
52
+ process.env.SLICC_UNIT_ALL !== '1' &&
53
+ !staged.some((file) => code.test(file) && inSources(file, root))
54
+ ) {
51
55
  say('no code staged, skipping unit tests and diff coverage');
52
56
  verdict(true, 'no code staged');
53
57
  }
58
+ const measured = staged.filter(
59
+ (file) => code.test(file) && inSources(file, root) && existsSync(join(root, file))
60
+ );
61
+ const jsx = measured.filter((file) => /x$/.test(file));
62
+ if (jsx.length > 0) {
63
+ verdict(false, `coverage cannot be measured for JSX, which is not transformed: ${jsx.join(' ')}`);
64
+ }
54
65
 
55
66
  const size = Number(process.env.SLICC_UNIT_BATCH ?? 60);
56
67
  const tests = selection(staged);
@@ -82,6 +93,18 @@ const cover = existsSync(local)
82
93
  : fileURLToPath(new URL('slicc-diff-cover.sh', import.meta.url));
83
94
  const files = tests === undefined ? 'every unit test file' : `${tests.length} unit test files`;
84
95
  const counted = `${batches.length} ${batches.length === 1 ? 'batch' : 'batches'}`;
96
+ const lcov = (reports.length > 0 ? reports : [join('coverage', 'unit', 'lcov.info')])
97
+ .map((report) => {
98
+ try {
99
+ return readFileSync(join(root, report), 'utf8');
100
+ } catch {
101
+ return '';
102
+ }
103
+ })
104
+ .join('\n');
105
+ const seen = new Set(lcov.match(/^SF:.*$/gm)?.map((line) => line.slice(3)) ?? []);
106
+ const missing = measured.filter((file) => !seen.has(file));
107
+ if (missing.length > 0) verdict(false, `no coverage was collected for ${missing.join(' ')}`);
85
108
  const covered = run(cover, reports, process.env);
86
109
  if (covered !== 0) verdict(false, `diff coverage is not 100% (${files} in ${counted})`);
87
110
  verdict(true, `${files} in ${counted}, diff coverage 100%`);
package/lefthook.yml CHANGED
@@ -15,5 +15,5 @@ pre-commit:
15
15
  - name: AGENTS.md
16
16
  run: npx --no-install slicc-agents-md
17
17
  - name: diff coverage
18
- glob: "src/**.{js,mjs,ts}"
18
+ glob: "*.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"
19
19
  run: npx --no-install slicc-unit-cover
package/mcr.config.mjs CHANGED
@@ -2,21 +2,22 @@ import { stripTypeScriptTypes } from 'node:module';
2
2
  import { relative, sep } from 'node:path';
3
3
  import { fileURLToPath } from 'node:url';
4
4
  import { inject } from './unit/inject.mjs';
5
+ import { inSources, unitSources } from './unit/sources.mjs';
5
6
 
6
7
  if (process.env.SLICC_UNIT_TESTS !== undefined) process.env.NODE_OPTIONS = inject();
7
8
 
9
+ const code = /\.(m|c)?(j|t)s$/;
10
+ const typed = /\.(m|c)?ts$/;
11
+
8
12
  const strip = (entry) => {
9
- if (entry.url.endsWith('.ts')) entry.source = stripTypeScriptTypes(entry.source);
13
+ if (typed.test(entry.url)) entry.source = stripTypeScriptTypes(entry.source);
10
14
  };
11
15
 
12
- const code = /\.(m?js|ts)$/;
16
+ const listed = unitSources();
13
17
 
14
- const source = (file) => {
15
- const parts = relative(process.cwd(), file).split(sep);
16
- return (
17
- parts[0] !== '..' && parts.includes('src') && !parts.includes('node_modules') && code.test(file)
18
- );
19
- };
18
+ const source = (file) =>
19
+ code.test(file) &&
20
+ inSources(relative(process.cwd(), file).split(sep).join('/'), process.cwd(), listed);
20
21
 
21
22
  const ours = (url) => url.startsWith('file:') && source(fileURLToPath(url));
22
23
 
@@ -24,7 +25,7 @@ export default {
24
25
  name: process.env.npm_package_name ?? 'unit',
25
26
  outputDir: process.env.SLICC_UNIT_COVERAGE_DIR ?? 'coverage/unit',
26
27
  reports: ['lcovonly', 'console-details'],
27
- all: { dir: 'src', filter: source, transformer: strip },
28
+ all: { dir: listed ?? 'src', filter: source, transformer: strip },
28
29
  entryFilter: (entry) => ours(entry.url),
29
30
  onEntry: strip,
30
31
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-ecoverse/slicc-shared-web",
3
- "version": "2.1.1",
3
+ "version": "2.2.1",
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",
@@ -65,7 +65,7 @@
65
65
  },
66
66
  "devDependencies": {
67
67
  "@ai-ecoverse/slicc-browsers": "1.13.0",
68
- "@ai-ecoverse/slicc-cdp": "1.30.19",
68
+ "@ai-ecoverse/slicc-cdp": "1.30.20",
69
69
  "@biomejs/biome": "2.5.15",
70
70
  "lefthook": "2.2.2",
71
71
  "monocart-coverage-reports": "2.13.2",
@@ -0,0 +1,37 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { join, relative, sep } from 'node:path';
3
+
4
+ export function unitSources(root = process.cwd()) {
5
+ let pkg;
6
+ try {
7
+ pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'));
8
+ } catch {
9
+ return null;
10
+ }
11
+ const list = pkg?.slicc?.unitSources;
12
+ if (list === undefined) return null;
13
+ if (
14
+ !Array.isArray(list) ||
15
+ !list.length ||
16
+ !list.every(
17
+ (d) =>
18
+ typeof d === 'string' &&
19
+ /^[\w.-]+(\/[\w.-]+)*$/.test(d) &&
20
+ d.split('/').every((p) => p !== '.' && p !== '..')
21
+ )
22
+ )
23
+ throw new Error(
24
+ `package.json slicc.unitSources must be a list of repo-relative directories, not ${JSON.stringify(list)}`
25
+ );
26
+ return list;
27
+ }
28
+
29
+ export function inSources(file, root = process.cwd(), list = unitSources(root)) {
30
+ const parts = relative(root, join(root, file)).split(sep);
31
+ if (parts[0] === '..' || parts.includes('node_modules')) return false;
32
+ if (!list) return parts.includes('src');
33
+ return list.some((dir) => {
34
+ const want = dir.split('/');
35
+ return want.every((p, i) => parts[i] === p) && parts.length > want.length;
36
+ });
37
+ }