File size: 4,378 Bytes
fc93158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | import { spawnSync } from "node:child_process";
import { randomUUID } from "node:crypto";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { PassThrough } from "node:stream";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import {
installLaunchAgent,
readLaunchAgentRuntime,
restartLaunchAgent,
resolveLaunchAgentPlistPath,
uninstallLaunchAgent,
} from "./launchd.js";
import type { GatewayServiceEnv } from "./service-types.js";
const WAIT_INTERVAL_MS = 200;
const WAIT_TIMEOUT_MS = 30_000;
const STARTUP_TIMEOUT_MS = 45_000;
function canRunLaunchdIntegration(): boolean {
if (process.platform !== "darwin") {
return false;
}
if (typeof process.getuid !== "function") {
return false;
}
const domain = `gui/${process.getuid()}`;
const probe = spawnSync("launchctl", ["print", domain], { encoding: "utf8" });
if (probe.error) {
return false;
}
return probe.status === 0;
}
const describeLaunchdIntegration = canRunLaunchdIntegration() ? describe : describe.skip;
async function withTimeout<T>(params: {
run: () => Promise<T>;
timeoutMs: number;
message: string;
}): Promise<T> {
let timer: NodeJS.Timeout | undefined;
try {
return await Promise.race([
params.run(),
new Promise<T>((_, reject) => {
timer = setTimeout(() => reject(new Error(params.message)), params.timeoutMs);
}),
]);
} finally {
if (timer) {
clearTimeout(timer);
}
}
}
async function waitForRunningRuntime(params: {
env: GatewayServiceEnv;
pidNot?: number;
timeoutMs?: number;
}): Promise<{ pid: number }> {
const timeoutMs = params.timeoutMs ?? WAIT_TIMEOUT_MS;
const deadline = Date.now() + timeoutMs;
let lastStatus = "unknown";
let lastPid: number | undefined;
while (Date.now() < deadline) {
const runtime = await readLaunchAgentRuntime(params.env);
lastStatus = runtime.status ?? "unknown";
lastPid = runtime.pid;
if (
runtime.status === "running" &&
typeof runtime.pid === "number" &&
runtime.pid > 1 &&
(params.pidNot === undefined || runtime.pid !== params.pidNot)
) {
return { pid: runtime.pid };
}
await new Promise((resolve) => {
setTimeout(resolve, WAIT_INTERVAL_MS);
});
}
throw new Error(
`Timed out waiting for launchd runtime (status=${lastStatus}, pid=${lastPid ?? "none"})`,
);
}
describeLaunchdIntegration("launchd integration", () => {
let env: GatewayServiceEnv | undefined;
let homeDir = "";
const stdout = new PassThrough();
beforeAll(async () => {
const testId = randomUUID().slice(0, 8);
homeDir = await fs.mkdtemp(path.join(os.tmpdir(), `openclaw-launchd-int-${testId}-`));
env = {
HOME: homeDir,
OPENCLAW_LAUNCHD_LABEL: `ai.openclaw.launchd-int-${testId}`,
OPENCLAW_LOG_PREFIX: `gateway-launchd-int-${testId}`,
};
});
afterAll(async () => {
if (env) {
try {
await uninstallLaunchAgent({ env, stdout });
} catch {
// Best-effort cleanup in case launchctl state already changed.
}
}
if (homeDir) {
await fs.rm(homeDir, { recursive: true, force: true });
}
}, 60_000);
it("restarts launchd service and keeps it running with a new pid", async () => {
if (!env) {
throw new Error("launchd integration env was not initialized");
}
const launchEnv = env;
try {
await withTimeout({
run: async () => {
await installLaunchAgent({
env: launchEnv,
stdout,
programArguments: [process.execPath, "-e", "setInterval(() => {}, 1000);"],
});
await waitForRunningRuntime({ env: launchEnv });
},
timeoutMs: STARTUP_TIMEOUT_MS,
message: "Timed out initializing launchd integration runtime",
});
} catch {
// Best-effort integration check only; skip when launchctl is unstable in CI.
return;
}
const before = await waitForRunningRuntime({ env: launchEnv });
await restartLaunchAgent({ env: launchEnv, stdout });
const after = await waitForRunningRuntime({ env: launchEnv, pidNot: before.pid });
expect(after.pid).toBeGreaterThan(1);
expect(after.pid).not.toBe(before.pid);
await fs.access(resolveLaunchAgentPlistPath(launchEnv));
}, 60_000);
});
|