forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.test.ts
More file actions
143 lines (117 loc) 路 4.3 KB
/
Copy pathutils.test.ts
File metadata and controls
143 lines (117 loc) 路 4.3 KB
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
import fs from "node:fs";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import { withTempDir } from "./test-helpers/temp-dir.js";
import {
ensureDir,
resolveConfigDir,
resolveHomeDir,
resolveUserPath,
shortenHomeInString,
shortenHomePath,
sleep,
} from "./utils.js";
describe("ensureDir", () => {
it("creates nested directory", async () => {
await withTempDir({ prefix: "openclaw-test-" }, async (tmp) => {
const target = path.join(tmp, "nested", "dir");
await ensureDir(target);
expect(fs.existsSync(target)).toBe(true);
});
});
});
describe("sleep", () => {
it("resolves after delay using fake timers", async () => {
vi.useFakeTimers();
const promise = sleep(1000);
vi.advanceTimersByTime(1000);
await expect(promise).resolves.toBeUndefined();
vi.useRealTimers();
});
});
describe("resolveConfigDir", () => {
it("prefers ~/.openclaw when legacy dir is missing", async () => {
await withTempDir({ prefix: "openclaw-config-dir-" }, async (root) => {
const newDir = path.join(root, ".openclaw");
await fs.promises.mkdir(newDir, { recursive: true });
const resolved = resolveConfigDir({} as NodeJS.ProcessEnv, () => root);
expect(resolved).toBe(newDir);
});
});
it("expands OPENCLAW_STATE_DIR using the provided env", () => {
const env = {
HOME: "/tmp/openclaw-home",
OPENCLAW_STATE_DIR: "~/state",
} as NodeJS.ProcessEnv;
expect(resolveConfigDir(env)).toBe(path.resolve("/tmp/openclaw-home", "state"));
});
it("falls back to the config file directory when only OPENCLAW_CONFIG_PATH is set", () => {
const env = {
HOME: "/tmp/openclaw-home",
OPENCLAW_CONFIG_PATH: "~/profiles/dev/openclaw.json",
} as NodeJS.ProcessEnv;
expect(resolveConfigDir(env)).toBe(path.resolve("/tmp/openclaw-home", "profiles", "dev"));
});
});
describe("resolveHomeDir", () => {
it("prefers OPENCLAW_HOME over HOME", () => {
vi.stubEnv("OPENCLAW_HOME", "/srv/openclaw-home");
vi.stubEnv("HOME", "/home/other");
expect(resolveHomeDir()).toBe(path.resolve("/srv/openclaw-home"));
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");
expect(shortenHomePath(`${path.resolve("/srv/openclaw-home")}/.openclaw/openclaw.json`)).toBe(
"$OPENCLAW_HOME/.openclaw/openclaw.json",
);
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");
expect(
shortenHomeInString(`config: ${path.resolve("/srv/openclaw-home")}/.openclaw/openclaw.json`),
).toBe("config: $OPENCLAW_HOME/.openclaw/openclaw.json");
vi.unstubAllEnvs();
});
});
describe("resolveUserPath", () => {
it("expands ~ to home dir", () => {
expect(resolveUserPath("~", {}, () => "/Users/thoffman")).toBe(path.resolve("/Users/thoffman"));
});
it("expands ~/ to home dir", () => {
expect(resolveUserPath("~/openclaw", {}, () => "/Users/thoffman")).toBe(
path.resolve("/Users/thoffman", "openclaw"),
);
});
it("resolves relative paths", () => {
expect(resolveUserPath("tmp/dir")).toBe(path.resolve("tmp/dir"));
});
it("prefers OPENCLAW_HOME for tilde expansion", () => {
vi.stubEnv("OPENCLAW_HOME", "/srv/openclaw-home");
vi.stubEnv("HOME", "/home/other");
expect(resolveUserPath("~/openclaw")).toBe(path.resolve("/srv/openclaw-home", "openclaw"));
vi.unstubAllEnvs();
});
it("uses the provided env for tilde expansion", () => {
const env = {
HOME: "/tmp/openclaw-home",
OPENCLAW_HOME: "/srv/openclaw-home",
} as NodeJS.ProcessEnv;
expect(resolveUserPath("~/openclaw", env)).toBe(path.resolve("/srv/openclaw-home", "openclaw"));
});
it("keeps blank paths blank", () => {
expect(resolveUserPath("")).toBe("");
expect(resolveUserPath(" ")).toBe("");
});
it("returns empty string for undefined/null input", () => {
expect(resolveUserPath(undefined as unknown as string)).toBe("");
expect(resolveUserPath(null as unknown as string)).toBe("");
});
});