Files
openclaw/src/test-utils/process-tree.test.ts
2026-07-13 17:13:16 -07:00

35 lines
1.0 KiB
TypeScript

// Tests race-safe process cleanup helpers.
import { afterEach, describe, expect, it, vi } from "vitest";
import { killPidIfAlive } from "./process-tree.js";
describe("killPidIfAlive", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("ignores ESRCH when a live process exits before SIGKILL", () => {
const killError = Object.assign(new Error("kill ESRCH"), { code: "ESRCH" });
const kill = vi
.spyOn(process, "kill")
.mockReturnValueOnce(true)
.mockImplementationOnce(() => {
throw killError;
});
expect(() => killPidIfAlive(123)).not.toThrow();
expect(kill).toHaveBeenNthCalledWith(1, 123, 0);
expect(kill).toHaveBeenNthCalledWith(2, 123, "SIGKILL");
});
it("rethrows other SIGKILL failures", () => {
const killError = Object.assign(new Error("kill EPERM"), { code: "EPERM" });
vi.spyOn(process, "kill")
.mockReturnValueOnce(true)
.mockImplementationOnce(() => {
throw killError;
});
expect(() => killPidIfAlive(123)).toThrow(killError);
});
});