Repository navigation
Expand file tree
/
Copy pathutils.test.ts
More file actions
225 lines (191 loc) 路 7.26 KB
/
Copy pathutils.test.ts
File metadata and controls
225 lines (191 loc) 路 7.26 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
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
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { describe, expect, it, vi } from "vitest";
import {
assertWebChannel,
CONFIG_DIR,
ensureDir,
jidToE164,
normalizeE164,
normalizePath,
resolveConfigDir,
resolveHomeDir,
resolveJidToE164,
resolveUserPath,
shortenHomeInString,
shortenHomePath,
sleep,
toWhatsappJid,
withWhatsAppPrefix,
} from "./utils.js";
describe("normalizePath", () => {
it("adds leading slash when missing", () => {
expect(normalizePath("foo")).toBe("/foo");
});
it("keeps existing slash", () => {
expect(normalizePath("/bar")).toBe("/bar");
});
});
describe("withWhatsAppPrefix", () => {
it("adds whatsapp prefix", () => {
expect(withWhatsAppPrefix("+1555")).toBe("whatsapp:+1555");
});
it("leaves prefixed intact", () => {
expect(withWhatsAppPrefix("whatsapp:+1555")).toBe("whatsapp:+1555");
});
});
describe("ensureDir", () => {
it("creates nested directory", async () => {
const tmp = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-test-"));
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("assertWebChannel", () => {
it("accepts valid channel", () => {
expect(() => assertWebChannel("web")).not.toThrow();
});
it("throws for invalid channel", () => {
expect(() => assertWebChannel("bad" as string)).toThrow();
});
});
describe("normalizeE164 & toWhatsappJid", () => {
it("strips formatting and prefixes", () => {
expect(normalizeE164("whatsapp:(555) 123-4567")).toBe("+5551234567");
expect(toWhatsappJid("whatsapp:+555 123 4567")).toBe("5551234567@s.whatsapp.net");
});
it("preserves existing JIDs", () => {
expect(toWhatsappJid("123456789-987654321@g.us")).toBe("123456789-987654321@g.us");
expect(toWhatsappJid("whatsapp:123456789-987654321@g.us")).toBe("123456789-987654321@g.us");
expect(toWhatsappJid("1555123@s.whatsapp.net")).toBe("1555123@s.whatsapp.net");
});
});
describe("jidToE164", () => {
it("maps @lid using reverse mapping file", () => {
const mappingPath = path.join(CONFIG_DIR, "credentials", "lid-mapping-123_reverse.json");
const original = fs.readFileSync;
const spy = vi.spyOn(fs, "readFileSync").mockImplementation((...args) => {
if (args[0] === mappingPath) {
return `"5551234"`;
}
return original(...args);
});
expect(jidToE164("123@lid")).toBe("+5551234");
spy.mockRestore();
});
it("maps @lid from authDir mapping files", () => {
const authDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-"));
const mappingPath = path.join(authDir, "lid-mapping-456_reverse.json");
fs.writeFileSync(mappingPath, JSON.stringify("5559876"));
expect(jidToE164("456@lid", { authDir })).toBe("+5559876");
fs.rmSync(authDir, { recursive: true, force: true });
});
it("maps @hosted.lid from authDir mapping files", () => {
const authDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-"));
const mappingPath = path.join(authDir, "lid-mapping-789_reverse.json");
fs.writeFileSync(mappingPath, JSON.stringify(4440001));
expect(jidToE164("789@hosted.lid", { authDir })).toBe("+4440001");
fs.rmSync(authDir, { recursive: true, force: true });
});
it("accepts hosted PN JIDs", () => {
expect(jidToE164("1555000:2@hosted")).toBe("+1555000");
});
it("falls back through lidMappingDirs in order", () => {
const first = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-lid-a-"));
const second = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-lid-b-"));
const mappingPath = path.join(second, "lid-mapping-321_reverse.json");
fs.writeFileSync(mappingPath, JSON.stringify("123321"));
expect(jidToE164("321@lid", { lidMappingDirs: [first, second] })).toBe("+123321");
fs.rmSync(first, { recursive: true, force: true });
fs.rmSync(second, { recursive: true, force: true });
});
});
describe("resolveConfigDir", () => {
it("prefers ~/.openclaw when legacy dir is missing", async () => {
const root = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-config-dir-"));
try {
const newDir = path.join(root, ".openclaw");
await fs.promises.mkdir(newDir, { recursive: true });
const resolved = resolveConfigDir({} as NodeJS.ProcessEnv, () => root);
expect(resolved).toBe(newDir);
} finally {
await fs.promises.rm(root, { recursive: true, force: true });
}
});
});
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("resolveJidToE164", () => {
it("resolves @lid via lidLookup when mapping file is missing", async () => {
const lidLookup = {
getPNForLID: vi.fn().mockResolvedValue("777:0@s.whatsapp.net"),
};
await expect(resolveJidToE164("777@lid", { lidLookup })).resolves.toBe("+777");
expect(lidLookup.getPNForLID).toHaveBeenCalledWith("777@lid");
});
it("skips lidLookup for non-lid JIDs", async () => {
const lidLookup = {
getPNForLID: vi.fn().mockResolvedValue("888:0@s.whatsapp.net"),
};
await expect(resolveJidToE164("888@s.whatsapp.net", { lidLookup })).resolves.toBe("+888");
expect(lidLookup.getPNForLID).not.toHaveBeenCalled();
});
});
describe("resolveUserPath", () => {
it("expands ~ to home dir", () => {
expect(resolveUserPath("~")).toBe(path.resolve(os.homedir()));
});
it("expands ~/ to home dir", () => {
expect(resolveUserPath("~/openclaw")).toBe(path.resolve(os.homedir(), "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("keeps blank paths blank", () => {
expect(resolveUserPath("")).toBe("");
expect(resolveUserPath(" ")).toBe("");
});
});