import assert from "node:assert/strict"; import { describe, it } from "node:test"; import { computeProcessStartKey, processLiveness, processStartKey, psProcessStartKey, startKeyScheme } from "../../src/shared/process-identity.ts"; function resolvers() { const calls: string[] = []; return { calls, linux: (pid: number) => { calls.push(`linux:${pid}`); return undefined as string | undefined; }, ps: (pid: number) => { calls.push(`ps:${pid}`); return `ps:started-${pid}` as string | undefined; }, windows: (pid: number) => { calls.push(`win:${pid}`); return `win:started-${pid}` as string | undefined; }, }; } describe("process start key", () => { it("uses ps on darwin and other non-linux, non-windows unixes", () => { for (const platform of ["darwin", "freebsd", "openbsd", "sunos"] as const) { const fakes = resolvers(); assert.equal(computeProcessStartKey(42, platform, fakes), "ps:started-42", platform); assert.deepEqual(fakes.calls, ["ps:42"], platform); } }); it("keeps Linux on /proc with a ps fallback and Windows on CIM", () => { const fakes = resolvers(); assert.equal(computeProcessStartKey(7, "linux", { ...fakes, linux: () => "linux:123" }), "linux:123"); assert.equal(computeProcessStartKey(7, "linux", fakes), "ps:started-7"); assert.deepEqual(fakes.calls, ["linux:7", "ps:7"]); const win = resolvers(); assert.equal(computeProcessStartKey(9, "win32", win), "win:started-9"); assert.deepEqual(win.calls, ["win:9"]); }); it("returns undefined when the identity cannot be established", () => { assert.equal(computeProcessStartKey(1, "darwin", { ps: () => undefined }), undefined); assert.equal(computeProcessStartKey(1, "win32", { windows: () => undefined }), undefined); }); it("caches only this process's own key", () => { const own = processStartKey(process.pid); assert.equal(processStartKey(process.pid), own); if (process.platform === "darwin") assert.match(own ?? "", /^ps:\S/); }); it("reads a real ps start key on darwin", { skip: process.platform !== "darwin" }, () => { const key = psProcessStartKey(process.pid); assert.match(key ?? "", /^ps:\S/); assert.equal(startKeyScheme(key!), "ps"); assert.equal(psProcessStartKey(2 ** 22 + 12345), undefined); }); it("names key schemes so unlike keys are never compared", () => { assert.equal(startKeyScheme("linux:123"), "linux"); assert.equal(startKeyScheme("ps:Mon Oct 4 10:00:00 2026"), "ps"); assert.equal(startKeyScheme("win:2026"), "win"); assert.equal(startKeyScheme("opaque"), ""); }); }); describe("process liveness", () => { const killing = (code: string | undefined) => () => { throw Object.assign(new Error(code ?? "boom"), code ? { code } : {}); }; it("maps kill(pid, 0) outcomes to alive, dead, or unknown", () => { assert.equal(processLiveness(10, () => true), "alive"); assert.equal(processLiveness(10, killing("ESRCH")), "dead"); assert.equal(processLiveness(10, killing("EPERM")), "alive"); assert.equal(processLiveness(10, killing("EINVAL")), "unknown"); assert.equal(processLiveness(10, killing(undefined)), "unknown"); }); it("never probes non-positive or fractional pids (kill(0) would signal the process group)", () => { const probe = () => { throw new Error("must not be called"); }; for (const pid of [0, -1, 1.5, Number.NaN]) assert.equal(processLiveness(pid, probe), "unknown"); }); });