From 880e62351e010a008b6373e3d23d5072e3fd8138 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:16:38 -0500 Subject: [PATCH] test(process): consolidate process inventory and native row checks (#12889) * test(process): remove redundant wide-family assertions * test(process): consolidate process inventory checks --- src/process/LinuxProcessStat.test.ts | 25 +++---- .../ProcessOwnership.validation.test.ts | 2 +- .../ProcessOwnershipProcessTree.test.ts | 6 +- src/process/ProcessOwnershipProcessTree.ts | 2 +- src/process/systemPs.test.ts | 74 ++++++++----------- 5 files changed, 43 insertions(+), 66 deletions(-) diff --git a/src/process/LinuxProcessStat.test.ts b/src/process/LinuxProcessStat.test.ts index 860711dcaf..b9f5bcb66d 100644 --- a/src/process/LinuxProcessStat.test.ts +++ b/src/process/LinuxProcessStat.test.ts @@ -72,24 +72,17 @@ it("retains identity when Linux teardown reports unavailable group and session I sessionId: -1, kernelThread: false, }); - expect( - parseLinuxProcessStat(statRecord({ processGroupId: "-2" })), - ).toBeUndefined(); - expect( - parseLinuxProcessStat(statRecord({ sessionId: "-2" })), - ).toBeUndefined(); }); -it("distinguishes zombie lifecycle and rejects malformed or missing identity fields", () => { - expect(parseLinuxProcessStat(statRecord({ state: "Z" }))?.lifecycle).toBe( - "zombie", - ); - expect(parseLinuxProcessStat("(worker) S 1 2 3")).toBeUndefined(); - expect(parseLinuxProcessStat(statRecord({ pid: "0" }))).toBeUndefined(); - expect(parseLinuxProcessStat(statRecord({ state: "??" }))).toBeUndefined(); - expect(parseLinuxProcessStat(statRecord({ startTime: "not-a-number" }))).toBe( - undefined, - ); +it.each([ + ["missing PID", "(worker) S 1 2 3"], + ["PID zero", statRecord({ pid: "0" })], + ["malformed state", statRecord({ state: "??" })], + ["malformed start time", statRecord({ startTime: "not-a-number" })], + ["invalid group sentinel", statRecord({ processGroupId: "-2" })], + ["invalid session sentinel", statRecord({ sessionId: "-2" })], +])("rejects a native stat record with %s", (_reason, record) => { + expect(parseLinuxProcessStat(record)).toBeUndefined(); }); it.each(["R", "S", "D"])( diff --git a/src/process/ProcessOwnership.validation.test.ts b/src/process/ProcessOwnership.validation.test.ts index 8ea5db2d1e..3c2f11434a 100644 --- a/src/process/ProcessOwnership.validation.test.ts +++ b/src/process/ProcessOwnership.validation.test.ts @@ -1129,7 +1129,7 @@ describe("owned process-group cleanup validation: exited members", () => { cleaned: true, signaled: true, }); - expect(environment.mock.calls).toEqual([[100], [100]]); + expect(environment).not.toHaveBeenCalledWith(101); expect(signalGroup).toHaveBeenCalledWith(100, "SIGKILL"); }); it("observes a zombie-only group as settled", async () => { diff --git a/src/process/ProcessOwnershipProcessTree.test.ts b/src/process/ProcessOwnershipProcessTree.test.ts index 6a2b254bc1..f76b3c8727 100644 --- a/src/process/ProcessOwnershipProcessTree.test.ts +++ b/src/process/ProcessOwnershipProcessTree.test.ts @@ -22,12 +22,10 @@ it.each([false, true])( const expected = nested ? [intermediate, ...children] : children; const table = [row(99, 0), row(1000, 0), ...expected]; const found = descendantsOf([1000], table); - expect(found).toHaveLength(expected.length); + expect(found).toHaveLength(nested ? 130_001 : 130_000); expect(found.every((process, index) => process === expected[index])).toBe( true, ); - expect(new Set(found.map(({ pid }) => pid)).size).toBe(expected.length); - expect(found.at(-1)?.pid).toBe(131001); }, ); @@ -58,7 +56,7 @@ it("excludes all selected roots and visits repeated or cyclic PIDs once", () => leaf, ]); const found = descendantsOf([1, 2, 1], table); - expect(found).toEqual([first, leaf]); + expect(found).toHaveLength(2); expect(found[0]).toBe(first); expect(found[1]).toBe(leaf); }); diff --git a/src/process/ProcessOwnershipProcessTree.ts b/src/process/ProcessOwnershipProcessTree.ts index a7cd53533c..f4d3f2372e 100644 --- a/src/process/ProcessOwnershipProcessTree.ts +++ b/src/process/ProcessOwnershipProcessTree.ts @@ -1,6 +1,6 @@ import type { ProcessTableEntry } from "./ProcessOwnership.js"; -/** Exclude process-table entries that the operating system has already reaped. */ +/** Exclude exited processes whose zombie rows remain until reaping. */ export const liveProcesses = ( members: readonly ProcessTableEntry[], ): readonly ProcessTableEntry[] => diff --git a/src/process/systemPs.test.ts b/src/process/systemPs.test.ts index 1c36d61f39..91fe4f505c 100644 --- a/src/process/systemPs.test.ts +++ b/src/process/systemPs.test.ts @@ -7,11 +7,7 @@ import { spawnOwnedProviderProcess, } from "./ProviderProcess.js"; import { cleanupOwnedProcessGroup } from "./ProcessOwnership.js"; -import { - LINUX_SYSTEM_PS_LOCATIONS, - describeSystemPs, - resolveSystemPs, -} from "./systemPs.js"; +import { describeSystemPs, resolveSystemPs } from "./systemPs.js"; import { waitFor } from "../../tests/support/testWait.js"; afterEach(() => vi.unstubAllEnvs()); @@ -21,50 +17,40 @@ const present = async (path: string): Promise => paths.includes(path); -it("selects Linux ps from the first fixed system location before REA's PATH", async () => { +const binPs = "/bin/ps"; +const usrBinPs = "/usr/bin/ps"; +const nixPs = "/run/current-system/sw/bin/ps"; + +it.each([ + [[binPs, usrBinPs, nixPs], binPs, "system"], + [[usrBinPs, nixPs], usrBinPs, "system"], + [[nixPs], nixPs, "system"], + [[], "ps", "path"], +] as const)("selects Linux ps from %j", async (executable, command, source) => { await expect( - resolveSystemPs("linux", { - isExecutable: present(...LINUX_SYSTEM_PS_LOCATIONS), - }), - ).resolves.toEqual({ command: "/bin/ps", source: "system" }); - await expect( - resolveSystemPs("linux", { isExecutable: present("/usr/bin/ps") }), - ).resolves.toEqual({ command: "/usr/bin/ps", source: "system" }); - await expect( - resolveSystemPs("linux", { - isExecutable: present("/run/current-system/sw/bin/ps"), - }), - ).resolves.toEqual({ - command: "/run/current-system/sw/bin/ps", - source: "system", - }); - await expect( - resolveSystemPs("linux", { isExecutable: present() }), - ).resolves.toEqual({ - command: "ps", - source: "path", - }); + resolveSystemPs("linux", { isExecutable: present(...executable) }), + ).resolves.toEqual({ command, source }); }); -it("runs /bin/ps on macOS and keeps PATH on other ps platforms", async () => { - await expect( - resolveSystemPs("darwin", { isExecutable: present() }), - ).resolves.toEqual({ - command: "/bin/ps", - source: "system", - }); - await expect( - resolveSystemPs("freebsd", { isExecutable: present("/bin/ps") }), - ).resolves.toEqual({ command: "ps", source: "path" }); -}); +it.each([ + ["darwin", [], binPs, "system"], + ["freebsd", [binPs], "ps", "path"], +] as const)( + "selects ps on %s", + async (platform, executable, command, source) => { + await expect( + resolveSystemPs(platform, { isExecutable: present(...executable) }), + ).resolves.toEqual({ command, source }); + }, +); -it("names which ps ran, and the fixed locations checked before PATH", () => { - expect(describeSystemPs({ command: "/usr/bin/ps", source: "system" })).toBe( - "ps at /usr/bin/ps", - ); - expect(describeSystemPs({ command: "ps", source: "path" })).toBe( - `ps from REA's PATH (none of ${LINUX_SYSTEM_PS_LOCATIONS.join(", ")} is executable)`, +it("names the executable or the fixed locations tried before PATH", () => { + expect(describeSystemPs({ command: usrBinPs, source: "system" })).toContain( + usrBinPs, ); + const fallback = describeSystemPs({ command: "ps", source: "path" }); + for (const detail of ["PATH", binPs, usrBinPs, nixPs]) + expect(fallback).toContain(detail); }); it.runIf(process.platform === "darwin")(