CLI: fix restart-health type checks in tests

This commit is contained in:
Gustavo Madeira Santana 2026-02-23 13:41:26 -05:00
parent d2f25b54ce
commit e07b8698d7
2 changed files with 18 additions and 10 deletions

View File

@ -1,18 +1,27 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { GatewayService } from "../../daemon/service.js";
import type { PortListenerKind, PortUsage } from "../../infra/ports.js";
const inspectPortUsage = vi.hoisted(() => vi.fn());
const classifyPortListener = vi.hoisted(() => vi.fn(() => "gateway"));
const inspectPortUsage = vi.hoisted(() => vi.fn<(port: number) => Promise<PortUsage>>());
const classifyPortListener = vi.hoisted(() =>
vi.fn<(_listener: unknown, _port: number) => PortListenerKind>(() => "gateway"),
);
vi.mock("../../infra/ports.js", () => ({
classifyPortListener: (...args: unknown[]) => classifyPortListener(...args),
classifyPortListener: (listener: unknown, port: number) => classifyPortListener(listener, port),
formatPortDiagnostics: vi.fn(() => []),
inspectPortUsage: (...args: unknown[]) => inspectPortUsage(...args),
inspectPortUsage: (port: number) => inspectPortUsage(port),
}));
describe("inspectGatewayRestart", () => {
beforeEach(() => {
inspectPortUsage.mockReset();
inspectPortUsage.mockResolvedValue({
port: 0,
status: "free",
listeners: [],
hints: [],
});
classifyPortListener.mockReset();
classifyPortListener.mockReturnValue("gateway");
});

View File

@ -61,11 +61,10 @@ export async function inspectGatewayRestart(params: {
)
: [];
const running = runtime.status === "running";
const runtimePid = runtime.pid;
const ownsPort =
runtime.pid != null
? portUsage.listeners.some((listener) =>
listenerOwnedByRuntimePid({ listener, runtimePid: runtime.pid }),
)
runtimePid != null
? portUsage.listeners.some((listener) => listenerOwnedByRuntimePid({ listener, runtimePid }))
: gatewayListeners.length > 0 ||
(portUsage.status === "busy" && portUsage.listeners.length === 0);
const healthy = running && ownsPort;
@ -77,10 +76,10 @@ export async function inspectGatewayRestart(params: {
if (!running) {
return true;
}
if (runtime.pid == null) {
if (runtimePid == null) {
return true;
}
return !listenerOwnedByRuntimePid({ listener, runtimePid: runtime.pid });
return !listenerOwnedByRuntimePid({ listener, runtimePid });
})
.map((listener) => listener.pid as number),
),