From 0a73328053c459b3e5c0e62b8496926f04538a40 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 7 Mar 2026 17:01:31 +0000 Subject: [PATCH] refactor(cli): dedupe restart health probe setup tests --- src/cli/daemon-cli/restart-health.test.ts | 52 ++++++++++------------- 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/src/cli/daemon-cli/restart-health.test.ts b/src/cli/daemon-cli/restart-health.test.ts index 6e5d42cf19d..88f8aa524c6 100644 --- a/src/cli/daemon-cli/restart-health.test.ts +++ b/src/cli/daemon-cli/restart-health.test.ts @@ -46,6 +46,26 @@ async function inspectUnknownListenerFallback(params: { }); } +async function inspectAmbiguousOwnershipWithProbe( + probeResult: Awaited>, +) { + 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"); + probeGateway.mockResolvedValue(probeResult); + + const { inspectGatewayRestart } = await import("./restart-health.js"); + return inspectGatewayRestart({ service, port: 18789 }); +} + describe("inspectGatewayRestart", () => { beforeEach(() => { inspectPortUsage.mockReset(); @@ -159,25 +179,11 @@ describe("inspectGatewayRestart", () => { }); it("uses a local gateway probe when ownership is ambiguous", async () => { - 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"); - probeGateway.mockResolvedValue({ + const snapshot = await inspectAmbiguousOwnershipWithProbe({ ok: true, close: null, }); - const { inspectGatewayRestart } = await import("./restart-health.js"); - const snapshot = await inspectGatewayRestart({ service, port: 18789 }); - expect(snapshot.healthy).toBe(true); expect(probeGateway).toHaveBeenCalledWith( expect.objectContaining({ url: "ws://127.0.0.1:18789" }), @@ -185,25 +191,11 @@ describe("inspectGatewayRestart", () => { }); it("treats auth-closed probe as healthy gateway reachability", async () => { - 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"); - probeGateway.mockResolvedValue({ + const snapshot = await inspectAmbiguousOwnershipWithProbe({ ok: false, close: { code: 1008, reason: "auth required" }, }); - const { inspectGatewayRestart } = await import("./restart-health.js"); - const snapshot = await inspectGatewayRestart({ service, port: 18789 }); - expect(snapshot.healthy).toBe(true); }); });