openclaw/src/daemon/service-env.test.ts
Peter Steinberger 47d1f23d55 fix(daemon): include HOME in service env (#1214)
Thanks @ameno-.

Co-authored-by: Ameno Osman <ameno.osman13@gmail.com>
2026-01-19 08:39:12 +00:00

92 lines
2.8 KiB
TypeScript

import path from "node:path";
import { describe, expect, it } from "vitest";
import {
buildMinimalServicePath,
buildNodeServiceEnvironment,
buildServiceEnvironment,
} from "./service-env.js";
describe("buildMinimalServicePath", () => {
it("includes Homebrew + system dirs on macOS", () => {
const result = buildMinimalServicePath({
platform: "darwin",
});
const parts = result.split(path.delimiter);
expect(parts).toContain("/opt/homebrew/bin");
expect(parts).toContain("/usr/local/bin");
expect(parts).toContain("/usr/bin");
expect(parts).toContain("/bin");
});
it("returns PATH as-is on Windows", () => {
const result = buildMinimalServicePath({
env: { PATH: "C:\\\\Windows\\\\System32" },
platform: "win32",
});
expect(result).toBe("C:\\\\Windows\\\\System32");
});
it("includes extra directories when provided", () => {
const result = buildMinimalServicePath({
platform: "linux",
extraDirs: ["/custom/tools"],
});
expect(result.split(path.delimiter)).toContain("/custom/tools");
});
it("deduplicates directories", () => {
const result = buildMinimalServicePath({
platform: "linux",
extraDirs: ["/usr/bin"],
});
const parts = result.split(path.delimiter);
const unique = [...new Set(parts)];
expect(parts.length).toBe(unique.length);
});
});
describe("buildServiceEnvironment", () => {
it("sets minimal PATH and gateway vars", () => {
const env = buildServiceEnvironment({
env: { HOME: "/home/user" },
port: 18789,
token: "secret",
});
expect(env.HOME).toBe("/home/user");
if (process.platform === "win32") {
expect(env.PATH).toBe("");
} else {
expect(env.PATH).toContain("/usr/bin");
}
expect(env.CLAWDBOT_GATEWAY_PORT).toBe("18789");
expect(env.CLAWDBOT_GATEWAY_TOKEN).toBe("secret");
expect(env.CLAWDBOT_SERVICE_MARKER).toBe("clawdbot");
expect(env.CLAWDBOT_SERVICE_KIND).toBe("gateway");
expect(typeof env.CLAWDBOT_SERVICE_VERSION).toBe("string");
expect(env.CLAWDBOT_SYSTEMD_UNIT).toBe("clawdbot-gateway.service");
if (process.platform === "darwin") {
expect(env.CLAWDBOT_LAUNCHD_LABEL).toBe("com.clawdbot.gateway");
}
});
it("uses profile-specific unit and label", () => {
const env = buildServiceEnvironment({
env: { HOME: "/home/user", CLAWDBOT_PROFILE: "work" },
port: 18789,
});
expect(env.CLAWDBOT_SYSTEMD_UNIT).toBe("clawdbot-gateway-work.service");
if (process.platform === "darwin") {
expect(env.CLAWDBOT_LAUNCHD_LABEL).toBe("com.clawdbot.work");
}
});
});
describe("buildNodeServiceEnvironment", () => {
it("passes through HOME for node services", () => {
const env = buildNodeServiceEnvironment({
env: { HOME: "/home/user" },
});
expect(env.HOME).toBe("/home/user");
});
});