@lynn123411/dsh-kimi-cu 0.2.0 → 0.3.0

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.
@@ -2,8 +2,8 @@
2
2
  * 状态卡渲染自检:用真 react 把卡片画进 jsdom,锁住四种状态与两个动作。
3
3
  *
4
4
  * 1. 未安装:命令、复制按钮、步骤都在;
5
- * 2. 已就绪:版本与恢复提示在,安装命令不再出现;
6
- * 3. 已安装但未就绪:失败项与修复提示在;
5
+ * 2. 本机检查通过:版本与恢复提示在,安装命令不再出现;
6
+ * 3. 已安装但本机检查未通过:失败项与修复提示在;
7
7
  * 4. 平台不支持 / 读取失败:各自给出明确说明;
8
8
  * 5. 复制把命令写进剪贴板,重新检测请求带 refresh=1。
9
9
  *
@@ -60,11 +60,32 @@ exported.apply(ctx);
60
60
  const card = registrations.find((entry) => entry.options.name === 'plugins.bundle.config').component;
61
61
  const t = (key) => zh[key] ?? key;
62
62
 
63
+ /** 默认的更新检查结果:已是最新。 */
64
+ const DEFAULT_UPDATE = { current: '0.7.5', latest: '0.7.5', hasUpdate: false, error: null, checkedAt: 1 };
65
+
63
66
  let fetchUrl = '';
67
+ let fetchMethod = 'GET';
64
68
  let fetchBody = null;
65
69
  let fetchCode = 200;
66
- globalThis.fetch = async (url) => {
70
+ let updateBody = DEFAULT_UPDATE;
71
+ let updateCode = 200;
72
+ let postController = null;
73
+
74
+ globalThis.fetch = async (url, init = {}) => {
67
75
  fetchUrl = String(url);
76
+ fetchMethod = init.method ?? 'GET';
77
+ if (fetchUrl.includes('/dsh-kimi-cu/update')) {
78
+ if (fetchMethod === 'POST') {
79
+ const stream = new ReadableStream({
80
+ start(controller) { postController = controller; },
81
+ });
82
+ return new Response(stream, { status: 200 });
83
+ }
84
+ return new Response(JSON.stringify(updateBody), {
85
+ status: updateCode,
86
+ headers: { 'Content-Type': 'application/json' },
87
+ });
88
+ }
68
89
  return new Response(JSON.stringify(fetchBody), {
69
90
  status: fetchCode,
70
91
  headers: { 'Content-Type': 'application/json' },
@@ -118,17 +139,30 @@ async function test(label, fn) {
118
139
  async function mount(status, options = {}) {
119
140
  fetchBody = status;
120
141
  fetchCode = options.httpStatus ?? 200;
142
+ updateBody = options.update ?? DEFAULT_UPDATE;
143
+ updateCode = options.updateStatus ?? 200;
121
144
  fetchUrl = '';
145
+ fetchMethod = 'GET';
146
+ postController = null;
122
147
  const container = document.createElement('div');
123
148
  document.body.appendChild(container);
124
149
  const reactRoot = createRoot(container);
125
150
  await act(async () => {
126
151
  reactRoot.render(createElement(card, { t }));
127
152
  await new Promise((done) => setTimeout(done, 0));
153
+ await new Promise((done) => setTimeout(done, 0));
128
154
  });
129
155
  return { container, reactRoot };
130
156
  }
131
157
 
158
+ /** 让挂起的 promise 链跑完一轮。 */
159
+ async function flush() {
160
+ await act(async () => {
161
+ await new Promise((done) => setTimeout(done, 0));
162
+ await new Promise((done) => setTimeout(done, 0));
163
+ });
164
+ }
165
+
132
166
  /**
133
167
  * 按文字找按钮。
134
168
  * @param container - 容器。
@@ -149,7 +183,7 @@ await test('未安装:显示安装命令、复制按钮与步骤', async () =>
149
183
  assert.ok(buttonByText(container, '复制命令') !== undefined);
150
184
  });
151
185
 
152
- await test('已就绪:显示版本与恢复提示,不再展示安装命令', async () => {
186
+ await test('本机检查通过:显示版本与恢复提示,不再展示安装命令', async () => {
153
187
  const { container } = await mount({
154
188
  ...BASE,
155
189
  installed: true,
@@ -158,13 +192,13 @@ await test('已就绪:显示版本与恢复提示,不再展示安装命令',
158
192
  doctor: { ran: true, ok: true, checks: [{ id: 'accessibility', label: 'Accessibility (AX)', ok: true }], raw: '' },
159
193
  });
160
194
  const text = container.textContent ?? '';
161
- assert.match(text, /KimiCU 已就绪/);
195
+ assert.match(text, /本机检查通过/);
162
196
  assert.match(text, /0\.6\.8/);
163
197
  assert.match(text, /如果模型看不到/);
164
198
  assert.doesNotMatch(text, /setup_macos\.sh/);
165
199
  });
166
200
 
167
- await test('已安装但未就绪:列出失败项与修复提示', async () => {
201
+ await test('已安装但本机检查未通过:列出失败项与修复提示', async () => {
168
202
  const { container } = await mount({
169
203
  ...BASE,
170
204
  installed: true,
@@ -178,7 +212,7 @@ await test('已安装但未就绪:列出失败项与修复提示', async () =>
178
212
  },
179
213
  });
180
214
  const text = container.textContent ?? '';
181
- assert.match(text, /已安装 KimiCU,但还没就绪/);
215
+ assert.match(text, /已安装 KimiCU,但本机检查未通过/);
182
216
  assert.match(text, /屏幕录制/);
183
217
  assert.match(text, /打开系统设置/);
184
218
  });
@@ -221,7 +255,7 @@ await test('覆盖路径缺失:显示 override-missing 附注', async () => {
221
255
  assert.match(text, /KIMI_CU_WINDOWS_EXE 指向的文件不存在/);
222
256
  });
223
257
 
224
- await test('MCP 命令缺失:即使 doctor 全绿也显示未就绪', async () => {
258
+ await test('MCP 命令缺失:即使 doctor 全绿也显示本机检查未通过', async () => {
225
259
  const { container } = await mount({
226
260
  ...BASE,
227
261
  installed: true,
@@ -231,7 +265,7 @@ await test('MCP 命令缺失:即使 doctor 全绿也显示未就绪', async ()
231
265
  notes: ['legacy-install', 'mcp-command-missing'],
232
266
  });
233
267
  const text = container.textContent ?? '';
234
- assert.match(text, /已安装 KimiCU,但还没就绪/);
268
+ assert.match(text, /已安装 KimiCU,但本机检查未通过/);
235
269
  assert.match(text, /MCP 行要启动的文件不存在/);
236
270
  });
237
271
 
@@ -246,4 +280,102 @@ await test('重新检测请求带 refresh=1', async () => {
246
280
  assert.match(fetchUrl, /refresh=1/);
247
281
  });
248
282
 
283
+ const INSTALLED = {
284
+ ...BASE,
285
+ installed: true,
286
+ path: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu',
287
+ version: '0.7.5',
288
+ doctor: { ran: true, ok: true, checks: [{ id: 'accessibility', label: 'Accessibility (AX)', ok: true }], raw: '' },
289
+ };
290
+
291
+ const NEWER = { current: '0.7.5', latest: '0.7.6', hasUpdate: true, error: null, checkedAt: 1 };
292
+
293
+ await test('已安装且有新版:按钮显示「更新到 0.7.6」', async () => {
294
+ const { container } = await mount(INSTALLED, { update: NEWER });
295
+ const button = buttonByText(container, '更新到 0.7.6');
296
+ assert.ok(button !== undefined, '有新版时按钮要带上目标版本');
297
+ assert.equal(button.disabled, false);
298
+ });
299
+
300
+ await test('已是最新:按钮显示「已是最新」且不可点', async () => {
301
+ const { container } = await mount(INSTALLED);
302
+ const button = buttonByText(container, '已是最新');
303
+ assert.ok(button !== undefined);
304
+ assert.equal(button.disabled, true);
305
+ });
306
+
307
+ await test('未安装:卡片上没有更新按钮', async () => {
308
+ const { container } = await mount(BASE);
309
+ assert.equal(buttonByText(container, '更新'), undefined);
310
+ });
311
+
312
+ await test('点更新:发出 POST,阶段文字与完成文案就地更新', async () => {
313
+ const { container } = await mount(INSTALLED, { update: NEWER });
314
+ const button = buttonByText(container, '更新到 0.7.6');
315
+ await act(async () => {
316
+ button.click();
317
+ await new Promise((done) => setTimeout(done, 0));
318
+ });
319
+ assert.equal(fetchMethod, 'POST');
320
+ assert.match(fetchUrl, /\/dsh-kimi-cu\/update$/);
321
+ assert.ok(postController !== null, 'POST 应当开出一条事件流');
322
+
323
+ await act(async () => {
324
+ postController.enqueue(new TextEncoder().encode(JSON.stringify({ type: 'stage', stage: 'downloading' }) + '\n'));
325
+ await new Promise((done) => setTimeout(done, 0));
326
+ });
327
+ assert.match(container.textContent ?? '', /正在下载/);
328
+ assert.equal(buttonByText(container, '重新检测').disabled, true, '更新进行中要禁用重新检测');
329
+
330
+ await act(async () => {
331
+ postController.enqueue(new TextEncoder().encode(JSON.stringify({
332
+ type: 'done', upToDate: false, version: '0.7.6', reconnect: 'done', raw: '✓ 升级完成',
333
+ }) + '\n'));
334
+ postController.close();
335
+ await new Promise((done) => setTimeout(done, 0));
336
+ });
337
+ await flush();
338
+ assert.match(container.textContent ?? '', /已更新到 0\.7\.6/);
339
+ });
340
+
341
+ await test('重连没发生时不提示重启 DSH', async () => {
342
+ const { container } = await mount(INSTALLED, { update: NEWER });
343
+ const button = buttonByText(container, '更新到 0.7.6');
344
+ await act(async () => {
345
+ button.click();
346
+ await new Promise((done) => setTimeout(done, 0));
347
+ });
348
+ await act(async () => {
349
+ postController.enqueue(new TextEncoder().encode(JSON.stringify({
350
+ type: 'done', upToDate: true, version: '0.7.5', reconnect: 'skipped', raw: '',
351
+ }) + '\n'));
352
+ postController.close();
353
+ await new Promise((done) => setTimeout(done, 0));
354
+ });
355
+ await flush();
356
+ assert.doesNotMatch(container.textContent ?? '', /重启 DSH 后生效/);
357
+ });
358
+
359
+ await test('更新失败:显示分类文案、可展开原始输出与重试', async () => {
360
+ const { container } = await mount(INSTALLED, { update: NEWER });
361
+ const button = buttonByText(container, '更新到 0.7.6');
362
+ await act(async () => {
363
+ button.click();
364
+ await new Promise((done) => setTimeout(done, 0));
365
+ });
366
+ await act(async () => {
367
+ postController.enqueue(new TextEncoder().encode(JSON.stringify({
368
+ type: 'error', kind: 'verify', raw: '✗ 下载包 SHA-256 与版本清单不一致,已中止。',
369
+ }) + '\n'));
370
+ postController.close();
371
+ await new Promise((done) => setTimeout(done, 0));
372
+ });
373
+ await flush();
374
+ const text = container.textContent ?? '';
375
+ assert.match(text, /下载的安装包校验没通过/);
376
+ assert.match(text, /查看原始输出/);
377
+ assert.match(text, /SHA-256/);
378
+ assert.ok(buttonByText(container, '重试') !== undefined);
379
+ });
380
+
249
381
  console.log('\ntest-card: ' + passed + '/' + total + ' 条断言通过');
@@ -0,0 +1,158 @@
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
+ import assert from 'node:assert/strict';
11
+
12
+ import { Context, Service } from '@deepseek-ai/cordis';
13
+ import SystemPrompt from '@deepseek-ai/dsh-system-prompt';
14
+ import ToolRuntime from '@deepseek-ai/dsh-tools';
15
+
16
+ import * as plugin from '../lib/index.js';
17
+
18
+ let passed = 0;
19
+ let total = 0;
20
+ async function test(label, fn) {
21
+ total += 1;
22
+ try {
23
+ await fn();
24
+ passed += 1;
25
+ console.log(' ok ' + label);
26
+ } catch (error) {
27
+ console.error(' FAIL ' + label + ': ' + error.message);
28
+ throw error;
29
+ }
30
+ }
31
+
32
+ /**
33
+ * 探测器替身:与平台无关,直接给出「已安装且 MCP 命令可用」的状态。
34
+ *
35
+ * 真探测器在 Linux 上必然给 `mcp: null`(不受支持的平台),注册那条链就跑不到,
36
+ * 而这里要验的是独占槽位与注册的先后关系,不是平台分流。
37
+ */
38
+ const fakeDetect = async () => ({
39
+ platform: 'darwin',
40
+ supported: true,
41
+ installed: true,
42
+ path: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu',
43
+ version: '0.7.5',
44
+ doctor: { ran: true, ok: true, checks: [], raw: '' },
45
+ mcp: { command: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu', args: ['mcp', '-s', 'user'] },
46
+ install: null,
47
+ notes: [],
48
+ checkedAt: 0,
49
+ });
50
+
51
+ /** 等若干个微任务,让注入回调里那条同步链跑完。 */
52
+ async function settle() {
53
+ for (let i = 0; i < 8; i += 1) await Promise.resolve();
54
+ }
55
+
56
+ /**
57
+ * 等一个条件成立。探测要真的跑一次 `doctor`,是外部进程,不能只等微任务。
58
+ * @param {() => boolean} predicate - 条件。
59
+ * @param {string} what - 超时时的说明。
60
+ */
61
+ async function waitFor(predicate, what) {
62
+ const deadline = Date.now() + 20_000;
63
+ while (Date.now() < deadline) {
64
+ if (predicate()) return;
65
+ await new Promise((resolve) => setTimeout(resolve, 20));
66
+ }
67
+ throw new Error('等不到:' + what);
68
+ }
69
+
70
+ /** 宿主的桌面操作独占登记,语义照抄:占住之后第二个注册抛错并报出已占位的名字。 */
71
+ class FakeComputerUse extends Service {
72
+ constructor(ctx) {
73
+ super(ctx, 'computerUse');
74
+ this.registration = undefined;
75
+ }
76
+
77
+ get providerName() {
78
+ return this.registration;
79
+ }
80
+
81
+ register(name) {
82
+ if (this.registration !== undefined) {
83
+ throw new Error(`computer use provider "${this.registration}" is already registered`);
84
+ }
85
+ return this.ctx.effect(() => {
86
+ this.registration = name;
87
+ return () => {
88
+ this.registration = undefined;
89
+ };
90
+ }, 'computerUse.register()');
91
+ }
92
+ }
93
+
94
+ /** MCP 管理器替身:只记录注册过的服务器与撤销次数。 */
95
+ class FakeMcpManager extends Service {
96
+ constructor(ctx) {
97
+ super(ctx, 'mcpManager');
98
+ this.calls = [];
99
+ this.disposed = 0;
100
+ }
101
+
102
+ register(name, entry) {
103
+ this.calls.push({ name, entry });
104
+ return () => {
105
+ this.disposed += 1;
106
+ };
107
+ }
108
+ }
109
+
110
+ /** 起一个只带工具表的上下文:工具表要提示词服务来写 schema。 */
111
+ function host() {
112
+ const ctx = new Context();
113
+ new SystemPrompt(ctx, {});
114
+ new ToolRuntime(ctx);
115
+ return ctx;
116
+ }
117
+
118
+ await test('组合里没有 computerUse 时行为不变:照常注册 MCP 连接', async () => {
119
+ const ctx = host();
120
+ const mcp = new FakeMcpManager(ctx);
121
+ plugin.apply(ctx, { detect: fakeDetect });
122
+ await waitFor(() => mcp.calls.length === 1, '没有独占服务时应当照常交出工具');
123
+ assert.equal(mcp.calls[0].name, 'kimi-cu');
124
+ });
125
+
126
+ await test('槽位空着时先占位,占到了才注册 MCP 连接', async () => {
127
+ const ctx = host();
128
+ const cu = new FakeComputerUse(ctx);
129
+ const mcp = new FakeMcpManager(ctx);
130
+ plugin.apply(ctx, { detect: fakeDetect });
131
+ await waitFor(() => mcp.calls.length === 1, '占到位之后应当注册 MCP 连接');
132
+ assert.equal(cu.providerName, 'kimi-cu', '本插件应当占住桌面操作槽位');
133
+ });
134
+
135
+ await test('槽位被别的后端占着时不抢,也不交出工具', async () => {
136
+ const ctx = host();
137
+ const cu = new FakeComputerUse(ctx);
138
+ const release = cu.register('cua-driver-mcp');
139
+ const mcp = new FakeMcpManager(ctx);
140
+ plugin.apply(ctx, { detect: fakeDetect });
141
+ await settle();
142
+ await new Promise((resolve) => setTimeout(resolve, 300));
143
+ assert.equal(cu.providerName, 'cua-driver-mcp', '别的后端占着时不能顶掉它');
144
+ assert.equal(mcp.calls.length, 0, '占不到位就不该注册 mcp__kimi-cu__* 工具');
145
+ await release();
146
+ });
147
+
148
+ await test('占位后别的后端再注册会被拒:独占是双向的', async () => {
149
+ const ctx = host();
150
+ const cu = new FakeComputerUse(ctx);
151
+ const mcp = new FakeMcpManager(ctx);
152
+ plugin.apply(ctx, { detect: fakeDetect });
153
+ await waitFor(() => mcp.calls.length === 1, '应当注册 MCP 连接');
154
+ assert.equal(cu.providerName, 'kimi-cu');
155
+ assert.throws(() => cu.register('cua-driver-mcp'), /already registered/);
156
+ });
157
+
158
+ console.log('\ntest-exclusivity: ' + passed + '/' + total + ' 条断言通过');