



























@@ -253,6 +253,97 @@ describeUnix("inspectPortUsage", () => {
253253});
254254});
255255256+it("deduplicates repeated lsof listener records for one process address", async () => {
257+runCommandWithTimeoutMock.mockImplementation(async (argv: string[]) => {
258+const command = argv[0];
259+if (typeof command !== "string") {
260+return { stdout: "", stderr: "", code: 1 };
261+}
262+if (command.includes("lsof")) {
263+return {
264+stdout: "p111\ncnode\nnTCP *:18789 (LISTEN)\nnTCP *:18789 (LISTEN)\n",
265+stderr: "",
266+code: 0,
267+};
268+}
269+if (command === "ps") {
270+if (argv.includes("command=")) {
271+return {
272+stdout: "node /tmp/openclaw/dist/index.js gateway run\n",
273+stderr: "",
274+code: 0,
275+};
276+}
277+if (argv.includes("user=")) {
278+return { stdout: "tester\n", stderr: "", code: 0 };
279+}
280+if (argv.includes("ppid=")) {
281+return { stdout: "1\n", stderr: "", code: 0 };
282+}
283+}
284+return { stdout: "", stderr: "", code: 1 };
285+});
286+287+const result = await inspectPortUsage(18789);
288+289+expect(result.listeners).toHaveLength(1);
290+expect(result.listeners[0]).toMatchObject({
291+pid: 111,
292+address: "TCP *:18789 (LISTEN)",
293+});
294+});
295+296+it("reports multiple lsof socket records for one process", async () => {
297+runCommandWithTimeoutMock.mockImplementation(async (argv: string[]) => {
298+const command = argv[0];
299+if (typeof command !== "string") {
300+return { stdout: "", stderr: "", code: 1 };
301+}
302+if (command.includes("lsof")) {
303+return {
304+stdout:
305+"p111\ncnode\n" +
306+"nTCP 127.0.0.1:50123->127.0.0.1:18789 (ESTABLISHED)\n" +
307+"nTCP 127.0.0.1:50124->127.0.0.1:18789 (ESTABLISHED)\n",
308+stderr: "",
309+code: 0,
310+};
311+}
312+if (command === "ps") {
313+if (argv.includes("command=")) {
314+return {
315+stdout: "node /tmp/newer-openclaw/dist/index.js logs --follow\n",
316+stderr: "",
317+code: 0,
318+};
319+}
320+if (argv.includes("user=")) {
321+return { stdout: "tester\n", stderr: "", code: 0 };
322+}
323+if (argv.includes("ppid=")) {
324+return { stdout: "1\n", stderr: "", code: 0 };
325+}
326+}
327+return { stdout: "", stderr: "", code: 1 };
328+});
329+330+const result = await inspectPortConnections(18789);
331+332+expect(result.connections).toHaveLength(2);
333+expect(result.connections).toEqual([
334+expect.objectContaining({
335+pid: 111,
336+direction: "client",
337+address: "TCP 127.0.0.1:50123->127.0.0.1:18789 (ESTABLISHED)",
338+}),
339+expect.objectContaining({
340+pid: 111,
341+direction: "client",
342+address: "TCP 127.0.0.1:50124->127.0.0.1:18789 (ESTABLISHED)",
343+}),
344+]);
345+});
346+256347it("falls back to ss for established gateway client connections", async () => {
257348runCommandWithTimeoutMock.mockImplementation(async (argv: string[]) => {
258349const command = argv[0];
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。