@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,11 +1,3 @@
1
- /**
2
- * 检测核心自检:平台解析、安装计划、`doctor` 解析与状态缓存。
3
- *
4
- * 这些是纯逻辑,不 spawn 任何进程:真 `doctor` 的调用在 smoke-host 与真机验收里。
5
- * 期望值来自官方 `INSTALL.md` 与实测输出,不是照抄实现。
6
- *
7
- * 跑法:node scripts/test-status.mjs
8
- */
9
1
  import assert from 'node:assert/strict';
10
2
 
11
3
  import {
@@ -17,7 +9,7 @@ import {
17
9
  WINDOWS_INSTALL_COMMAND,
18
10
  } from '../src/shared/status.ts';
19
11
  import { installPlan, resolveInstall } from '../src/server/resolve.ts';
20
- import { parseDoctor } from '../src/server/doctor.ts';
12
+ import { parseDoctor, runDoctor } from '../src/server/doctor.ts';
21
13
  import { createStatusDetector, parseMacosVersion } from '../src/server/status.ts';
22
14
 
23
15
  let passed = 0;
@@ -82,21 +74,21 @@ await test('macOS:找到二进制和官方启动脚本时使用 CodeAct', () =
82
74
  assert.deepEqual(resolved.notes, []);
83
75
  });
84
76
 
85
- await test('macOS:没找到二进制时 installed 为假,但 MCP 命令仍是约定路径', () => {
77
+ await test('macOS:没找到二进制时未安装,也没有可启动的 MCP 命令', () => {
86
78
  const resolved = resolveInstall({ platform: 'darwin', env: {}, exists: () => false });
87
79
  assert.equal(resolved.installed, false);
88
80
  assert.equal(resolved.path, null);
89
- assert.equal(resolved.mcp.command, MACOS_MCP_LAUNCHER);
81
+ assert.equal(resolved.mcp, null);
90
82
  });
91
83
 
92
84
  await test('macOS:启动脚本缺失时报告未就绪,不回退旧入口', () => {
93
85
  const resolved = resolveInstall({ platform: 'darwin', env: {}, exists: (path) => path === MACOS_BINARY });
94
86
  assert.equal(resolved.installed, true);
95
87
  assert.deepEqual(resolved.notes, ['mcp-command-missing']);
96
- assert.equal(resolved.mcp.command, MACOS_MCP_LAUNCHER);
88
+ assert.equal(resolved.mcp, null);
97
89
  });
98
90
 
99
- await test('Windows:新目录存在时用它,MCP 命令与 patch 表达式一致', () => {
91
+ await test('Windows:新安装目录用同一个文件做诊断和启动 MCP', () => {
100
92
  const resolved = resolveInstall({
101
93
  platform: 'win32',
102
94
  env: { LOCALAPPDATA: LOCAL },
@@ -128,8 +120,8 @@ await test('Windows:只有旧目录时标记 legacy-install 与 mcp-command-mi
128
120
  assert.equal(resolved.installed, true);
129
121
  assert.equal(resolved.path, WIN_LEGACY);
130
122
  assert.deepEqual(resolved.notes, ['legacy-install', 'mcp-command-missing']);
131
- assert.equal(resolved.mcp.command, WIN_PRIMARY);
132
- assert.ok(resolved.install.steps.includes('upgrade'), '旧目录安装要给出升级步骤');
123
+ assert.equal(resolved.mcp, null);
124
+ assert.deepEqual(resolved.install.steps, ['upgrade', 'recheck']);
133
125
  });
134
126
 
135
127
  await test('Windows:两个目录都没有时 installed 为假', () => {
@@ -139,23 +131,28 @@ await test('Windows:两个目录都没有时 installed 为假', () => {
139
131
  assert.deepEqual(resolved.notes, []);
140
132
  });
141
133
 
142
- await test('Windows:KIMI_CU_WINDOWS_EXE 指向不存在的文件时不再回退到默认目录', () => {
134
+ await test('Windows:覆盖路径无效仍能发现默认安装,但阻止 MCP 启动', () => {
143
135
  const missing = 'D:\\missing.exe';
144
136
  const resolved = resolveInstall({
145
137
  platform: 'win32',
146
138
  env: { LOCALAPPDATA: LOCAL, KIMI_CU_WINDOWS_EXE: missing },
147
139
  exists: (path) => path === WIN_PRIMARY,
148
140
  });
149
- assert.equal(resolved.installed, false);
150
- assert.equal(resolved.path, null);
141
+ assert.equal(resolved.installed, true);
142
+ assert.equal(resolved.path, WIN_PRIMARY);
151
143
  assert.deepEqual(resolved.notes, ['override-missing']);
152
- assert.equal(resolved.mcp.command, missing, 'MCP 行启动的是覆盖值,状态必须跟着它');
144
+ assert.equal(resolved.mcp, null);
153
145
  });
154
146
 
155
- await test('Windows:LOCALAPPDATA 缺失时命令与 patch 表达式同一公式', () => {
156
- const resolved = resolveInstall({ platform: 'win32', env: {}, exists: () => false });
147
+ await test('Windows:LOCALAPPDATA 缺失时不尝试当前盘根目录', () => {
148
+ const probed = [];
149
+ const resolved = resolveInstall({ platform: 'win32', env: {}, exists: (path) => { probed.push(path); return true; } });
157
150
  assert.equal(resolved.installed, false);
158
- assert.equal(resolved.mcp.command, '\\Kimi Computer Use\\kimi-cu.exe');
151
+ assert.equal(resolved.mcp, null);
152
+ assert.deepEqual(resolved.notes, ['localappdata-missing']);
153
+ assert.deepEqual(probed, []);
154
+ const custom = 'D:\\tools\\kimi-cu.exe';
155
+ assert.equal(resolveInstall({ platform: 'win32', env: { KIMI_CU_WINDOWS_EXE: custom }, exists: () => true }).mcp.command, custom);
159
156
  });
160
157
 
161
158
  await test('不支持的平台:supported 为假,没有 MCP 命令与安装计划', () => {
@@ -188,11 +185,24 @@ await test('macOS doctor:以服务权限为准,忽略调用进程的权限',
188
185
  ].join('\n');
189
186
  const report = parseDoctor('darwin', raw);
190
187
  assert.equal(report.ok, true);
191
- assert.equal(report.checks.length, 3);
188
+ assert.deepEqual(report.checks.map(({ id }) => id), ['accessibility', 'screen-recording', 'runtime-control']);
189
+ assert.deepEqual(report.diagnosticChecks.map(({ id, ok }) => ({ id, ok })), [{ id: 'screen-recording', ok: false }]);
192
190
  const failed = parseDoctor('darwin', raw.replace('✅ Accessibility (AX)', '❌ Accessibility (AX)'));
193
191
  assert.equal(failed.ok, false);
194
192
  });
195
193
 
194
+ await test('macOS doctor:服务段缺关键项时结论未知,调用进程权限不补位', () => {
195
+ const raw = [
196
+ 'Service (authoritative):', '✅ Runtime control channel: listening',
197
+ 'This process (diagnostic only):', '✅ Accessibility (AX)', '✅ Screen Recording',
198
+ ].join('\n');
199
+ const report = parseDoctor('darwin', raw);
200
+ assert.equal(report.outcome, 'unknown');
201
+ assert.equal(report.ok, false);
202
+ assert.deepEqual(report.checks.filter(({ ok }) => ok === null).map(({ id }) => id), ['accessibility', 'screen-recording']);
203
+ assert.equal(report.diagnosticChecks.length, 2);
204
+ });
205
+
196
206
  await test('macOS doctor:有一项失败时 ok 为假', () => {
197
207
  const parsed = parseDoctor('darwin', MAC_DOCTOR_BAD);
198
208
  assert.equal(parsed.ok, false);
@@ -208,10 +218,37 @@ await test('Windows doctor:mcp=true 时 ok 为真,版本从输出里读', ()
208
218
 
209
219
  await test('Windows doctor:mcp=false 时 ok 为假', () => {
210
220
  const parsed = parseDoctor('win32', WIN_DOCTOR_BAD);
221
+ assert.equal(parsed.outcome, 'failed');
211
222
  assert.equal(parsed.ok, false);
212
223
  assert.equal(parsed.version, '0.3.13');
213
224
  });
214
225
 
226
+ await test('Windows doctor:MCP 字段缺失或冲突时结论未知', () => {
227
+ for (const raw of ['version=0.3.13', 'mcp=true\nmcp=false']) {
228
+ const report = parseDoctor('win32', raw);
229
+ assert.equal(report.outcome, 'unknown');
230
+ assert.equal(report.checks[0].ok, null);
231
+ }
232
+ });
233
+
234
+ await test('doctor:进程非零退出时,全绿输出不能通过,明确失败仍可报告', async () => {
235
+ const previousOptions = process.env.NODE_OPTIONS;
236
+ try {
237
+ for (const [value, expected] of [['true', 'unknown'], ['false', 'failed']]) {
238
+ const child = `console.log(${JSON.stringify('mcp=' + value)}); process.exit(1);`;
239
+ process.env.NODE_OPTIONS = '--import=data:text/javascript,' + encodeURIComponent(child);
240
+ const report = await runDoctor('win32', process.execPath);
241
+ assert.equal(report.outcome, expected);
242
+ assert.equal(report.ok, false);
243
+ assert.equal(report.ran, true);
244
+ assert.match(report.error, /Command failed/);
245
+ }
246
+ } finally {
247
+ if (previousOptions === undefined) delete process.env.NODE_OPTIONS;
248
+ else process.env.NODE_OPTIONS = previousOptions;
249
+ }
250
+ });
251
+
215
252
  await test('Info.plist 里能读出 CFBundleShortVersionString', () => {
216
253
  const plist = [
217
254
  '<?xml version="1.0" encoding="UTF-8"?>',
@@ -224,7 +261,6 @@ await test('Info.plist 里能读出 CFBundleShortVersionString', () => {
224
261
  assert.equal(parseMacosVersion('not a plist'), null);
225
262
  });
226
263
 
227
- /** 一个可推进的时钟与可计数的 doctor。 */
228
264
  function detectorHarness(overrides = {}) {
229
265
  let clock = 1_000;
230
266
  let doctorCalls = 0;
@@ -235,7 +271,7 @@ function detectorHarness(overrides = {}) {
235
271
  readVersion: () => '0.6.8',
236
272
  runDoctor: async () => {
237
273
  doctorCalls += 1;
238
- return { ran: true, ok: true, checks: [], version: null, raw: '' };
274
+ return { ran: true, ok: true, outcome: 'passed', checks: [], diagnosticChecks: [], version: null, raw: '' };
239
275
  },
240
276
  now: () => clock,
241
277
  ttlMs: 10_000,
@@ -274,20 +310,41 @@ await test('并发调用共用一次探测', async () => {
274
310
  assert.equal(harness.calls(), 1);
275
311
  });
276
312
 
313
+ await test('升级后验收等待并跳过升级前开始的探测', async () => {
314
+ const oldDoctor = Promise.withResolvers();
315
+ const freshDoctor = { ran: true, ok: true, outcome: 'passed', checks: [], diagnosticChecks: [], version: null, raw: '' };
316
+ let version = '0.7.5';
317
+ let calls = 0;
318
+ const { detect } = detectorHarness({
319
+ readVersion: () => version,
320
+ runDoctor: async () => ++calls === 1 ? oldDoctor.promise : freshDoctor,
321
+ });
322
+ const before = detect(true);
323
+ version = '0.7.6';
324
+ const after = detect('after-upgrade');
325
+ assert.equal(calls, 1, '避免同时运行两个 doctor');
326
+ oldDoctor.resolve(freshDoctor);
327
+ assert.equal((await before).version, '0.7.5');
328
+ assert.equal((await after).version, '0.7.6');
329
+ assert.equal((await detect()).version, '0.7.6');
330
+ assert.equal(calls, 2);
331
+ });
332
+
277
333
  await test('doctor 失败时状态仍保留 installed,错误写进 doctor.error', async () => {
278
334
  const harness = detectorHarness({
279
- runDoctor: async () => ({ ran: true, ok: false, checks: [], version: null, raw: '', error: 'timeout' }),
335
+ runDoctor: async () => ({ ran: true, ok: false, outcome: 'unknown', checks: [], diagnosticChecks: [], version: null, raw: '', error: 'timeout' }),
280
336
  });
281
337
  const status = await harness.detect();
282
338
  assert.equal(status.installed, true);
283
339
  assert.equal(status.doctor.ok, false);
340
+ assert.equal(status.doctor.outcome, 'unknown');
284
341
  assert.equal(status.doctor.error, 'timeout');
285
342
  });
286
343
 
287
344
  await test('版本读不出来时回退到 doctor 输出里的版本', async () => {
288
345
  const harness = detectorHarness({
289
346
  readVersion: () => null,
290
- runDoctor: async () => ({ ran: true, ok: true, checks: [], version: '0.3.13', raw: '' }),
347
+ runDoctor: async () => ({ ran: true, ok: true, outcome: 'passed', checks: [], diagnosticChecks: [], version: '0.3.13', raw: '' }),
291
348
  });
292
349
  const status = await harness.detect();
293
350
  assert.equal(status.version, '0.3.13');
@@ -306,12 +363,13 @@ await test('exists 抛错时探测器不 reject,返回 detection-failed 状态
306
363
  env: {},
307
364
  exists: () => { throw new Error('boom'); },
308
365
  readVersion: () => null,
309
- runDoctor: async () => ({ ran: true, ok: true, checks: [], version: null, raw: '' }),
366
+ runDoctor: async () => { throw new Error('should not run'); },
310
367
  now: () => 1,
311
368
  });
312
369
  const status = await detect();
313
370
  assert.equal(status.installed, false);
314
371
  assert.deepEqual(status.notes, ['detection-failed']);
372
+ assert.equal(status.doctor.outcome, 'unknown');
315
373
  assert.match(status.doctor.error, /boom/);
316
374
  });
317
375
 
@@ -1,11 +1,3 @@
1
- /**
2
- * 更新链路自检:清单解析、版本比较、阶段映射、失败分类,以及更新器的行为。
3
- *
4
- * 纯函数部分用固定输入验;更新器用替身验「已是最新不执行」「清单读不到时 macOS 照跑、
5
- * Windows 报错」「单飞锁」「超时不 kill」「版本变了才重连」。
6
- *
7
- * 跑法:node scripts/test-update.mjs
8
- */
9
1
  import assert from 'node:assert/strict';
10
2
  import { dirname, join, resolve } from 'node:path';
11
3
  import { fileURLToPath } from 'node:url';
@@ -103,7 +95,6 @@ await test('classifyFailure 把已知失败分到五类', () => {
103
95
  assert.equal(classifyFailure('something else entirely', false), 'unknown');
104
96
  });
105
97
 
106
- /** 造一个更新器替身环境:记录每次读清单与每次 upgrade 的入参。 */
107
98
  function harness(overrides = {}) {
108
99
  const calls = { fetch: [], upgrade: [], reconnect: 0 };
109
100
  const base = {
@@ -133,7 +124,6 @@ function harness(overrides = {}) {
133
124
  return { updater: createUpdater(deps), calls };
134
125
  }
135
126
 
136
- /** 收集一次 run 的全部事件。 */
137
127
  async function collect(updater) {
138
128
  const events = [];
139
129
  await updater.run((event) => events.push(event));
@@ -183,8 +173,12 @@ await test('run:已是最新时不执行 upgrade', async () => {
183
173
 
184
174
  await test('run:有新版时先报阶段再执行,版本变了才重连', async () => {
185
175
  let version = '0.7.5';
176
+ const refreshes = [];
186
177
  const { updater, calls } = harness({
187
- current: async () => ({ path: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu', version }),
178
+ current: async (refresh) => {
179
+ refreshes.push(refresh);
180
+ return { path: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu', version };
181
+ },
188
182
  runUpgrade: async (input) => {
189
183
  calls.upgrade.push({ version: input.version });
190
184
  input.onLine('==> 校验代码签名与 Apple 公证 …');
@@ -199,6 +193,7 @@ await test('run:有新版时先报阶段再执行,版本变了才重连', as
199
193
  assert.equal(done.type, 'done');
200
194
  assert.equal(done.version, '0.7.6');
201
195
  assert.equal(done.reconnect, 'done');
196
+ assert.deepEqual(refreshes, [true, 'after-upgrade']);
202
197
  assert.equal(calls.reconnect, 1);
203
198
  assert.equal(calls.upgrade[0].version, null, 'macOS 不带版本号,让 KimiCU 自己判断');
204
199
  });
@@ -281,6 +276,17 @@ await test('classifyFailure 跳过进度行,只看错误行', () => {
281
276
  );
282
277
  });
283
278
 
279
+ await test('classifyFailure 只收到进度或成功行时归 unknown,超时优先', () => {
280
+ const progress = '==> 校验代码签名与 Apple 公证 …';
281
+ const success = '✓ 签名 + 公证校验通过。';
282
+ const signatureError = '✗ 代码签名校验失败。';
283
+ assert.equal(classifyFailure(progress, false), 'unknown');
284
+ assert.equal(classifyFailure(success, false), 'unknown');
285
+ assert.equal(classifyFailure(`${progress}\n${success}`, false), 'unknown');
286
+ assert.equal(classifyFailure(signatureError, false), 'verify');
287
+ assert.equal(classifyFailure(signatureError, true), 'timeout');
288
+ });
289
+
284
290
  await test('run:超时后锁留到子进程真的退出', async () => {
285
291
  let exit;
286
292
  const whenExited = new Promise((done) => { exit = done; });