@specific.dev/spectest 0.59.4 → 0.60.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.
@@ -15,6 +15,17 @@
15
15
  // stored bytes, so a change in that derivation is a re-read of stored
16
16
  // runs, never a hole in the history.
17
17
  //
18
+ // The one transformation done here is the **delta**: a stored report
19
+ // describes what ran between the previous capture and this one, for
20
+ // every source alike. A tool that resets its own counters (V8) writes
21
+ // deltas already; a cumulative tool (coverage.py, Go, JaCoCo — most)
22
+ // gets subtracted against the previous capture, which lives in module
23
+ // memory and so forks with the environment: a forked child's "previous"
24
+ // is its parent's last capture, exactly the state it started from. Which
25
+ // of the two a file is must be declared (an adapter's `reports`, or the
26
+ // service's) — nothing in the bytes tells a delta that happens to equal
27
+ // the previous one from a cumulative report with no new execution.
28
+ //
18
29
  // The container side (mount flag, `command` exec) is in `daemon.ts`.
19
30
 
20
31
  import { promises as fs } from "node:fs";
@@ -211,3 +222,159 @@ export function coverageBundleRef(
211
222
  })),
212
223
  };
213
224
  }
225
+
226
+ // ── delta ─────────────────────────────────────────────────────────
227
+
228
+ /** How a report's counters behave across captures. */
229
+ export type CoverageReportsMode = "delta" | "cumulative";
230
+
231
+ /** One lcov record's execution counts: `DA:` by line, `FNDA:` by function
232
+ * name. Branch data is dropped: file and line are what the derivation
233
+ * reads. */
234
+ export interface LcovRecordCounts {
235
+ lines: Map<number, number>;
236
+ functions: Map<string, number>;
237
+ /** `FN:<line>,<name>` declarations, kept so the delta names them. */
238
+ functionLines: Map<string, number>;
239
+ }
240
+
241
+ /** An lcov document parsed to counts, by `SF:` path. Records for one
242
+ * path are merged (a tool may split a file across records). */
243
+ export function parseLcovCounts(text: string): Map<string, LcovRecordCounts> {
244
+ const out = new Map<string, LcovRecordCounts>();
245
+ let cur: LcovRecordCounts | null = null;
246
+ for (const raw of text.split(/\r?\n/)) {
247
+ const line = raw.trim();
248
+ if (line.startsWith("SF:")) {
249
+ const file = line.slice(3).trim();
250
+ if (!file) {
251
+ cur = null;
252
+ continue;
253
+ }
254
+ cur = out.get(file) ?? { lines: new Map(), functions: new Map(), functionLines: new Map() };
255
+ out.set(file, cur);
256
+ } else if (!cur) {
257
+ continue;
258
+ } else if (line.startsWith("DA:")) {
259
+ const [ln, hits] = line.slice(3).split(",");
260
+ const l = Number(ln), h = Number(hits);
261
+ if (Number.isFinite(l) && Number.isFinite(h)) cur.lines.set(l, (cur.lines.get(l) ?? 0) + h);
262
+ } else if (line.startsWith("FNDA:")) {
263
+ const i = line.indexOf(",", 5);
264
+ if (i < 0) continue;
265
+ const h = Number(line.slice(5, i)), name = line.slice(i + 1);
266
+ if (Number.isFinite(h)) cur.functions.set(name, (cur.functions.get(name) ?? 0) + h);
267
+ } else if (line.startsWith("FN:")) {
268
+ const i = line.indexOf(",", 3);
269
+ if (i < 0) continue;
270
+ const l = Number(line.slice(3, i)), name = line.slice(i + 1);
271
+ if (Number.isFinite(l)) cur.functionLines.set(name, l);
272
+ } else if (line === "end_of_record") {
273
+ cur = null;
274
+ }
275
+ }
276
+ return out;
277
+ }
278
+
279
+ /**
280
+ * `now` minus `prev`, per line and per function. A count that went
281
+ * *down* is a counter that restarted (the process was replaced): the
282
+ * new value stands on its own. A file not in `prev` is new: everything
283
+ * in it counts. Only records with at least one execution are kept — a
284
+ * file with nothing new in it did not run.
285
+ */
286
+ export function subtractLcovCounts(
287
+ now: Map<string, LcovRecordCounts>,
288
+ prev: Map<string, LcovRecordCounts>,
289
+ ): Map<string, LcovRecordCounts> {
290
+ const out = new Map<string, LcovRecordCounts>();
291
+ for (const [file, rec] of now) {
292
+ const before = prev.get(file);
293
+ const lines = new Map<number, number>();
294
+ const functions = new Map<string, number>();
295
+ let executed = false;
296
+ for (const [l, h] of rec.lines) {
297
+ const b = before?.lines.get(l) ?? 0;
298
+ const d = h < b ? h : h - b;
299
+ lines.set(l, d);
300
+ if (d > 0) executed = true;
301
+ }
302
+ for (const [name, h] of rec.functions) {
303
+ const b = before?.functions.get(name) ?? 0;
304
+ const d = h < b ? h : h - b;
305
+ functions.set(name, d);
306
+ if (d > 0) executed = true;
307
+ }
308
+ // A record with no counts at all carries presence as its only
309
+ // signal; it is new only when it was not there before.
310
+ if (rec.lines.size === 0 && rec.functions.size === 0 && !before) executed = true;
311
+ if (executed) out.set(file, { lines, functions, functionLines: rec.functionLines });
312
+ }
313
+ return out;
314
+ }
315
+
316
+ /** Counts rendered back to lcov, records in path order. */
317
+ export function renderLcovCounts(counts: Map<string, LcovRecordCounts>): string {
318
+ const parts: string[] = ["TN:"];
319
+ for (const file of [...counts.keys()].sort()) {
320
+ const rec = counts.get(file)!;
321
+ parts.push(`SF:${file}`);
322
+ for (const [name, l] of [...rec.functionLines].sort((a, b) => a[1] - b[1])) parts.push(`FN:${l},${name}`);
323
+ let fnh = 0;
324
+ for (const [name, h] of rec.functions) {
325
+ parts.push(`FNDA:${h},${name}`);
326
+ if (h > 0) fnh++;
327
+ }
328
+ parts.push(`FNF:${rec.functions.size}`, `FNH:${fnh}`);
329
+ let lh = 0;
330
+ for (const [l, h] of [...rec.lines].sort((a, b) => a[0] - b[0])) {
331
+ parts.push(`DA:${l},${h}`);
332
+ if (h > 0) lh++;
333
+ }
334
+ parts.push(`LF:${rec.lines.size}`, `LH:${lh}`, "end_of_record");
335
+ }
336
+ return parts.join("\n") + "\n";
337
+ }
338
+
339
+ /** The previous capture's counts: service → report name → parsed lcov.
340
+ * Module memory, so it forks with the environment — a forked child's
341
+ * previous capture is its parent's last one. */
342
+ const PREVIOUS_CAPTURE = new Map<string, Map<string, Map<string, LcovRecordCounts>>>();
343
+
344
+ /** Forget every previous capture (tests). */
345
+ export function resetCoverageDeltaMemory(): void {
346
+ PREVIOUS_CAPTURE.clear();
347
+ }
348
+
349
+ /**
350
+ * Turn a capture's cumulative lcov reports into deltas against the
351
+ * previous capture of the same service and report name, and remember
352
+ * this capture as the new previous. Reports named in `deltaReports`
353
+ * (written by an adapter that declares `reports: "delta"`) and
354
+ * non-lcov reports (V8 JSON is a delta by construction) pass through
355
+ * untouched. With `mode === "delta"` every report passes through.
356
+ */
357
+ export function applyCoverageDelta(
358
+ capture: ServiceCoverageCapture,
359
+ mode: CoverageReportsMode,
360
+ deltaReports: ReadonlySet<string>,
361
+ ): ServiceCoverageCapture {
362
+ if (capture.error) return capture;
363
+ let prevByName = PREVIOUS_CAPTURE.get(capture.service);
364
+ if (!prevByName) {
365
+ prevByName = new Map();
366
+ PREVIOUS_CAPTURE.set(capture.service, prevByName);
367
+ }
368
+ const reports: CoverageReport[] = [];
369
+ for (const r of capture.reports) {
370
+ if (mode === "delta" || deltaReports.has(r.name) || !isLcovShaped(r.content)) {
371
+ reports.push(r);
372
+ continue;
373
+ }
374
+ const now = parseLcovCounts(r.content);
375
+ const prev = prevByName.get(r.name) ?? new Map<string, LcovRecordCounts>();
376
+ prevByName.set(r.name, now);
377
+ reports.push({ name: r.name, content: renderLcovCounts(subtractLcovCounts(now, prev)) });
378
+ }
379
+ return { ...capture, reports };
380
+ }