@specific.dev/spectest 0.90.1 → 0.90.3
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/dist/browser-coverage.d.ts +5 -5
- package/dist/browser-coverage.js +11 -14
- package/dist/coverage.d.ts +3 -1
- package/dist/coverage.js +18 -13
- package/dist/harness/browser-coverage.d.ts +4 -3
- package/dist/harness/browser-coverage.js +14 -8
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -2
- package/dist/project-files.js +4 -35
- package/package.json +1 -1
- package/src/browser-coverage.ts +11 -13
- package/src/coverage.test.ts +78 -0
- package/src/coverage.ts +18 -13
- package/src/harness/browser-coverage.test.ts +9 -6
- package/src/harness/browser-coverage.ts +12 -7
- package/src/index.ts +1 -2
- package/src/project-files.ts +4 -37
|
@@ -22,10 +22,10 @@ export declare function foldBrowserCoverage(service: string, files: FileCounts):
|
|
|
22
22
|
/** Harvest every live session, so a capture sees the counters of code
|
|
23
23
|
* that ran since the last browser op. */
|
|
24
24
|
export declare function harvestAllBrowserCoverage(): Promise<void>;
|
|
25
|
-
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
export declare function
|
|
25
|
+
/** Take one service's lcov report and clear only its totals. Other
|
|
26
|
+
* services retain their coverage until their own capture. Each capture
|
|
27
|
+
* is a delta; after every service is captured, a forked child inherits
|
|
28
|
+
* empty totals from its parent. */
|
|
29
|
+
export declare function takeBrowserCoverageReport(service: string): string;
|
|
30
30
|
/** The current totals, without clearing them (tests). */
|
|
31
31
|
export declare function browserCoverageReports(): Map<string, string>;
|
package/dist/browser-coverage.js
CHANGED
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
//
|
|
13
13
|
// Harvest runs before every browser step (a click that navigates
|
|
14
14
|
// cross-site replaces the renderer, and the old counters with it) and at
|
|
15
|
-
// every coverage capture. `
|
|
16
|
-
// one
|
|
15
|
+
// every coverage capture. `takeBrowserCoverageReport` renders and clears
|
|
16
|
+
// one service's totals for the `case-coverage` bundle.
|
|
17
17
|
//
|
|
18
18
|
// Configured by the daemon at /load with the host → service table; a
|
|
19
19
|
// project with no `coverage: { browser: true }` service never attaches
|
|
@@ -196,18 +196,15 @@ function parseDataUrl(ref) {
|
|
|
196
196
|
export async function harvestAllBrowserCoverage() {
|
|
197
197
|
await Promise.all([...LIVE_COLLECTORS].map((c) => harvestBrowserCoverage(c.cdp)));
|
|
198
198
|
}
|
|
199
|
-
/**
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
export function
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
TOTALS.clear();
|
|
210
|
-
return out;
|
|
199
|
+
/** Take one service's lcov report and clear only its totals. Other
|
|
200
|
+
* services retain their coverage until their own capture. Each capture
|
|
201
|
+
* is a delta; after every service is captured, a forked child inherits
|
|
202
|
+
* empty totals from its parent. */
|
|
203
|
+
export function takeBrowserCoverageReport(service) {
|
|
204
|
+
const files = TOTALS.get(service);
|
|
205
|
+
const report = files ? lcovDocument(files) : "TN:\n";
|
|
206
|
+
TOTALS.delete(service);
|
|
207
|
+
return report;
|
|
211
208
|
}
|
|
212
209
|
/** The current totals, without clearing them (tests). */
|
|
213
210
|
export function browserCoverageReports() {
|
package/dist/coverage.d.ts
CHANGED
|
@@ -213,6 +213,8 @@ export declare const NODE_COVERAGE_DUMP_PREFIX = "node-";
|
|
|
213
213
|
* source map: the inline `data:` map in the text, the file the last
|
|
214
214
|
* `sourceMappingURL` comment names, or `<script>.map` next to the
|
|
215
215
|
* script; `sourcesContent` dropped (the sources are the repository).
|
|
216
|
+
* Records are keyed by their descriptor and compiled-source hash, not
|
|
217
|
+
* their URL: a recompilation can reuse a URL with different source.
|
|
216
218
|
* "Once per branch" is a marker file under `.spectest/scripts/`: the
|
|
217
219
|
* directory is shared by every process of the service and forks with
|
|
218
220
|
* the environment, so a record written at bring-up or by an ancestor
|
|
@@ -292,7 +294,7 @@ export declare function collectNodeReports(dir: string): Promise<NodeReportStats
|
|
|
292
294
|
* loads from this service's origins, maps it back to source through the
|
|
293
295
|
* served source map, and writes one lcov per capture (`browser.lcov`)
|
|
294
296
|
* holding what ran since the previous capture: every live session is
|
|
295
|
-
* harvested first, and
|
|
297
|
+
* harvested first, and only this service's totals are taken and cleared. Nothing ran ⇒
|
|
296
298
|
* an lcov with no records, which is a report saying so.
|
|
297
299
|
*/
|
|
298
300
|
export declare function browser(): CoverageAdapter;
|
package/dist/coverage.js
CHANGED
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
// import * as coverage from "@specific.dev/spectest/coverage";
|
|
28
28
|
// coverage: { adapters: [coverage.node(), coverage.browser()] }
|
|
29
29
|
import { createHash } from "node:crypto";
|
|
30
|
-
import { harvestAllBrowserCoverage,
|
|
30
|
+
import { harvestAllBrowserCoverage, takeBrowserCoverageReport } from "./browser-coverage.js";
|
|
31
31
|
import { COVERAGE_CONTAINER_DIR } from "./harness/coverage.js";
|
|
32
32
|
export { COVERAGE_CONTAINER_DIR };
|
|
33
33
|
/** Where `node()` mounts its hook inside the container. */
|
|
@@ -196,6 +196,8 @@ export const NODE_COVERAGE_DUMP_PREFIX = "node-";
|
|
|
196
196
|
* source map: the inline `data:` map in the text, the file the last
|
|
197
197
|
* `sourceMappingURL` comment names, or `<script>.map` next to the
|
|
198
198
|
* script; `sourcesContent` dropped (the sources are the repository).
|
|
199
|
+
* Records are keyed by their descriptor and compiled-source hash, not
|
|
200
|
+
* their URL: a recompilation can reuse a URL with different source.
|
|
199
201
|
* "Once per branch" is a marker file under `.spectest/scripts/`: the
|
|
200
202
|
* directory is shared by every process of the service and forks with
|
|
201
203
|
* the environment, so a record written at bring-up or by an ancestor
|
|
@@ -242,7 +244,9 @@ export const NODE_COVERAGE_HOOK = `"use strict";
|
|
|
242
244
|
const SELF = url.pathToFileURL(__filename).href;
|
|
243
245
|
const SCRIPTS = path.join(dir, ${JSON.stringify(NODE_COVERAGE_SCRIPTS_SUBDIR)});
|
|
244
246
|
const MARKERS = path.join(dir, ${JSON.stringify(NODE_COVERAGE_MARKERS_SUBDIR)});
|
|
245
|
-
|
|
247
|
+
// V8 script ids identify a compiled script within this process. A URL
|
|
248
|
+
// can be reused by another process, vm context, or compilation.
|
|
249
|
+
const known = new Map();
|
|
246
250
|
let seq = 0;
|
|
247
251
|
|
|
248
252
|
function isApp(u) {
|
|
@@ -375,7 +379,6 @@ export const NODE_COVERAGE_HOOK = `"use strict";
|
|
|
375
379
|
fs.renameSync(tmp, file);
|
|
376
380
|
}
|
|
377
381
|
function describe(s, source) {
|
|
378
|
-
const id = idOf(s.url);
|
|
379
382
|
let scriptPath = null;
|
|
380
383
|
try {
|
|
381
384
|
scriptPath = url.fileURLToPath(s.url);
|
|
@@ -383,16 +386,20 @@ export const NODE_COVERAGE_HOOK = `"use strict";
|
|
|
383
386
|
const text = typeof source === "string" ? source : null;
|
|
384
387
|
const record = {
|
|
385
388
|
url: s.url,
|
|
389
|
+
sourceHash: text !== null ? crypto.createHash("sha256").update(text).digest("hex") : null,
|
|
386
390
|
lineLengths: text !== null ? lineLengths(text) : null,
|
|
387
391
|
functions: s.functions.map(function (f) {
|
|
388
392
|
return [f.functionName, f.ranges[0].startOffset, f.ranges[0].endOffset];
|
|
389
393
|
}),
|
|
390
394
|
sourceMap: text !== null && scriptPath !== null ? readMap(scriptPath, text) : null,
|
|
391
395
|
};
|
|
392
|
-
|
|
393
|
-
|
|
396
|
+
const document = JSON.stringify({ spectestScript: record });
|
|
397
|
+
const id = idOf(document);
|
|
398
|
+
if (!fs.existsSync(path.join(MARKERS, id))) {
|
|
399
|
+
writeAtomic(path.join(SCRIPTS, id + ".json"), document);
|
|
394
400
|
fs.writeFileSync(path.join(MARKERS, id), "");
|
|
395
|
-
}
|
|
401
|
+
}
|
|
402
|
+
return id;
|
|
396
403
|
}
|
|
397
404
|
function take() {
|
|
398
405
|
const res = post("Profiler.takePreciseCoverage");
|
|
@@ -413,9 +420,7 @@ export const NODE_COVERAGE_HOOK = `"use strict";
|
|
|
413
420
|
if (kept.length === 0 && units.size === 0) return;
|
|
414
421
|
const fresh = [];
|
|
415
422
|
for (const s of kept) {
|
|
416
|
-
if (known.has(s.
|
|
417
|
-
known.add(s.url);
|
|
418
|
-
if (!fs.existsSync(path.join(MARKERS, idOf(s.url)))) fresh.push(s);
|
|
423
|
+
if (!known.has(s.scriptId)) fresh.push(s);
|
|
419
424
|
}
|
|
420
425
|
if (fresh.length > 0) {
|
|
421
426
|
try {
|
|
@@ -426,7 +431,7 @@ export const NODE_COVERAGE_HOOK = `"use strict";
|
|
|
426
431
|
for (const s of fresh) {
|
|
427
432
|
const r = post("Debugger.getScriptSource", { scriptId: s.scriptId });
|
|
428
433
|
try {
|
|
429
|
-
describe(s, r && r.scriptSource);
|
|
434
|
+
known.set(s.scriptId, describe(s, r && r.scriptSource));
|
|
430
435
|
} catch (e) {}
|
|
431
436
|
}
|
|
432
437
|
post("Debugger.disable");
|
|
@@ -442,7 +447,7 @@ export const NODE_COVERAGE_HOOK = `"use strict";
|
|
|
442
447
|
result: kept.map(function (s) {
|
|
443
448
|
return {
|
|
444
449
|
url: s.url,
|
|
445
|
-
spectestScript:
|
|
450
|
+
spectestScript: known.get(s.scriptId) || null,
|
|
446
451
|
functions: s.functions.filter(ran).map(function (f) {
|
|
447
452
|
const first = f.ranges[0];
|
|
448
453
|
const out = [first.startOffset, first.endOffset - first.startOffset, first.count];
|
|
@@ -805,7 +810,7 @@ export async function collectNodeReports(dir) {
|
|
|
805
810
|
* loads from this service's origins, maps it back to source through the
|
|
806
811
|
* served source map, and writes one lcov per capture (`browser.lcov`)
|
|
807
812
|
* holding what ran since the previous capture: every live session is
|
|
808
|
-
* harvested first, and
|
|
813
|
+
* harvested first, and only this service's totals are taken and cleared. Nothing ran ⇒
|
|
809
814
|
* an lcov with no records, which is a report saying so.
|
|
810
815
|
*/
|
|
811
816
|
export function browser() {
|
|
@@ -817,7 +822,7 @@ export function browser() {
|
|
|
817
822
|
},
|
|
818
823
|
async capture(ctx) {
|
|
819
824
|
await harvestAllBrowserCoverage();
|
|
820
|
-
const lcov =
|
|
825
|
+
const lcov = takeBrowserCoverageReport(ctx.service);
|
|
821
826
|
await ctx.writeReport("browser.lcov", lcov);
|
|
822
827
|
},
|
|
823
828
|
};
|
|
@@ -41,11 +41,12 @@ export interface DecodedSourceMap {
|
|
|
41
41
|
/** Decode one VLQ-encoded segment into its fields. */
|
|
42
42
|
export declare function decodeVlq(segment: string): number[];
|
|
43
43
|
/**
|
|
44
|
-
* Normalise a source-map `sources` entry
|
|
44
|
+
* Normalise a source-map `sources` entry without discarding its identity. Bundlers prefix
|
|
45
45
|
* their own scheme (`webpack:///./src/a.ts`), dev servers serve absolute
|
|
46
46
|
* URLs paths (`/src/a.ts`), builds write relative ones (`../src/a.ts`).
|
|
47
|
-
* The server
|
|
48
|
-
*
|
|
47
|
+
* The server validates file identity against the repository. Preserve
|
|
48
|
+
* virtual namespaces and external URLs; losing them makes foreign
|
|
49
|
+
* sources indistinguishable from project files.
|
|
49
50
|
*/
|
|
50
51
|
export declare function normalizeSourcePath(src: string, sourceRoot?: string): string;
|
|
51
52
|
/** Sources nobody's change can be attributed to: dependencies and bundler
|
|
@@ -86,22 +86,28 @@ export function decodeVlq(segment) {
|
|
|
86
86
|
return out;
|
|
87
87
|
}
|
|
88
88
|
/**
|
|
89
|
-
* Normalise a source-map `sources` entry
|
|
89
|
+
* Normalise a source-map `sources` entry without discarding its identity. Bundlers prefix
|
|
90
90
|
* their own scheme (`webpack:///./src/a.ts`), dev servers serve absolute
|
|
91
91
|
* URLs paths (`/src/a.ts`), builds write relative ones (`../src/a.ts`).
|
|
92
|
-
* The server
|
|
93
|
-
*
|
|
92
|
+
* The server validates file identity against the repository. Preserve
|
|
93
|
+
* virtual namespaces and external URLs; losing them makes foreign
|
|
94
|
+
* sources indistinguishable from project files.
|
|
94
95
|
*/
|
|
95
96
|
export function normalizeSourcePath(src, sourceRoot) {
|
|
96
97
|
let s = src;
|
|
97
98
|
if (sourceRoot && !/^[a-z]+:/i.test(s)) {
|
|
98
99
|
s = sourceRoot.replace(/\/?$/, "/") + s;
|
|
99
100
|
}
|
|
100
|
-
|
|
101
|
-
//
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
101
|
+
// Keep virtual source identity intact. Stripping the scheme turned
|
|
102
|
+
// webpack://_N_E/../src/utils.ts (a foreign source) into a plausible
|
|
103
|
+
// project path; a suffix match then attributed its lines to lib/utils.ts.
|
|
104
|
+
// Do not use URL here: it collapses the traversal we need to preserve.
|
|
105
|
+
if (/^(webpack|turbopack):\/\//.test(s))
|
|
106
|
+
return s;
|
|
107
|
+
s = s.replace(/^file:\/\//, "");
|
|
108
|
+
// External URLs keep their identity; Vite's /@fs is an explicit file path.
|
|
109
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(s))
|
|
110
|
+
return s;
|
|
105
111
|
s = s.replace(/^\/@fs\//, "/");
|
|
106
112
|
s = s.replace(/\?.*$/, "");
|
|
107
113
|
// Turbopack (Next.js) roots every source at the `[project]` token —
|
package/dist/index.d.ts
CHANGED
|
@@ -829,8 +829,7 @@ export type ServiceImage = {
|
|
|
829
829
|
* Runs in `defineEnvironment`, and in the daemon's runtime `startService`
|
|
830
830
|
* twin, inside the VM, where the repo sits at the project root. Existence
|
|
831
831
|
* checks go through the project-file resolver, so the same rules as
|
|
832
|
-
* `ctx.readProjectFile` apply
|
|
833
|
-
* instead of read stale).
|
|
832
|
+
* `ctx.readProjectFile` apply.
|
|
834
833
|
*/
|
|
835
834
|
export declare function validateServiceImage(serviceName: string, image: ServiceImage): ServiceImage;
|
|
836
835
|
export interface VolumeMount {
|
package/dist/index.js
CHANGED
|
@@ -253,8 +253,7 @@ const BUILD_TARGET_RE = /^[^\s]+$/;
|
|
|
253
253
|
* Runs in `defineEnvironment`, and in the daemon's runtime `startService`
|
|
254
254
|
* twin, inside the VM, where the repo sits at the project root. Existence
|
|
255
255
|
* checks go through the project-file resolver, so the same rules as
|
|
256
|
-
* `ctx.readProjectFile` apply
|
|
257
|
-
* instead of read stale).
|
|
256
|
+
* `ctx.readProjectFile` apply.
|
|
258
257
|
*/
|
|
259
258
|
export function validateServiceImage(serviceName, image) {
|
|
260
259
|
if (image.type !== "dockerfile")
|
package/dist/project-files.js
CHANGED
|
@@ -9,46 +9,20 @@
|
|
|
9
9
|
// on EVERY start, warm ones included.
|
|
10
10
|
//
|
|
11
11
|
// A warm start happens only when the warm-template hash matches, so /workspace
|
|
12
|
-
// is correct for every file that is IN that hash. It can be behind for the
|
|
13
|
-
//
|
|
14
|
-
// edit keeps the fast start)
|
|
15
|
-
// `spectest/.specignore`.
|
|
12
|
+
// is correct for every file that is IN that hash. It can be behind for the one
|
|
13
|
+
// kind of file the hash excludes: `spectest/tests/**` (excluded so a test-only
|
|
14
|
+
// edit keeps the fast start).
|
|
16
15
|
//
|
|
17
16
|
// Hence the rule below: a path under `spectest/` resolves against the app copy
|
|
18
17
|
// first, because that copy is always current — this is what lets a fixture live
|
|
19
18
|
// in `spectest/tests/fixtures/` and still be found after it was added. Any
|
|
20
19
|
// other path resolves against /workspace, which the hash keeps current.
|
|
21
|
-
|
|
22
|
-
// The remaining hole is a file that `.specignore` excludes AND that sits outside
|
|
23
|
-
// `spectest/`: /workspace holds whatever the cold start uploaded, so a later
|
|
24
|
-
// edit is invisible. Nothing can repair those bytes at read time, so we refuse
|
|
25
|
-
// to read them instead of returning stale content. The control plane writes the
|
|
26
|
-
// exact path list it excluded (env.rs) into ENV_IGNORED_FILE.
|
|
27
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
20
|
+
import { existsSync } from "node:fs";
|
|
28
21
|
import path from "node:path";
|
|
29
22
|
/** The extracted repo — `ctx.projectRoot`. */
|
|
30
23
|
export const WORKSPACE = process.env.SPECTEST_WORKSPACE ?? "/workspace";
|
|
31
24
|
/** The app dir; the user's `spectest/` sits directly under it. */
|
|
32
25
|
export const APP_DIR = process.env.SPECTEST_APP_DIR ?? "/opt/spectest/app";
|
|
33
|
-
/** Paths (repo-relative) the warm-template hash skipped because of
|
|
34
|
-
* `spectest/.specignore`. Written by the control plane at env start; absent
|
|
35
|
-
* when the project ships no `.specignore`. */
|
|
36
|
-
const ENV_IGNORED_FILE = "/run/spectest-env-ignored.json";
|
|
37
|
-
let _envIgnored;
|
|
38
|
-
function envIgnored() {
|
|
39
|
-
if (_envIgnored)
|
|
40
|
-
return _envIgnored;
|
|
41
|
-
let paths = [];
|
|
42
|
-
try {
|
|
43
|
-
const raw = JSON.parse(readFileSync(ENV_IGNORED_FILE, "utf8"));
|
|
44
|
-
paths = raw.paths ?? [];
|
|
45
|
-
}
|
|
46
|
-
catch {
|
|
47
|
-
/* No file (no .specignore, or an older env) — nothing to refuse. */
|
|
48
|
-
}
|
|
49
|
-
_envIgnored = new Set(paths);
|
|
50
|
-
return _envIgnored;
|
|
51
|
-
}
|
|
52
26
|
/** Project files resolved since the last take — what a test read
|
|
53
27
|
* through the SDK. Module memory: forks with the environment; the
|
|
54
28
|
* coverage capture takes and clears it after every case, so a forked
|
|
@@ -89,11 +63,6 @@ export function resolveProjectPath(p, opts = {}) {
|
|
|
89
63
|
if (existsSync(app))
|
|
90
64
|
return app;
|
|
91
65
|
}
|
|
92
|
-
if (envIgnored().has(rel)) {
|
|
93
|
-
throw new Error(`project file "${p}" is excluded by the environment cache ignore rules, so the copy in the VM ` +
|
|
94
|
-
`is whatever a cold start uploaded and can be out of date. Move it under ` +
|
|
95
|
-
`spectest/tests/ (still cache-free, and always re-uploaded), or drop the pattern.`);
|
|
96
|
-
}
|
|
97
66
|
return path.join(WORKSPACE, rel);
|
|
98
67
|
}
|
|
99
68
|
/** {@link resolveProjectPath}, but a missing file is an error that names every
|
package/package.json
CHANGED
package/src/browser-coverage.ts
CHANGED
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
//
|
|
13
13
|
// Harvest runs before every browser step (a click that navigates
|
|
14
14
|
// cross-site replaces the renderer, and the old counters with it) and at
|
|
15
|
-
// every coverage capture. `
|
|
16
|
-
// one
|
|
15
|
+
// every coverage capture. `takeBrowserCoverageReport` renders and clears
|
|
16
|
+
// one service's totals for the `case-coverage` bundle.
|
|
17
17
|
//
|
|
18
18
|
// Configured by the daemon at /load with the host → service table; a
|
|
19
19
|
// project with no `coverage: { browser: true }` service never attaches
|
|
@@ -220,17 +220,15 @@ export async function harvestAllBrowserCoverage(): Promise<void> {
|
|
|
220
220
|
await Promise.all([...LIVE_COLLECTORS].map((c) => harvestBrowserCoverage(c.cdp)));
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
-
/**
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
export function
|
|
228
|
-
const
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
TOTALS.clear();
|
|
233
|
-
return out;
|
|
223
|
+
/** Take one service's lcov report and clear only its totals. Other
|
|
224
|
+
* services retain their coverage until their own capture. Each capture
|
|
225
|
+
* is a delta; after every service is captured, a forked child inherits
|
|
226
|
+
* empty totals from its parent. */
|
|
227
|
+
export function takeBrowserCoverageReport(service: string): string {
|
|
228
|
+
const files = TOTALS.get(service);
|
|
229
|
+
const report = files ? lcovDocument(files) : "TN:\n";
|
|
230
|
+
TOTALS.delete(service);
|
|
231
|
+
return report;
|
|
234
232
|
}
|
|
235
233
|
|
|
236
234
|
/** The current totals, without clearing them (tests). */
|
package/src/coverage.test.ts
CHANGED
|
@@ -135,6 +135,56 @@ describe("units", () => {
|
|
|
135
135
|
});
|
|
136
136
|
|
|
137
137
|
describe("browserCoverage", () => {
|
|
138
|
+
for (const order of ["dashboard first", "admin first", "concurrent"] as const) {
|
|
139
|
+
test(`services retain their own coverage and deltas: ${order}`, async () => {
|
|
140
|
+
const reports = new Map<string, string>();
|
|
141
|
+
const contexts = ["dashboard", "admin"].map((service): CoverageCaptureContext => ({
|
|
142
|
+
...fakeCtx("unused"),
|
|
143
|
+
service,
|
|
144
|
+
async writeReport(name, content) {
|
|
145
|
+
expect(name).toBe("browser.lcov");
|
|
146
|
+
reports.set(service, content);
|
|
147
|
+
},
|
|
148
|
+
}));
|
|
149
|
+
if (order === "admin first") contexts.reverse();
|
|
150
|
+
const capture = async () => {
|
|
151
|
+
if (order === "concurrent") {
|
|
152
|
+
await Promise.all(contexts.map((ctx) => browser().capture!(ctx)));
|
|
153
|
+
} else {
|
|
154
|
+
for (const ctx of contexts) await browser().capture!(ctx);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
foldBrowserCoverage("dashboard", new Map([["src/dashboard.ts", new Map([[1, 2]])]]));
|
|
158
|
+
foldBrowserCoverage("admin", new Map([["src/admin.ts", new Map([[3, 4]])]]));
|
|
159
|
+
await capture();
|
|
160
|
+
expect(reports.get("dashboard")).toBe("TN:\nSF:src/dashboard.ts\nDA:1,2\nLF:1\nLH:1\nend_of_record\n");
|
|
161
|
+
expect(reports.get("admin")).toBe("TN:\nSF:src/admin.ts\nDA:3,4\nLF:1\nLH:1\nend_of_record\n");
|
|
162
|
+
|
|
163
|
+
await capture();
|
|
164
|
+
expect(reports.get("dashboard")).toBe("TN:\n");
|
|
165
|
+
expect(reports.get("admin")).toBe("TN:\n");
|
|
166
|
+
|
|
167
|
+
foldBrowserCoverage("admin", new Map([["src/admin.ts", new Map([[3, 1]])]]));
|
|
168
|
+
await capture();
|
|
169
|
+
expect(reports.get("dashboard")).toBe("TN:\n");
|
|
170
|
+
expect(reports.get("admin")).toBe("TN:\nSF:src/admin.ts\nDA:3,1\nLF:1\nLH:1\nend_of_record\n");
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
test("capturing a service with no coverage preserves other services' totals", async () => {
|
|
175
|
+
const reports = new Map<string, string>();
|
|
176
|
+
foldBrowserCoverage("dashboard", new Map([["src/dashboard.ts", new Map([[1, 2]])]]));
|
|
177
|
+
for (const service of ["admin", "dashboard"]) {
|
|
178
|
+
await browser().capture!({
|
|
179
|
+
...fakeCtx("unused"),
|
|
180
|
+
service,
|
|
181
|
+
async writeReport(_name, content) { reports.set(service, content); },
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
expect(reports.get("admin")).toBe("TN:\n");
|
|
185
|
+
expect(reports.get("dashboard")).toBe("TN:\nSF:src/dashboard.ts\nDA:1,2\nLF:1\nLH:1\nend_of_record\n");
|
|
186
|
+
});
|
|
187
|
+
|
|
138
188
|
test("writes an empty lcov when nothing ran for the service", async () => {
|
|
139
189
|
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "spectest-cov-"));
|
|
140
190
|
await browser().capture!(fakeCtx(dir));
|
|
@@ -297,6 +347,34 @@ describe.if(hasNode)("nodeCoverage hook (real node)", () => {
|
|
|
297
347
|
}
|
|
298
348
|
});
|
|
299
349
|
|
|
350
|
+
test("recompiled scripts at the same URL get distinct records within and across processes", async () => {
|
|
351
|
+
const dir = await scratch();
|
|
352
|
+
const sharedUrl = `file://${path.join(dir, "shared.js")}`;
|
|
353
|
+
await fs.writeFile(path.join(dir, "main.js"), `
|
|
354
|
+
const vm = require("node:vm");
|
|
355
|
+
vm.runInThisContext("globalThis.value = 1;", { filename: ${JSON.stringify(sharedUrl)} });
|
|
356
|
+
vm.runInThisContext("globalThis.value = 2;", { filename: ${JSON.stringify(sharedUrl)} });
|
|
357
|
+
`);
|
|
358
|
+
expect(await runOnce(dir, "main.js")).toBe(0);
|
|
359
|
+
const first = (await Promise.all((await dumpsIn(dir)).map((n) => readDump(dir, n))))
|
|
360
|
+
.flatMap((d) => d.result).filter((s) => s.url === sharedUrl);
|
|
361
|
+
expect(first).toHaveLength(2);
|
|
362
|
+
const ids = new Set(first.map((s) => s.spectestScript));
|
|
363
|
+
expect(ids.size).toBe(2); // same URL, line lengths and extents, different source
|
|
364
|
+
for (const id of ids) {
|
|
365
|
+
const record = JSON.parse(await fs.readFile(path.join(dir, NODE_COVERAGE_SCRIPTS_SUBDIR, `${id}.json`), "utf8"));
|
|
366
|
+
expect(record.spectestScript.url).toBe(sharedUrl);
|
|
367
|
+
expect(record.spectestScript.sourceHash).toMatch(/^[a-f0-9]{64}$/);
|
|
368
|
+
}
|
|
369
|
+
// A second process shares the marker directory. Identical descriptors
|
|
370
|
+
// deduplicate, but must never borrow the other compilation's identity.
|
|
371
|
+
expect(await runOnce(dir, "main.js")).toBe(0);
|
|
372
|
+
const all = (await Promise.all((await dumpsIn(dir)).map((n) => readDump(dir, n))))
|
|
373
|
+
.flatMap((d) => d.result).filter((s) => s.url === sharedUrl);
|
|
374
|
+
expect(all).toHaveLength(4);
|
|
375
|
+
expect(new Set(all.map((s) => s.spectestScript))).toEqual(ids);
|
|
376
|
+
});
|
|
377
|
+
|
|
300
378
|
test("short-lived CLI processes write at exit; the map next to the script is recorded once, no NODE_V8_COVERAGE", async () => {
|
|
301
379
|
const dir = await scratch();
|
|
302
380
|
await fs.writeFile(path.join(dir, "cli.js"), "function used() { return 1 }\nfunction unused() { return 2 }\nused();\n");
|
package/src/coverage.ts
CHANGED
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
import { createHash } from "node:crypto";
|
|
31
31
|
|
|
32
32
|
import type { ServiceConfig } from "./index.js";
|
|
33
|
-
import { harvestAllBrowserCoverage,
|
|
33
|
+
import { harvestAllBrowserCoverage, takeBrowserCoverageReport } from "./browser-coverage.js";
|
|
34
34
|
|
|
35
35
|
import { COVERAGE_CONTAINER_DIR, type CoverageReportsMode } from "./harness/coverage.js";
|
|
36
36
|
export { COVERAGE_CONTAINER_DIR, type CoverageReportsMode };
|
|
@@ -342,6 +342,8 @@ export const NODE_COVERAGE_DUMP_PREFIX = "node-";
|
|
|
342
342
|
* source map: the inline `data:` map in the text, the file the last
|
|
343
343
|
* `sourceMappingURL` comment names, or `<script>.map` next to the
|
|
344
344
|
* script; `sourcesContent` dropped (the sources are the repository).
|
|
345
|
+
* Records are keyed by their descriptor and compiled-source hash, not
|
|
346
|
+
* their URL: a recompilation can reuse a URL with different source.
|
|
345
347
|
* "Once per branch" is a marker file under `.spectest/scripts/`: the
|
|
346
348
|
* directory is shared by every process of the service and forks with
|
|
347
349
|
* the environment, so a record written at bring-up or by an ancestor
|
|
@@ -388,7 +390,9 @@ export const NODE_COVERAGE_HOOK = `"use strict";
|
|
|
388
390
|
const SELF = url.pathToFileURL(__filename).href;
|
|
389
391
|
const SCRIPTS = path.join(dir, ${JSON.stringify(NODE_COVERAGE_SCRIPTS_SUBDIR)});
|
|
390
392
|
const MARKERS = path.join(dir, ${JSON.stringify(NODE_COVERAGE_MARKERS_SUBDIR)});
|
|
391
|
-
|
|
393
|
+
// V8 script ids identify a compiled script within this process. A URL
|
|
394
|
+
// can be reused by another process, vm context, or compilation.
|
|
395
|
+
const known = new Map();
|
|
392
396
|
let seq = 0;
|
|
393
397
|
|
|
394
398
|
function isApp(u) {
|
|
@@ -521,7 +525,6 @@ export const NODE_COVERAGE_HOOK = `"use strict";
|
|
|
521
525
|
fs.renameSync(tmp, file);
|
|
522
526
|
}
|
|
523
527
|
function describe(s, source) {
|
|
524
|
-
const id = idOf(s.url);
|
|
525
528
|
let scriptPath = null;
|
|
526
529
|
try {
|
|
527
530
|
scriptPath = url.fileURLToPath(s.url);
|
|
@@ -529,16 +532,20 @@ export const NODE_COVERAGE_HOOK = `"use strict";
|
|
|
529
532
|
const text = typeof source === "string" ? source : null;
|
|
530
533
|
const record = {
|
|
531
534
|
url: s.url,
|
|
535
|
+
sourceHash: text !== null ? crypto.createHash("sha256").update(text).digest("hex") : null,
|
|
532
536
|
lineLengths: text !== null ? lineLengths(text) : null,
|
|
533
537
|
functions: s.functions.map(function (f) {
|
|
534
538
|
return [f.functionName, f.ranges[0].startOffset, f.ranges[0].endOffset];
|
|
535
539
|
}),
|
|
536
540
|
sourceMap: text !== null && scriptPath !== null ? readMap(scriptPath, text) : null,
|
|
537
541
|
};
|
|
538
|
-
|
|
539
|
-
|
|
542
|
+
const document = JSON.stringify({ spectestScript: record });
|
|
543
|
+
const id = idOf(document);
|
|
544
|
+
if (!fs.existsSync(path.join(MARKERS, id))) {
|
|
545
|
+
writeAtomic(path.join(SCRIPTS, id + ".json"), document);
|
|
540
546
|
fs.writeFileSync(path.join(MARKERS, id), "");
|
|
541
|
-
}
|
|
547
|
+
}
|
|
548
|
+
return id;
|
|
542
549
|
}
|
|
543
550
|
function take() {
|
|
544
551
|
const res = post("Profiler.takePreciseCoverage");
|
|
@@ -559,9 +566,7 @@ export const NODE_COVERAGE_HOOK = `"use strict";
|
|
|
559
566
|
if (kept.length === 0 && units.size === 0) return;
|
|
560
567
|
const fresh = [];
|
|
561
568
|
for (const s of kept) {
|
|
562
|
-
if (known.has(s.
|
|
563
|
-
known.add(s.url);
|
|
564
|
-
if (!fs.existsSync(path.join(MARKERS, idOf(s.url)))) fresh.push(s);
|
|
569
|
+
if (!known.has(s.scriptId)) fresh.push(s);
|
|
565
570
|
}
|
|
566
571
|
if (fresh.length > 0) {
|
|
567
572
|
try {
|
|
@@ -572,7 +577,7 @@ export const NODE_COVERAGE_HOOK = `"use strict";
|
|
|
572
577
|
for (const s of fresh) {
|
|
573
578
|
const r = post("Debugger.getScriptSource", { scriptId: s.scriptId });
|
|
574
579
|
try {
|
|
575
|
-
describe(s, r && r.scriptSource);
|
|
580
|
+
known.set(s.scriptId, describe(s, r && r.scriptSource));
|
|
576
581
|
} catch (e) {}
|
|
577
582
|
}
|
|
578
583
|
post("Debugger.disable");
|
|
@@ -588,7 +593,7 @@ export const NODE_COVERAGE_HOOK = `"use strict";
|
|
|
588
593
|
result: kept.map(function (s) {
|
|
589
594
|
return {
|
|
590
595
|
url: s.url,
|
|
591
|
-
spectestScript:
|
|
596
|
+
spectestScript: known.get(s.scriptId) || null,
|
|
592
597
|
functions: s.functions.filter(ran).map(function (f) {
|
|
593
598
|
const first = f.ranges[0];
|
|
594
599
|
const out = [first.startOffset, first.endOffset - first.startOffset, first.count];
|
|
@@ -964,7 +969,7 @@ export async function collectNodeReports(dir: string): Promise<NodeReportStats>
|
|
|
964
969
|
* loads from this service's origins, maps it back to source through the
|
|
965
970
|
* served source map, and writes one lcov per capture (`browser.lcov`)
|
|
966
971
|
* holding what ran since the previous capture: every live session is
|
|
967
|
-
* harvested first, and
|
|
972
|
+
* harvested first, and only this service's totals are taken and cleared. Nothing ran ⇒
|
|
968
973
|
* an lcov with no records, which is a report saying so.
|
|
969
974
|
*/
|
|
970
975
|
export function browser(): CoverageAdapter {
|
|
@@ -976,7 +981,7 @@ export function browser(): CoverageAdapter {
|
|
|
976
981
|
},
|
|
977
982
|
async capture(ctx) {
|
|
978
983
|
await harvestAllBrowserCoverage();
|
|
979
|
-
const lcov =
|
|
984
|
+
const lcov = takeBrowserCoverageReport(ctx.service);
|
|
980
985
|
await ctx.writeReport("browser.lcov", lcov);
|
|
981
986
|
},
|
|
982
987
|
};
|
|
@@ -41,10 +41,10 @@ describe("source maps", () => {
|
|
|
41
41
|
mappings: "AAAA,ICAA;ACIA",
|
|
42
42
|
});
|
|
43
43
|
expect(map).not.toBeNull();
|
|
44
|
-
expect(map!.sources).toEqual(["src/a.ts", "node_modules/x.js", "src/b.ts"]);
|
|
44
|
+
expect(map!.sources).toEqual(["webpack:///./src/a.ts", "webpack:///./node_modules/x.js", "src/b.ts"]);
|
|
45
45
|
const files = mapToOriginal(map!, new Map([[1, 2], [2, 0]]));
|
|
46
|
-
expect([...files.keys()]).toEqual(["src/a.ts", "src/b.ts"]);
|
|
47
|
-
expect(files.get("src/a.ts")!.get(1)).toBe(2);
|
|
46
|
+
expect([...files.keys()]).toEqual(["webpack:///./src/a.ts", "src/b.ts"]);
|
|
47
|
+
expect(files.get("webpack:///./src/a.ts")!.get(1)).toBe(2);
|
|
48
48
|
expect(files.get("src/b.ts")!.get(5)).toBe(0);
|
|
49
49
|
});
|
|
50
50
|
|
|
@@ -54,9 +54,12 @@ describe("source maps", () => {
|
|
|
54
54
|
expect(decodeSourceMap("nope")).toBeNull();
|
|
55
55
|
});
|
|
56
56
|
|
|
57
|
-
test("normalizeSourcePath
|
|
58
|
-
expect(normalizeSourcePath("webpack:///./src/a.ts")).toBe("src/a.ts");
|
|
59
|
-
expect(normalizeSourcePath("
|
|
57
|
+
test("normalizeSourcePath preserves namespaces and external origins", () => {
|
|
58
|
+
expect(normalizeSourcePath("webpack:///./src/a.ts")).toBe("webpack:///./src/a.ts");
|
|
59
|
+
expect(normalizeSourcePath("webpack://_N_E/../src/utils.ts")).toBe("webpack://_N_E/../src/utils.ts");
|
|
60
|
+
expect(normalizeSourcePath("webpack://_N_E/./lib/utils.ts")).toBe("webpack://_N_E/./lib/utils.ts");
|
|
61
|
+
expect(normalizeSourcePath("turbopack:///[project]/src/a.ts")).toBe("turbopack:///[project]/src/a.ts");
|
|
62
|
+
expect(normalizeSourcePath("http://app.test/src/a.ts?t=1")).toBe("http://app.test/src/a.ts?t=1");
|
|
60
63
|
expect(normalizeSourcePath("/@fs/repo/src/a.ts")).toBe("/repo/src/a.ts");
|
|
61
64
|
expect(normalizeSourcePath("a.ts", "/app/src")).toBe("/app/src/a.ts");
|
|
62
65
|
expect(normalizeSourcePath("file:///app/src/a.ts")).toBe("/app/src/a.ts");
|
|
@@ -119,21 +119,26 @@ export function decodeVlq(segment: string): number[] {
|
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
/**
|
|
122
|
-
* Normalise a source-map `sources` entry
|
|
122
|
+
* Normalise a source-map `sources` entry without discarding its identity. Bundlers prefix
|
|
123
123
|
* their own scheme (`webpack:///./src/a.ts`), dev servers serve absolute
|
|
124
124
|
* URLs paths (`/src/a.ts`), builds write relative ones (`../src/a.ts`).
|
|
125
|
-
* The server
|
|
126
|
-
*
|
|
125
|
+
* The server validates file identity against the repository. Preserve
|
|
126
|
+
* virtual namespaces and external URLs; losing them makes foreign
|
|
127
|
+
* sources indistinguishable from project files.
|
|
127
128
|
*/
|
|
128
129
|
export function normalizeSourcePath(src: string, sourceRoot?: string): string {
|
|
129
130
|
let s = src;
|
|
130
131
|
if (sourceRoot && !/^[a-z]+:/i.test(s)) {
|
|
131
132
|
s = sourceRoot.replace(/\/?$/, "/") + s;
|
|
132
133
|
}
|
|
133
|
-
|
|
134
|
-
//
|
|
135
|
-
|
|
136
|
-
|
|
134
|
+
// Keep virtual source identity intact. Stripping the scheme turned
|
|
135
|
+
// webpack://_N_E/../src/utils.ts (a foreign source) into a plausible
|
|
136
|
+
// project path; a suffix match then attributed its lines to lib/utils.ts.
|
|
137
|
+
// Do not use URL here: it collapses the traversal we need to preserve.
|
|
138
|
+
if (/^(webpack|turbopack):\/\//.test(s)) return s;
|
|
139
|
+
s = s.replace(/^file:\/\//, "");
|
|
140
|
+
// External URLs keep their identity; Vite's /@fs is an explicit file path.
|
|
141
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(s)) return s;
|
|
137
142
|
s = s.replace(/^\/@fs\//, "/");
|
|
138
143
|
s = s.replace(/\?.*$/, "");
|
|
139
144
|
// Turbopack (Next.js) roots every source at the `[project]` token —
|
package/src/index.ts
CHANGED
|
@@ -1272,8 +1272,7 @@ const BUILD_TARGET_RE = /^[^\s]+$/;
|
|
|
1272
1272
|
* Runs in `defineEnvironment`, and in the daemon's runtime `startService`
|
|
1273
1273
|
* twin, inside the VM, where the repo sits at the project root. Existence
|
|
1274
1274
|
* checks go through the project-file resolver, so the same rules as
|
|
1275
|
-
* `ctx.readProjectFile` apply
|
|
1276
|
-
* instead of read stale).
|
|
1275
|
+
* `ctx.readProjectFile` apply.
|
|
1277
1276
|
*/
|
|
1278
1277
|
export function validateServiceImage(serviceName: string, image: ServiceImage): ServiceImage {
|
|
1279
1278
|
if (image.type !== "dockerfile") return image;
|
package/src/project-files.ts
CHANGED
|
@@ -9,23 +9,16 @@
|
|
|
9
9
|
// on EVERY start, warm ones included.
|
|
10
10
|
//
|
|
11
11
|
// A warm start happens only when the warm-template hash matches, so /workspace
|
|
12
|
-
// is correct for every file that is IN that hash. It can be behind for the
|
|
13
|
-
//
|
|
14
|
-
// edit keeps the fast start)
|
|
15
|
-
// `spectest/.specignore`.
|
|
12
|
+
// is correct for every file that is IN that hash. It can be behind for the one
|
|
13
|
+
// kind of file the hash excludes: `spectest/tests/**` (excluded so a test-only
|
|
14
|
+
// edit keeps the fast start).
|
|
16
15
|
//
|
|
17
16
|
// Hence the rule below: a path under `spectest/` resolves against the app copy
|
|
18
17
|
// first, because that copy is always current — this is what lets a fixture live
|
|
19
18
|
// in `spectest/tests/fixtures/` and still be found after it was added. Any
|
|
20
19
|
// other path resolves against /workspace, which the hash keeps current.
|
|
21
|
-
//
|
|
22
|
-
// The remaining hole is a file that `.specignore` excludes AND that sits outside
|
|
23
|
-
// `spectest/`: /workspace holds whatever the cold start uploaded, so a later
|
|
24
|
-
// edit is invisible. Nothing can repair those bytes at read time, so we refuse
|
|
25
|
-
// to read them instead of returning stale content. The control plane writes the
|
|
26
|
-
// exact path list it excluded (env.rs) into ENV_IGNORED_FILE.
|
|
27
20
|
|
|
28
|
-
import { existsSync
|
|
21
|
+
import { existsSync } from "node:fs";
|
|
29
22
|
import path from "node:path";
|
|
30
23
|
|
|
31
24
|
/** The extracted repo — `ctx.projectRoot`. */
|
|
@@ -33,25 +26,6 @@ export const WORKSPACE = process.env.SPECTEST_WORKSPACE ?? "/workspace";
|
|
|
33
26
|
/** The app dir; the user's `spectest/` sits directly under it. */
|
|
34
27
|
export const APP_DIR = process.env.SPECTEST_APP_DIR ?? "/opt/spectest/app";
|
|
35
28
|
|
|
36
|
-
/** Paths (repo-relative) the warm-template hash skipped because of
|
|
37
|
-
* `spectest/.specignore`. Written by the control plane at env start; absent
|
|
38
|
-
* when the project ships no `.specignore`. */
|
|
39
|
-
const ENV_IGNORED_FILE = "/run/spectest-env-ignored.json";
|
|
40
|
-
|
|
41
|
-
let _envIgnored: Set<string> | undefined;
|
|
42
|
-
function envIgnored(): Set<string> {
|
|
43
|
-
if (_envIgnored) return _envIgnored;
|
|
44
|
-
let paths: string[] = [];
|
|
45
|
-
try {
|
|
46
|
-
const raw = JSON.parse(readFileSync(ENV_IGNORED_FILE, "utf8")) as { paths?: string[] };
|
|
47
|
-
paths = raw.paths ?? [];
|
|
48
|
-
} catch {
|
|
49
|
-
/* No file (no .specignore, or an older env) — nothing to refuse. */
|
|
50
|
-
}
|
|
51
|
-
_envIgnored = new Set(paths);
|
|
52
|
-
return _envIgnored;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
29
|
/** Project files resolved since the last take — what a test read
|
|
56
30
|
* through the SDK. Module memory: forks with the environment; the
|
|
57
31
|
* coverage capture takes and clears it after every case, so a forked
|
|
@@ -92,13 +66,6 @@ export function resolveProjectPath(p: string, opts: { record?: boolean } = {}):
|
|
|
92
66
|
const app = path.join(APP_DIR, rel);
|
|
93
67
|
if (existsSync(app)) return app;
|
|
94
68
|
}
|
|
95
|
-
if (envIgnored().has(rel)) {
|
|
96
|
-
throw new Error(
|
|
97
|
-
`project file "${p}" is excluded by the environment cache ignore rules, so the copy in the VM ` +
|
|
98
|
-
`is whatever a cold start uploaded and can be out of date. Move it under ` +
|
|
99
|
-
`spectest/tests/ (still cache-free, and always re-uploaded), or drop the pattern.`,
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
69
|
return path.join(WORKSPACE, rel);
|
|
103
70
|
}
|
|
104
71
|
|