Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { describe, expect, it } from "vite-plus/test";
3
4import { loadWarnings } from "../core/report.ts";
5import {
6 compareMetrics,
7 activityRenewalMissed,
8 heapGrowth,
9 heapRetainers,
10 nextActivityReport,
11 noiseReport,
12 noisePairProblem,
13 parsePsCpuTime,
14 profileMismatch,
15 parseRunSelection,
16 retainerGrowth,
17 pathRewrites,
18 pathSpellings,
19 ServerMetrics,
20 slope,
21} from "./report.ts";
22
23const decodeMetrics = Schema.decodeSync(ServerMetrics);
24
25type Quiet = {
26 cpuPct: number;
27 wakeupsPerSec: number | null;
28 rssPeakMB: number;
29 loopDelayP99Ms?: number;
30 loopDelayMaxMs?: number;
31 gcPauseMs?: number;
32};
33const loopAtRest = {
34 loopDelayP50Ms: 0.1,
35 loopDelayP99Ms: 1.5,
36 loopDelayMaxMs: 8,
37 gcCount: 12,
38 gcPauseMs: 6,
39};
40const meta = { commit: "abc", platform: "linux-x64", noAgentScope: true };
41const quietRuns = (...runs: Quiet[]) =>
42 ({
43 meta,
44 runs: runs.map((quiet) => ({
45 quiet: { ...loopAtRest, ...quiet, rssMB: 500, heapAfterGcMB: 200 },
46 })),
47 }) satisfies ServerMetrics;
48const base: Quiet = { cpuPct: 0.3, wakeupsPerSec: 7, rssPeakMB: 500 };
49const cpuRuns = (...values: number[]) =>
50 quietRuns(...values.map((cpuPct) => ({ ...base, cpuPct })));
51
52// Fixed floors, so these cases test how compare gates rather than the calibrated table.
53const gatedRules = [
54 { pattern: /cpuPct$/, floor: 1, gate: true },
55 { pattern: /wakeupsPerSec$/, floor: 20, gate: true },
56 { pattern: /MB$/, floor: 15, gate: true },
57 { pattern: /loopDelayP99Ms$/, floor: 5, gate: true },
58 { pattern: /loopDelayMaxMs$/, floor: 20, gate: true },
59 { pattern: /gcPauseMs$/, floor: 10, gate: true },
60 { pattern: /./, floor: 300, gate: true },
61];
62
63describe("compareMetrics", () => {
64 it.each([
65 ["a CPU jump past both the percentage and the floor", { cpuPct: 18.6 }, ["quiet.cpuPct"], []],
66 ["a large relative change under the 1 point CPU floor", { cpuPct: 0.9 }, [], []],
67 ["RSS growth under the 15 MB floor", { rssPeakMB: 512 }, [], []],
68 ["RSS growth past it", { rssPeakMB: 585 }, ["quiet.rssPeakMB"], []],
69 ["a doubled p99 loop delay under the 5 ms floor", { loopDelayP99Ms: 3 }, [], []],
70 ["a p99 loop delay past it", { loopDelayP99Ms: 40 }, ["quiet.loopDelayP99Ms"], []],
71 ["one longer stall under the 20 ms max floor", { loopDelayMaxMs: 25 }, [], []],
72 ["GC pauses past the 10 ms floor", { gcPauseMs: 30 }, ["quiet.gcPauseMs"], []],
73 [
74 "an improvement past the floors",
75 { cpuPct: 0.1, wakeupsPerSec: 7, rssPeakMB: 400 },
76 [],
77 ["quiet.rssPeakMB"],
78 ],
79 ])("one run each: %s", (_name, change, regressed, improved) => {
80 const result = compareMetrics(
81 quietRuns(base),
82 quietRuns({ ...base, ...change }),
83 10,
84 gatedRules,
85 );
86 expect(result.regressed).toEqual(regressed);
87 expect(result.improved).toEqual(improved);
88 });
89
90 it("shows a metric missing on one side as n/a, not as a change", () => {
91 const before = quietRuns({ ...base, wakeupsPerSec: null });
92 const result = compareMetrics(before, quietRuns({ ...base, wakeupsPerSec: 50 }));
93 expect(result.regressed).toEqual([]);
94 expect(result.table).toContain("| quiet.wakeupsPerSec | – | 50 | n/a | | |");
95 });
96
97 it("flags repeated runs only when the whole interval clears the limit", () => {
98 const steady = cpuRuns(26.1, 27.4, 25.8);
99 const fixed = cpuRuns(2.2, 2.5, 2.1);
100 expect(compareMetrics(steady, fixed, 10, gatedRules).improved).toContain("quiet.cpuPct");
101 expect(compareMetrics(fixed, steady, 10, gatedRules).regressed).toContain("quiet.cpuPct");
102 // The medians differ by 2.8 points, past the floor, but a resampled median can match.
103 expect(compareMetrics(fixed, cpuRuns(2.2, 5, 9), 10, gatedRules).regressed).toEqual([]);
104 });
105
106 it("compares the unequal run counts an ab that stopped early leaves", () => {
107 const steady = cpuRuns(26.1, 27.4, 25.8);
108 const partial = compareMetrics(steady, cpuRuns(2.2, 2.5), 10, gatedRules);
109 expect(partial.improved).toContain("quiet.cpuPct");
110 expect(partial.table).toContain("(3 runs), after `abc` (2 runs)");
111 const single = compareMetrics(steady, cpuRuns(2.2), 10, gatedRules);
112 expect(single.improved).toContain("quiet.cpuPct");
113 expect(single.table).toContain("| quiet.cpuPct | 26.1 | 2.2 | -92% | | better |");
114 expect(compareMetrics(steady, cpuRuns(), 10, gatedRules).compared).toBe(0);
115 });
116
117 it("reads a file written before loop delay and GC were recorded", () => {
118 const older = decodeMetrics({
119 meta,
120 runs: [
121 {
122 quiet: { cpuPct: 0.3, wakeupsPerSec: 7, rssPeakMB: 500, rssMB: 500, heapAfterGcMB: 200 },
123 },
124 ],
125 });
126 const result = compareMetrics(older, quietRuns(base));
127 expect(result.compared).toBe(5);
128 expect(result.table).toContain("| quiet.loopDelayP99Ms | – | 1.5 | n/a | | |");
129 });
130
131 it("does not compare wakeups counted with and without the delay sampler", () => {
132 const sampled = quietRuns({ ...base, wakeupsPerSec: 160 });
133 const withSamples: ServerMetrics = {
134 meta,
135 runs: sampled.runs.map((run) => ({ quiet: { ...run.quiet, loopSamples: 2_900 } })),
136 };
137 const result = compareMetrics(quietRuns(base), withSamples);
138 expect(result.regressed).toEqual([]);
139 expect(result.table).toContain("| quiet.wakeupsPerSec | 7 | 160 | not comparable | | |");
140 // The per-thread fit comes from the quiet windows, so it follows their method.
141 const scaling = (wakeupsPerThread: number) => ({
142 heapMBPerThread: 1,
143 rssMBPerThread: 2,
144 cpuPctPerThread: 0.1,
145 wakeupsPerThread,
146 points: [],
147 });
148 const fitted = (metrics: ServerMetrics, wakeupsPerThread: number): ServerMetrics => ({
149 meta,
150 runs: metrics.runs.map((run) => ({ ...run, scaling: scaling(wakeupsPerThread) })),
151 });
152 const fit = compareMetrics(fitted(quietRuns(base), 0.5), fitted(withSamples, 6));
153 expect(fit.regressed).toEqual([]);
154 expect(fit.table).toContain("| scaling.wakeupsPerThread | 0.5 | 6 | not comparable | | |");
155 expect(compareMetrics(withSamples, withSamples).compared).toBe(10);
156 });
157
158 it("counts how many metrics both sides share", () => {
159 const startupOnly: ServerMetrics = { meta, runs: [{ startup: { readyMs: 9_000 } }] };
160 expect(compareMetrics(startupOnly, quietRuns(base)).compared).toBe(0);
161 expect(compareMetrics(quietRuns(base), quietRuns(base)).compared).toBe(10);
162 });
163});
164
165describe("compareMetrics with noise rules", () => {
166 const rules = [
167 { pattern: /cpuPct$/, floor: 1, gate: true },
168 { pattern: /loopDelayMaxMs$/, floor: 20, gate: false },
169 { pattern: /./, floor: 1_000, gate: true },
170 ];
171 it.each([
172 ["a gated metric past its floor", { cpuPct: 5 }, ["quiet.cpuPct"], []],
173 ["an ungated metric far past its floor", { loopDelayMaxMs: 300 }, [], ["quiet.loopDelayMaxMs"]],
174 ["an ungated metric under its floor", { loopDelayMaxMs: 20 }, [], []],
175 ])("%s", (_name, change, regressed, informational) => {
176 const result = compareMetrics(quietRuns(base), quietRuns({ ...base, ...change }), 10, rules);
177 expect(result.regressed).toEqual(regressed);
178 expect(result.informational).toEqual(informational);
179 });
180
181 it("marks an ungated metric in the table either way", () => {
182 const result = compareMetrics(quietRuns(base), quietRuns(base), 10, rules);
183 expect(result.table).toContain("| quiet.loopDelayMaxMs | 8 | 8 | 0% | | noisy |");
184 });
185});
186
187describe("compareMetrics on each platform's noise floors", () => {
188 // The default floor for working.rssMBPerThread is 0.2 MB; Windows A/A runs needed 0.6.
189 const onPlatform = (platform: string, rssMBPerThread: number) => {
190 const working = {
191 active: {},
192 heapMBPerThread: 0.2,
193 rssMBPerThread,
194 cpuPctPerThread: 0,
195 loopDelayP99MsPerThread: 0,
196 };
197 return {
198 meta: { ...meta, platform },
199 runs: [{ working }, { working }],
200 } satisfies ServerMetrics;
201 };
202 const regressed = (before: ServerMetrics, after: ServerMetrics) =>
203 compareMetrics(before, after, 10).regressed;
204
205 it("gates Linux files on the default floor", () => {
206 expect(regressed(onPlatform("linux-x64", 1), onPlatform("linux-x64", 1.4))).toEqual([
207 "working.rssMBPerThread",
208 ]);
209 });
210
211 it("gates Windows files on the Windows floor", () => {
212 expect(regressed(onPlatform("win32-x64", 1), onPlatform("win32-x64", 1.4))).toEqual([]);
213 expect(regressed(onPlatform("win32-x64", 1), onPlatform("win32-x64", 1.8))).toEqual([
214 "working.rssMBPerThread",
215 ]);
216 });
217
218 it("falls back to the default floor with a warning when the platforms differ", () => {
219 const result = compareMetrics(onPlatform("linux-x64", 1), onPlatform("win32-x64", 1.4));
220 expect(result.regressed).toEqual(["working.rssMBPerThread"]);
221 expect(result.warnings).toEqual([
222 "the files come from linux-x64 and win32-x64, so the default noise floors apply",
223 ]);
224 });
225
226 it("marks the noise report against the platform's floor", () => {
227 const pair = (platform: string) => [onPlatform(platform, 1), onPlatform(platform, 1)] as const;
228 const floor = (platform: string) =>
229 noiseReport([pair(platform)]).rows.find(({ metric }) => metric === "working.rssMBPerThread")
230 ?.floor;
231 expect(floor("linux-x64")).toBe(0.2);
232 expect(floor("win32-x64")).toBe(0.6);
233 });
234});
235
236describe("noisePairProblem", () => {
237 const at = (commit: string | null): ServerMetrics => ({ meta: { ...meta, commit }, runs: [] });
238 it.each([
239 ["one commit against itself", at("abc"), at("abc"), undefined],
240 [
241 "an A/B directory",
242 at("abc"),
243 at("def"),
244 "ab-out: a.json is abc and b.json def; noise floors need one commit against itself",
245 ],
246 [
247 "a file with no commit",
248 at(null),
249 at(null),
250 "ab-out: a.json or b.json records no commit, so it cannot be shown to be an A/A run",
251 ],
252 ])("%s", (_name, a, b, problem) => {
253 expect(noisePairProblem("ab-out", a, b)).toBe(problem);
254 });
255});
256
257describe("loadWarnings", () => {
258 const loaded = (cpus: number | null, ...machines: Array<[number | null, number | null]>) =>
259 ({
260 meta: { ...meta, cpus },
261 runs: machines.map(([load1, otherCpuCores]) => ({
262 machine: { load1, otherCpuCores, minutes: 10 },
263 })),
264 }) satisfies ServerMetrics;
265 it.each([
266 ["a quiet machine", loaded(16, [4, 1], [5, 2]), 0],
267 ["load past 0.75 per CPU with other processes unknown", loaded(16, [4, null], [12.5, null]), 1],
268 ["high load from the run's own agents", loaded(16, [21, 1]), 0],
269 ["other processes on a quarter of the cores", loaded(16, [4, 4.1]), 1],
270 ["no CPU count", loaded(null, [40, 40]), 0],
271 ["no machine state, as in older files", { meta: { ...meta, cpus: 16 }, runs: [{}] }, 0],
272 ])("%s", (_name, metrics, count) => {
273 expect(loadWarnings("before", metrics)).toHaveLength(count);
274 });
275
276 it("names the worst run", () => {
277 expect(loadWarnings("after", loaded(16, [13, 4.5], [14.2, 5]))[0]).toContain(
278 "after ran under heavy load in 2 of 2 runs (load up to 14.2 on 16 CPUs, other node/T3/agent processes up to 5 cores)",
279 );
280 });
281
282 it("is reported by compareMetrics", () => {
283 expect(compareMetrics(quietRuns(base), loaded(16, [20, null])).warnings).toHaveLength(1);
284 });
285});
286
287describe("noiseReport", () => {
288 it.each([
289 // Resampled medians differ by -0.3 to 1.1: half-width 0.7, so the floor is 0.35.
290 ["the A/A interval", cpuRuns(2, 2.2, 2.4), cpuRuns(2.1, 3.1, 2.3), 0.7, 0.1, 0.35],
291 // Constant sides: no interval, and medians 2 and 3.25 differ by 1.25.
292 ["the A/A median difference", cpuRuns(2, 2), cpuRuns(3.25, 3.25), 0, 1.25, 1.3],
293 ])("estimates a floor from %s, rounded up", (_name, a, b, halfWidth, aaDifference, floor) => {
294 const [row] = noiseReport([[a, b]]).rows.filter(({ metric }) => metric === "quiet.cpuPct");
295 expect(row).toMatchObject({ halfWidth, aaDifference, suggestedFloor: floor });
296 });
297
298 it("leaves out wakeups counted with the delay sampler on one side only", () => {
299 const sampled: ServerMetrics = {
300 meta,
301 runs: quietRuns({ ...base, wakeupsPerSec: 160 }).runs.map((run) => ({
302 quiet: { ...run.quiet, loopSamples: 2_900 },
303 })),
304 };
305 const metrics = noiseReport([[quietRuns(base), sampled]]).rows.map(({ metric }) => metric);
306 expect(metrics).not.toContain("quiet.wakeupsPerSec");
307 expect(metrics).toContain("quiet.cpuPct");
308 });
309
310 it("takes the widest pair, and calls a metric noisy when its interval passes half its median", () => {
311 const quiet = [cpuRuns(2, 2.1), cpuRuns(2, 2.1)] as const;
312 const wild = [cpuRuns(1, 4), cpuRuns(2, 2)] as const;
313 const [row] = noiseReport([quiet, wild]).rows.filter(({ metric }) => metric === "quiet.cpuPct");
314 expect(row?.halfWidth).toBe(1.5);
315 expect(row?.suggestedGate).toBe(false);
316 });
317
318 it("leaves out fields that describe a run", () => {
319 const withMachine = (metrics: ServerMetrics): ServerMetrics => ({
320 ...metrics,
321 runs: metrics.runs.map((run) => ({
322 ...run,
323 machine: { load1: 3, otherCpuCores: 1, minutes: 9 },
324 })),
325 });
326 const rows = noiseReport([[withMachine(cpuRuns(1)), withMachine(cpuRuns(1))]]).rows;
327 expect(rows.some(({ metric }) => metric.startsWith("machine."))).toBe(false);
328 });
329});
330
331describe("compareMetrics on working throughput", () => {
332 const working = (turnsPerMin: number) =>
333 ({
334 meta,
335 runs: [
336 {
337 working: {
338 active: {
339 "20": {
340 ...loopAtRest,
341 cpuPct: 90,
342 wakeupsPerSec: 1_600,
343 rssPeakMB: 700,
344 rssMB: 560,
345 heapAfterGcMB: 218,
346 turnsPerMin,
347 toRequestMs: { median: 4_000, p90: 9_000 },
348 turnEndMs: { median: 16_000, p90: 25_000 },
349 turnMs: { median: 24_000, p90: 36_000 },
350 },
351 },
352 heapMBPerThread: 0.2,
353 rssMBPerThread: 0.8,
354 cpuPctPerThread: 0,
355 loopDelayP99MsPerThread: 0,
356 },
357 },
358 ],
359 }) satisfies ServerMetrics;
360 const rules = [{ pattern: /turnsPerMin$/, floor: 2, gate: true }];
361 it.each([
362 ["fewer turns a minute", 20, ["working.active.20.turnsPerMin"], []],
363 ["more turns a minute", 45, [], ["working.active.20.turnsPerMin"]],
364 ["a move under the floor", 34, [], []],
365 ])("%s", (_name, turnsPerMin, regressed, improved) => {
366 const result = compareMetrics(working(33), working(turnsPerMin), 5, rules);
367 expect(result.regressed).toEqual(regressed);
368 expect(result.improved).toEqual(improved);
369 });
370});
371
372describe("compareMetrics on the busy shell snapshot", () => {
373 const window = { cpuPct: 1, wakeupsPerSec: 10, rssPeakMB: 500, rssMB: 500, heapAfterGcMB: 200 };
374 const busyRun = (shellMs: number, shellKB: number) =>
375 ({
376 meta,
377 runs: [
378 {
379 busy: {
380 readyMs: 8_000,
381 projects: 9,
382 ballastMB: 768,
383 rest: window,
384 shellMs: { median: shellMs, p90: shellMs },
385 shellKB,
386 git: window,
387 },
388 },
389 ],
390 }) satisfies ServerMetrics;
391 it.each([
392 ["40 ms slower, under the 50 ms floor", busyRun(230, 100), []],
393 ["150 ms slower, past it", busyRun(340, 100), ["busy.shellMs.median", "busy.shellMs.p90"]],
394 ["15 KB larger, under the 20 KB floor", busyRun(190, 115), []],
395 ["40 KB larger, past it", busyRun(190, 140), ["busy.shellKB"]],
396 ])("%s", (_name, after, regressed) => {
397 expect(compareMetrics(busyRun(190, 100), after).regressed).toEqual(regressed);
398 });
399});
400
401describe("profileMismatch", () => {
402 const withProfile = (profile?: string): ServerMetrics => ({
403 meta: profile === undefined ? meta : { ...meta, profile },
404 runs: [],
405 });
406 it.each([
407 ["two full runs", withProfile("full"), withProfile("full"), false],
408 ["a file from before profiles, against a full run", withProfile(), withProfile("full"), false],
409 ["two quick runs", withProfile("quick"), withProfile("quick"), false],
410 ["a full run against a quick one", withProfile(), withProfile("quick"), true],
411 ])("%s", (_name, before, after, refused) => {
412 expect(profileMismatch(before, after) !== undefined).toBe(refused);
413 });
414});
415
416describe("heapGrowth", () => {
417 it("lists only constructors that grew, small growth included", () => {
418 const before = new Map([
419 ["Shrank", { count: 10, bytes: 5_000_000 }],
420 ["Flat", { count: 3, bytes: 1_000 }],
421 ["Grew", { count: 1, bytes: 1_000 }],
422 ]);
423 const after = new Map([
424 ["Shrank", { count: 5, bytes: 1_000_000 }],
425 ["Flat", { count: 3, bytes: 1_000 }],
426 ["Grew", { count: 2, bytes: 1_500 }],
427 ["New", { count: 4, bytes: 3_000_000 }],
428 ]);
429 expect(heapGrowth(before, after)).toEqual([
430 { constructor: "New", count: 4, MB: 3 },
431 { constructor: "Grew", count: 1, MB: 0 },
432 ]);
433 });
434});
435
436describe("heapRetainers", () => {
437 it("sums what each object alone keeps alive and names only code on the way to it", () => {
438 // root → Holder.cache → Map → [0] array → [0] string; Holder also points at the string weakly.
439 const node = (type: number, name: number, id: number, size: number, edges: number) => [
440 type,
441 name,
442 id,
443 size,
444 edges,
445 ];
446 const retainers = heapRetainers(
447 {
448 snapshot: {
449 meta: {
450 node_fields: ["type", "name", "id", "self_size", "edge_count"],
451 node_types: [["synthetic", "object", "array", "string"]],
452 edge_fields: ["type", "name_or_index", "to_node"],
453 edge_types: [["property", "element", "weak", "hidden"]],
454 },
455 },
456 nodes: [
457 ...node(0, 4, 1, 0, 1),
458 ...node(1, 1, 3, 100, 2),
459 ...node(1, 2, 5, 50, 1),
460 ...node(2, 0, 7, 1_000, 1),
461 ...node(3, 5, 9, 500, 0),
462 ],
463 edges: [0, 6, 5, 0, 3, 10, 2, 0, 20, 3, 0, 15, 1, 0, 20],
464 strings: ["", "Holder", "Map", "cache", "(GC roots)", "a secret", "holder"],
465 },
466 0,
467 0,
468 );
469 expect(retainers.get(5)).toEqual({
470 retained: 1_550,
471 dominator: 3,
472 path: "holder → Holder.cache → Map",
473 });
474 expect(retainers.get(3)?.retained).toBe(1_650);
475 expect(retainers.get(9)?.path).toBe("holder → Holder.cache → Map.[0] → (array).[0] → (string)");
476 });
477});
478
479describe("retainerGrowth", () => {
480 const at = (sizes: Record<number, number>) =>
481 new Map(
482 Object.entries(sizes).map(([id, retained]) => [
483 Number(id),
484 { retained, dominator: Number(id) === 3 ? undefined : 3, path: `node ${id}` },
485 ]),
486 );
487 it("reports the deepest object holding the growth, and only growth at every snapshot", () => {
488 const growth = retainerGrowth([
489 at({ 3: 1_000_000, 5: 900_000, 7: 2_000_000 }),
490 at({ 3: 3_000_000, 5: 2_900_000, 7: 4_000_000 }),
491 at({ 3: 5_000_000, 5: 4_900_000, 7: 3_500_000 }),
492 ]);
493 expect(growth).toEqual([{ path: "node 5", MB: 4, retainedMB: 4.9 }]);
494 });
495 it("keeps an ancestor its reported descendant does not account for", () => {
496 // A chain 1 → 2 → 3 growing by 7, 6 and 5 MB, and 4 under 1 growing by 2 MB.
497 const chain = (scale: number) =>
498 new Map(
499 [
500 [1, 7, undefined],
501 [2, 6, 1],
502 [3, 5, 2],
503 [4, 2, 1],
504 ].map(([id, mb, dominator]) => [
505 id!,
506 { retained: (10 + mb! * scale) * 1e6, dominator, path: `node ${id}` },
507 ]),
508 );
509 expect(retainerGrowth([chain(0), chain(0.5), chain(1)]).map(({ path }) => path)).toEqual([
510 "node 1",
511 "node 3",
512 "node 4",
513 ]);
514 });
515});
516
517describe("slope", () => {
518 it("fits the cost per thread", () => {
519 const points: Array<readonly [number, number]> = [
520 [0, 175],
521 [5, 180],
522 [10, 185],
523 [20, 195],
524 ];
525 expect(slope(points)).toBeCloseTo(1);
526 });
527});
528
529describe("parsePsCpuTime", () => {
530 it.each([
531 ["0:00.00", 0],
532 ["1:02.50", 62.5],
533 ["1:00:00.00", 3600],
534 ["2-01:00:00", 2 * 86_400 + 3600],
535 ["1-02:03", Number.NaN],
536 ["::", Number.NaN],
537 ["", Number.NaN],
538 ["1:2:3:4", Number.NaN],
539 ])("%s", (text, seconds) => {
540 expect(parsePsCpuTime(text)).toBe(seconds);
541 });
542});
543
544describe("parseRunSelection", () => {
545 const scenarios = ["rest", "quiet", "streaming"];
546 it.each([
547 [
548 "the default thread counts",
549 { only: "", threads: "0,5,10,20", soak: 0 },
550 { only: [], threads: [0, 5, 10, 20] },
551 ],
552 [
553 "descending thread counts",
554 { only: "", threads: "5,20,10", soak: 0 },
555 { error: "--threads takes ascending counts such as 0,5,10,20" },
556 ],
557 [
558 "an empty thread count",
559 { only: "", threads: "0,,10", soak: 0 },
560 { error: "--threads takes ascending counts such as 0,5,10,20" },
561 ],
562 [
563 "named scenarios",
564 { only: "rest,quiet", threads: "0", soak: 0 },
565 { only: ["rest", "quiet"], threads: [0] },
566 ],
567 [
568 "an unknown scenario",
569 { only: "rest,idle", threads: "0", soak: 0 },
570 { error: "unknown scenario: idle" },
571 ],
572 ["a soak alone", { only: "", threads: "0", soak: 10 }, { only: ["none"], threads: [0] }],
573 [
574 "a soak with scenarios",
575 { only: "rest", threads: "0", soak: 10 },
576 { only: ["rest"], threads: [0] },
577 ],
578 [
579 "a negative soak",
580 { only: "", threads: "0", soak: -1 },
581 { error: "--soak takes minutes, 0 for none" },
582 ],
583 [
584 "an infinite soak",
585 { only: "", threads: "0", soak: Number.POSITIVE_INFINITY },
586 { error: "--soak takes minutes, 0 for none" },
587 ],
588 ])("%s", (_name, input, expected) => {
589 expect(parseRunSelection({ ...input, scenarios })).toEqual(expected);
590 });
591});
592
593describe("pathSpellings", () => {
594 it.each([
595 ["a POSIX path, one spelling", "/home/dev/repo", ["/home/dev/repo"]],
596 [
597 "a Windows path as written, JSON-escaped, and with forward slashes",
598 "C:\\Users\\dev\\repo",
599 ["C:\\Users\\dev\\repo", "C:\\\\Users\\\\dev\\\\repo", "C:/Users/dev/repo"],
600 ],
601 [
602 "a path with a quote, escaped the way JSON stores it",
603 '/mnt/dev/quote"repo',
604 ['/mnt/dev/quote"repo', '/mnt/dev/quote\\"repo'],
605 ],
606 [
607 "a path with a control character, escaped the way JSON stores it",
608 "/mnt/dev/new\nline",
609 ["/mnt/dev/new\nline", "/mnt/dev/new\\nline"],
610 ],
611 ])("%s", (_name, path, spellings) => {
612 expect(pathSpellings(path)).toEqual(spellings);
613 });
614});
615
616describe("pathRewrites", () => {
617 const rewrite = (text: string, from: string, to: string) =>
618 pathRewrites(from, to).reduce((value, [a, b]) => value.replaceAll(a, b), text);
619 it.each([
620 [
621 "a POSIX path in JSON",
622 '{"cwd":"/home/dev/repo/x"}',
623 "/home/dev/repo",
624 "/tmp/run/gone/root-0",
625 '{"cwd":"/tmp/run/gone/root-0/x"}',
626 ],
627 [
628 "a Windows path stored JSON-escaped",
629 '{"cwd":"C:\\\\Users\\\\dev\\\\repo\\\\x"}',
630 "C:\\Users\\dev\\repo",
631 "D:\\run\\gone\\root-0",
632 '{"cwd":"D:\\\\run\\\\gone\\\\root-0\\\\x"}',
633 ],
634 [
635 "a Windows path written with forward slashes",
636 "see C:/Users/dev/repo/x",
637 "C:\\Users\\dev\\repo",
638 "D:\\run\\gone\\root-0",
639 "see D:/run/gone/root-0/x",
640 ],
641 ])("%s", (_name, text, from, to, expected) => {
642 expect(rewrite(text, from, to)).toBe(expected);
643 });
644});
645
646describe("nextActivityReport", () => {
647 it.each([
648 ["before it is due: wait out the interval", 1_000, 10_000, { waitMs: 16_000, late: false }],
649 ["a slow reply: send now, still inside the lease", 1_000, 31_000, { waitMs: 0, late: false }],
650 ["past the safe point: the lease would lapse", 1_000, 42_000, { waitMs: 0, late: true }],
651 ])("%s", (_name, lastSentAt, now, expected) => {
652 expect(nextActivityReport(lastSentAt, now)).toEqual(expected);
653 });
654});
655
656describe("activityRenewalMissed", () => {
657 it.each([
658 ["sent on time, answered inside the lease", 25_000, 30_000, false],
659 ["a delayed wake: sent past the safe point", 41_000, 42_000, true],
660 ["sent on time, answered after the lease lapsed", 25_000, 46_000, true],
661 ])("%s", (_name, sentAt, repliedAt, missed) => {
662 expect(activityRenewalMissed(0, sentAt, repliedAt)).toBe(missed);
663 });
664});