@ai-ecoverse/slicc-shared-web 1.23.2 → 1.24.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 +3 -3
- package/bin/slicc-diff-cover.sh +2 -1
- package/bin/slicc-unit-cover.mjs +32 -9
- package/harness/recorder.mjs +70 -44
- package/mcr.config.mjs +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -144,7 +144,7 @@ The diff-coverage hook, `slicc-unit-cover`, runs only the unit tests that cover
|
|
|
144
144
|
- **A staged file no unit test reaches:** no tests run, its lines show as uncovered, and diff coverage fails as before.
|
|
145
145
|
- **A file that won't parse:** one warning line, then every unit test.
|
|
146
146
|
|
|
147
|
-
The hook passes the list to the repo's own `test:unit` in `SLICC_UNIT_TESTS` (one path per line). 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:
|
|
147
|
+
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:
|
|
148
148
|
|
|
149
149
|
```sh
|
|
150
150
|
SLICC_UNIT_ALL=1 git commit
|
|
@@ -187,7 +187,7 @@ test('boots', async (t) => {
|
|
|
187
187
|
| `roots` | `[urlPrefix, directory]` pairs the local server serves, first match wins | `[['/', 'src/']]` |
|
|
188
188
|
| `aliases` | Extra `[urlPrefix, directory]` pairs used only to map coverage and profiles back to files, for scripts that something else serves, such as a service worker | none |
|
|
189
189
|
| `isolated` | Send COOP, COEP and CORP on every response, so pages are cross-origin isolated | `false` |
|
|
190
|
-
| `coverage` | URL prefixes whose `.js`/`.mjs` scripts count for coverage. `node_modules/` never does. A `.map` next to a file is used | `['/']` |
|
|
190
|
+
| `coverage` | URL prefixes whose `.js`/`.mjs` scripts count for coverage. `node_modules/` never does. A `.map` next to a file is used. `[]` collects nothing: without profiling, no target gets the debugger, the profiler, exit breakpoints or a `beforeunload` pause, and workers don't wait for the harness to start | `['/']` |
|
|
191
191
|
| `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 |
|
|
192
192
|
| `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 |
|
|
193
193
|
| `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 |
|
|
@@ -217,7 +217,7 @@ test('boots', async (t) => {
|
|
|
217
217
|
- the summary runs in the test process, which also serves the page's files and talks CDP, so it is linear in the trace's events: a busy page's half-million-event trace takes well under a second, and the page's fetches keep getting answers meanwhile;
|
|
218
218
|
- 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.
|
|
219
219
|
|
|
220
|
-
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`.
|
|
220
|
+
Coverage is kept as one entry per script URL, each worker's counts summed into it as the worker's coverage is taken, so a run of thousands of workers keeps no more than its distinct scripts. A worker paused at its exit breakpoint is resumed as soon as its coverage and profile are taken; the profile is written to disk after. 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`.
|
|
221
221
|
|
|
222
222
|
Why it works the way it does:
|
|
223
223
|
|
package/bin/slicc-diff-cover.sh
CHANGED
|
@@ -4,4 +4,5 @@ if ! command -v uvx > /dev/null 2>&1; then
|
|
|
4
4
|
printf 'slicc-diff-cover runs diff-cover through uvx; install uv first: https://docs.astral.sh/uv/getting-started/installation/\n' >&2
|
|
5
5
|
exit 1
|
|
6
6
|
fi
|
|
7
|
-
|
|
7
|
+
[ "$#" -gt 0 ] || set -- coverage/unit/lcov.info
|
|
8
|
+
exec uvx diff-cover@10.6.0 "$@" --compare-branch HEAD --ignore-unstaged --fail-under 100
|
package/bin/slicc-unit-cover.mjs
CHANGED
|
@@ -8,6 +8,10 @@ import { inject } from '../unit/inject.mjs';
|
|
|
8
8
|
|
|
9
9
|
const root = process.cwd();
|
|
10
10
|
const say = (line) => process.stderr.write(`slicc-unit-cover: ${line}\n`);
|
|
11
|
+
const verdict = (ok, line) => {
|
|
12
|
+
process.stdout.write(`slicc-unit-cover: ${ok ? 'PASS' : 'FAIL'} ${line}\n`);
|
|
13
|
+
process.exit(ok ? 0 : 1);
|
|
14
|
+
};
|
|
11
15
|
|
|
12
16
|
function git(...args) {
|
|
13
17
|
const run = spawnSync('git', args, { cwd: root, encoding: 'utf8' });
|
|
@@ -45,20 +49,39 @@ const staged = (git('diff', '--cached', '--name-only', '--relative', '--no-renam
|
|
|
45
49
|
.filter(Boolean);
|
|
46
50
|
if (process.env.SLICC_UNIT_ALL !== '1' && !staged.some((file) => code.test(file))) {
|
|
47
51
|
say('no code staged, skipping unit tests and diff coverage');
|
|
48
|
-
|
|
52
|
+
verdict(true, 'no code staged');
|
|
49
53
|
}
|
|
50
54
|
|
|
51
|
-
const
|
|
55
|
+
const size = Number(process.env.SLICC_UNIT_BATCH ?? 60);
|
|
52
56
|
const tests = selection(staged);
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
+
const batches = [];
|
|
58
|
+
if (tests === undefined || !(size > 0) || tests.length <= size) batches.push(tests);
|
|
59
|
+
else for (let at = 0; at < tests.length; at += size) batches.push(tests.slice(at, at + size));
|
|
60
|
+
const reports = [];
|
|
61
|
+
for (const [index, batch] of batches.entries()) {
|
|
62
|
+
const env = { ...process.env };
|
|
63
|
+
if (batch === undefined) delete env.SLICC_UNIT_TESTS;
|
|
64
|
+
else {
|
|
65
|
+
env.SLICC_UNIT_TESTS = batch.join('\n');
|
|
66
|
+
env.NODE_OPTIONS = inject(env.NODE_OPTIONS ?? '');
|
|
67
|
+
}
|
|
68
|
+
if (batches.length > 1) {
|
|
69
|
+
env.SLICC_UNIT_COVERAGE_DIR = join('coverage', 'unit', `batch-${index + 1}`);
|
|
70
|
+
reports.push(join(env.SLICC_UNIT_COVERAGE_DIR, 'lcov.info'));
|
|
71
|
+
say(`batch ${index + 1} of ${batches.length}: ${batch.length} files`);
|
|
72
|
+
}
|
|
73
|
+
const status = run('npm', ['run', '--silent', 'test:unit'], env);
|
|
74
|
+
if (status !== 0) {
|
|
75
|
+
const which = batches.length > 1 ? `batch ${index + 1} of ${batches.length}` : 'the unit tests';
|
|
76
|
+
verdict(false, `${which} failed (status ${status})`);
|
|
77
|
+
}
|
|
57
78
|
}
|
|
58
|
-
const status = run('npm', ['run', '--silent', 'test:unit'], env);
|
|
59
|
-
if (status !== 0) process.exit(status);
|
|
60
79
|
const local = join(root, 'node_modules', '.bin', 'slicc-diff-cover');
|
|
61
80
|
const cover = existsSync(local)
|
|
62
81
|
? local
|
|
63
82
|
: fileURLToPath(new URL('slicc-diff-cover.sh', import.meta.url));
|
|
64
|
-
|
|
83
|
+
const files = tests === undefined ? 'every unit test file' : `${tests.length} unit test files`;
|
|
84
|
+
const counted = `${batches.length} ${batches.length === 1 ? 'batch' : 'batches'}`;
|
|
85
|
+
const covered = run(cover, reports, process.env);
|
|
86
|
+
if (covered !== 0) verdict(false, `diff coverage is not 100% (${files} in ${counted})`);
|
|
87
|
+
verdict(true, `${files} in ${counted}, diff coverage 100%`);
|
package/harness/recorder.mjs
CHANGED
|
@@ -2,12 +2,12 @@ import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
|
2
2
|
import { basename, relative } from 'node:path';
|
|
3
3
|
import { cwd } from 'node:process';
|
|
4
4
|
import { pathToFileURL } from 'node:url';
|
|
5
|
+
import Util from 'monocart-coverage-reports/util';
|
|
5
6
|
import { CRASHED } from './cdp.mjs';
|
|
6
7
|
import { trace } from './trace.mjs';
|
|
7
8
|
import { bounded, ignore, raw, samples, sleep, TIMED_OUT } from './util.mjs';
|
|
8
9
|
|
|
9
10
|
const profiled = new Set(['page', 'worker', 'service_worker', 'shared_worker']);
|
|
10
|
-
const nested = { autoAttach: true, waitForDebuggerOnStart: true, flatten: true };
|
|
11
11
|
const sampling = [['Profiler.setSamplingInterval', { interval: 100 }], ['Profiler.start']];
|
|
12
12
|
const unload = "addEventListener('beforeunload', () => { debugger; })";
|
|
13
13
|
|
|
@@ -91,13 +91,13 @@ async function sample(cdp, pauses, [sessionId, target], dir, tag, profiling) {
|
|
|
91
91
|
return `${label(target)}: ${state}, profile ${name}`;
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
function collect(send, profiling) {
|
|
95
|
-
const
|
|
96
|
-
const taken = Promise.all([
|
|
97
|
-
|
|
98
|
-
(
|
|
99
|
-
|
|
100
|
-
);
|
|
94
|
+
function collect(send, profiling, collecting) {
|
|
95
|
+
const asked = (on, method) => (on ? send(method).catch(() => ({})) : {});
|
|
96
|
+
const taken = Promise.all([
|
|
97
|
+
asked(collecting, 'Profiler.takePreciseCoverage'),
|
|
98
|
+
asked(profiling, 'Profiler.stop'),
|
|
99
|
+
]);
|
|
100
|
+
return Promise.race([taken, sleep(3000)]).then((answer) => answer ?? [{}, {}]);
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
function held(pauses) {
|
|
@@ -131,7 +131,48 @@ function contextOf(sessions, parents, sessionId) {
|
|
|
131
131
|
return undefined;
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
+
export function retain(scripts, result) {
|
|
135
|
+
for (const script of result) {
|
|
136
|
+
const prev = scripts.get(script.url);
|
|
137
|
+
const functions = prev ? Util.mergeV8Coverage([prev, script]).functions : script.functions;
|
|
138
|
+
scripts.set(script.url, { ...(prev ?? script), functions });
|
|
139
|
+
}
|
|
140
|
+
return scripts;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function commands({ type, url }, { coverage = ['/'], exits = new Map(), profiling } = {}) {
|
|
144
|
+
const collecting = coverage.length > 0;
|
|
145
|
+
const watching = collecting || Boolean(profiling);
|
|
146
|
+
const nested = { autoAttach: true, waitForDebuggerOnStart: watching, flatten: true };
|
|
147
|
+
const list = [];
|
|
148
|
+
if (profiled.has(type)) {
|
|
149
|
+
list.push(['Runtime.enable']);
|
|
150
|
+
if (watching) list.push(['Profiler.enable']);
|
|
151
|
+
if (collecting) {
|
|
152
|
+
list.push(['Profiler.startPreciseCoverage', { callCount: true, detailed: true }]);
|
|
153
|
+
}
|
|
154
|
+
list.push(['Target.setAutoAttach', nested]);
|
|
155
|
+
if (profiling) list.push(...sampling);
|
|
156
|
+
}
|
|
157
|
+
const lines = type === 'worker' && watching ? exits.get(pathOf(url)) : undefined;
|
|
158
|
+
if (lines) {
|
|
159
|
+
list.push(
|
|
160
|
+
['Debugger.enable'],
|
|
161
|
+
...lines.map((lineNumber) => ['Debugger.setBreakpointByUrl', { url, lineNumber }])
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
if (type === 'page') {
|
|
165
|
+
list.push(['Network.enable'], ['Page.enable']);
|
|
166
|
+
if (watching) {
|
|
167
|
+
list.push(['Debugger.enable'], ['Page.addScriptToEvaluateOnNewDocument', { source: unload }]);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return list;
|
|
171
|
+
}
|
|
172
|
+
|
|
134
173
|
export function recorder(cdp, server, { coverage = ['/'], exits = new Map(), profiling } = {}) {
|
|
174
|
+
const collecting = coverage.length > 0;
|
|
175
|
+
const watching = collecting || Boolean(profiling);
|
|
135
176
|
const sessions = new Map();
|
|
136
177
|
const pauses = new Map();
|
|
137
178
|
const parents = new Map();
|
|
@@ -143,18 +184,21 @@ export function recorder(cdp, server, { coverage = ['/'], exits = new Map(), pro
|
|
|
143
184
|
async function dump(sessionId, target, restart) {
|
|
144
185
|
const current = run;
|
|
145
186
|
const send = (method) => cdp.send(method, {}, sessionId);
|
|
146
|
-
const [{ result }, { profile }] = await collect(send, profiling);
|
|
147
|
-
if (!
|
|
148
|
-
current.scripts
|
|
187
|
+
const [{ result }, { profile }] = await collect(send, profiling, collecting);
|
|
188
|
+
if (!current) return;
|
|
189
|
+
retain(current.scripts, (result ?? []).filter(ours));
|
|
149
190
|
if (!profile) return;
|
|
150
191
|
if (restart) send('Profiler.start').catch(ignore);
|
|
151
192
|
current.profiles += 1;
|
|
152
193
|
selfTimes(profile, server, current.self);
|
|
153
194
|
const name = `${String(current.profiles).padStart(3, '0')}-${label(target)}.cpuprofile`;
|
|
154
|
-
|
|
195
|
+
current.writes.push(
|
|
196
|
+
writeFile(new URL(name, current.dir), JSON.stringify(profile)).catch(ignore)
|
|
197
|
+
);
|
|
155
198
|
}
|
|
156
199
|
|
|
157
200
|
async function checkpoint() {
|
|
201
|
+
if (!watching) return;
|
|
158
202
|
const current = [...sessions].filter(([, target]) => target.browserContextId === run?.context);
|
|
159
203
|
await Promise.all(current.map(([sessionId, target]) => dump(sessionId, target, true)));
|
|
160
204
|
}
|
|
@@ -170,38 +214,9 @@ export function recorder(cdp, server, { coverage = ['/'], exits = new Map(), pro
|
|
|
170
214
|
return tabs.get(targetId);
|
|
171
215
|
}
|
|
172
216
|
|
|
173
|
-
function commands({ type, url }) {
|
|
174
|
-
const list = [];
|
|
175
|
-
if (profiled.has(type)) {
|
|
176
|
-
list.push(
|
|
177
|
-
['Runtime.enable'],
|
|
178
|
-
['Profiler.enable'],
|
|
179
|
-
['Profiler.startPreciseCoverage', { callCount: true, detailed: true }],
|
|
180
|
-
['Target.setAutoAttach', nested]
|
|
181
|
-
);
|
|
182
|
-
if (profiling) list.push(...sampling);
|
|
183
|
-
}
|
|
184
|
-
const lines = type === 'worker' ? exits.get(pathOf(url)) : undefined;
|
|
185
|
-
if (lines) {
|
|
186
|
-
list.push(
|
|
187
|
-
['Debugger.enable'],
|
|
188
|
-
...lines.map((lineNumber) => ['Debugger.setBreakpointByUrl', { url, lineNumber }])
|
|
189
|
-
);
|
|
190
|
-
}
|
|
191
|
-
if (type === 'page') {
|
|
192
|
-
list.push(
|
|
193
|
-
['Network.enable'],
|
|
194
|
-
['Debugger.enable'],
|
|
195
|
-
['Page.enable'],
|
|
196
|
-
['Page.addScriptToEvaluateOnNewDocument', { source: unload }]
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
return list;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
217
|
function attach({ sessionId, targetInfo, waitingForDebugger }) {
|
|
203
218
|
if (profiled.has(targetInfo.type)) sessions.set(sessionId, targetInfo);
|
|
204
|
-
const list = commands(targetInfo);
|
|
219
|
+
const list = commands(targetInfo, { coverage, exits, profiling });
|
|
205
220
|
if (waitingForDebugger) list.push(['Runtime.runIfWaitingForDebugger']);
|
|
206
221
|
const ready = Promise.all(
|
|
207
222
|
list.map(([method, params]) => cdp.send(method, params, sessionId).catch(ignore))
|
|
@@ -283,14 +298,25 @@ export function recorder(cdp, server, { coverage = ['/'], exits = new Map(), pro
|
|
|
283
298
|
}
|
|
284
299
|
|
|
285
300
|
function begin(context, dir, name) {
|
|
286
|
-
run = {
|
|
301
|
+
run = {
|
|
302
|
+
context,
|
|
303
|
+
dir,
|
|
304
|
+
name,
|
|
305
|
+
scripts: new Map(),
|
|
306
|
+
writes: [],
|
|
307
|
+
console: early.splice(0),
|
|
308
|
+
profiles: 0,
|
|
309
|
+
self: {},
|
|
310
|
+
};
|
|
287
311
|
}
|
|
288
312
|
|
|
289
313
|
async function end() {
|
|
290
314
|
await checkpoint();
|
|
291
315
|
const current = run;
|
|
292
316
|
run = null;
|
|
293
|
-
|
|
317
|
+
await Promise.all(current.writes);
|
|
318
|
+
const scripts = [...current.scripts.values()];
|
|
319
|
+
const entries = await Promise.all(scripts.map((script) => entry(server, script)));
|
|
294
320
|
const found = { profiles: current.profiles, self: current.self };
|
|
295
321
|
await mkdir(raw, { recursive: true });
|
|
296
322
|
await mkdir(samples, { recursive: true });
|
package/mcr.config.mjs
CHANGED
|
@@ -12,7 +12,7 @@ const ours = (url) =>
|
|
|
12
12
|
|
|
13
13
|
export default {
|
|
14
14
|
name: process.env.npm_package_name ?? 'unit',
|
|
15
|
-
outputDir: 'coverage/unit',
|
|
15
|
+
outputDir: process.env.SLICC_UNIT_COVERAGE_DIR ?? 'coverage/unit',
|
|
16
16
|
reports: ['lcovonly', 'console-details'],
|
|
17
17
|
all: { dir: 'src', filter: '**/*.{js,mjs,ts}', transformer: strip },
|
|
18
18
|
entryFilter: (entry) => ours(entry.url),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-ecoverse/slicc-shared-web",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.24.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",
|
|
@@ -62,11 +62,11 @@
|
|
|
62
62
|
"release": "semantic-release"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
|
-
"@ai-ecoverse/slicc-browsers": "1.
|
|
65
|
+
"@ai-ecoverse/slicc-browsers": "1.8.4",
|
|
66
66
|
"@biomejs/biome": "2.5.15",
|
|
67
67
|
"monocart-coverage-reports": "2.13.2",
|
|
68
68
|
"playwright-core": "1.64.0",
|
|
69
|
-
"pnpm": "12.
|
|
69
|
+
"pnpm": "12.12.0",
|
|
70
70
|
"semantic-release": "25.0.9"
|
|
71
71
|
},
|
|
72
72
|
"peerDependencies": {
|