mu-agent-bridge 1.0.73 → 1.0.78

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.
Files changed (152) hide show
  1. package/README.md +12 -8
  2. package/dist/src-ts/cli.js +67 -37
  3. package/dist/src-ts/control-server.js +7 -28
  4. package/dist/src-ts/core-updater.js +141 -33
  5. package/dist/src-ts/daemon-process.js +2 -1
  6. package/dist/src-ts/dsh-agent.js +7 -2
  7. package/dist/src-ts/http-agent.js +36 -0
  8. package/dist/src-ts/plugin-manager.js +185 -58
  9. package/dist/src-ts/plugins/abweb/index.js +1 -1
  10. package/dist/src-ts/plugins/abweb/public/app.js +8 -6
  11. package/dist/src-ts/plugins/abweb/public/index.html +3 -3
  12. package/dist/src-ts/plugins/aun/attachments.js +3 -21
  13. package/dist/src-ts/plugins/aun/index.js +89 -11
  14. package/dist/src-ts/plugins/aun/messages.js +1 -1
  15. package/dist/src-ts/plugins/aun/public/app.js +2 -1
  16. package/dist/src-ts/plugins/aun/public/index.html +1 -1
  17. package/dist/src-ts/plugins/cdp-draft.js +5 -2
  18. package/dist/src-ts/plugins/chatgpt/index.js +3 -2
  19. package/dist/src-ts/plugins/claude-cli/index.js +1 -1
  20. package/dist/src-ts/plugins/desktop-launch.js +44 -3
  21. package/dist/src-ts/plugins/doubao/cdp-client.js +231 -45
  22. package/dist/src-ts/plugins/doubao/index.js +119 -7
  23. package/dist/src-ts/plugins/doubao/native-task.js +134 -0
  24. package/dist/src-ts/plugins/dsh/cdp-client.js +334 -0
  25. package/dist/src-ts/plugins/dsh/index.js +102 -23
  26. package/dist/src-ts/plugins/dsh/session-history.js +31 -3
  27. package/dist/src-ts/plugins/feishu/index.js +1 -1
  28. package/dist/src-ts/plugins/personal-assistant/index.js +29 -8
  29. package/dist/src-ts/plugins/personal-assistant/model-client.js +20 -6
  30. package/dist/src-ts/plugins/personal-assistant/session-memory.js +124 -0
  31. package/dist/src-ts/plugins/personal-assistant/tools.js +4 -3
  32. package/dist/src-ts/plugins/qwenwork/cdp-client.js +43 -7
  33. package/dist/src-ts/plugins/qwenwork/index.js +52 -18
  34. package/dist/src-ts/plugins/workbuddy/cdp-client.js +349 -83
  35. package/dist/src-ts/plugins/workbuddy/index.js +24 -12
  36. package/dist/src-ts/plugins/zcode/cdp-client.js +500 -0
  37. package/dist/src-ts/plugins/zcode/index.js +97 -0
  38. package/dist/src-ts/session-management.js +27 -8
  39. package/dist/src-ts/version-manager.js +6 -3
  40. package/dist/tests-ts/abweb-model-config.test.js +44 -41
  41. package/dist/tests-ts/aun-attachments.test.js +19 -17
  42. package/dist/tests-ts/aun-default-owner-page.test.js +2 -1
  43. package/dist/tests-ts/aun-messages.test.js +14 -0
  44. package/dist/tests-ts/aun-plugin.test.js +207 -19
  45. package/dist/tests-ts/bundled-package.test.js +1 -1
  46. package/dist/tests-ts/cdp-draft-recovery.test.js +182 -3
  47. package/dist/tests-ts/chatgpt-plugin.test.js +19 -0
  48. package/dist/tests-ts/control-server.test.js +20 -0
  49. package/dist/tests-ts/core-updater.test.js +215 -46
  50. package/dist/tests-ts/daemon-process.test.js +126 -1
  51. package/dist/tests-ts/desktop-launch.test.js +73 -0
  52. package/dist/tests-ts/doubao-native-task.test.js +237 -0
  53. package/dist/tests-ts/doubao-plugin.test.js +688 -19
  54. package/dist/tests-ts/dsh-agent.test.js +91 -19
  55. package/dist/tests-ts/dsh-cdp.test.js +148 -0
  56. package/dist/tests-ts/dsh-history.test.js +37 -0
  57. package/dist/tests-ts/feishu-plugin.test.js +1 -1
  58. package/dist/tests-ts/http-agent.test.js +18 -0
  59. package/dist/tests-ts/local-cli.test.js +29 -2
  60. package/dist/tests-ts/model-provider-cancellation.test.js +2 -0
  61. package/dist/tests-ts/native-session-management.test.js +8 -7
  62. package/dist/tests-ts/personal-assistant-coordination.test.js +16 -0
  63. package/dist/tests-ts/personal-assistant-extensions.test.js +7 -1
  64. package/dist/tests-ts/personal-assistant-memory.test.js +117 -0
  65. package/dist/tests-ts/personal-assistant.test.js +277 -3
  66. package/dist/tests-ts/platform-integration.test.js +60 -1
  67. package/dist/tests-ts/plugin-manager.test.js +78 -6
  68. package/dist/tests-ts/plugin-upgrade-deadline.test.js +354 -0
  69. package/dist/tests-ts/publish-packages.test.js +1 -1
  70. package/dist/tests-ts/qwenwork-plugin.test.js +30 -0
  71. package/dist/tests-ts/qwenwork-reconnect.test.js +195 -0
  72. package/dist/tests-ts/qwenwork-sse.test.js +33 -0
  73. package/dist/tests-ts/session-access-defaults.test.js +112 -0
  74. package/dist/tests-ts/session-management.test.js +8 -8
  75. package/dist/tests-ts/version-manager.test.js +40 -0
  76. package/dist/tests-ts/workbuddy-cdp-recovery.test.js +311 -0
  77. package/dist/tests-ts/workbuddy-plugin.test.js +356 -13
  78. package/dist/tests-ts/zcode-cdp.test.js +894 -0
  79. package/dist/tests-ts/zcode-plugin.test.js +396 -0
  80. package/package.json +1 -1
  81. package/plugins/abweb/dist/src-ts/plugins/abweb/index.js +1 -1
  82. package/plugins/abweb/dist/src-ts/plugins/abweb/public/app.js +8 -6
  83. package/plugins/abweb/dist/src-ts/plugins/abweb/public/app.ts +7 -5
  84. package/plugins/abweb/dist/src-ts/plugins/abweb/public/index.html +3 -3
  85. package/plugins/abweb/package.json +1 -1
  86. package/plugins/abweb/plugin.json +1 -1
  87. package/plugins/aun/config.schema.json +1 -1
  88. package/plugins/aun/dist/src-ts/plugins/aun/attachments.js +3 -21
  89. package/plugins/aun/dist/src-ts/plugins/aun/index.js +89 -11
  90. package/plugins/aun/dist/src-ts/plugins/aun/messages.js +1 -1
  91. package/plugins/aun/dist/src-ts/plugins/aun/public/app.js +2 -1
  92. package/plugins/aun/dist/src-ts/plugins/aun/public/index.html +1 -1
  93. package/plugins/aun/package.json +1 -1
  94. package/plugins/aun/plugin.json +1 -1
  95. package/plugins/chatgpt/dist/src-ts/plugins/cdp-draft.js +5 -2
  96. package/plugins/chatgpt/dist/src-ts/plugins/chatgpt/index.js +3 -2
  97. package/plugins/chatgpt/dist/src-ts/plugins/desktop-launch.js +44 -3
  98. package/plugins/claude-cli/dist/src-ts/plugins/claude-cli/index.js +1 -1
  99. package/plugins/claude-cli/package.json +1 -1
  100. package/plugins/claude-cli/plugin.json +1 -1
  101. package/plugins/doubao/config.schema.json +2 -1
  102. package/plugins/doubao/dist/src-ts/plugins/desktop-launch.js +44 -3
  103. package/plugins/doubao/dist/src-ts/plugins/doubao/cdp-client.js +231 -45
  104. package/plugins/doubao/dist/src-ts/plugins/doubao/index.js +119 -7
  105. package/plugins/doubao/dist/src-ts/plugins/doubao/native-task.js +134 -0
  106. package/plugins/doubao/package.json +1 -1
  107. package/plugins/doubao/plugin.json +1 -1
  108. package/plugins/dsh/config.schema.json +5 -2
  109. package/plugins/dsh/dist/src-ts/dsh-agent.js +7 -2
  110. package/plugins/dsh/dist/src-ts/plugins/desktop-launch.js +44 -3
  111. package/plugins/dsh/dist/src-ts/plugins/dsh/cdp-client.js +334 -0
  112. package/plugins/dsh/dist/src-ts/plugins/dsh/index.js +102 -23
  113. package/plugins/dsh/dist/src-ts/plugins/dsh/session-history.js +31 -3
  114. package/plugins/dsh/package.json +1 -1
  115. package/plugins/dsh/plugin.json +1 -1
  116. package/plugins/feishu/dist/src-ts/plugins/feishu/index.js +1 -1
  117. package/plugins/feishu/package.json +1 -1
  118. package/plugins/feishu/plugin.json +1 -1
  119. package/plugins/personal-assistant/config.schema.json +3 -2
  120. package/plugins/personal-assistant/dist/src-ts/attachments.js +62 -0
  121. package/plugins/personal-assistant/dist/src-ts/plugins/personal-assistant/index.js +29 -8
  122. package/plugins/personal-assistant/dist/src-ts/plugins/personal-assistant/model-client.js +20 -6
  123. package/plugins/personal-assistant/dist/src-ts/plugins/personal-assistant/session-memory.js +124 -0
  124. package/plugins/personal-assistant/dist/src-ts/plugins/personal-assistant/tools.js +4 -3
  125. package/plugins/personal-assistant/package.json +2 -2
  126. package/plugins/personal-assistant/plugin.json +1 -1
  127. package/plugins/qwenwork/dist/src-ts/plugins/cdp-draft.js +5 -2
  128. package/plugins/qwenwork/dist/src-ts/plugins/desktop-launch.js +44 -3
  129. package/plugins/qwenwork/dist/src-ts/plugins/qwenwork/cdp-client.js +43 -7
  130. package/plugins/qwenwork/dist/src-ts/plugins/qwenwork/index.js +52 -18
  131. package/plugins/qwenwork/package.json +1 -1
  132. package/plugins/qwenwork/plugin.json +1 -1
  133. package/plugins/workbuddy/config.schema.json +1 -1
  134. package/plugins/workbuddy/dist/src-ts/plugins/desktop-launch.js +44 -3
  135. package/plugins/workbuddy/dist/src-ts/plugins/workbuddy/cdp-client.js +349 -83
  136. package/plugins/workbuddy/dist/src-ts/plugins/workbuddy/index.js +24 -12
  137. package/plugins/workbuddy/package.json +1 -1
  138. package/plugins/workbuddy/plugin.json +1 -1
  139. package/plugins/zcode/config.schema.json +13 -0
  140. package/plugins/zcode/dist/src-ts/agent.js +69 -0
  141. package/plugins/zcode/dist/src-ts/attachments.js +62 -0
  142. package/plugins/zcode/dist/src-ts/plugin.js +13 -0
  143. package/plugins/zcode/dist/src-ts/plugins/desktop-launch.js +238 -0
  144. package/plugins/zcode/dist/src-ts/plugins/workbuddy/cdp-client.js +896 -0
  145. package/plugins/zcode/dist/src-ts/plugins/workbuddy/cli-client.js +39 -0
  146. package/plugins/zcode/dist/src-ts/plugins/workbuddy/desktop-launch.js +10 -0
  147. package/plugins/zcode/dist/src-ts/plugins/workbuddy/index.js +567 -0
  148. package/plugins/zcode/dist/src-ts/plugins/workbuddy/session-history.js +121 -0
  149. package/plugins/zcode/dist/src-ts/plugins/zcode/cdp-client.js +500 -0
  150. package/plugins/zcode/dist/src-ts/plugins/zcode/index.js +97 -0
  151. package/plugins/zcode/package.json +16 -0
  152. package/plugins/zcode/plugin.json +11 -0
@@ -4,85 +4,254 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const strict_1 = __importDefault(require("node:assert/strict"));
7
+ const node_child_process_1 = require("node:child_process");
7
8
  const promises_1 = require("node:fs/promises");
8
9
  const node_os_1 = require("node:os");
9
10
  const node_path_1 = require("node:path");
10
11
  const node_test_1 = __importDefault(require("node:test"));
12
+ const node_util_1 = require("node:util");
11
13
  const core_updater_1 = require("../src-ts/core-updater");
12
- async function fixture() {
14
+ const execFileAsync = (0, node_util_1.promisify)(node_child_process_1.execFile);
15
+ async function fixture(directory = "测试目录/package", version = "1.0.0") {
13
16
  const root = await (0, promises_1.mkdtemp)((0, node_path_1.join)((0, node_os_1.tmpdir)(), "mab-core-update-"));
14
- const packageRoot = (0, node_path_1.join)(root, "node_modules", "mu-agent-bridge");
15
- const globalRoot = (0, node_path_1.join)(root, "node_modules");
17
+ const packageRoot = (0, node_path_1.join)(root, directory);
16
18
  await (0, promises_1.mkdir)((0, node_path_1.join)(packageRoot, "dist", "src-ts"), { recursive: true });
17
- await (0, promises_1.writeFile)((0, node_path_1.join)(packageRoot, "package.json"), JSON.stringify({ name: "mu-agent-bridge", version: "1.0.0" }));
18
- await (0, promises_1.writeFile)((0, node_path_1.join)(packageRoot, "dist", "src-ts", "cli.js"), "old");
19
- return { root, packageRoot, globalRoot };
19
+ await (0, promises_1.writeFile)((0, node_path_1.join)(packageRoot, "package.json"), JSON.stringify({ name: "mu-agent-bridge", version }));
20
+ await (0, promises_1.writeFile)((0, node_path_1.join)(packageRoot, "dist", "src-ts", "cli.js"), `console.log(${JSON.stringify(version)});`);
21
+ await (0, promises_1.writeFile)((0, node_path_1.join)(packageRoot, "dist", "obsolete.js"), "旧发布文件");
22
+ await (0, promises_1.writeFile)((0, node_path_1.join)(packageRoot, "local-config.json"), "保留本地配置");
23
+ await (0, promises_1.mkdir)((0, node_path_1.join)(packageRoot, "node_modules", "local-tool"), { recursive: true });
24
+ await (0, promises_1.writeFile)((0, node_path_1.join)(packageRoot, "node_modules", "local-tool", "index.js"), "保留本地依赖");
25
+ await (0, promises_1.mkdir)((0, node_path_1.join)(packageRoot, "plugins", "local-plugin"), { recursive: true });
26
+ await (0, promises_1.mkdir)((0, node_path_1.join)(packageRoot, "plugins", "dsh", "9.0.0"), { recursive: true });
27
+ await (0, promises_1.writeFile)((0, node_path_1.join)(packageRoot, "plugins", "local-plugin", "plugin.json"), "本地插件");
28
+ await (0, promises_1.writeFile)((0, node_path_1.join)(packageRoot, "plugins", "dsh", "9.0.0", "plugin.json"), "高版本插件");
29
+ return { root, packageRoot, stateDir: (0, node_path_1.join)(root, "state") };
20
30
  }
21
- (0, node_test_1.default)("mab start 在核心有新版本时安装、启动并等待健康检查", async () => {
31
+ async function stagePackage(args, version = "1.1.0") {
32
+ const prefixIndex = args.indexOf("--prefix");
33
+ strict_1.default.notEqual(prefixIndex, -1, "安装应在暂存目录执行");
34
+ strict_1.default.ok(args.includes("--install-strategy=nested"), "依赖需要随核心一起迁移");
35
+ const candidate = (0, node_path_1.join)(args[prefixIndex + 1], "node_modules", "mu-agent-bridge");
36
+ await (0, promises_1.mkdir)((0, node_path_1.join)(candidate, "dist", "src-ts"), { recursive: true });
37
+ await (0, promises_1.mkdir)((0, node_path_1.join)(candidate, "node_modules", "fixture-dependency"), { recursive: true });
38
+ await (0, promises_1.writeFile)((0, node_path_1.join)(candidate, "package.json"), JSON.stringify({ name: "mu-agent-bridge", version }));
39
+ await (0, promises_1.writeFile)((0, node_path_1.join)(candidate, "dist", "src-ts", "cli.js"), 'console.log(require("../../package.json").version + ":" + require("fixture-dependency"));');
40
+ await (0, promises_1.writeFile)((0, node_path_1.join)(candidate, "node_modules", "fixture-dependency", "index.js"), 'module.exports="依赖可用";');
41
+ if (process.platform !== "win32") {
42
+ await (0, promises_1.mkdir)((0, node_path_1.join)(candidate, "node_modules", ".bin"));
43
+ await (0, promises_1.symlink)("../fixture-dependency/index.js", (0, node_path_1.join)(candidate, "node_modules", ".bin", "fixture"));
44
+ }
45
+ await (0, promises_1.writeFile)((0, node_path_1.join)(candidate, "LICENSE"), "新发布文件");
46
+ await (0, promises_1.mkdir)((0, node_path_1.join)(candidate, "plugins", "dsh"), { recursive: true });
47
+ await (0, promises_1.writeFile)((0, node_path_1.join)(candidate, "plugins", "dsh", "plugin.json"), "新版内置插件");
48
+ return "";
49
+ }
50
+ for (const directory of ["node_modules/mu-agent-bridge", "测试目录/package"]) {
51
+ (0, node_test_1.default)(`${directory} 的低版本升级后仍由原路径运行新核心及依赖`, async () => {
52
+ const value = await fixture(directory);
53
+ const events = [];
54
+ try {
55
+ const updated = await (0, core_updater_1.updateCoreAtStartup)({
56
+ ...value,
57
+ npm: async (args) => {
58
+ events.push(args[0]);
59
+ if (args[0] === "root")
60
+ return (0, node_path_1.join)(value.root, "node_modules");
61
+ if (args[0] === "view")
62
+ return '"1.1.0"';
63
+ return stagePackage(args);
64
+ },
65
+ stopDaemon: async () => { events.push("stop"); },
66
+ startDaemon: async () => {
67
+ events.push("start");
68
+ const { stdout } = await execFileAsync(process.execPath, [(0, node_path_1.join)(value.packageRoot, "dist", "src-ts", "cli.js")]);
69
+ strict_1.default.equal(stdout.trim(), "1.1.0:依赖可用");
70
+ },
71
+ waitForHealth: async () => true,
72
+ });
73
+ strict_1.default.equal(updated, true);
74
+ strict_1.default.deepEqual(events, ["view", "install", "stop", "start"]);
75
+ strict_1.default.equal(await (0, promises_1.readFile)((0, node_path_1.join)(value.packageRoot, "local-config.json"), "utf8"), "保留本地配置");
76
+ strict_1.default.equal(await (0, promises_1.readFile)((0, node_path_1.join)(value.packageRoot, "node_modules", "local-tool", "index.js"), "utf8"), "保留本地依赖");
77
+ strict_1.default.equal(await (0, promises_1.readFile)((0, node_path_1.join)(value.packageRoot, "plugins", "local-plugin", "plugin.json"), "utf8"), "本地插件");
78
+ strict_1.default.equal(await (0, promises_1.readFile)((0, node_path_1.join)(value.packageRoot, "plugins", "dsh", "9.0.0", "plugin.json"), "utf8"), "高版本插件");
79
+ strict_1.default.equal(await (0, promises_1.readFile)((0, node_path_1.join)(value.packageRoot, "plugins", "dsh", "plugin.json"), "utf8"), "新版内置插件");
80
+ if (process.platform !== "win32")
81
+ strict_1.default.equal(await (0, promises_1.readlink)((0, node_path_1.join)(value.packageRoot, "node_modules", ".bin", "fixture")), "../fixture-dependency/index.js");
82
+ await strict_1.default.rejects((0, promises_1.readFile)((0, node_path_1.join)(value.packageRoot, "dist", "obsolete.js")), { code: "ENOENT" });
83
+ strict_1.default.deepEqual(await (0, promises_1.readdir)(value.stateDir), []);
84
+ }
85
+ finally {
86
+ await (0, promises_1.rm)(value.root, { recursive: true, force: true });
87
+ }
88
+ });
89
+ for (const currentVersion of ["1.1.0", "9.0.0"]) {
90
+ (0, node_test_1.default)(`${directory} 的当前版本 ${currentVersion} 不被 npmjs 的相同或较低版本覆盖`, async () => {
91
+ const value = await fixture(directory, currentVersion);
92
+ const calls = [];
93
+ try {
94
+ const updated = await (0, core_updater_1.updateCoreAtStartup)({
95
+ ...value,
96
+ npm: async (args) => { calls.push(args[0]); return args[0] === "root" ? (0, node_path_1.join)(value.root, "node_modules") : '"1.1.0"'; },
97
+ stopDaemon: async () => { strict_1.default.fail("不应停止当前后台"); },
98
+ startDaemon: async () => { strict_1.default.fail("不应启动其他版本"); },
99
+ waitForHealth: async () => true,
100
+ });
101
+ strict_1.default.equal(updated, false);
102
+ strict_1.default.deepEqual(calls, ["view"]);
103
+ strict_1.default.equal(JSON.parse(await (0, promises_1.readFile)((0, node_path_1.join)(value.packageRoot, "package.json"), "utf8")).version, currentVersion);
104
+ }
105
+ finally {
106
+ await (0, promises_1.rm)(value.root, { recursive: true, force: true });
107
+ }
108
+ });
109
+ }
110
+ }
111
+ (0, node_test_1.default)("新 daemon 健康检查失败时恢复原路径的旧核心及文件", async () => {
22
112
  const value = await fixture();
23
113
  const events = [];
114
+ let healthChecks = 0;
24
115
  try {
25
116
  const updated = await (0, core_updater_1.updateCoreAtStartup)({
26
- packageRoot: value.packageRoot,
27
- globalRoot: value.globalRoot,
28
- stateDir: (0, node_path_1.join)(value.root, "state"),
29
- npm: async (args) => {
30
- events.push(args.join(" "));
31
- if (args[0] === "view")
32
- return '"1.1.0"';
33
- await (0, promises_1.writeFile)((0, node_path_1.join)(value.packageRoot, "package.json"), JSON.stringify({ name: "mu-agent-bridge", version: "1.1.0" }));
34
- await (0, promises_1.writeFile)((0, node_path_1.join)(value.packageRoot, "dist", "src-ts", "cli.js"), "new");
35
- return "";
36
- },
117
+ ...value,
118
+ npm: async (args) => args[0] === "root" ? (0, node_path_1.join)(value.root, "node_modules") : args[0] === "view" ? '"1.1.0"' : stagePackage(args),
37
119
  stopDaemon: async () => { events.push("stop"); },
38
- startDaemon: async () => { events.push("start"); },
39
- waitForHealth: async () => true,
120
+ startDaemon: async () => {
121
+ const { stdout } = await execFileAsync(process.execPath, [(0, node_path_1.join)(value.packageRoot, "dist", "src-ts", "cli.js")]);
122
+ events.push(stdout.trim());
123
+ },
124
+ waitForHealth: async () => ++healthChecks > 1,
40
125
  });
41
- strict_1.default.equal(updated, true);
42
- strict_1.default.equal(JSON.parse(await (0, promises_1.readFile)((0, node_path_1.join)(value.packageRoot, "package.json"), "utf8")).version, "1.1.0");
43
- strict_1.default.deepEqual(events, ["view mu-agent-bridge version --json", "stop", "install -g mu-agent-bridge@1.1.0 --no-fund --no-audit", "start"]);
126
+ strict_1.default.equal(updated, false);
127
+ strict_1.default.deepEqual(events, ["stop", "1.1.0:依赖可用", "stop", "1.0.0"]);
128
+ strict_1.default.equal(await (0, promises_1.readFile)((0, node_path_1.join)(value.packageRoot, "dist", "obsolete.js"), "utf8"), "旧发布文件");
129
+ strict_1.default.equal(await (0, promises_1.readFile)((0, node_path_1.join)(value.packageRoot, "local-config.json"), "utf8"), "保留本地配置");
130
+ await strict_1.default.rejects((0, promises_1.readFile)((0, node_path_1.join)(value.packageRoot, "LICENSE")), { code: "ENOENT" });
44
131
  }
45
132
  finally {
46
133
  await (0, promises_1.rm)(value.root, { recursive: true, force: true });
47
134
  }
48
135
  });
49
- (0, node_test_1.default)("新 daemon 健康检查失败时恢复旧核心包并继续旧进程", async () => {
136
+ (0, node_test_1.default)("并发检查遇到已有进程的升级锁时中止,避免启动入口继续覆盖后台", async () => {
50
137
  const value = await fixture();
51
- const events = [];
138
+ const lock = (0, node_path_1.join)(value.stateDir, "core-update.lock");
139
+ await (0, promises_1.mkdir)(lock, { recursive: true });
140
+ await (0, promises_1.writeFile)((0, node_path_1.join)(lock, String(process.pid)), "");
141
+ try {
142
+ await strict_1.default.rejects((0, core_updater_1.updateCoreAtStartup)({
143
+ ...value,
144
+ npm: async () => strict_1.default.fail("锁冲突时不能调用 npm"),
145
+ stopDaemon: async () => strict_1.default.fail("锁冲突时不能停止后台"),
146
+ startDaemon: async () => strict_1.default.fail("锁冲突时不能启动后台"),
147
+ waitForHealth: async () => false,
148
+ }), /已有核心升级检查正在运行/);
149
+ strict_1.default.equal(JSON.parse(await (0, promises_1.readFile)((0, node_path_1.join)(value.packageRoot, "package.json"), "utf8")).version, "1.0.0");
150
+ strict_1.default.deepEqual(await (0, promises_1.readdir)(lock), [String(process.pid)]);
151
+ }
152
+ finally {
153
+ await (0, promises_1.rm)(value.root, { recursive: true, force: true });
154
+ }
155
+ });
156
+ (0, node_test_1.default)("回滚后的旧后台仍不健康时清理本次进程并保留备份", async () => {
157
+ const value = await fixture();
158
+ const stops = [];
159
+ let launches = 0;
160
+ try {
161
+ await strict_1.default.rejects((0, core_updater_1.updateCoreAtStartup)({
162
+ ...value,
163
+ npm: async (args) => args[0] === "view" ? '"1.1.0"' : stagePackage(args),
164
+ stopDaemon: async (pid) => { stops.push(pid); },
165
+ startDaemon: async () => ({ pid: 1000 + ++launches }),
166
+ waitForHealth: async () => false,
167
+ }), /旧 daemon 恢复后的健康检查失败/);
168
+ strict_1.default.deepEqual(stops, [undefined, 1001, 1002]);
169
+ strict_1.default.equal(JSON.parse(await (0, promises_1.readFile)((0, node_path_1.join)(value.packageRoot, "package.json"), "utf8")).version, "1.0.0");
170
+ const remaining = await (0, promises_1.readdir)(value.stateDir);
171
+ strict_1.default.equal(remaining.length, 1);
172
+ strict_1.default.match(remaining[0], /^mab-core-update-/);
173
+ strict_1.default.equal(JSON.parse(await (0, promises_1.readFile)((0, node_path_1.join)(value.stateDir, remaining[0], "backup", "package.json"), "utf8")).version, "1.0.0");
174
+ }
175
+ finally {
176
+ await (0, promises_1.rm)(value.root, { recursive: true, force: true });
177
+ }
178
+ });
179
+ (0, node_test_1.default)("退出进程遗留的升级锁由下一次检查清理", async () => {
180
+ const value = await fixture();
181
+ const lock = (0, node_path_1.join)(value.stateDir, "core-update.lock");
182
+ await (0, promises_1.mkdir)(lock, { recursive: true });
183
+ await (0, promises_1.writeFile)((0, node_path_1.join)(lock, "2147483647"), "");
184
+ let views = 0;
52
185
  try {
53
186
  const updated = await (0, core_updater_1.updateCoreAtStartup)({
54
- packageRoot: value.packageRoot,
55
- globalRoot: value.globalRoot,
56
- stateDir: (0, node_path_1.join)(value.root, "state"),
57
- npm: async (args) => {
58
- if (args[0] === "view")
59
- return '"1.1.0"';
60
- await (0, promises_1.writeFile)((0, node_path_1.join)(value.packageRoot, "package.json"), JSON.stringify({ name: "mu-agent-bridge", version: "1.1.0" }));
61
- return "";
62
- },
63
- stopDaemon: async () => { events.push("stop"); },
64
- startDaemon: async () => { events.push("start"); },
65
- waitForHealth: async () => { events.push("health"); return false; },
187
+ ...value,
188
+ npm: async () => { views++; return '"1.0.0"'; },
189
+ stopDaemon: async () => strict_1.default.fail("版本相同时不能停止后台"),
190
+ startDaemon: async () => strict_1.default.fail("版本相同时不能启动后台"),
191
+ waitForHealth: async () => false,
66
192
  });
67
193
  strict_1.default.equal(updated, false);
68
- strict_1.default.equal(JSON.parse(await (0, promises_1.readFile)((0, node_path_1.join)(value.packageRoot, "package.json"), "utf8")).version, "1.0.0");
69
- strict_1.default.deepEqual(events, ["stop", "start", "health", "stop", "start"]);
194
+ strict_1.default.equal(views, 1);
195
+ strict_1.default.deepEqual(await (0, promises_1.readdir)(value.stateDir), []);
70
196
  }
71
197
  finally {
72
198
  await (0, promises_1.rm)(value.root, { recursive: true, force: true });
73
199
  }
74
200
  });
75
- (0, node_test_1.default)("核心已是最新或不是 npm 全局安装时不停止 daemon", async () => {
201
+ (0, node_test_1.default)("两个检查并发时只有锁持有者可以调用 npm", async () => {
76
202
  const value = await fixture();
77
- let stops = 0;
203
+ let release;
204
+ let entered;
205
+ const pending = new Promise(done => { release = done; });
206
+ const started = new Promise(done => { entered = done; });
207
+ let checks = 0;
208
+ const options = {
209
+ ...value,
210
+ npm: async () => { checks++; entered(); await pending; return '"1.0.0"'; },
211
+ stopDaemon: async () => strict_1.default.fail("版本相同时不能停止后台"),
212
+ startDaemon: async () => strict_1.default.fail("版本相同时不能启动后台"),
213
+ waitForHealth: async () => false,
214
+ };
215
+ const first = (0, core_updater_1.updateCoreAtStartup)(options);
78
216
  try {
79
- const current = await (0, core_updater_1.updateCoreAtStartup)({ packageRoot: value.packageRoot, globalRoot: value.globalRoot, stateDir: (0, node_path_1.join)(value.root, "state"), npm: async () => '"1.0.0"', stopDaemon: async () => { stops++; }, startDaemon: async () => undefined, waitForHealth: async () => true });
80
- const local = await (0, core_updater_1.updateCoreAtStartup)({ packageRoot: value.packageRoot, globalRoot: value.root, stateDir: (0, node_path_1.join)(value.root, "state"), npm: async () => '"2.0.0"', stopDaemon: async () => { stops++; }, startDaemon: async () => undefined, waitForHealth: async () => true });
81
- strict_1.default.equal(current, false);
82
- strict_1.default.equal(local, false);
83
- strict_1.default.equal(stops, 0);
217
+ await started;
218
+ await strict_1.default.rejects((0, core_updater_1.updateCoreAtStartup)(options), /已有核心升级检查正在运行/);
219
+ strict_1.default.equal(checks, 1);
84
220
  }
85
221
  finally {
222
+ release();
223
+ await first;
86
224
  await (0, promises_1.rm)(value.root, { recursive: true, force: true });
87
225
  }
88
226
  });
227
+ for (const failure of ["下载失败", "包版本不匹配"]) {
228
+ (0, node_test_1.default)(`${failure} 时保留原后台和安装内容`, async () => {
229
+ const value = await fixture();
230
+ const logs = [];
231
+ try {
232
+ const updated = await (0, core_updater_1.updateCoreAtStartup)({
233
+ ...value,
234
+ npm: async (args) => {
235
+ if (args[0] === "root")
236
+ return (0, node_path_1.join)(value.root, "node_modules");
237
+ if (args[0] === "view")
238
+ return '"1.1.0"';
239
+ if (failure === "下载失败")
240
+ throw new Error(failure);
241
+ return stagePackage(args, "1.0.9");
242
+ },
243
+ stopDaemon: async () => { strict_1.default.fail("下载和验证完成前不能停止后台"); },
244
+ startDaemon: async () => { strict_1.default.fail("不应启动无效包"); },
245
+ waitForHealth: async () => true,
246
+ log: (_level, message, fields) => { logs.push(message + JSON.stringify(fields)); },
247
+ });
248
+ strict_1.default.equal(updated, false);
249
+ strict_1.default.equal(JSON.parse(await (0, promises_1.readFile)((0, node_path_1.join)(value.packageRoot, "package.json"), "utf8")).version, "1.0.0");
250
+ strict_1.default.ok(logs.some(line => /下载失败|版本不匹配/.test(line)), JSON.stringify(logs));
251
+ strict_1.default.deepEqual(await (0, promises_1.readdir)(value.stateDir), []);
252
+ }
253
+ finally {
254
+ await (0, promises_1.rm)(value.root, { recursive: true, force: true });
255
+ }
256
+ });
257
+ }
@@ -43,12 +43,15 @@ const promises_1 = require("node:fs/promises");
43
43
  const node_os_1 = require("node:os");
44
44
  const node_path_1 = require("node:path");
45
45
  const node_test_1 = __importDefault(require("node:test"));
46
+ const node_util_1 = require("node:util");
46
47
  const daemon_process_1 = require("../src-ts/daemon-process");
47
48
  const cli = "C:\\Users\\molian\\AppData\\Roaming\\npm\\node_modules\\mu-agent-bridge\\dist\\src-ts\\cli.js";
48
49
  (0, node_test_1.default)("识别同一安装的全部旧 daemon,兼容 Windows 路径与引号", () => {
49
50
  strict_1.default.equal((0, daemon_process_1.isDaemonCommand)(`"C:\\Program Files\\nodejs\\node.exe" ${cli.replaceAll("\\", "/")} start`, cli), true);
50
51
  strict_1.default.equal((0, daemon_process_1.isDaemonCommand)(`node.exe "${cli}" --serve`, cli), true);
51
52
  strict_1.default.equal((0, daemon_process_1.isDaemonCommand)(`node.exe "${cli.replaceAll("\\", "\\\\")}" start`, cli), true);
53
+ const spacedCli = cli.replace("molian", "测试 目录");
54
+ strict_1.default.equal((0, daemon_process_1.isDaemonCommand)(["node", spacedCli, "start"], spacedCli), true);
52
55
  });
53
56
  (0, node_test_1.default)("清理进程时保留 CLI 操作、其他安装及普通 Node 程序", () => {
54
57
  for (const args of ["restart", "stop", "update", "status"])
@@ -68,7 +71,7 @@ const cli = "C:\\Users\\molian\\AppData\\Roaming\\npm\\node_modules\\mu-agent-br
68
71
  await (0, promises_1.mkdir)(plugin, { recursive: true });
69
72
  await (0, promises_1.writeFile)((0, node_path_1.join)(plugin, "plugin.json"), JSON.stringify({ id: "aun", version: "1.0.0", type: "channel", entry: "index.cjs", apiVersion: "1", capabilities: [] }));
70
73
  await (0, promises_1.writeFile)((0, node_path_1.join)(plugin, "index.cjs"), "module.exports={activate:async()=>{},deactivate:async()=>{},health:async()=>({status:'ready'})};");
71
- await (0, promises_1.writeFile)(preload, `require(${JSON.stringify((0, node_path_1.resolve)(__dirname, "../src-ts/plugin-manager.js"))}).PluginManager.prototype.checkPackageUpdates=async()=>{console.log('UPDATE_BLOCKED');return new Promise(()=>{});};`);
74
+ await (0, promises_1.writeFile)(preload, `require(${JSON.stringify((0, node_path_1.resolve)(__dirname, "../src-ts/core-updater.js"))}).updateCoreAtStartup=async()=>false;require(${JSON.stringify((0, node_path_1.resolve)(__dirname, "../src-ts/plugin-manager.js"))}).PluginManager.prototype.checkPackageUpdates=async()=>{console.log('UPDATE_BLOCKED');return new Promise(()=>{});};`);
72
75
  child = (0, node_child_process_1.spawn)(process.execPath, ["--require", preload, (0, node_path_1.resolve)(__dirname, "../src-ts/cli.js"), "start"], {
73
76
  env: { ...process.env, AGENT_BRIDGE_STATE_DIR: state, AGENT_BRIDGE_PLUGINS_DIR: (0, node_path_1.join)(root, "plugins"), AGENT_BRIDGE_HOST: "127.0.0.1", AGENT_BRIDGE_PORT: "0", AGENT_BRIDGE_TOKEN: "" },
74
77
  stdio: ["ignore", "pipe", "pipe"],
@@ -118,3 +121,125 @@ const cli = "C:\\Users\\molian\\AppData\\Roaming\\npm\\node_modules\\mu-agent-br
118
121
  await (0, promises_1.rm)(root, { recursive: true, force: true });
119
122
  }
120
123
  });
124
+ (0, node_test_1.default)("核心升级等待旧后台慢停,且只认可新进程的健康状态", { skip: process.platform === "win32", timeout: 15_000 }, async () => {
125
+ const root = await (0, promises_1.mkdtemp)((0, node_path_1.join)((0, node_os_1.tmpdir)(), "mab-update-restart-"));
126
+ const state = (0, node_path_1.join)(root, "state");
127
+ const addressFile = (0, node_path_1.join)(state, "daemon-address.json");
128
+ const stoppedFile = (0, node_path_1.join)(root, "old-stopped");
129
+ const preload = (0, node_path_1.join)(root, "update.cjs");
130
+ let old;
131
+ let newPid;
132
+ try {
133
+ await (0, promises_1.mkdir)(state);
134
+ const plugin = (0, node_path_1.join)(root, "slow-plugins", "aun");
135
+ await (0, promises_1.mkdir)(plugin, { recursive: true });
136
+ await (0, promises_1.writeFile)((0, node_path_1.join)(plugin, "plugin.json"), JSON.stringify({ id: "aun", version: "1.0.0", type: "channel", entry: "index.cjs", apiVersion: "1", capabilities: [] }));
137
+ await (0, promises_1.writeFile)((0, node_path_1.join)(plugin, "index.cjs"), `module.exports={activate:async()=>{},deactivate:async()=>{await new Promise(done=>setTimeout(done,1200));require('node:fs').writeFileSync(${JSON.stringify(stoppedFile)},'stopped');},health:async()=>({status:'ready'})};`);
138
+ const oldPreload = (0, node_path_1.join)(root, "old.cjs");
139
+ await (0, promises_1.writeFile)(oldPreload, `require(${JSON.stringify((0, node_path_1.resolve)(__dirname, "../src-ts/plugin-manager.js"))}).PluginManager.prototype.checkPackageUpdates=async()=>{console.log('READY');return [];};`);
140
+ old = (0, node_child_process_1.spawn)(process.execPath, [(0, node_path_1.resolve)(__dirname, "../src-ts/cli.js"), "start"], {
141
+ env: { ...process.env, NODE_OPTIONS: `--require=${oldPreload}`, AGENT_BRIDGE_CORE_UPDATE_CHILD: "1", AGENT_BRIDGE_STATE_DIR: state, AGENT_BRIDGE_PLUGINS_DIR: (0, node_path_1.join)(root, "slow-plugins"), AGENT_BRIDGE_HOST: "127.0.0.1", AGENT_BRIDGE_PORT: "0", AGENT_BRIDGE_TOKEN: "" },
142
+ stdio: ["ignore", "pipe", "pipe"],
143
+ });
144
+ let output = "";
145
+ await new Promise(done => old.stdout.on("data", chunk => { output += chunk; if (output.includes("READY"))
146
+ done(); }));
147
+ const previous = JSON.parse(await (0, promises_1.readFile)(addressFile, "utf8"));
148
+ await (0, promises_1.writeFile)(preload, `
149
+ const assert=require('node:assert/strict');
150
+ const fs=require('node:fs/promises');
151
+ const readFile=fs.readFile;
152
+ let maskPid=false;
153
+ fs.readFile=async function(path,...args){
154
+ const data=await readFile(path,...args);
155
+ return maskPid && String(path)===${JSON.stringify(addressFile)} ? JSON.stringify({...JSON.parse(data),pid:process.pid}) : data;
156
+ };
157
+ require(${JSON.stringify((0, node_path_1.resolve)(__dirname, "../src-ts/plugin-manager.js"))}).PluginManager.prototype.checkPackageUpdates=async()=>[];
158
+ if(process.env.MAB_TEST_HOLD_DISCOVERY==='1'){
159
+ require(${JSON.stringify((0, node_path_1.resolve)(__dirname, "../src-ts/plugin-manager.js"))}).PluginManager.prototype.discover=async()=>{
160
+ await fs.writeFile(${JSON.stringify((0, node_path_1.join)(root, "hanging-pid"))},String(process.pid));
161
+ setInterval(()=>{},1000);return new Promise(()=>{});
162
+ };
163
+ }
164
+ require(${JSON.stringify((0, node_path_1.resolve)(__dirname, "../src-ts/core-updater.js"))}).updateCoreAtStartup=async options=>{
165
+ await options.stopDaemon();
166
+ assert.equal(await readFile(${JSON.stringify(stoppedFile)},'utf8'),'stopped','停止操作必须等旧后台释放端口');
167
+ await options.startDaemon();
168
+ for(let attempt=0;attempt<100;attempt++){
169
+ try { await readFile(${JSON.stringify(addressFile)},'utf8');break; } catch { await new Promise(done=>setTimeout(done,20)); }
170
+ }
171
+ maskPid=true;
172
+ let settled=false;
173
+ const health=options.waitForHealth().then(value=>{settled=true;return value;});
174
+ await new Promise(done=>setTimeout(done,200));
175
+ try { assert.equal(settled,false,'其他进程的健康响应不能判定升级成功'); }
176
+ finally { maskPid=false; }
177
+ assert.equal(await health,true);
178
+ await options.stopDaemon();
179
+ process.env.MAB_TEST_HOLD_DISCOVERY='1';
180
+ const hanging=await options.startDaemon();
181
+ for(let attempt=0;attempt<100;attempt++){
182
+ try { await readFile(${JSON.stringify((0, node_path_1.join)(root, "hanging-pid"))},'utf8');break; } catch { await new Promise(done=>setTimeout(done,20)); }
183
+ }
184
+ await assert.rejects(readFile(${JSON.stringify(addressFile)},'utf8'),{code:'ENOENT'});
185
+ await options.stopDaemon(hanging.pid);
186
+ assert.throws(()=>process.kill(hanging.pid,0),{code:'ESRCH'},'未登记的新进程也必须在回滚前停止');
187
+ delete process.env.MAB_TEST_HOLD_DISCOVERY;
188
+ await options.startDaemon();
189
+ assert.equal(await options.waitForHealth(),true);
190
+ console.log('UPDATER_HEALTH=true');
191
+ return true;
192
+ };
193
+ `);
194
+ const { stdout } = await (0, node_util_1.promisify)(node_child_process_1.execFile)(process.execPath, [(0, node_path_1.resolve)(__dirname, "../src-ts/cli.js"), "update"], {
195
+ env: { ...process.env, NODE_OPTIONS: `--require=${preload}`, AGENT_BRIDGE_STATE_DIR: state, AGENT_BRIDGE_PLUGINS_DIR: (0, node_path_1.join)(root, "plugins"), AGENT_BRIDGE_HOST: "127.0.0.1", AGENT_BRIDGE_PORT: String(previous.port), AGENT_BRIDGE_TOKEN: "" },
196
+ timeout: 10_000,
197
+ });
198
+ strict_1.default.match(stdout, /UPDATER_HEALTH=true/);
199
+ const address = JSON.parse(await (0, promises_1.readFile)(addressFile, "utf8"));
200
+ newPid = address.pid;
201
+ strict_1.default.notEqual(newPid, old.pid);
202
+ strict_1.default.equal(address.port, previous.port);
203
+ strict_1.default.equal((await fetch(`http://127.0.0.1:${address.port}/api/health`, { signal: AbortSignal.timeout(2000) })).status, 200);
204
+ }
205
+ finally {
206
+ try {
207
+ newPid ??= JSON.parse(await (0, promises_1.readFile)(addressFile, "utf8")).pid;
208
+ }
209
+ catch { /* 新后台可能未启动 */ }
210
+ if (newPid && newPid !== old?.pid) {
211
+ try {
212
+ process.kill(newPid, "SIGKILL");
213
+ }
214
+ catch { /* 已退出 */ }
215
+ }
216
+ if (old?.pid && old.exitCode === null && old.signalCode === null) {
217
+ const exited = (0, node_events_1.once)(old, "exit");
218
+ old.kill("SIGKILL");
219
+ await exited;
220
+ }
221
+ await (0, promises_1.rm)(root, { recursive: true, force: true });
222
+ }
223
+ });
224
+ (0, node_test_1.default)("旧地址文件的 PID 指向无关进程时只清理地址,不停止该进程", { skip: process.platform === "win32", timeout: 10_000 }, async () => {
225
+ const root = await (0, promises_1.mkdtemp)((0, node_path_1.join)((0, node_os_1.tmpdir)(), "mab-stale-pid-"));
226
+ const unrelated = (0, node_child_process_1.spawn)(process.execPath, ["-e", "setInterval(()=>{},1000);console.log('READY');"], { stdio: ["ignore", "pipe", "pipe"] });
227
+ try {
228
+ await (0, node_events_1.once)(unrelated.stdout, "data");
229
+ const addressFile = (0, node_path_1.join)(root, "daemon-address.json");
230
+ await (0, promises_1.writeFile)(addressFile, JSON.stringify({ pid: unrelated.pid, port: 26107 }));
231
+ await (0, node_util_1.promisify)(node_child_process_1.execFile)(process.execPath, [(0, node_path_1.resolve)(__dirname, "../src-ts/cli.js"), "stop"], {
232
+ env: { ...process.env, AGENT_BRIDGE_STATE_DIR: root }, timeout: 5000,
233
+ });
234
+ strict_1.default.doesNotThrow(() => process.kill(unrelated.pid, 0), "无关进程应继续运行");
235
+ await strict_1.default.rejects((0, promises_1.readFile)(addressFile), { code: "ENOENT" });
236
+ }
237
+ finally {
238
+ if (unrelated.exitCode === null && unrelated.signalCode === null) {
239
+ const exited = (0, node_events_1.once)(unrelated, "exit");
240
+ unrelated.kill("SIGKILL");
241
+ await exited;
242
+ }
243
+ await (0, promises_1.rm)(root, { recursive: true, force: true });
244
+ }
245
+ });
@@ -59,6 +59,31 @@ const desktop_launch_1 = require("../src-ts/plugins/desktop-launch");
59
59
  await (0, promises_1.rm)(root, { recursive: true, force: true });
60
60
  }
61
61
  });
62
+ (0, node_test_1.default)("豆包自动发现支持 LOCALAPPDATA 下的用户安装目录", async () => {
63
+ const root = await (0, promises_1.mkdtemp)((0, node_path_1.join)((0, node_os_1.tmpdir)(), "desktop-localappdata-"));
64
+ const platform = Object.getOwnPropertyDescriptor(process, "platform");
65
+ const previousLocalAppData = process.env.LOCALAPPDATA;
66
+ try {
67
+ const application = (0, node_path_1.join)(root, "Doubao", "Application");
68
+ const executable = (0, node_path_1.join)(application, "app", "Doubao.exe");
69
+ await (0, promises_1.mkdir)((0, node_path_1.join)(application, "app"), { recursive: true });
70
+ await (0, promises_1.writeFile)((0, node_path_1.join)(application, "Doubao.exe"), "");
71
+ await (0, promises_1.writeFile)(executable, "");
72
+ process.env.LOCALAPPDATA = root;
73
+ Object.defineProperty(process, "platform", { value: "win32" });
74
+ const unregistered = async () => ({ stdout: "" });
75
+ strict_1.default.equal(await (0, desktop_launch_1.findWindowsExecutable)("Doubao.exe", undefined, unregistered), executable);
76
+ strict_1.default.equal(await (0, desktop_launch_1.findWindowsExecutable)("Doubao.exe", (0, node_path_1.join)(root, "missing", "Doubao.exe"), unregistered), executable);
77
+ }
78
+ finally {
79
+ Object.defineProperty(process, "platform", platform);
80
+ if (previousLocalAppData === undefined)
81
+ delete process.env.LOCALAPPDATA;
82
+ else
83
+ process.env.LOCALAPPDATA = previousLocalAppData;
84
+ await (0, promises_1.rm)(root, { recursive: true, force: true });
85
+ }
86
+ });
62
87
  (0, node_test_1.default)("桌面程序发现优先选择较新的版本目录", async () => {
63
88
  const root = await (0, promises_1.mkdtemp)((0, node_path_1.join)((0, node_os_1.tmpdir)(), "desktop-launch-versions-"));
64
89
  try {
@@ -131,6 +156,11 @@ const desktop_launch_1 = require("../src-ts/plugins/desktop-launch");
131
156
  await (0, promises_1.rm)(root, { recursive: true, force: true });
132
157
  }
133
158
  });
159
+ (0, node_test_1.default)("管理式启动把 spawn 异步错误转成可捕获异常并清理归属", async () => {
160
+ let owned;
161
+ await strict_1.default.rejects(() => Promise.resolve().then(() => __importStar(require("../src-ts/plugins/desktop-launch"))).then(({ launchManagedAndWait }) => launchManagedAndWait((0, node_path_1.join)((0, node_os_1.tmpdir)(), `missing-desktop-${Date.now()}.exe`), [], undefined, async () => { throw new Error("仍在连接"); }, 400, process => { owned = process?.pid; })), error => error.code === "ENOENT");
162
+ strict_1.default.equal(owned, undefined);
163
+ });
134
164
  (0, node_test_1.default)("使用客户端原有用户目录时,管理式启动不覆盖用户数据目录", async () => {
135
165
  const { launchManagedAndWait } = await Promise.resolve().then(() => __importStar(require("../src-ts/plugins/desktop-launch")));
136
166
  let args = [];
@@ -230,3 +260,46 @@ const desktop_launch_1 = require("../src-ts/plugins/desktop-launch");
230
260
  await (0, promises_1.rm)(root, { recursive: true, force: true });
231
261
  }
232
262
  });
263
+ (0, node_test_1.default)("豆包优先使用注册表或开始菜单路径,并从启动器定位实际客户端", async () => {
264
+ const root = await (0, promises_1.mkdtemp)((0, node_path_1.join)((0, node_os_1.tmpdir)(), "doubao-system-registration-"));
265
+ const platform = Object.getOwnPropertyDescriptor(process, "platform");
266
+ try {
267
+ const launcher = (0, node_path_1.join)(root, "Application", "Doubao.exe");
268
+ const client = (0, node_path_1.join)(root, "Application", "app", "Doubao.exe");
269
+ await (0, promises_1.mkdir)((0, node_path_1.join)(root, "Application", "app"), { recursive: true });
270
+ await (0, promises_1.writeFile)(launcher, "");
271
+ await (0, promises_1.writeFile)(client, "");
272
+ Object.defineProperty(process, "platform", { value: "win32" });
273
+ const run = async (_file, _args) => ({ stdout: `${launcher}\r\n` });
274
+ strict_1.default.equal(await (0, desktop_launch_1.findWindowsExecutable)("Doubao.exe", undefined, run), client);
275
+ }
276
+ finally {
277
+ Object.defineProperty(process, "platform", platform);
278
+ await (0, promises_1.rm)(root, { recursive: true, force: true });
279
+ }
280
+ });
281
+ (0, node_test_1.default)("ZCode 从安装注册信息或开始菜单发现自定义路径,失效时查当前用户目录", async () => {
282
+ const root = await (0, promises_1.mkdtemp)((0, node_path_1.join)((0, node_os_1.tmpdir)(), "zcode-discovery-"));
283
+ const platform = Object.getOwnPropertyDescriptor(process, "platform");
284
+ const previous = process.env.LOCALAPPDATA;
285
+ try {
286
+ const registered = (0, node_path_1.join)(root, "自定义应用", "ZCode.exe");
287
+ const fallback = (0, node_path_1.join)(root, "Programs", "ZCode", "ZCode.exe");
288
+ await (0, promises_1.mkdir)((0, node_path_1.join)(root, "自定义应用"), { recursive: true });
289
+ await (0, promises_1.mkdir)((0, node_path_1.join)(root, "Programs", "ZCode"), { recursive: true });
290
+ await (0, promises_1.writeFile)(registered, "");
291
+ await (0, promises_1.writeFile)(fallback, "");
292
+ Object.defineProperty(process, "platform", { value: "win32" });
293
+ process.env.LOCALAPPDATA = root;
294
+ strict_1.default.equal(await (0, desktop_launch_1.findWindowsExecutable)("ZCode.exe", undefined, async () => ({ stdout: registered })), registered);
295
+ strict_1.default.equal(await (0, desktop_launch_1.findWindowsExecutable)("ZCode.exe", undefined, async () => ({ stdout: (0, node_path_1.join)(root, "已卸载", "ZCode.exe") })), fallback);
296
+ }
297
+ finally {
298
+ Object.defineProperty(process, "platform", platform);
299
+ if (previous === undefined)
300
+ delete process.env.LOCALAPPDATA;
301
+ else
302
+ process.env.LOCALAPPDATA = previous;
303
+ await (0, promises_1.rm)(root, { recursive: true, force: true });
304
+ }
305
+ });