@xiaozhi-client/cli 1.9.4-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -0
- package/package.json +31 -0
- package/project.json +75 -0
- package/src/Constants.ts +105 -0
- package/src/Container.ts +212 -0
- package/src/Types.ts +79 -0
- package/src/commands/CommandHandlerFactory.ts +98 -0
- package/src/commands/ConfigCommandHandler.ts +279 -0
- package/src/commands/EndpointCommandHandler.ts +158 -0
- package/src/commands/McpCommandHandler.ts +778 -0
- package/src/commands/ProjectCommandHandler.ts +254 -0
- package/src/commands/ServiceCommandHandler.ts +182 -0
- package/src/commands/__tests__/CommandHandlerFactory.test.ts +323 -0
- package/src/commands/__tests__/CommandRegistry.test.ts +287 -0
- package/src/commands/__tests__/ConfigCommandHandler.test.ts +844 -0
- package/src/commands/__tests__/EndpointCommandHandler.test.ts +426 -0
- package/src/commands/__tests__/McpCommandHandler.test.ts +753 -0
- package/src/commands/__tests__/ProjectCommandHandler.test.ts +230 -0
- package/src/commands/__tests__/ServiceCommands.integration.test.ts +408 -0
- package/src/commands/index.ts +351 -0
- package/src/errors/ErrorHandlers.ts +141 -0
- package/src/errors/ErrorMessages.ts +121 -0
- package/src/errors/__tests__/index.test.ts +186 -0
- package/src/errors/index.ts +163 -0
- package/src/global.d.ts +19 -0
- package/src/index.ts +53 -0
- package/src/interfaces/Command.ts +128 -0
- package/src/interfaces/CommandTypes.ts +95 -0
- package/src/interfaces/Config.ts +25 -0
- package/src/interfaces/Service.ts +99 -0
- package/src/services/DaemonManager.ts +318 -0
- package/src/services/ProcessManager.ts +235 -0
- package/src/services/ServiceManager.ts +319 -0
- package/src/services/TemplateManager.ts +382 -0
- package/src/services/__tests__/DaemonManager.test.ts +378 -0
- package/src/services/__tests__/DaemonMode.integration.test.ts +321 -0
- package/src/services/__tests__/ProcessManager.test.ts +296 -0
- package/src/services/__tests__/ServiceManager.test.ts +774 -0
- package/src/services/__tests__/TemplateManager.test.ts +337 -0
- package/src/types/backend.d.ts +48 -0
- package/src/utils/FileUtils.ts +320 -0
- package/src/utils/FormatUtils.ts +198 -0
- package/src/utils/PathUtils.ts +255 -0
- package/src/utils/PlatformUtils.ts +217 -0
- package/src/utils/Validation.ts +274 -0
- package/src/utils/VersionUtils.ts +141 -0
- package/src/utils/__tests__/FileUtils.test.ts +728 -0
- package/src/utils/__tests__/FormatUtils.test.ts +243 -0
- package/src/utils/__tests__/PathUtils.test.ts +1165 -0
- package/src/utils/__tests__/PlatformUtils.test.ts +723 -0
- package/src/utils/__tests__/Validation.test.ts +560 -0
- package/src/utils/__tests__/VersionUtils.test.ts +410 -0
- package/tsconfig.json +32 -0
- package/tsup.config.ts +100 -0
- package/vitest.config.ts +97 -0
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 版本管理工具单元测试
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
9
|
+
import { FileError } from "../../errors/index";
|
|
10
|
+
import { VersionUtils } from "../VersionUtils";
|
|
11
|
+
|
|
12
|
+
// Mock fileURLToPath
|
|
13
|
+
vi.mock("node:url");
|
|
14
|
+
const mockedFileURLToPath = vi.mocked(fileURLToPath);
|
|
15
|
+
|
|
16
|
+
// Mock path module
|
|
17
|
+
vi.mock("node:path");
|
|
18
|
+
const mockedPath = vi.mocked(path);
|
|
19
|
+
|
|
20
|
+
// Mock fs module
|
|
21
|
+
vi.mock("node:fs");
|
|
22
|
+
const mockedFs = vi.mocked(fs);
|
|
23
|
+
|
|
24
|
+
describe("VersionUtils", () => {
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
vi.clearAllMocks();
|
|
27
|
+
// Clear static cache
|
|
28
|
+
VersionUtils.clearCache();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
vi.restoreAllMocks();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("获取版本号", () => {
|
|
36
|
+
it("缓存可用时应返回缓存的版本号", () => {
|
|
37
|
+
// Set cache directly
|
|
38
|
+
VersionUtils.clearCache();
|
|
39
|
+
(VersionUtils as any).cachedVersion = "1.0.0";
|
|
40
|
+
|
|
41
|
+
const result = VersionUtils.getVersion();
|
|
42
|
+
|
|
43
|
+
expect(result).toBe("1.0.0");
|
|
44
|
+
expect(mockedFs.existsSync).not.toHaveBeenCalled();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("应从dist目录中的package.json读取版本号", () => {
|
|
48
|
+
const mockPackageJson = { version: "1.2.3", name: "test-package" };
|
|
49
|
+
|
|
50
|
+
// Mock fileURLToPath
|
|
51
|
+
mockedFileURLToPath.mockReturnValue("/dist/cli/utils/VersionUtils.js");
|
|
52
|
+
|
|
53
|
+
mockedPath.dirname.mockReturnValue("/dist/cli/utils");
|
|
54
|
+
mockedPath.join
|
|
55
|
+
.mockReturnValueOnce("/dist/cli/utils/package.json")
|
|
56
|
+
.mockReturnValueOnce("/dist/cli/package.json")
|
|
57
|
+
.mockReturnValueOnce("/package.json")
|
|
58
|
+
.mockReturnValueOnce("/../package.json");
|
|
59
|
+
|
|
60
|
+
mockedFs.existsSync.mockImplementation((filePath) => {
|
|
61
|
+
return filePath === "/dist/cli/utils/package.json";
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
mockedFs.readFileSync.mockReturnValue(JSON.stringify(mockPackageJson));
|
|
65
|
+
|
|
66
|
+
const result = VersionUtils.getVersion();
|
|
67
|
+
|
|
68
|
+
expect(result).toBe("1.2.3");
|
|
69
|
+
expect(mockedFs.existsSync).toHaveBeenCalledWith(
|
|
70
|
+
"/dist/cli/utils/package.json"
|
|
71
|
+
);
|
|
72
|
+
expect(mockedFs.readFileSync).toHaveBeenCalledWith(
|
|
73
|
+
"/dist/cli/utils/package.json",
|
|
74
|
+
"utf8"
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("当前目录中未找到时应从父级package.json读取版本号", () => {
|
|
79
|
+
const mockPackageJson = { version: "2.0.0", name: "test-package" };
|
|
80
|
+
|
|
81
|
+
mockedFileURLToPath.mockReturnValue("/dist/cli/utils/VersionUtils.js");
|
|
82
|
+
|
|
83
|
+
mockedPath.dirname.mockReturnValue("/dist/cli/utils");
|
|
84
|
+
mockedPath.join
|
|
85
|
+
.mockReturnValueOnce("/dist/cli/utils/package.json")
|
|
86
|
+
.mockReturnValueOnce("/dist/cli/package.json")
|
|
87
|
+
.mockReturnValueOnce("/package.json")
|
|
88
|
+
.mockReturnValueOnce("/../package.json");
|
|
89
|
+
|
|
90
|
+
mockedFs.existsSync.mockImplementation((filePath) => {
|
|
91
|
+
return filePath === "/package.json";
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
mockedFs.readFileSync.mockReturnValue(JSON.stringify(mockPackageJson));
|
|
95
|
+
|
|
96
|
+
const result = VersionUtils.getVersion();
|
|
97
|
+
|
|
98
|
+
expect(result).toBe("2.0.0");
|
|
99
|
+
expect(mockedFs.existsSync).toHaveBeenCalledTimes(3);
|
|
100
|
+
expect(mockedFs.readFileSync).toHaveBeenCalledWith(
|
|
101
|
+
"/package.json",
|
|
102
|
+
"utf8"
|
|
103
|
+
);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("未找到package.json时应返回 'unknown'", () => {
|
|
107
|
+
mockedFileURLToPath.mockReturnValue("/dist/cli/utils/VersionUtils.js");
|
|
108
|
+
|
|
109
|
+
mockedPath.dirname.mockReturnValue("/dist/cli/utils");
|
|
110
|
+
mockedPath.join
|
|
111
|
+
.mockReturnValueOnce("/dist/cli/utils/package.json")
|
|
112
|
+
.mockReturnValueOnce("/dist/cli/package.json")
|
|
113
|
+
.mockReturnValueOnce("/package.json")
|
|
114
|
+
.mockReturnValueOnce("/../package.json");
|
|
115
|
+
|
|
116
|
+
mockedFs.existsSync.mockReturnValue(false);
|
|
117
|
+
|
|
118
|
+
const result = VersionUtils.getVersion();
|
|
119
|
+
|
|
120
|
+
expect(result).toBe("unknown");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("package.json没有版本字段时应返回 'unknown'", () => {
|
|
124
|
+
const mockPackageJson = { name: "test-package" };
|
|
125
|
+
|
|
126
|
+
mockedFileURLToPath.mockReturnValue("/dist/cli/utils/VersionUtils.js");
|
|
127
|
+
|
|
128
|
+
mockedPath.dirname.mockReturnValue("/dist/cli/utils");
|
|
129
|
+
mockedPath.join.mockReturnValue("/dist/cli/utils/package.json");
|
|
130
|
+
|
|
131
|
+
mockedFs.existsSync.mockReturnValue(true);
|
|
132
|
+
mockedFs.readFileSync.mockReturnValue(JSON.stringify(mockPackageJson));
|
|
133
|
+
|
|
134
|
+
const result = VersionUtils.getVersion();
|
|
135
|
+
|
|
136
|
+
expect(result).toBe("unknown");
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("读取package.json失败时应返回 'unknown'", () => {
|
|
140
|
+
mockedFileURLToPath.mockReturnValue("/dist/cli/utils/VersionUtils.js");
|
|
141
|
+
|
|
142
|
+
mockedPath.dirname.mockReturnValue("/dist/cli/utils");
|
|
143
|
+
mockedPath.join.mockReturnValue("/dist/cli/utils/package.json");
|
|
144
|
+
|
|
145
|
+
mockedFs.existsSync.mockReturnValue(true);
|
|
146
|
+
mockedFs.readFileSync.mockImplementation(() => {
|
|
147
|
+
throw new Error("Read error");
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const result = VersionUtils.getVersion();
|
|
151
|
+
|
|
152
|
+
expect(result).toBe("unknown");
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("首次成功读取后应缓存版本号", () => {
|
|
156
|
+
const mockPackageJson = { version: "1.0.0", name: "test-package" };
|
|
157
|
+
|
|
158
|
+
mockedFileURLToPath.mockReturnValue("/dist/cli/utils/VersionUtils.js");
|
|
159
|
+
|
|
160
|
+
mockedPath.dirname.mockReturnValue("/dist/cli/utils");
|
|
161
|
+
mockedPath.join.mockReturnValue("/dist/cli/utils/package.json");
|
|
162
|
+
|
|
163
|
+
mockedFs.existsSync.mockReturnValue(true);
|
|
164
|
+
mockedFs.readFileSync.mockReturnValue(JSON.stringify(mockPackageJson));
|
|
165
|
+
|
|
166
|
+
// First call
|
|
167
|
+
const result1 = VersionUtils.getVersion();
|
|
168
|
+
expect(result1).toBe("1.0.0");
|
|
169
|
+
expect(mockedFs.readFileSync).toHaveBeenCalledTimes(1);
|
|
170
|
+
|
|
171
|
+
// Second call should use cache
|
|
172
|
+
const result2 = VersionUtils.getVersion();
|
|
173
|
+
expect(result2).toBe("1.0.0");
|
|
174
|
+
expect(mockedFs.readFileSync).toHaveBeenCalledTimes(1); // No additional calls
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe("获取版本信息", () => {
|
|
179
|
+
it("应从package.json返回完整的版本信息", () => {
|
|
180
|
+
const mockPackageJson = {
|
|
181
|
+
version: "1.2.3",
|
|
182
|
+
name: "xiaozhi-client",
|
|
183
|
+
description: "A test package",
|
|
184
|
+
author: "Test Author",
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
mockedFileURLToPath.mockReturnValue("/dist/cli/utils/VersionUtils.js");
|
|
188
|
+
|
|
189
|
+
mockedPath.dirname.mockReturnValue("/dist/cli/utils");
|
|
190
|
+
mockedPath.join
|
|
191
|
+
.mockReturnValueOnce("/dist/cli/utils/package.json")
|
|
192
|
+
.mockReturnValueOnce("/dist/cli/package.json")
|
|
193
|
+
.mockReturnValueOnce("/package.json")
|
|
194
|
+
.mockReturnValueOnce("/../package.json");
|
|
195
|
+
|
|
196
|
+
mockedFs.existsSync.mockReturnValue(true);
|
|
197
|
+
mockedFs.readFileSync.mockReturnValue(JSON.stringify(mockPackageJson));
|
|
198
|
+
|
|
199
|
+
const result = VersionUtils.getVersionInfo();
|
|
200
|
+
|
|
201
|
+
expect(result).toEqual({
|
|
202
|
+
version: "1.2.3",
|
|
203
|
+
name: "xiaozhi-client",
|
|
204
|
+
description: "A test package",
|
|
205
|
+
author: "Test Author",
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("应处理信息最少的package.json", () => {
|
|
210
|
+
const mockPackageJson = { version: "1.0.0" };
|
|
211
|
+
|
|
212
|
+
mockedFileURLToPath.mockReturnValue("/dist/cli/utils/VersionUtils.js");
|
|
213
|
+
|
|
214
|
+
mockedPath.dirname.mockReturnValue("/dist/cli/utils");
|
|
215
|
+
mockedPath.join.mockReturnValue("/dist/cli/utils/package.json");
|
|
216
|
+
|
|
217
|
+
mockedFs.existsSync.mockReturnValue(true);
|
|
218
|
+
mockedFs.readFileSync.mockReturnValue(JSON.stringify(mockPackageJson));
|
|
219
|
+
|
|
220
|
+
const result = VersionUtils.getVersionInfo();
|
|
221
|
+
|
|
222
|
+
expect(result).toEqual({
|
|
223
|
+
version: "1.0.0",
|
|
224
|
+
name: undefined,
|
|
225
|
+
description: undefined,
|
|
226
|
+
author: undefined,
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("未找到package.json时应返回默认版本信息", () => {
|
|
231
|
+
mockedFileURLToPath.mockReturnValue("/dist/cli/utils/VersionUtils.js");
|
|
232
|
+
|
|
233
|
+
mockedPath.dirname.mockReturnValue("/dist/cli/utils");
|
|
234
|
+
mockedPath.join
|
|
235
|
+
.mockReturnValueOnce("/dist/cli/utils/package.json")
|
|
236
|
+
.mockReturnValueOnce("/dist/cli/package.json")
|
|
237
|
+
.mockReturnValueOnce("/package.json")
|
|
238
|
+
.mockReturnValueOnce("/../package.json");
|
|
239
|
+
|
|
240
|
+
mockedFs.existsSync.mockReturnValue(false);
|
|
241
|
+
|
|
242
|
+
const result = VersionUtils.getVersionInfo();
|
|
243
|
+
|
|
244
|
+
expect(result).toEqual({ version: "unknown" });
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("读取失败时应抛出文件错误", () => {
|
|
248
|
+
mockedFileURLToPath.mockReturnValue("/dist/cli/utils/VersionUtils.js");
|
|
249
|
+
|
|
250
|
+
mockedPath.dirname.mockReturnValue("/dist/cli/utils");
|
|
251
|
+
mockedPath.join.mockReturnValue("/dist/cli/utils/package.json");
|
|
252
|
+
|
|
253
|
+
mockedFs.existsSync.mockReturnValue(true);
|
|
254
|
+
mockedFs.readFileSync.mockImplementation(() => {
|
|
255
|
+
throw new Error("Read error");
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
expect(() => VersionUtils.getVersionInfo()).toThrow(FileError);
|
|
259
|
+
expect(() => VersionUtils.getVersionInfo()).toThrow("无法读取版本信息");
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
describe("比较版本号", () => {
|
|
264
|
+
it("当version1大于version2时应返回1", () => {
|
|
265
|
+
expect(VersionUtils.compareVersions("2.0.0", "1.0.0")).toBe(1);
|
|
266
|
+
expect(VersionUtils.compareVersions("1.1.0", "1.0.0")).toBe(1);
|
|
267
|
+
expect(VersionUtils.compareVersions("1.0.1", "1.0.0")).toBe(1);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("当version1小于version2时应返回-1", () => {
|
|
271
|
+
expect(VersionUtils.compareVersions("1.0.0", "2.0.0")).toBe(-1);
|
|
272
|
+
expect(VersionUtils.compareVersions("1.0.0", "1.1.0")).toBe(-1);
|
|
273
|
+
expect(VersionUtils.compareVersions("1.0.0", "1.0.1")).toBe(-1);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it("版本号相等时应返回0", () => {
|
|
277
|
+
expect(VersionUtils.compareVersions("1.0.0", "1.0.0")).toBe(0);
|
|
278
|
+
expect(VersionUtils.compareVersions("2.1.3", "2.1.3")).toBe(0);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("应处理不同部分数量的版本号", () => {
|
|
282
|
+
expect(VersionUtils.compareVersions("1.0", "1.0.0")).toBe(0);
|
|
283
|
+
expect(VersionUtils.compareVersions("1.0.0", "1.0")).toBe(0);
|
|
284
|
+
expect(VersionUtils.compareVersions("1.0.0.1", "1.0.0")).toBe(1);
|
|
285
|
+
expect(VersionUtils.compareVersions("1.0.0", "1.0.0.1")).toBe(-1);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("应处理带预发布标识符的版本号", () => {
|
|
289
|
+
// The current implementation ignores pre-release identifiers and just compares numeric parts
|
|
290
|
+
expect(VersionUtils.compareVersions("1.0.0-alpha", "1.0.0")).toBe(0);
|
|
291
|
+
expect(VersionUtils.compareVersions("1.0.0", "1.0.0-alpha")).toBe(0);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it("应处理带构建元数据的版本号", () => {
|
|
295
|
+
// The current implementation doesn't properly handle build metadata
|
|
296
|
+
// "1.0.0+build.1" becomes ["1", "0", "0+build", "1"] and "0+build" becomes NaN
|
|
297
|
+
expect(VersionUtils.compareVersions("1.0.0+build.1", "1.0.0")).toBe(1);
|
|
298
|
+
expect(VersionUtils.compareVersions("1.0.0", "1.0.0+build.1")).toBe(-1);
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
describe("验证版本号有效性", () => {
|
|
303
|
+
it("应接受有效的语义化版本号", () => {
|
|
304
|
+
expect(VersionUtils.isValidVersion("1.0.0")).toBe(true);
|
|
305
|
+
expect(VersionUtils.isValidVersion("0.0.1")).toBe(true);
|
|
306
|
+
expect(VersionUtils.isValidVersion("10.20.30")).toBe(true);
|
|
307
|
+
expect(VersionUtils.isValidVersion("1.0.0-alpha")).toBe(true);
|
|
308
|
+
expect(VersionUtils.isValidVersion("1.0.0-alpha.1")).toBe(true);
|
|
309
|
+
expect(VersionUtils.isValidVersion("1.0.0-0.3.7")).toBe(true);
|
|
310
|
+
expect(VersionUtils.isValidVersion("1.0.0-x.7.z.92")).toBe(true);
|
|
311
|
+
// Note: Build metadata is not supported by current regex
|
|
312
|
+
expect(VersionUtils.isValidVersion("1.0.0-alpha+001")).toBe(false);
|
|
313
|
+
expect(VersionUtils.isValidVersion("1.0.0+20130313144700")).toBe(false);
|
|
314
|
+
expect(VersionUtils.isValidVersion("1.0.0-beta+exp.sha.5114f85")).toBe(
|
|
315
|
+
false
|
|
316
|
+
);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
it("应拒绝无效的语义化版本号", () => {
|
|
320
|
+
expect(VersionUtils.isValidVersion("1")).toBe(false);
|
|
321
|
+
expect(VersionUtils.isValidVersion("1.0")).toBe(false);
|
|
322
|
+
expect(VersionUtils.isValidVersion("1.0.0.0")).toBe(false);
|
|
323
|
+
expect(VersionUtils.isValidVersion("v1.0.0")).toBe(false);
|
|
324
|
+
expect(VersionUtils.isValidVersion("1.0.0-")).toBe(false);
|
|
325
|
+
// The current regex actually accepts "1.0.0-.." because it matches the pattern
|
|
326
|
+
expect(VersionUtils.isValidVersion("1.0.0-..")).toBe(true);
|
|
327
|
+
expect(VersionUtils.isValidVersion("1.0.0-alpha..")).toBe(true);
|
|
328
|
+
expect(VersionUtils.isValidVersion("1.0.0-alpha-beta")).toBe(true);
|
|
329
|
+
expect(VersionUtils.isValidVersion("")).toBe(false);
|
|
330
|
+
expect(VersionUtils.isValidVersion("not.a.version")).toBe(false);
|
|
331
|
+
expect(VersionUtils.isValidVersion("1.0.0.1")).toBe(false);
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
describe("清除缓存", () => {
|
|
336
|
+
it("应清除缓存的版本号", () => {
|
|
337
|
+
// Set cache first
|
|
338
|
+
(VersionUtils as any).cachedVersion = "1.0.0";
|
|
339
|
+
|
|
340
|
+
VersionUtils.clearCache();
|
|
341
|
+
|
|
342
|
+
expect((VersionUtils as any).cachedVersion).toBeNull();
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it("应处理null缓存", () => {
|
|
346
|
+
// Ensure cache is null
|
|
347
|
+
VersionUtils.clearCache();
|
|
348
|
+
|
|
349
|
+
// Should not throw
|
|
350
|
+
VersionUtils.clearCache();
|
|
351
|
+
|
|
352
|
+
expect((VersionUtils as any).cachedVersion).toBeNull();
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
describe("集成测试", () => {
|
|
357
|
+
it("清除缓存后应正常工作", () => {
|
|
358
|
+
const mockPackageJson = { version: "1.0.0" };
|
|
359
|
+
|
|
360
|
+
mockedFileURLToPath.mockReturnValue("/dist/cli/utils/VersionUtils.js");
|
|
361
|
+
|
|
362
|
+
mockedPath.dirname.mockReturnValue("/dist/cli/utils");
|
|
363
|
+
mockedPath.join.mockReturnValue("/dist/cli/utils/package.json");
|
|
364
|
+
|
|
365
|
+
mockedFs.existsSync.mockReturnValue(true);
|
|
366
|
+
mockedFs.readFileSync.mockReturnValue(JSON.stringify(mockPackageJson));
|
|
367
|
+
|
|
368
|
+
// First call
|
|
369
|
+
const result1 = VersionUtils.getVersion();
|
|
370
|
+
expect(result1).toBe("1.0.0");
|
|
371
|
+
|
|
372
|
+
// Clear cache
|
|
373
|
+
VersionUtils.clearCache();
|
|
374
|
+
|
|
375
|
+
// Should read file again
|
|
376
|
+
const result2 = VersionUtils.getVersion();
|
|
377
|
+
expect(result2).toBe("1.0.0");
|
|
378
|
+
expect(mockedFs.readFileSync).toHaveBeenCalledTimes(2);
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it("应正确处理多次路径尝试", () => {
|
|
382
|
+
const mockPackageJson = { version: "2.0.0" };
|
|
383
|
+
|
|
384
|
+
mockedFileURLToPath.mockReturnValue("/dist/cli/utils/VersionUtils.js");
|
|
385
|
+
|
|
386
|
+
mockedPath.dirname.mockReturnValue("/dist/cli/utils");
|
|
387
|
+
mockedPath.join
|
|
388
|
+
.mockReturnValueOnce("/dist/cli/utils/package.json")
|
|
389
|
+
.mockReturnValueOnce("/dist/cli/package.json")
|
|
390
|
+
.mockReturnValueOnce("/package.json")
|
|
391
|
+
.mockReturnValueOnce("/../package.json");
|
|
392
|
+
|
|
393
|
+
// Simulate finding package.json in the third attempt
|
|
394
|
+
mockedFs.existsSync.mockImplementation((filePath) => {
|
|
395
|
+
return filePath === "/package.json";
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
mockedFs.readFileSync.mockReturnValue(JSON.stringify(mockPackageJson));
|
|
399
|
+
|
|
400
|
+
const result = VersionUtils.getVersion();
|
|
401
|
+
|
|
402
|
+
expect(result).toBe("2.0.0");
|
|
403
|
+
expect(mockedFs.existsSync).toHaveBeenCalledTimes(3);
|
|
404
|
+
expect(mockedFs.readFileSync).toHaveBeenCalledWith(
|
|
405
|
+
"/package.json",
|
|
406
|
+
"utf8"
|
|
407
|
+
);
|
|
408
|
+
});
|
|
409
|
+
});
|
|
410
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"strict": false,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"forceConsistentCasingInFileNames": true,
|
|
11
|
+
"resolveJsonModule": true,
|
|
12
|
+
"allowSyntheticDefaultImports": true,
|
|
13
|
+
"noEmit": true,
|
|
14
|
+
"baseUrl": ".",
|
|
15
|
+
"moduleSuffixes": [".ts", ".js"],
|
|
16
|
+
"paths": {
|
|
17
|
+
"@/*": ["src/*"],
|
|
18
|
+
"@cli/commands": ["src/commands"],
|
|
19
|
+
"@cli/commands/*": ["src/commands/*"],
|
|
20
|
+
"@cli/services": ["src/services"],
|
|
21
|
+
"@cli/services/*": ["src/services/*"],
|
|
22
|
+
"@cli/utils": ["src/utils"],
|
|
23
|
+
"@cli/utils/*": ["src/utils/*"],
|
|
24
|
+
"@cli/errors": ["src/errors"],
|
|
25
|
+
"@cli/errors/*": ["src/errors/*"],
|
|
26
|
+
"@cli/interfaces": ["src/interfaces"],
|
|
27
|
+
"@cli/interfaces/*": ["src/interfaces/*"]
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"include": ["src/**/*.ts"],
|
|
31
|
+
"exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]
|
|
32
|
+
}
|
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { defineConfig } from "tsup";
|
|
4
|
+
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
entry: {
|
|
7
|
+
index: "src/index.ts",
|
|
8
|
+
},
|
|
9
|
+
format: ["esm"],
|
|
10
|
+
target: "node18",
|
|
11
|
+
outDir: "../../dist/cli",
|
|
12
|
+
clean: true,
|
|
13
|
+
sourcemap: true,
|
|
14
|
+
dts: false,
|
|
15
|
+
minify: process.env.NODE_ENV === "production",
|
|
16
|
+
splitting: false,
|
|
17
|
+
bundle: true,
|
|
18
|
+
keepNames: true,
|
|
19
|
+
platform: "node",
|
|
20
|
+
esbuildOptions: (options) => {
|
|
21
|
+
options.resolveExtensions = [".ts", ".js", ".json"];
|
|
22
|
+
// 使用 esbuild 的别名机制
|
|
23
|
+
// 由于 esbuild 的 alias 不支持通配符,我们需要注入多个具体的别名
|
|
24
|
+
},
|
|
25
|
+
external: [
|
|
26
|
+
// Node.js 内置模块
|
|
27
|
+
"ws",
|
|
28
|
+
"child_process",
|
|
29
|
+
"fs",
|
|
30
|
+
"path",
|
|
31
|
+
"url",
|
|
32
|
+
"process",
|
|
33
|
+
"dotenv",
|
|
34
|
+
"os",
|
|
35
|
+
"stream",
|
|
36
|
+
"events",
|
|
37
|
+
"util",
|
|
38
|
+
"crypto",
|
|
39
|
+
"http",
|
|
40
|
+
"https",
|
|
41
|
+
// 依赖的外部包(不打包)
|
|
42
|
+
"commander",
|
|
43
|
+
"chalk",
|
|
44
|
+
"consola",
|
|
45
|
+
"ora",
|
|
46
|
+
"express",
|
|
47
|
+
"cli-table3",
|
|
48
|
+
// @xiaozhi-client/config 包(运行时从 dist/config 读取)
|
|
49
|
+
"@xiaozhi-client/config",
|
|
50
|
+
"@xiaozhi-client/config.js",
|
|
51
|
+
// Backend 模块(运行时从 dist/backend 读取)
|
|
52
|
+
"@root/WebServer",
|
|
53
|
+
"@root/WebServer.js",
|
|
54
|
+
],
|
|
55
|
+
outExtension: () => ({
|
|
56
|
+
js: ".js",
|
|
57
|
+
}),
|
|
58
|
+
onSuccess: async () => {
|
|
59
|
+
// 构建后处理:修复导入路径
|
|
60
|
+
const filePath = resolve("../../dist/cli/index.js");
|
|
61
|
+
let content = readFileSync(filePath, "utf-8");
|
|
62
|
+
|
|
63
|
+
// 替换 @root/* 为指向正确位置的相对路径
|
|
64
|
+
// 注意:@xiaozhi-client/config 现在从 node_modules 解析,不需要替换
|
|
65
|
+
content = content
|
|
66
|
+
.replace(
|
|
67
|
+
/from\s*["']@root\/WebServer\.js["']/g,
|
|
68
|
+
'from "../backend/WebServer.js"'
|
|
69
|
+
)
|
|
70
|
+
.replace(
|
|
71
|
+
/from\s*["']@root\/WebServer["']/g,
|
|
72
|
+
'from "../backend/WebServer.js"'
|
|
73
|
+
)
|
|
74
|
+
// 替换动态导入中的 @root/WebServer.js
|
|
75
|
+
.replace(
|
|
76
|
+
/import\(["']@root\/WebServer\.js["']\)/g,
|
|
77
|
+
'import("../backend/WebServer.js")'
|
|
78
|
+
)
|
|
79
|
+
.replace(
|
|
80
|
+
/import\(["']@root\/WebServer["']\)/g,
|
|
81
|
+
'import("../backend/WebServer.js")'
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
writeFileSync(filePath, content);
|
|
85
|
+
console.log("✅ 已修复 dist/cli/index.js 中的导入路径");
|
|
86
|
+
|
|
87
|
+
// 同步根 package.json 版本到与 CLI 包一致
|
|
88
|
+
const cliPkgPath = resolve("../../packages/cli/package.json");
|
|
89
|
+
const rootPkgPath = resolve("../../package.json");
|
|
90
|
+
|
|
91
|
+
const cliPkg = JSON.parse(readFileSync(cliPkgPath, "utf-8"));
|
|
92
|
+
const rootPkg = JSON.parse(readFileSync(rootPkgPath, "utf-8"));
|
|
93
|
+
|
|
94
|
+
if (cliPkg.version !== rootPkg.version) {
|
|
95
|
+
rootPkg.version = cliPkg.version;
|
|
96
|
+
writeFileSync(rootPkgPath, `${JSON.stringify(rootPkg, null, 2)}\n`);
|
|
97
|
+
console.log(`✅ 已同步根 package.json 版本到 ${cliPkg.version}`);
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
});
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import tsconfigPaths from "vite-tsconfig-paths";
|
|
5
|
+
import { defineConfig } from "vitest/config";
|
|
6
|
+
|
|
7
|
+
// ESM 兼容的 __dirname
|
|
8
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
+
const __dirname = dirname(__filename);
|
|
10
|
+
|
|
11
|
+
export default defineConfig({
|
|
12
|
+
plugins: [
|
|
13
|
+
// 添加 tsconfig 路径解析插件
|
|
14
|
+
tsconfigPaths(),
|
|
15
|
+
],
|
|
16
|
+
test: {
|
|
17
|
+
globals: true,
|
|
18
|
+
environment: "node",
|
|
19
|
+
testTimeout: 10000,
|
|
20
|
+
hookTimeout: 10000,
|
|
21
|
+
include: ["**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"],
|
|
22
|
+
exclude: ["node_modules", "dist"],
|
|
23
|
+
coverage: {
|
|
24
|
+
enabled: true,
|
|
25
|
+
provider: "v8",
|
|
26
|
+
reporter: ["text", "json", "html", "lcov"],
|
|
27
|
+
reportsDirectory: resolve(__dirname, "../coverage"),
|
|
28
|
+
exclude: [
|
|
29
|
+
"node_modules/**",
|
|
30
|
+
"dist/**",
|
|
31
|
+
"**/*.d.ts",
|
|
32
|
+
"**/*.config.{js,ts}",
|
|
33
|
+
"coverage/**",
|
|
34
|
+
],
|
|
35
|
+
include: [resolve(__dirname, "src/**/*.ts")],
|
|
36
|
+
all: true,
|
|
37
|
+
thresholds: {
|
|
38
|
+
global: {
|
|
39
|
+
branches: 80,
|
|
40
|
+
functions: 80,
|
|
41
|
+
lines: 80,
|
|
42
|
+
statements: 80,
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
resolve: {
|
|
48
|
+
alias: {
|
|
49
|
+
// CLI 内部路径别名(__dirname 是 packages/cli,所以使用相对路径)
|
|
50
|
+
"@cli/commands": resolve(__dirname, "./src/commands"),
|
|
51
|
+
"@cli/commands/*": resolve(__dirname, "./src/commands/*"),
|
|
52
|
+
"@cli/services": resolve(__dirname, "./src/services"),
|
|
53
|
+
"@cli/services/*": resolve(__dirname, "./src/services/*"),
|
|
54
|
+
"@cli/utils": resolve(__dirname, "./src/utils"),
|
|
55
|
+
"@cli/utils/*": resolve(__dirname, "./src/utils/*"),
|
|
56
|
+
"@cli/errors": resolve(__dirname, "./src/errors"),
|
|
57
|
+
"@cli/errors/*": resolve(__dirname, "./src/errors/*"),
|
|
58
|
+
"@cli/interfaces": resolve(__dirname, "./src/interfaces"),
|
|
59
|
+
"@cli/interfaces/*": resolve(__dirname, "./src/interfaces/*"),
|
|
60
|
+
// Backend 路径别名(从 packages/cli 向上到项目根目录)
|
|
61
|
+
"@handlers": resolve(__dirname, "../../apps/backend/handlers"),
|
|
62
|
+
"@handlers/*": resolve(__dirname, "../../apps/backend/handlers/*"),
|
|
63
|
+
"@middlewares": resolve(__dirname, "../../apps/backend/middlewares"),
|
|
64
|
+
"@middlewares/*": resolve(__dirname, "../../apps/backend/middlewares/*"),
|
|
65
|
+
"@services": resolve(__dirname, "../../apps/backend/services"),
|
|
66
|
+
"@services/*": resolve(__dirname, "../../apps/backend/services/*"),
|
|
67
|
+
"@errors": resolve(__dirname, "../../apps/backend/errors"),
|
|
68
|
+
"@errors/*": resolve(__dirname, "../../apps/backend/errors/*"),
|
|
69
|
+
"@utils": resolve(__dirname, "../../apps/backend/utils"),
|
|
70
|
+
"@utils/*": resolve(__dirname, "../../apps/backend/utils/*"),
|
|
71
|
+
"@core": resolve(__dirname, "../../apps/backend/core"),
|
|
72
|
+
"@core/*": resolve(__dirname, "../../apps/backend/core/*"),
|
|
73
|
+
"@transports": resolve(
|
|
74
|
+
__dirname,
|
|
75
|
+
"../../apps/backend/lib/mcp/transports"
|
|
76
|
+
),
|
|
77
|
+
"@transports/*": resolve(
|
|
78
|
+
__dirname,
|
|
79
|
+
"../../apps/backend/lib/mcp/transports/*"
|
|
80
|
+
),
|
|
81
|
+
"@adapters": resolve(__dirname, "../../apps/backend/adapters"),
|
|
82
|
+
"@adapters/*": resolve(__dirname, "../../apps/backend/adapters/*"),
|
|
83
|
+
"@managers": resolve(__dirname, "../../apps/backend/managers"),
|
|
84
|
+
"@managers/*": resolve(__dirname, "../../apps/backend/managers/*"),
|
|
85
|
+
"@types": resolve(__dirname, "../../apps/backend/types"),
|
|
86
|
+
"@types/*": resolve(__dirname, "../../apps/backend/types/*"),
|
|
87
|
+
"@/lib": resolve(__dirname, "../../apps/backend/lib"),
|
|
88
|
+
"@/lib/*": resolve(__dirname, "../../apps/backend/lib/*"),
|
|
89
|
+
"@root": resolve(__dirname, "../../apps/backend"),
|
|
90
|
+
"@root/*": resolve(__dirname, "../../apps/backend/*"),
|
|
91
|
+
"@routes": resolve(__dirname, "../../apps/backend/routes"),
|
|
92
|
+
"@routes/*": resolve(__dirname, "../../apps/backend/routes/*"),
|
|
93
|
+
"@constants": resolve(__dirname, "../../apps/backend/constants"),
|
|
94
|
+
"@constants/*": resolve(__dirname, "../../apps/backend/constants/*"),
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
});
|