mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-02 23:10:24 +00:00
perf(test): fold ports + terminal note suites
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
import net from "node:net";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const runCommandWithTimeoutMock = vi.fn();
|
||||
|
||||
vi.mock("../process/exec.js", () => ({
|
||||
runCommandWithTimeout: (...args: unknown[]) => runCommandWithTimeoutMock(...args),
|
||||
}));
|
||||
|
||||
const describeUnix = process.platform === "win32" ? describe.skip : describe;
|
||||
|
||||
describeUnix("inspectPortUsage", () => {
|
||||
beforeEach(() => {
|
||||
runCommandWithTimeoutMock.mockReset();
|
||||
});
|
||||
|
||||
it("reports busy when lsof is missing but loopback listener exists", async () => {
|
||||
const server = net.createServer();
|
||||
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
const port = (server.address() as net.AddressInfo).port;
|
||||
|
||||
runCommandWithTimeoutMock.mockRejectedValueOnce(
|
||||
Object.assign(new Error("spawn lsof ENOENT"), { code: "ENOENT" }),
|
||||
);
|
||||
|
||||
try {
|
||||
const { inspectPortUsage } = await import("./ports-inspect.js");
|
||||
const result = await inspectPortUsage(port);
|
||||
expect(result.status).toBe("busy");
|
||||
expect(result.errors?.some((err) => err.includes("ENOENT"))).toBe(true);
|
||||
} finally {
|
||||
server.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,12 @@
|
||||
import net from "node:net";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const runCommandWithTimeoutMock = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock("../process/exec.js", () => ({
|
||||
runCommandWithTimeout: (...args: unknown[]) => runCommandWithTimeoutMock(...args),
|
||||
}));
|
||||
import { inspectPortUsage } from "./ports-inspect.js";
|
||||
import {
|
||||
buildPortHints,
|
||||
classifyPortListener,
|
||||
@@ -9,6 +16,8 @@ import {
|
||||
PortInUseError,
|
||||
} from "./ports.js";
|
||||
|
||||
const describeUnix = process.platform === "win32" ? describe.skip : describe;
|
||||
|
||||
describe("ports helpers", () => {
|
||||
it("ensurePortAvailable rejects when port busy", async () => {
|
||||
const server = net.createServer();
|
||||
@@ -58,3 +67,27 @@ describe("ports helpers", () => {
|
||||
expect(lines.some((line) => line.includes("SSH tunnel"))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describeUnix("inspectPortUsage", () => {
|
||||
beforeEach(() => {
|
||||
runCommandWithTimeoutMock.mockReset();
|
||||
});
|
||||
|
||||
it("reports busy when lsof is missing but loopback listener exists", async () => {
|
||||
const server = net.createServer();
|
||||
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
const port = (server.address() as net.AddressInfo).port;
|
||||
|
||||
runCommandWithTimeoutMock.mockRejectedValueOnce(
|
||||
Object.assign(new Error("spawn lsof ENOENT"), { code: "ENOENT" }),
|
||||
);
|
||||
|
||||
try {
|
||||
const result = await inspectPortUsage(port);
|
||||
expect(result.status).toBe("busy");
|
||||
expect(result.errors?.some((err) => err.includes("ENOENT"))).toBe(true);
|
||||
} finally {
|
||||
server.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user