Spaces:
Sleeping
Sleeping
File size: 10,464 Bytes
fb4d8fe | 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { PassThrough } from "node:stream";
import { describe, expect, it } from "vitest";
import {
installLaunchAgent,
isLaunchAgentListed,
parseLaunchctlPrint,
repairLaunchAgentBootstrap,
resolveLaunchAgentPlistPath,
} from "./launchd.js";
async function withLaunchctlStub(
options: { listOutput?: string },
run: (context: { env: Record<string, string | undefined>; logPath: string }) => Promise<void>,
) {
const originalPath = process.env.PATH;
const originalLogPath = process.env.OPENCLAW_TEST_LAUNCHCTL_LOG;
const originalListOutput = process.env.OPENCLAW_TEST_LAUNCHCTL_LIST_OUTPUT;
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-launchctl-test-"));
try {
const binDir = path.join(tmpDir, "bin");
const homeDir = path.join(tmpDir, "home");
const logPath = path.join(tmpDir, "launchctl.log");
await fs.mkdir(binDir, { recursive: true });
await fs.mkdir(homeDir, { recursive: true });
const stubJsPath = path.join(binDir, "launchctl.js");
await fs.writeFile(
stubJsPath,
[
'import fs from "node:fs";',
"const args = process.argv.slice(2);",
"const logPath = process.env.OPENCLAW_TEST_LAUNCHCTL_LOG;",
"if (logPath) {",
' fs.appendFileSync(logPath, JSON.stringify(args) + "\\n", "utf8");',
"}",
'if (args[0] === "list") {',
' const output = process.env.OPENCLAW_TEST_LAUNCHCTL_LIST_OUTPUT || "";',
" process.stdout.write(output);",
"}",
"process.exit(0);",
"",
].join("\n"),
"utf8",
);
if (process.platform === "win32") {
await fs.writeFile(
path.join(binDir, "launchctl.cmd"),
`@echo off\r\nnode "%~dp0\\launchctl.js" %*\r\n`,
"utf8",
);
} else {
const shPath = path.join(binDir, "launchctl");
await fs.writeFile(shPath, `#!/bin/sh\nnode "$(dirname "$0")/launchctl.js" "$@"\n`, "utf8");
await fs.chmod(shPath, 0o755);
}
process.env.OPENCLAW_TEST_LAUNCHCTL_LOG = logPath;
process.env.OPENCLAW_TEST_LAUNCHCTL_LIST_OUTPUT = options.listOutput ?? "";
process.env.PATH = `${binDir}${path.delimiter}${originalPath ?? ""}`;
await run({
env: {
HOME: homeDir,
OPENCLAW_PROFILE: "default",
},
logPath,
});
} finally {
process.env.PATH = originalPath;
if (originalLogPath === undefined) {
delete process.env.OPENCLAW_TEST_LAUNCHCTL_LOG;
} else {
process.env.OPENCLAW_TEST_LAUNCHCTL_LOG = originalLogPath;
}
if (originalListOutput === undefined) {
delete process.env.OPENCLAW_TEST_LAUNCHCTL_LIST_OUTPUT;
} else {
process.env.OPENCLAW_TEST_LAUNCHCTL_LIST_OUTPUT = originalListOutput;
}
await fs.rm(tmpDir, { recursive: true, force: true });
}
}
describe("launchd runtime parsing", () => {
it("parses state, pid, and exit status", () => {
const output = [
"state = running",
"pid = 4242",
"last exit status = 1",
"last exit reason = exited",
].join("\n");
expect(parseLaunchctlPrint(output)).toEqual({
state: "running",
pid: 4242,
lastExitStatus: 1,
lastExitReason: "exited",
});
});
});
describe("launchctl list detection", () => {
it("detects the resolved label in launchctl list", async () => {
await withLaunchctlStub({ listOutput: "123 0 ai.openclaw.gateway\n" }, async ({ env }) => {
const listed = await isLaunchAgentListed({ env });
expect(listed).toBe(true);
});
});
it("returns false when the label is missing", async () => {
await withLaunchctlStub({ listOutput: "123 0 com.other.service\n" }, async ({ env }) => {
const listed = await isLaunchAgentListed({ env });
expect(listed).toBe(false);
});
});
});
describe("launchd bootstrap repair", () => {
it("bootstraps and kickstarts the resolved label", async () => {
await withLaunchctlStub({}, async ({ env, logPath }) => {
const repair = await repairLaunchAgentBootstrap({ env });
expect(repair.ok).toBe(true);
const calls = (await fs.readFile(logPath, "utf8"))
.split("\n")
.filter(Boolean)
.map((line) => JSON.parse(line) as string[]);
const domain = typeof process.getuid === "function" ? `gui/${process.getuid()}` : "gui/501";
const label = "ai.openclaw.gateway";
const plistPath = resolveLaunchAgentPlistPath(env);
expect(calls).toContainEqual(["bootstrap", domain, plistPath]);
expect(calls).toContainEqual(["kickstart", "-k", `${domain}/${label}`]);
});
});
});
describe("launchd install", () => {
it("enables service before bootstrap (clears persisted disabled state)", async () => {
const originalPath = process.env.PATH;
const originalLogPath = process.env.OPENCLAW_TEST_LAUNCHCTL_LOG;
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-launchctl-test-"));
try {
const binDir = path.join(tmpDir, "bin");
const homeDir = path.join(tmpDir, "home");
const logPath = path.join(tmpDir, "launchctl.log");
await fs.mkdir(binDir, { recursive: true });
await fs.mkdir(homeDir, { recursive: true });
const stubJsPath = path.join(binDir, "launchctl.js");
await fs.writeFile(
stubJsPath,
[
'import fs from "node:fs";',
"const logPath = process.env.OPENCLAW_TEST_LAUNCHCTL_LOG;",
"if (logPath) {",
' fs.appendFileSync(logPath, JSON.stringify(process.argv.slice(2)) + "\\n", "utf8");',
"}",
"process.exit(0);",
"",
].join("\n"),
"utf8",
);
if (process.platform === "win32") {
await fs.writeFile(
path.join(binDir, "launchctl.cmd"),
`@echo off\r\nnode "%~dp0\\launchctl.js" %*\r\n`,
"utf8",
);
} else {
const shPath = path.join(binDir, "launchctl");
await fs.writeFile(shPath, `#!/bin/sh\nnode "$(dirname "$0")/launchctl.js" "$@"\n`, "utf8");
await fs.chmod(shPath, 0o755);
}
process.env.OPENCLAW_TEST_LAUNCHCTL_LOG = logPath;
process.env.PATH = `${binDir}${path.delimiter}${originalPath ?? ""}`;
const env: Record<string, string | undefined> = {
HOME: homeDir,
OPENCLAW_PROFILE: "default",
};
await installLaunchAgent({
env,
stdout: new PassThrough(),
programArguments: ["node", "-e", "process.exit(0)"],
});
const calls = (await fs.readFile(logPath, "utf8"))
.split("\n")
.filter(Boolean)
.map((line) => JSON.parse(line) as string[]);
const domain = typeof process.getuid === "function" ? `gui/${process.getuid()}` : "gui/501";
const label = "ai.openclaw.gateway";
const plistPath = resolveLaunchAgentPlistPath(env);
const serviceId = `${domain}/${label}`;
const enableCalls = calls.filter((c) => c[0] === "enable" && c[1] === serviceId);
expect(enableCalls).toHaveLength(1);
const enableIndex = calls.findIndex((c) => c[0] === "enable" && c[1] === serviceId);
const bootstrapIndex = calls.findIndex(
(c) => c[0] === "bootstrap" && c[1] === domain && c[2] === plistPath,
);
expect(enableIndex).toBeGreaterThanOrEqual(0);
expect(bootstrapIndex).toBeGreaterThanOrEqual(0);
expect(enableIndex).toBeLessThan(bootstrapIndex);
} finally {
process.env.PATH = originalPath;
if (originalLogPath === undefined) {
delete process.env.OPENCLAW_TEST_LAUNCHCTL_LOG;
} else {
process.env.OPENCLAW_TEST_LAUNCHCTL_LOG = originalLogPath;
}
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
});
describe("resolveLaunchAgentPlistPath", () => {
it("uses default label when OPENCLAW_PROFILE is default", () => {
const env = { HOME: "/Users/test", OPENCLAW_PROFILE: "default" };
expect(resolveLaunchAgentPlistPath(env)).toBe(
"/Users/test/Library/LaunchAgents/ai.openclaw.gateway.plist",
);
});
it("uses default label when OPENCLAW_PROFILE is unset", () => {
const env = { HOME: "/Users/test" };
expect(resolveLaunchAgentPlistPath(env)).toBe(
"/Users/test/Library/LaunchAgents/ai.openclaw.gateway.plist",
);
});
it("uses profile-specific label when OPENCLAW_PROFILE is set to a custom value", () => {
const env = { HOME: "/Users/test", OPENCLAW_PROFILE: "jbphoenix" };
expect(resolveLaunchAgentPlistPath(env)).toBe(
"/Users/test/Library/LaunchAgents/ai.openclaw.jbphoenix.plist",
);
});
it("prefers OPENCLAW_LAUNCHD_LABEL over OPENCLAW_PROFILE", () => {
const env = {
HOME: "/Users/test",
OPENCLAW_PROFILE: "jbphoenix",
OPENCLAW_LAUNCHD_LABEL: "com.custom.label",
};
expect(resolveLaunchAgentPlistPath(env)).toBe(
"/Users/test/Library/LaunchAgents/com.custom.label.plist",
);
});
it("trims whitespace from OPENCLAW_LAUNCHD_LABEL", () => {
const env = {
HOME: "/Users/test",
OPENCLAW_LAUNCHD_LABEL: " com.custom.label ",
};
expect(resolveLaunchAgentPlistPath(env)).toBe(
"/Users/test/Library/LaunchAgents/com.custom.label.plist",
);
});
it("ignores empty OPENCLAW_LAUNCHD_LABEL and falls back to profile", () => {
const env = {
HOME: "/Users/test",
OPENCLAW_PROFILE: "myprofile",
OPENCLAW_LAUNCHD_LABEL: " ",
};
expect(resolveLaunchAgentPlistPath(env)).toBe(
"/Users/test/Library/LaunchAgents/ai.openclaw.myprofile.plist",
);
});
it("handles case-insensitive 'Default' profile", () => {
const env = { HOME: "/Users/test", OPENCLAW_PROFILE: "Default" };
expect(resolveLaunchAgentPlistPath(env)).toBe(
"/Users/test/Library/LaunchAgents/ai.openclaw.gateway.plist",
);
});
it("handles case-insensitive 'DEFAULT' profile", () => {
const env = { HOME: "/Users/test", OPENCLAW_PROFILE: "DEFAULT" };
expect(resolveLaunchAgentPlistPath(env)).toBe(
"/Users/test/Library/LaunchAgents/ai.openclaw.gateway.plist",
);
});
it("trims whitespace from OPENCLAW_PROFILE", () => {
const env = { HOME: "/Users/test", OPENCLAW_PROFILE: " myprofile " };
expect(resolveLaunchAgentPlistPath(env)).toBe(
"/Users/test/Library/LaunchAgents/ai.openclaw.myprofile.plist",
);
});
});
|