




























@@ -0,0 +1,76 @@
1+import { spawnSync } from "node:child_process";
2+import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3+import { tmpdir } from "node:os";
4+import { join } from "node:path";
5+import { afterEach, describe, expect, it } from "vitest";
6+7+const helperPath = "scripts/lib/restart-mac-gateway.sh";
8+const restartScriptPath = "scripts/restart-mac.sh";
9+const tempRoots: string[] = [];
10+11+function shellQuote(value: string): string {
12+return `'${value.replaceAll("'", "'\\''")}'`;
13+}
14+15+function runGatewayPortCheck(fakeLsof: string) {
16+const root = mkdtempSync(join(tmpdir(), "openclaw-restart-mac-test-"));
17+tempRoots.push(root);
18+19+const binDir = join(root, "bin");
20+mkdirSync(binDir);
21+const lsofPath = join(binDir, "lsof");
22+writeFileSync(lsofPath, fakeLsof);
23+chmodSync(lsofPath, 0o755);
24+25+return spawnSync(
26+"bash",
27+["-c", `source ${shellQuote(helperPath)}; verify_gateway_port_listening 18789`],
28+{
29+encoding: "utf8",
30+env: {
31+ ...process.env,
32+PATH: `${binDir}:${process.env.PATH ?? ""}`,
33+},
34+},
35+);
36+}
37+38+afterEach(() => {
39+for (const root of tempRoots.splice(0)) {
40+rmSync(root, { force: true, recursive: true });
41+}
42+});
43+44+describe("scripts/restart-mac.sh", () => {
45+it("fails the gateway verification when lsof finds no listener", () => {
46+const result = runGatewayPortCheck("#!/usr/bin/env bash\nexit 1\n");
47+48+expect(result.status).toBe(1);
49+expect(result.stderr).toContain("No process is listening on gateway port 18789.");
50+expect(result.stdout).toBe("");
51+});
52+53+it("prints listener diagnostics when the gateway port is open", () => {
54+const result = runGatewayPortCheck(
55+[
56+"#!/usr/bin/env bash",
57+"printf '%s\\n' 'COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME'",
58+"printf '%s\\n' 'node 12345 user 21u IPv4 0x123 0t0 TCP 127.0.0.1:18789 (LISTEN)'",
59+].join("\n"),
60+);
61+62+expect(result.status).toBe(0);
63+expect(result.stdout).toContain("127.0.0.1:18789 (LISTEN)");
64+expect(result.stderr).toBe("");
65+});
66+67+it("uses a fail-closed gateway port verification helper", () => {
68+const script = readFileSync(restartScriptPath, "utf8");
69+70+expect(script).toContain('source "${ROOT_DIR}/scripts/lib/restart-mac-gateway.sh"');
71+expect(script).toContain(
72+'run_step "verify gateway port ${GATEWAY_PORT} (unsigned)" verify_gateway_port_listening "${GATEWAY_PORT}"',
73+);
74+expect(script).not.toContain("lsof -iTCP:${GATEWAY_PORT} -sTCP:LISTEN | head -n 5 || true");
75+});
76+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。