test: share restart health helpers

This commit is contained in:
Peter Steinberger
2026-03-14 00:06:38 +00:00
parent d07c6c0bc6
commit 6e7e82e5e7

View File

@@ -20,28 +20,45 @@ vi.mock("../../gateway/probe.js", () => ({
const originalPlatform = process.platform; const originalPlatform = process.platform;
function makeGatewayService(
runtime: { status: "running"; pid: number } | { status: "stopped" },
): GatewayService {
return {
readRuntime: vi.fn(async () => runtime),
} as unknown as GatewayService;
}
async function inspectGatewayRestartWithSnapshot(params: {
runtime: { status: "running"; pid: number } | { status: "stopped" };
portUsage: PortUsage;
includeUnknownListenersAsStale?: boolean;
}) {
const service = makeGatewayService(params.runtime);
inspectPortUsage.mockResolvedValue(params.portUsage);
const { inspectGatewayRestart } = await import("./restart-health.js");
return inspectGatewayRestart({
service,
port: 18789,
...(params.includeUnknownListenersAsStale === undefined
? {}
: { includeUnknownListenersAsStale: params.includeUnknownListenersAsStale }),
});
}
async function inspectUnknownListenerFallback(params: { async function inspectUnknownListenerFallback(params: {
runtime: { status: "running"; pid: number } | { status: "stopped" }; runtime: { status: "running"; pid: number } | { status: "stopped" };
includeUnknownListenersAsStale: boolean; includeUnknownListenersAsStale: boolean;
}) { }) {
Object.defineProperty(process, "platform", { value: "win32", configurable: true }); Object.defineProperty(process, "platform", { value: "win32", configurable: true });
classifyPortListener.mockReturnValue("unknown"); classifyPortListener.mockReturnValue("unknown");
return inspectGatewayRestartWithSnapshot({
const service = { runtime: params.runtime,
readRuntime: vi.fn(async () => params.runtime), portUsage: {
} as unknown as GatewayService; port: 18789,
status: "busy",
inspectPortUsage.mockResolvedValue({ listeners: [{ pid: 10920, command: "unknown" }],
port: 18789, hints: [],
status: "busy", },
listeners: [{ pid: 10920, command: "unknown" }],
hints: [],
});
const { inspectGatewayRestart } = await import("./restart-health.js");
return inspectGatewayRestart({
service,
port: 18789,
includeUnknownListenersAsStale: params.includeUnknownListenersAsStale, includeUnknownListenersAsStale: params.includeUnknownListenersAsStale,
}); });
} }
@@ -49,21 +66,17 @@ async function inspectUnknownListenerFallback(params: {
async function inspectAmbiguousOwnershipWithProbe( async function inspectAmbiguousOwnershipWithProbe(
probeResult: Awaited<ReturnType<typeof probeGateway>>, probeResult: Awaited<ReturnType<typeof probeGateway>>,
) { ) {
const service = {
readRuntime: vi.fn(async () => ({ status: "running", pid: 8000 })),
} as unknown as GatewayService;
inspectPortUsage.mockResolvedValue({
port: 18789,
status: "busy",
listeners: [{ commandLine: "" }],
hints: [],
});
classifyPortListener.mockReturnValue("unknown"); classifyPortListener.mockReturnValue("unknown");
probeGateway.mockResolvedValue(probeResult); probeGateway.mockResolvedValue(probeResult);
return inspectGatewayRestartWithSnapshot({
const { inspectGatewayRestart } = await import("./restart-health.js"); runtime: { status: "running", pid: 8000 },
return inspectGatewayRestart({ service, port: 18789 }); portUsage: {
port: 18789,
status: "busy",
listeners: [{ commandLine: "" }],
hints: [],
},
});
} }
describe("inspectGatewayRestart", () => { describe("inspectGatewayRestart", () => {
@@ -89,39 +102,31 @@ describe("inspectGatewayRestart", () => {
}); });
it("treats a gateway listener child pid as healthy ownership", async () => { it("treats a gateway listener child pid as healthy ownership", async () => {
const service = { const snapshot = await inspectGatewayRestartWithSnapshot({
readRuntime: vi.fn(async () => ({ status: "running", pid: 7000 })), runtime: { status: "running", pid: 7000 },
} as unknown as GatewayService; portUsage: {
port: 18789,
inspectPortUsage.mockResolvedValue({ status: "busy",
port: 18789, listeners: [{ pid: 7001, ppid: 7000, commandLine: "openclaw-gateway" }],
status: "busy", hints: [],
listeners: [{ pid: 7001, ppid: 7000, commandLine: "openclaw-gateway" }], },
hints: [],
}); });
const { inspectGatewayRestart } = await import("./restart-health.js");
const snapshot = await inspectGatewayRestart({ service, port: 18789 });
expect(snapshot.healthy).toBe(true); expect(snapshot.healthy).toBe(true);
expect(snapshot.staleGatewayPids).toEqual([]); expect(snapshot.staleGatewayPids).toEqual([]);
}); });
it("marks non-owned gateway listener pids as stale while runtime is running", async () => { it("marks non-owned gateway listener pids as stale while runtime is running", async () => {
const service = { const snapshot = await inspectGatewayRestartWithSnapshot({
readRuntime: vi.fn(async () => ({ status: "running", pid: 8000 })), runtime: { status: "running", pid: 8000 },
} as unknown as GatewayService; portUsage: {
port: 18789,
inspectPortUsage.mockResolvedValue({ status: "busy",
port: 18789, listeners: [{ pid: 9000, ppid: 8999, commandLine: "openclaw-gateway" }],
status: "busy", hints: [],
listeners: [{ pid: 9000, ppid: 8999, commandLine: "openclaw-gateway" }], },
hints: [],
}); });
const { inspectGatewayRestart } = await import("./restart-health.js");
const snapshot = await inspectGatewayRestart({ service, port: 18789 });
expect(snapshot.healthy).toBe(false); expect(snapshot.healthy).toBe(false);
expect(snapshot.staleGatewayPids).toEqual([9000]); expect(snapshot.staleGatewayPids).toEqual([9000]);
}); });
@@ -157,21 +162,14 @@ describe("inspectGatewayRestart", () => {
Object.defineProperty(process, "platform", { value: "win32", configurable: true }); Object.defineProperty(process, "platform", { value: "win32", configurable: true });
classifyPortListener.mockReturnValue("ssh"); classifyPortListener.mockReturnValue("ssh");
const service = { const snapshot = await inspectGatewayRestartWithSnapshot({
readRuntime: vi.fn(async () => ({ status: "stopped" })), runtime: { status: "stopped" },
} as unknown as GatewayService; portUsage: {
port: 18789,
inspectPortUsage.mockResolvedValue({ status: "busy",
port: 18789, listeners: [{ pid: 22001, command: "nginx.exe" }],
status: "busy", hints: [],
listeners: [{ pid: 22001, command: "nginx.exe" }], },
hints: [],
});
const { inspectGatewayRestart } = await import("./restart-health.js");
const snapshot = await inspectGatewayRestart({
service,
port: 18789,
includeUnknownListenersAsStale: true, includeUnknownListenersAsStale: true,
}); });
@@ -192,25 +190,21 @@ describe("inspectGatewayRestart", () => {
it("treats a busy port as healthy when runtime status lags but the probe succeeds", async () => { it("treats a busy port as healthy when runtime status lags but the probe succeeds", async () => {
Object.defineProperty(process, "platform", { value: "win32", configurable: true }); Object.defineProperty(process, "platform", { value: "win32", configurable: true });
const service = {
readRuntime: vi.fn(async () => ({ status: "stopped" })),
} as unknown as GatewayService;
inspectPortUsage.mockResolvedValue({
port: 18789,
status: "busy",
listeners: [{ pid: 9100, commandLine: "openclaw-gateway" }],
hints: [],
});
classifyPortListener.mockReturnValue("gateway"); classifyPortListener.mockReturnValue("gateway");
probeGateway.mockResolvedValue({ probeGateway.mockResolvedValue({
ok: true, ok: true,
close: null, close: null,
}); });
const { inspectGatewayRestart } = await import("./restart-health.js"); const snapshot = await inspectGatewayRestartWithSnapshot({
const snapshot = await inspectGatewayRestart({ service, port: 18789 }); runtime: { status: "stopped" },
portUsage: {
port: 18789,
status: "busy",
listeners: [{ pid: 9100, commandLine: "openclaw-gateway" }],
hints: [],
},
});
expect(snapshot.healthy).toBe(true); expect(snapshot.healthy).toBe(true);
expect(snapshot.staleGatewayPids).toEqual([]); expect(snapshot.staleGatewayPids).toEqual([]);