@lynn123411/dsh-kimi-cu 0.3.7 → 0.3.9

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.
@@ -1,17 +1,3 @@
1
- /**
2
- * 状态卡渲染自检:用真 react 把卡片画进 jsdom,锁住四种状态与两个动作。
3
- *
4
- * 1. 未安装:命令、复制按钮、步骤都在;
5
- * 2. 本机检查通过:版本与恢复提示在,安装命令不再出现;
6
- * 3. 已安装但本机检查未通过:失败项与修复提示在;
7
- * 4. 平台不支持 / 读取失败:各自给出明确说明;
8
- * 5. 复制把命令写进剪贴板,重新检测请求带 refresh=1。
9
- *
10
- * ui-primitives 是替身:真组件是浏览器里的 ESM、带 .css 导入,Node 里跑不起来。
11
- * 替身只把 children 与 onClick 原样渲染,所以这里验的是本插件自己的视图结构。
12
- *
13
- * 跑法:node scripts/test-card.mjs
14
- */
15
1
  import assert from 'node:assert/strict';
16
2
  import { dirname, join, resolve } from 'node:path';
17
3
  import { fileURLToPath } from 'node:url';
@@ -60,7 +46,6 @@ exported.apply(ctx);
60
46
  const card = registrations.find((entry) => entry.options.name === 'plugins.bundle.config').component;
61
47
  const t = (key) => zh[key] ?? key;
62
48
 
63
- /** 默认的更新检查结果:已是最新。 */
64
49
  const DEFAULT_UPDATE = { current: '0.7.5', latest: '0.7.5', hasUpdate: false, error: null, checkedAt: 1 };
65
50
 
66
51
  let fetchUrl = '';
@@ -108,8 +93,9 @@ const BASE = {
108
93
  installed: false,
109
94
  path: null,
110
95
  version: null,
111
- doctor: { ran: false, ok: false, checks: [], raw: '' },
112
- mcp: { command: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu', args: ['mcp', '-s', 'user'] },
96
+ doctor: { ran: false, ok: false, outcome: 'not-run', checks: [], diagnosticChecks: [], raw: '' },
97
+ mcp: null,
98
+ mcpStatus: null,
113
99
  install: {
114
100
  command: 'curl -fsSL https://cdn.kimi.com/kimi-computer-use/latest/setup_macos.sh | bash',
115
101
  steps: ['install', 'grant-permissions', 'recheck'],
@@ -120,6 +106,31 @@ const BASE = {
120
106
  checkedAt: 1,
121
107
  };
122
108
 
109
+ const DOCTOR_PASSED = {
110
+ ran: true,
111
+ ok: true,
112
+ outcome: 'passed',
113
+ checks: [{ id: 'accessibility', label: 'Accessibility (AX)', ok: true }],
114
+ diagnosticChecks: [],
115
+ raw: '',
116
+ };
117
+
118
+ const INSTALLED = {
119
+ ...BASE,
120
+ installed: true,
121
+ path: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu',
122
+ version: '0.7.5',
123
+ doctor: DOCTOR_PASSED,
124
+ mcp: { command: '/Applications/KimiCU.app/Contents/Resources/kimi-cu-mcp', args: ['mcp', '-s', 'user'] },
125
+ mcpStatus: { state: 'connected' },
126
+ };
127
+
128
+ const WINDOWS = {
129
+ ...BASE,
130
+ platform: 'win32',
131
+ install: { ...BASE.install, command: 'powershell.exe -File setup_windows.ps1', steps: ['install', 'recheck'] },
132
+ };
133
+
123
134
  let passed = 0;
124
135
  let total = 0;
125
136
  async function test(label, fn) {
@@ -134,12 +145,6 @@ async function test(label, fn) {
134
145
  }
135
146
  }
136
147
 
137
- /**
138
- * 把卡片画进一个新容器。
139
- * @param status - 这一次 fetch 返回的状态。
140
- * @param options - httpStatus 覆盖响应码。
141
- * @returns 容器与 react root。
142
- */
143
148
  async function mount(status, options = {}) {
144
149
  fetchBody = status;
145
150
  fetchCode = options.httpStatus ?? 200;
@@ -160,7 +165,6 @@ async function mount(status, options = {}) {
160
165
  return { container, reactRoot };
161
166
  }
162
167
 
163
- /** 让挂起的 promise 链跑完一轮。 */
164
168
  async function flush() {
165
169
  await act(async () => {
166
170
  await new Promise((done) => setTimeout(done, 0));
@@ -168,16 +172,31 @@ async function flush() {
168
172
  });
169
173
  }
170
174
 
171
- /**
172
- * 按文字找按钮。
173
- * @param container - 容器。
174
- * @param text - 文字片段。
175
- * @returns 按钮或 undefined。
176
- */
177
175
  function buttonByText(container, text) {
178
176
  return [...container.querySelectorAll('button')].find((button) => (button.textContent ?? '').includes(text));
179
177
  }
180
178
 
179
+ async function recheck(container) {
180
+ const button = buttonByText(container, '重新检测');
181
+ assert.ok(button !== undefined);
182
+ await act(async () => { button.click(); });
183
+ await flush();
184
+ }
185
+
186
+ function checkResult(container, label) {
187
+ const term = [...container.querySelectorAll('dt')].find((node) => node.textContent === label);
188
+ if (term !== undefined) {
189
+ assert.equal(term.nextElementSibling?.tagName, 'DD');
190
+ return [...term.nextElementSibling.childNodes]
191
+ .filter((node) => node.getAttribute?.('aria-hidden') !== 'true')
192
+ .map((node) => node.textContent).join('');
193
+ }
194
+ const item = [...container.querySelectorAll('.dsh-kimi-cu-check-list li')]
195
+ .find((node) => !node.closest('details:not([open])') && node.querySelector('.dsh-kimi-cu-check-name')?.textContent === label);
196
+ assert.ok(item, `应显示检查项「${label}」`);
197
+ return item.querySelector('[role="img"]')?.getAttribute('aria-label');
198
+ }
199
+
181
200
  await test('未安装:显示安装命令、复制按钮与步骤', async () => {
182
201
  const { container } = await mount(BASE);
183
202
  const text = container.textContent ?? '';
@@ -189,38 +208,34 @@ await test('未安装:显示安装命令、复制按钮与步骤', async () =>
189
208
  });
190
209
 
191
210
  await test('本机检查通过:显示版本与恢复提示,不再展示安装命令', async () => {
192
- const { container } = await mount({
193
- ...BASE,
194
- installed: true,
195
- path: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu',
196
- version: '0.6.8',
197
- mcpStatus: { state: 'connected' },
198
- doctor: { ran: true, ok: true, checks: [{ id: 'accessibility', label: 'Accessibility (AX)', ok: true }], raw: '' },
199
- });
211
+ const { container } = await mount(INSTALLED);
200
212
  const text = container.textContent ?? '';
201
213
  assert.match(text, /本机检查通过/);
202
- assert.match(text, /0\.6\.8/);
214
+ assert.match(text, /0\.7\.5/);
203
215
  assert.match(text, /如果模型看不到/);
204
- assert.doesNotMatch(text, /setup_macos\.sh/);
216
+ assert.doesNotMatch(text, /setup_macos\.sh|MCP 启动入口|辅助功能/);
205
217
  });
206
218
 
207
219
  await test('已安装但本机检查未通过:列出失败项与修复提示', async () => {
208
220
  const { container } = await mount({
209
- ...BASE,
210
- installed: true,
211
- path: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu',
212
- version: '0.6.8',
221
+ ...INSTALLED,
222
+ mcpStatus: null,
213
223
  doctor: {
214
- ran: true,
224
+ ...DOCTOR_PASSED,
215
225
  ok: false,
216
- checks: [{ id: 'screen-recording', label: 'Screen Recording', ok: false }],
217
- raw: '',
226
+ outcome: 'failed',
227
+ checks: [...DOCTOR_PASSED.checks, { id: 'screen-recording', label: 'Screen Recording', ok: false }],
218
228
  },
219
229
  });
220
230
  const text = container.textContent ?? '';
221
231
  assert.match(text, /已安装 KimiCU,但本机检查未通过/);
222
- assert.match(text, /屏幕录制/);
223
- assert.match(text, /打开系统设置/);
232
+ assert.equal(checkResult(container, '诊断结果'), '未通过');
233
+ assert.equal(checkResult(container, '屏幕录制'), '未通过');
234
+ assert.match(text, /打开系统设置.*为 KimiCU 开启屏幕录制/);
235
+ assert.doesNotMatch(text, /辅助功能/);
236
+ await recheck(container);
237
+ assert.equal(checkResult(container, '辅助功能'), '通过');
238
+ assert.equal(checkResult(container, '屏幕录制'), '未通过');
224
239
  });
225
240
 
226
241
  await test('平台不支持:只说明不支持,不展示安装命令', async () => {
@@ -255,32 +270,100 @@ await test('复制按钮把安装命令写进剪贴板', async () => {
255
270
  assert.equal(copied, BASE.install.command);
256
271
  });
257
272
 
258
- await test('覆盖路径缺失:显示 override-missing 附注', async () => {
259
- const { container } = await mount({ ...BASE, notes: ['override-missing'] });
273
+ await test('已找到 Windows 安装但覆盖路径缺失:保留安装状态并优先修配置', async () => {
274
+ const { container } = await mount({
275
+ ...WINDOWS,
276
+ installed: true,
277
+ path: String.raw`C:\Users\test\AppData\Local\Kimi Computer Use\kimi-cu.exe`,
278
+ doctor: { ...DOCTOR_PASSED, checks: [{ id: 'mcp', label: 'MCP', ok: true }] },
279
+ notes: ['mcp-command-missing', 'override-missing'],
280
+ });
260
281
  const text = container.textContent ?? '';
261
282
  assert.match(text, /KIMI_CU_WINDOWS_EXE 指向的文件不存在/);
283
+ assert.doesNotMatch(text, /未检测到 KimiCU|setup_windows\.ps1|重新安装/);
284
+ await recheck(container);
285
+ assert.equal(checkResult(container, '安装状态'), '已安装');
286
+ assert.equal(checkResult(container, 'MCP 启动入口'), '未找到');
262
287
  });
263
288
 
264
- await test('MCP 命令缺失:即使 doctor 全绿也显示本机检查未通过', async () => {
289
+ await test('LOCALAPPDATA 缺失时无法确认安装和启动入口,显示路径修复方法', async () => {
290
+ const { container } = await mount({ ...WINDOWS, notes: ['localappdata-missing'] });
291
+ await recheck(container);
292
+ assert.match(container.textContent, /未设置 LOCALAPPDATA/);
293
+ assert.doesNotMatch(container.textContent, /未检测到 KimiCU|setup_windows\.ps1/);
294
+ assert.equal(checkResult(container, '安装状态'), '无法确认');
295
+ assert.equal(checkResult(container, 'MCP 启动入口'), '无法确认');
296
+ assert.equal(checkResult(container, '诊断结果'), '未运行');
297
+ });
298
+
299
+ await test('探测错误:显示原因,安装和启动入口为未知', async () => {
265
300
  const { container } = await mount({
266
301
  ...BASE,
302
+ notes: ['detection-failed'],
303
+ doctor: { ...BASE.doctor, error: 'file probe failed' },
304
+ });
305
+ await recheck(container);
306
+ assert.match(container.textContent, /KimiCU 检测失败/);
307
+ assert.match(container.textContent, /file probe failed/);
308
+ assert.doesNotMatch(container.textContent, /未检测到 KimiCU|setup_macos\.sh/);
309
+ assert.equal(checkResult(container, '安装状态'), '无法确认');
310
+ assert.equal(checkResult(container, 'MCP 启动入口'), '无法确认');
311
+ });
312
+
313
+ await test('MCP 命令缺失:即使 doctor 全绿也显示本机检查未通过', async () => {
314
+ const { container } = await mount({
315
+ ...WINDOWS,
267
316
  installed: true,
268
- path: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu',
317
+ path: String.raw`C:\Users\test\AppData\Local\KimiCU\kimi-cu.exe`,
269
318
  version: '0.3.13',
270
- doctor: { ran: true, ok: true, checks: [], raw: '' },
319
+ doctor: { ...DOCTOR_PASSED, checks: [{ id: 'mcp', label: 'MCP', ok: true }] },
271
320
  notes: ['legacy-install', 'mcp-command-missing'],
272
321
  });
273
322
  const text = container.textContent ?? '';
274
323
  assert.match(text, /已安装 KimiCU,但本机检查未通过/);
324
+ assert.match(text, /检测到旧版 KimiCU 安装目录/);
275
325
  assert.match(text, /MCP 启动文件不存在/);
326
+ await recheck(container);
327
+ assert.equal(checkResult(container, 'MCP 启动入口'), '未找到');
328
+ assert.equal(checkResult(container, '诊断结果'), '通过');
329
+ assert.equal(checkResult(container, '连接'), 'MCP 未连接');
330
+ });
331
+
332
+ await test('macOS 启动文件缺失且权限通过时,保留修复指引', async () => {
333
+ const { container } = await mount({
334
+ ...INSTALLED,
335
+ mcp: null,
336
+ mcpStatus: null,
337
+ notes: ['mcp-command-missing'],
338
+ });
339
+ assert.match(container.textContent, /MCP 启动文件不存在/);
340
+ assert.doesNotMatch(container.textContent, /开启辅助功能|屏幕录制/);
341
+ });
342
+
343
+ await test('Windows doctor 的 mcp=false 只标记该项失败', async () => {
344
+ const path = String.raw`C:\Users\test\AppData\Local\Kimi Computer Use\kimi-cu.exe`;
345
+ const { container } = await mount({
346
+ ...WINDOWS,
347
+ installed: true,
348
+ path,
349
+ mcp: { command: path, args: ['mcp'] },
350
+ doctor: { ...DOCTOR_PASSED, ok: false, outcome: 'failed', checks: [{ id: 'mcp', label: 'MCP', ok: false }] },
351
+ });
352
+ await recheck(container);
353
+ assert.equal(checkResult(container, 'MCP 服务'), '未通过');
354
+ assert.equal(checkResult(container, 'MCP 启动入口'), '已找到');
355
+ assert.doesNotMatch(container.textContent, /权限|管理员/);
276
356
  });
277
357
 
278
358
  await test('MCP 连接失败时显示原因,即使本机检查通过', async () => {
279
- const { container } = await mount({ ...BASE, installed: true, path: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu', doctor: { ran: true, ok: true, checks: [], raw: '' }, mcpStatus: { state: 'failed', lastError: 'launcher failed' } });
359
+ const { container } = await mount({ ...INSTALLED, mcpStatus: { state: 'failed', lastError: 'launcher failed' } });
280
360
  const text = container.textContent ?? '';
281
361
  assert.match(text, /MCP 连接失败/);
282
362
  assert.match(text, /launcher failed/);
283
363
  assert.match(text, /MCP 尚未就绪/);
364
+ await recheck(container);
365
+ assert.equal(checkResult(container, '连接'), 'MCP 连接失败 · launcher failed');
366
+ assert.equal(container.textContent.match(/launcher failed/g)?.length, 1);
284
367
  });
285
368
 
286
369
  await test('本机检查通过但连接未建立时显示实际状态', async () => {
@@ -290,31 +373,103 @@ await test('本机检查通过但连接未建立时显示实际状态', async ()
290
373
  [{ state: 'reconnecting', lastError: 'launcher failed' }, /正在重新连接 MCP.*launcher failed/],
291
374
  [{ state: 'conflict', lastError: 'cua-driver-mcp' }, /桌面操作已由其他后端占用.*cua-driver-mcp/],
292
375
  ]) {
293
- const { container, reactRoot } = await mount({ ...BASE, installed: true, doctor: { ...BASE.doctor, ran: true, ok: true }, mcpStatus });
376
+ const { container, reactRoot } = await mount({ ...INSTALLED, mcpStatus });
294
377
  assert.match(container.textContent ?? '', hint);
295
378
  assert.doesNotMatch(container.textContent ?? '', /本机检查通过|MCP 连接失败/);
296
379
  await act(async () => { reactRoot.unmount(); });
297
380
  }
298
381
  });
299
382
 
300
- await test('重新检测请求带 refresh=1', async () => {
383
+ await test('重新检测请求带 refresh=1,未安装时也列出各项状态', async () => {
301
384
  const { container } = await mount(BASE);
302
- const button = buttonByText(container, '重新检测');
303
- assert.ok(button !== undefined);
304
- await act(async () => {
305
- button.click();
306
- await new Promise((done) => setTimeout(done, 0));
307
- });
385
+ await recheck(container);
308
386
  assert.match(statusUrl, /refresh=1/);
387
+ assert.equal(checkResult(container, '安装状态'), '未找到');
388
+ assert.equal(checkResult(container, 'MCP 启动入口'), '未找到');
389
+ assert.equal(checkResult(container, '诊断结果'), '未运行');
390
+ assert.equal(checkResult(container, '连接'), 'MCP 未连接');
309
391
  });
310
392
 
311
- const INSTALLED = {
312
- ...BASE,
313
- installed: true,
314
- path: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu',
315
- version: '0.7.5',
316
- doctor: { ran: true, ok: true, checks: [{ id: 'accessibility', label: 'Accessibility (AX)', ok: true }], raw: '' },
317
- };
393
+ await test('重新检测展示通过和仅供排查的检查,连接更新后保留明细', async () => {
394
+ const checked = {
395
+ ...INSTALLED,
396
+ doctor: {
397
+ ...DOCTOR_PASSED,
398
+ checks: [...DOCTOR_PASSED.checks, { id: 'runtime-control', label: 'Runtime control: ready', ok: true }],
399
+ diagnosticChecks: [
400
+ { id: 'accessibility', label: 'Accessibility (AX)', ok: false },
401
+ { id: 'skylight', label: 'SkyLight post-to-pid', ok: null },
402
+ ],
403
+ },
404
+ };
405
+ const { container, reactRoot } = await mount(checked);
406
+ assert.doesNotMatch(container.textContent, /检查明细|辅助功能|仅供排查/);
407
+ fetchBody = { ...checked, mcpStatus: { state: 'connecting' } };
408
+ await recheck(container);
409
+ const service = container.querySelector('section[aria-label="KimiCU 服务"]');
410
+ const diagnostic = container.querySelector('details.dsh-kimi-cu-check-group');
411
+ assert.ok(service);
412
+ assert.ok(diagnostic);
413
+ assert.equal(diagnostic.open, false);
414
+ assert.match(diagnostic.querySelector('summary').textContent, /排查详情/);
415
+ assert.equal(checkResult(container, '安装状态'), '已安装');
416
+ assert.equal(checkResult(container, 'MCP 启动入口'), '已找到');
417
+ assert.equal(diagnostic.querySelector('code').textContent, checked.mcp.command);
418
+ assert.equal(checkResult(container, '诊断结果'), '通过');
419
+ assert.equal(checkResult(service, '辅助功能'), '通过');
420
+ assert.equal(checkResult(service, '运行时控制通道'), '通过');
421
+ assert.equal(service.querySelector('[aria-label="通过"]').textContent, '✅');
422
+ await act(async () => { diagnostic.querySelector('summary').click(); });
423
+ assert.equal(diagnostic.open, true);
424
+ assert.ok(diagnostic.querySelector('section[aria-label="运行检测的进程(仅供排查)"]'));
425
+ assert.equal(checkResult(diagnostic, '辅助功能'), '未通过');
426
+ assert.equal(checkResult(diagnostic, 'SkyLight post-to-pid'), '无法确认');
427
+ assert.equal(diagnostic.querySelector('[aria-label="未通过"]').textContent, '❌');
428
+ assert.equal(diagnostic.querySelector('[aria-label="无法确认"]').textContent, '❔');
429
+ assert.equal(checkResult(container, '连接'), '正在连接 MCP…');
430
+ assert.doesNotMatch(container.textContent, /打开系统设置/);
431
+
432
+ fetchBody = checked;
433
+ await act(async () => { await new Promise((done) => setTimeout(done, 1100)); });
434
+ assert.equal(checkResult(container, '连接'), 'MCP 已连接');
435
+ assert.equal(container.querySelector('details.dsh-kimi-cu-check-group').open, true);
436
+ assert.equal(checkResult(container, '辅助功能'), '通过');
437
+ assert.match(container.textContent, /本机检查通过/);
438
+ await act(async () => { reactRoot.unmount(); });
439
+ });
440
+
441
+ await test('doctor 无法确认权限时展示未知和错误原因', async () => {
442
+ const { container } = await mount({
443
+ ...INSTALLED,
444
+ mcpStatus: null,
445
+ doctor: {
446
+ ...DOCTOR_PASSED,
447
+ ok: false,
448
+ outcome: 'unknown',
449
+ checks: [...DOCTOR_PASSED.checks, { id: 'screen-recording', label: 'Screen Recording', ok: null }],
450
+ error: 'doctor timed out',
451
+ },
452
+ });
453
+ assert.match(container.textContent, /本机检查结果无法确认/);
454
+ assert.match(container.textContent, /doctor timed out/);
455
+ assert.doesNotMatch(container.textContent, /打开系统设置|本机检查未通过|辅助功能/);
456
+ await recheck(container);
457
+ assert.equal(checkResult(container, '诊断结果'), '无法确认');
458
+ assert.equal(checkResult(container, '辅助功能'), '通过');
459
+ assert.equal(checkResult(container, '屏幕录制'), '无法确认');
460
+ });
461
+
462
+ await test('旧宿主缺 outcome 和 diagnosticChecks 时仍能完成重新检测', async () => {
463
+ const { container } = await mount({
464
+ ...INSTALLED,
465
+ doctor: { ran: true, ok: true, checks: DOCTOR_PASSED.checks, raw: '' },
466
+ });
467
+ assert.match(container.textContent, /本机检查通过/);
468
+ await recheck(container);
469
+ assert.equal(checkResult(container, '诊断结果'), '通过');
470
+ assert.equal(checkResult(container, '辅助功能'), '通过');
471
+ assert.equal(checkResult(container, '连接'), 'MCP 已连接');
472
+ });
318
473
 
319
474
  const NEWER = { current: '0.7.5', latest: '0.7.6', hasUpdate: true, error: null, checkedAt: 1 };
320
475
 
@@ -1,12 +1,3 @@
1
- /**
2
- * 桌面操作独占槽位的自检:本插件要和官方 CUA、Cua Driver 一样经 `computerUse.register()`
3
- * 占位,占不到就不把 `mcp__kimi-cu__*` 工具交出去。
4
- *
5
- * 用真 cordis Context 与真 `ctx.inject`,只把两个服务换成可观察的替身:
6
- * `computerUse` 照抄宿主的独占语义(第二个注册直接抛错),`mcpManager` 记录每次注册。
7
- *
8
- * 跑法:pnpm test / node scripts/test-exclusivity.mjs
9
- */
10
1
  import assert from 'node:assert/strict';
11
2
 
12
3
  import { Context, Service } from '@deepseek-ai/cordis';
@@ -31,35 +22,23 @@ async function test(label, fn) {
31
22
  }
32
23
  }
33
24
 
34
- /**
35
- * 探测器替身:与平台无关,直接给出「已安装且 MCP 命令可用」的状态。
36
- *
37
- * 真探测器在 Linux 上必然给 `mcp: null`(不受支持的平台),注册那条链就跑不到,
38
- * 而这里要验的是独占槽位与注册的先后关系,不是平台分流。
39
- */
40
25
  const fakeDetect = async () => ({
41
26
  platform: 'darwin',
42
27
  supported: true,
43
28
  installed: true,
44
29
  path: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu',
45
30
  version: '0.7.5',
46
- doctor: { ran: true, ok: true, checks: [], raw: '' },
31
+ doctor: { ran: true, ok: true, outcome: 'passed', checks: [], diagnosticChecks: [], raw: '' },
47
32
  mcp: { command: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu', args: ['mcp', '-s', 'user'] },
48
33
  install: null,
49
34
  notes: [],
50
35
  checkedAt: 0,
51
36
  });
52
37
 
53
- /** 等若干个微任务,让注入回调里那条同步链跑完。 */
54
38
  async function settle() {
55
39
  for (let i = 0; i < 8; i += 1) await Promise.resolve();
56
40
  }
57
41
 
58
- /**
59
- * 等一个条件成立。探测要真的跑一次 `doctor`,是外部进程,不能只等微任务。
60
- * @param {() => boolean} predicate - 条件。
61
- * @param {string} what - 超时时的说明。
62
- */
63
42
  async function waitFor(predicate, what) {
64
43
  const deadline = Date.now() + 20_000;
65
44
  while (Date.now() < deadline) {
@@ -69,7 +48,6 @@ async function waitFor(predicate, what) {
69
48
  throw new Error('等不到:' + what);
70
49
  }
71
50
 
72
- /** 宿主的桌面操作独占登记,语义照抄:占住之后第二个注册抛错并报出已占位的名字。 */
73
51
  class FakeComputerUse extends Service {
74
52
  constructor(ctx) {
75
53
  super(ctx, 'computerUse');
@@ -93,7 +71,6 @@ class FakeComputerUse extends Service {
93
71
  }
94
72
  }
95
73
 
96
- /** MCP 管理器替身:只记录注册过的服务器与撤销次数。 */
97
74
  class FakeMcpManager extends Service {
98
75
  constructor(ctx) {
99
76
  super(ctx, 'mcpManager');
@@ -138,7 +115,6 @@ class FakeMcpManager extends Service {
138
115
  }
139
116
  }
140
117
 
141
- /** 起一个只带工具表的上下文:工具表要提示词服务来写 schema。 */
142
118
  function host() {
143
119
  const ctx = new Context();
144
120
  new SystemPrompt(ctx, {});
@@ -214,11 +190,16 @@ await test('缺装、缺启动文件或 doctor 失败均不占槽,重新检测
214
190
  const cu = new FakeComputerUse(ctx);
215
191
  const mcp = new FakeMcpManager(ctx);
216
192
  const ready = await fakeDetect();
217
- let status = { ...ready, installed: false };
193
+ let status = { ...ready, installed: false, mcp: null };
218
194
  plugin.apply(ctx, { detect: async () => status });
219
195
  const tool = ctx.tools.get('kimi_cu_status');
220
196
  const exec = { signal: new AbortController().signal };
221
- for (const unavailable of [status, { ...ready, notes: ['mcp-command-missing'] }, { ...ready, doctor: { ...ready.doctor, ok: false } }]) {
197
+ for (const unavailable of [
198
+ status,
199
+ { ...ready, mcp: null, notes: ['mcp-command-missing'] },
200
+ { ...ready, doctor: { ...ready.doctor, ok: false, outcome: 'failed' } },
201
+ { ...ready, doctor: { ...ready.doctor, ok: false, outcome: 'unknown' } },
202
+ ]) {
222
203
  status = unavailable;
223
204
  await tool.execute({ refresh: true }, exec);
224
205
  assert.equal(cu.providerName, undefined);
@@ -230,6 +211,44 @@ await test('缺装、缺启动文件或 doctor 失败均不占槽,重新检测
230
211
  assert.equal(mcp.calls.length, 1);
231
212
  });
232
213
 
214
+ await test('重检无法确认时保留已连接的工具,明确失败或入口消失才撤回', async () => {
215
+ const ctx = host();
216
+ const cu = new FakeComputerUse(ctx);
217
+ const mcp = new FakeMcpManager(ctx);
218
+ mcp.status = { state: 'connected' };
219
+ const ready = await fakeDetect();
220
+ let status = ready;
221
+ plugin.apply(ctx, { detect: async () => status });
222
+ await waitFor(() => mcp.calls.length === 1, '应当注册 MCP 连接');
223
+ const tool = ctx.tools.get('kimi_cu_status');
224
+ const exec = { signal: new AbortController().signal };
225
+ const unknown = { ...ready.doctor, ok: false, outcome: 'unknown', error: 'timeout' };
226
+
227
+ for (const unconfirmed of [
228
+ { ...ready, doctor: unknown },
229
+ { ...ready, installed: false, mcp: null, doctor: unknown, notes: ['detection-failed'] },
230
+ ]) {
231
+ status = unconfirmed;
232
+ const result = JSON.parse((await tool.execute({ refresh: true }, exec)).status);
233
+ assert.equal(result.doctor.outcome, 'unknown');
234
+ assert.equal(result.mcpStatus.state, 'connected');
235
+ assert.equal(cu.providerName, 'kimi-cu');
236
+ assert.equal(mcp.disposed, 0);
237
+ }
238
+
239
+ for (const blocked of [
240
+ { ...ready, doctor: { ...ready.doctor, ok: false, outcome: 'failed' } },
241
+ { ...ready, mcp: null, notes: ['mcp-command-missing'] },
242
+ ]) {
243
+ status = ready;
244
+ await tool.execute({ refresh: true }, exec);
245
+ status = blocked;
246
+ await tool.execute({ refresh: true }, exec);
247
+ assert.equal(cu.providerName, undefined);
248
+ }
249
+ assert.equal(mcp.disposed, 2);
250
+ });
251
+
233
252
  await test('MCP 注册失败后释放槽,重检能补连', async () => {
234
253
  const ctx = host();
235
254
  const cu = new FakeComputerUse(ctx);