test: scope utility home env

This commit is contained in:
Shakker
2026-06-04 23:05:56 +01:00
parent f3128f92d0
commit 3f6268ebd7
+9 -24
View File
@@ -4,6 +4,7 @@ import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import { MAX_TIMER_TIMEOUT_MS } from "./shared/number-coercion.js";
import { withTempDir } from "./test-helpers/temp-dir.js";
import { withEnv } from "./test-utils/env.js";
import {
ensureDir,
resolveConfigDir,
@@ -84,43 +85,31 @@ describe("resolveConfigDir", () => {
describe("resolveHomeDir", () => {
it("prefers OPENCLAW_HOME over HOME", () => {
vi.stubEnv("OPENCLAW_HOME", "/srv/openclaw-home");
vi.stubEnv("HOME", "/home/other");
try {
withEnv({ OPENCLAW_HOME: "/srv/openclaw-home", HOME: "/home/other" }, () => {
expect(resolveHomeDir()).toBe(path.resolve("/srv/openclaw-home"));
} finally {
vi.unstubAllEnvs();
}
});
});
});
describe("shortenHomePath", () => {
it("uses $OPENCLAW_HOME prefix when OPENCLAW_HOME is set", () => {
vi.stubEnv("OPENCLAW_HOME", "/srv/openclaw-home");
vi.stubEnv("HOME", "/home/other");
try {
withEnv({ OPENCLAW_HOME: "/srv/openclaw-home", HOME: "/home/other" }, () => {
expect(shortenHomePath(`${path.resolve("/srv/openclaw-home")}/.openclaw/openclaw.json`)).toBe(
"$OPENCLAW_HOME/.openclaw/openclaw.json",
);
} finally {
vi.unstubAllEnvs();
}
});
});
});
describe("shortenHomeInString", () => {
it("uses $OPENCLAW_HOME replacement when OPENCLAW_HOME is set", () => {
vi.stubEnv("OPENCLAW_HOME", "/srv/openclaw-home");
vi.stubEnv("HOME", "/home/other");
try {
withEnv({ OPENCLAW_HOME: "/srv/openclaw-home", HOME: "/home/other" }, () => {
expect(
shortenHomeInString(
`config: ${path.resolve("/srv/openclaw-home")}/.openclaw/openclaw.json`,
),
).toBe("config: $OPENCLAW_HOME/.openclaw/openclaw.json");
} finally {
vi.unstubAllEnvs();
}
});
});
});
@@ -140,13 +129,9 @@ describe("resolveUserPath", () => {
});
it("prefers OPENCLAW_HOME for tilde expansion", () => {
vi.stubEnv("OPENCLAW_HOME", "/srv/openclaw-home");
vi.stubEnv("HOME", "/home/other");
try {
withEnv({ OPENCLAW_HOME: "/srv/openclaw-home", HOME: "/home/other" }, () => {
expect(resolveUserPath("~/openclaw")).toBe(path.resolve("/srv/openclaw-home", "openclaw"));
} finally {
vi.unstubAllEnvs();
}
});
});
it("uses the provided env for tilde expansion", () => {