Files
wehub-resource-sync 070959e133
landing-page-staging / Deploy landing page to staging (push) Has been skipped
landing-page-ci / Validate landing page (push) Failing after 4s
visual-baseline / Capture visual baselines (push) Has been cancelled
bake-plugin-previews / Bake plugin previews (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:00:47 +08:00

42 lines
1.5 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { collectProcessTreePids, type ProcessSnapshot } from "../src/index.js";
function snapshot(pid: number, ppid: number, command = `pid-${pid}`): ProcessSnapshot {
return { command, pid, ppid };
}
describe("collectProcessTreePids", () => {
it("returns an empty array when no roots are supplied", () => {
expect(collectProcessTreePids([snapshot(100, 1), snapshot(101, 100)], [])).toEqual([]);
expect(collectProcessTreePids([], [null, undefined])).toEqual([]);
});
it("returns a single root with no descendants", () => {
expect(collectProcessTreePids([snapshot(101, 1)], [100])).toEqual([100]);
});
it("walks two levels of descendants and sorts pids descending", () => {
const processes = [
snapshot(100, 1),
snapshot(200, 100),
snapshot(201, 100),
snapshot(300, 200),
];
expect(collectProcessTreePids(processes, [100])).toEqual([300, 201, 200, 100]);
});
it("returns the root even when no matching ppid exists in the process list", () => {
expect(collectProcessTreePids([snapshot(500, 1)], [100])).toEqual([100]);
});
it("dedupes repeated root pids", () => {
expect(collectProcessTreePids([snapshot(200, 100)], [100, 100])).toEqual([200, 100]);
});
it("terminates on parent-child cycles instead of looping forever", () => {
const processes = [snapshot(100, 200), snapshot(200, 100)];
expect(collectProcessTreePids(processes, [100])).toEqual([200, 100]);
});
});