@lynn123411/dsh-kimi-cu 0.1.3 → 0.2.1

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.
@@ -0,0 +1,124 @@
1
+ /**
2
+ * 浏览器半边自检:按 Web shell 的方式装载 lib/client.js,喂假 cordis 服务,断言
3
+ * 字典与插件页状态卡注册。**未声明的服务读取必须抛**——漏声明不会报错,只会让插件
4
+ * 在页面里消失(docs/rules/client-wiring.md),守卫自身要有反例断言证明它还能抛。
5
+ *
6
+ * 跑法:pnpm test / node scripts/smoke-client.mjs
7
+ */
8
+ import { fileURLToPath } from 'node:url';
9
+ import { dirname, join, resolve } from 'node:path';
10
+
11
+ import { loadClientBundle } from './lib/client-bundle.mjs';
12
+
13
+ const root = resolve(process.argv[2] ?? join(dirname(fileURLToPath(import.meta.url)), '..'));
14
+ const pkgName = JSON.parse((await import('node:fs')).readFileSync(join(root, 'package.json'), 'utf8')).name;
15
+
16
+ const failures = [];
17
+ const check = (label, ok) => {
18
+ if (ok) console.log(' ok ' + label);
19
+ else { console.error(' FAIL ' + label); failures.push(label); }
20
+ };
21
+
22
+ // --- 浏览器环境的最小替身:插件 apply 期间不碰真实 DOM,只碰样式表那几样 ---
23
+ const stubElement = {
24
+ dataset: {}, style: {}, appendChild() {}, remove() {}, setAttribute() {},
25
+ addEventListener() {}, removeEventListener() {}, classList: { add() {}, remove() {} },
26
+ };
27
+ const appendedStyles = [];
28
+ globalThis.document = {
29
+ ...stubElement,
30
+ documentElement: stubElement,
31
+ body: stubElement,
32
+ head: {
33
+ ...stubElement,
34
+ appendChild: (node) => { appendedStyles.push(node); },
35
+ querySelector: () => null,
36
+ querySelectorAll: () => [],
37
+ },
38
+ createElement: () => ({ ...stubElement, id: '', textContent: '' }),
39
+ createTextNode: () => ({}),
40
+ getElementById: () => null,
41
+ querySelector: () => null,
42
+ querySelectorAll: () => [],
43
+ };
44
+
45
+ const { exported, requires } = await loadClientBundle(root);
46
+
47
+ const locales = [];
48
+ const registrations = [];
49
+ const errors = [];
50
+
51
+ /** cordis 忠实的上下文:读未声明的服务抛 cannot get property "x" without inject。 */
52
+ function makeCtx(label, declared = []) {
53
+ const table = {
54
+ locale: {
55
+ register: (ns, dictionaries) => { locales.push({ ns, dictionaries }); return () => {}; },
56
+ bind: () => (key) => key,
57
+ },
58
+ slots: {
59
+ inject: (_slot, register) => {
60
+ try {
61
+ register();
62
+ } catch (error) {
63
+ errors.push('slots.inject threw: ' + error?.message);
64
+ }
65
+ return () => {};
66
+ },
67
+ register: (options, component) => {
68
+ registrations.push({ options, component });
69
+ return () => {};
70
+ },
71
+ },
72
+ };
73
+ const base = {
74
+ label,
75
+ effect: (fn) => { fn(); return () => {}; },
76
+ inject: () => () => {},
77
+ get: (name) => table[name],
78
+ };
79
+ return new Proxy(base, {
80
+ get(target, prop) {
81
+ if (typeof prop === 'symbol' || prop === 'then') return undefined;
82
+ if (prop in target) return target[prop];
83
+ if (declared.includes(prop)) return table[prop];
84
+ throw new Error('cannot get property "' + prop + '" without inject');
85
+ },
86
+ });
87
+ }
88
+
89
+ check('bundle 以包 id 自注册', requires[0] === pkgName);
90
+ check('导出 name / apply / inject', exported?.name === pkgName && typeof exported?.apply === 'function' && Array.isArray(exported?.inject));
91
+ check('inject 写的就是它真正读的服务', JSON.stringify(exported?.inject) === '["slots","locale"]');
92
+
93
+ try {
94
+ makeCtx('teeth').locale;
95
+ check('守卫拒绝未声明的服务读取', false);
96
+ } catch (error) {
97
+ check('守卫拒绝未声明的服务读取', /cannot get property "locale" without inject/.test(error.message));
98
+ }
99
+
100
+ try {
101
+ exported.apply(makeCtx('root', exported.inject ?? []));
102
+ check('apply() 过得了 inject 守卫', true);
103
+ } catch (error) {
104
+ check('apply() 过得了 inject 守卫', false);
105
+ console.error(' ' + error.message);
106
+ }
107
+
108
+ const dictionary = locales.find((entry) => entry.ns !== undefined)?.dictionaries;
109
+ check('字典注册上了', dictionary !== undefined);
110
+ const zhKeys = Object.keys(dictionary?.zh ?? {}).sort();
111
+ const enKeys = Object.keys(dictionary?.en ?? {}).sort();
112
+ check('zh/en 键集一致且非空', zhKeys.length > 0 && JSON.stringify(zhKeys) === JSON.stringify(enKeys));
113
+ check('每条文案都非空', enKeys.every((key) => typeof dictionary.en[key] === 'string' && dictionary.en[key].length > 0));
114
+
115
+ const card = registrations.find((entry) => entry.options?.name === 'plugins.bundle.config');
116
+ check('状态卡注册在 plugins.bundle.config 且 key 是包名', card?.options?.key === pkgName);
117
+ check('状态卡带本插件的字典命名空间', card?.options?.locale === 'kimiCu');
118
+ check('状态卡组件是个函数', typeof card?.component === 'function');
119
+
120
+ check('apply 期间没有接线错误', errors.length === 0);
121
+ for (const error of errors) console.error(' ' + error);
122
+
123
+ console.log(failures.length === 0 ? '\nsmoke-client: PASS' : '\nsmoke-client: FAIL (' + failures.length + ')');
124
+ process.exit(failures.length === 0 ? 0 : 1);
@@ -0,0 +1,102 @@
1
+ /**
2
+ * 宿主半边自检:真 cordis Context + 真 ToolRuntime,断言 kimi_cu_status 注册得上、
3
+ * 返回的状态形状对、patch 的五条行各就各位。
4
+ *
5
+ * 跑法:pnpm test / node scripts/smoke-host.mjs
6
+ */
7
+ import assert from 'node:assert/strict';
8
+ import { readFileSync } from 'node:fs';
9
+ import { dirname, join, resolve } from 'node:path';
10
+ import { fileURLToPath } from 'node:url';
11
+
12
+ import { Context } 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
+ import { STATUS_ROUTE_PATH } from '../src/shared/status.ts';
18
+
19
+ const root = resolve(process.argv[2] ?? join(dirname(fileURLToPath(import.meta.url)), '..'));
20
+ const pkgName = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')).name;
21
+
22
+ let passed = 0;
23
+ let total = 0;
24
+ async function test(label, fn) {
25
+ total += 1;
26
+ try {
27
+ await fn();
28
+ passed += 1;
29
+ console.log(' ok ' + label);
30
+ } catch (error) {
31
+ console.error(' FAIL ' + label + ': ' + error.message);
32
+ throw error;
33
+ }
34
+ }
35
+
36
+ /** 起一个真内核上下文:工具表 + 它写工具 schema 用的提示词服务。 */
37
+ function host() {
38
+ const ctx = new Context();
39
+ new SystemPrompt(ctx, {});
40
+ new ToolRuntime(ctx);
41
+ return ctx;
42
+ }
43
+
44
+ const ctx = host();
45
+ plugin.apply(ctx);
46
+ const tool = ctx.tools.get('kimi_cu_status');
47
+ const signal = new AbortController().signal;
48
+
49
+ await test('导出的行身份与 patch 里那条根层行同名', () => {
50
+ assert.equal(plugin.name, 'dsh-kimi-cu');
51
+ assert.deepEqual(plugin.inject, ['tools']);
52
+ });
53
+
54
+ await test('工具注册得上,refresh 参数是可选布尔', () => {
55
+ assert.ok(tool !== undefined, '工具没注册上');
56
+ assert.equal(tool.name, 'kimi_cu_status');
57
+ assert.equal(tool.parameters.properties.refresh.type, 'boolean');
58
+ assert.equal(tool.parameters.required, undefined);
59
+ });
60
+
61
+ await test('执行返回 JSON 状态,形状对', async () => {
62
+ const value = await tool.execute({}, { signal });
63
+ assert.equal(typeof value.status, 'string');
64
+ const status = JSON.parse(value.status);
65
+ assert.ok(['darwin', 'win32', 'other'].includes(status.platform));
66
+ assert.equal(typeof status.supported, 'boolean');
67
+ assert.equal(typeof status.installed, 'boolean');
68
+ assert.equal(typeof status.checkedAt, 'number');
69
+ assert.equal(typeof status.doctor, 'object');
70
+ assert.ok(status.install === null || typeof status.install.command === 'string');
71
+ assert.ok(status.mcp === null || Array.isArray(status.mcp.args));
72
+ });
73
+
74
+ await test('render 把 JSON 原样交给模型', async () => {
75
+ const value = await tool.execute({}, { signal });
76
+ assert.deepEqual(tool.output.render({}, value), [{ type: 'text', text: value.status }]);
77
+ });
78
+
79
+ await test('只读工具标记为并发安全', () => {
80
+ assert.equal(tool.isConcurrencySafe({}), true);
81
+ });
82
+
83
+ await test('状态路由路径固定', () => {
84
+ assert.equal(STATUS_ROUTE_PATH, '/api/dsh-kimi-cu/status');
85
+ });
86
+
87
+ await test('patch 有三行,根层行 id 与包名对得上', () => {
88
+ const patch = readFileSync(join(root, 'cordis.patch.yml'), 'utf8')
89
+ .split('\n')
90
+ .map((line) => line.replace(/(^|\s)#.*$/, ''))
91
+ .filter((line) => line.trim() !== '')
92
+ .join('\n');
93
+ const rows = [...patch.matchAll(/-\s*id:\s*(\S+)\s*\n\s*name:\s*'([^']+)'/g)].map((match) => [match[1], match[2]]);
94
+ assert.equal(rows.length, 3, 'patch 应当有三条装配行:本包自己与两份技能 provider');
95
+ assert.deepEqual(rows[0], ['dsh-kimi-cu', pkgName]);
96
+ assert.deepEqual(rows[1], ['skill-kimi-cu-macos', '@deepseek-ai/dsh-skill-filesystem']);
97
+ assert.deepEqual(rows[2], ['skill-kimi-cu-windows', '@deepseek-ai/dsh-skill-filesystem']);
98
+ // MCP 连接由宿主半边注册,patch 里不该再有 mcp-client 行。
99
+ assert.equal(patch.includes('dsh-mcp-client'), false, 'MCP 连接已交给 mcpManager,patch 里不该再出现静态行');
100
+ });
101
+
102
+ console.log('\nsmoke-host: ' + passed + '/' + total + ' 条断言通过');
@@ -0,0 +1,249 @@
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
+ import assert from 'node:assert/strict';
16
+ import { dirname, join, resolve } from 'node:path';
17
+ import { fileURLToPath } from 'node:url';
18
+ import { JSDOM } from 'jsdom';
19
+
20
+ import { installDom, loadClientBundle, primitivesStub } from '../../../tools/lib/dom-harness.mjs';
21
+ import { zh } from '../src/client/locales.ts';
22
+
23
+ const root = resolve(process.argv[2] ?? join(dirname(fileURLToPath(import.meta.url)), '..'));
24
+ const dom = installDom(JSDOM, { storage: false, raf: true });
25
+
26
+ const { createElement, act } = await import('react');
27
+ const { createRoot } = await import('react-dom/client');
28
+
29
+ const primitives = primitivesStub({
30
+ Button: (props) => createElement(
31
+ 'button',
32
+ { type: 'button', onClick: props.onClick, disabled: props.disabled },
33
+ props.children,
34
+ ),
35
+ Tag: (props) => createElement('span', { 'data-tone': props.tone }, props.children),
36
+ });
37
+
38
+ const exported = await loadClientBundle({ root, primitives });
39
+
40
+ const registrations = [];
41
+ const table = {
42
+ locale: { register: () => () => {}, bind: () => (key) => key },
43
+ slots: {
44
+ inject: (_slot, register) => { register(); return () => {}; },
45
+ register: (options, component) => { registrations.push({ options, component }); return () => {}; },
46
+ },
47
+ };
48
+ const ctx = new Proxy(
49
+ { effect: (fn) => { fn(); return () => {}; }, inject: () => () => {}, get: (name) => table[name] },
50
+ {
51
+ get(target, prop) {
52
+ if (typeof prop === 'symbol' || prop === 'then') return undefined;
53
+ if (prop in target) return target[prop];
54
+ if (prop === 'locale' || prop === 'slots') return table[prop];
55
+ throw new Error('cannot get property "' + String(prop) + '" without inject');
56
+ },
57
+ },
58
+ );
59
+ exported.apply(ctx);
60
+ const card = registrations.find((entry) => entry.options.name === 'plugins.bundle.config').component;
61
+ const t = (key) => zh[key] ?? key;
62
+
63
+ let fetchUrl = '';
64
+ let fetchBody = null;
65
+ let fetchCode = 200;
66
+ globalThis.fetch = async (url) => {
67
+ fetchUrl = String(url);
68
+ return new Response(JSON.stringify(fetchBody), {
69
+ status: fetchCode,
70
+ headers: { 'Content-Type': 'application/json' },
71
+ });
72
+ };
73
+
74
+ let copied = '';
75
+ Object.defineProperty(dom.window.navigator, 'clipboard', {
76
+ value: { writeText: async (text) => { copied = text; } },
77
+ configurable: true,
78
+ });
79
+
80
+ const BASE = {
81
+ platform: 'darwin',
82
+ supported: true,
83
+ installed: false,
84
+ path: null,
85
+ version: null,
86
+ doctor: { ran: false, ok: false, checks: [], raw: '' },
87
+ mcp: { command: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu', args: ['mcp', '-s', 'user'] },
88
+ install: {
89
+ command: 'curl -fsSL https://cdn.kimi.com/kimi-computer-use/latest/setup_macos.sh | bash',
90
+ steps: ['install', 'grant-permissions', 'reload-plugin'],
91
+ docsUrl: 'https://example.com/zh',
92
+ docsUrlEn: 'https://example.com/en',
93
+ },
94
+ notes: [],
95
+ checkedAt: 1,
96
+ };
97
+
98
+ let passed = 0;
99
+ let total = 0;
100
+ async function test(label, fn) {
101
+ total += 1;
102
+ try {
103
+ await fn();
104
+ passed += 1;
105
+ console.log(' ok ' + label);
106
+ } catch (error) {
107
+ console.error(' FAIL ' + label + ': ' + error.message);
108
+ throw error;
109
+ }
110
+ }
111
+
112
+ /**
113
+ * 把卡片画进一个新容器。
114
+ * @param status - 这一次 fetch 返回的状态。
115
+ * @param options - httpStatus 覆盖响应码。
116
+ * @returns 容器与 react root。
117
+ */
118
+ async function mount(status, options = {}) {
119
+ fetchBody = status;
120
+ fetchCode = options.httpStatus ?? 200;
121
+ fetchUrl = '';
122
+ const container = document.createElement('div');
123
+ document.body.appendChild(container);
124
+ const reactRoot = createRoot(container);
125
+ await act(async () => {
126
+ reactRoot.render(createElement(card, { t }));
127
+ await new Promise((done) => setTimeout(done, 0));
128
+ });
129
+ return { container, reactRoot };
130
+ }
131
+
132
+ /**
133
+ * 按文字找按钮。
134
+ * @param container - 容器。
135
+ * @param text - 文字片段。
136
+ * @returns 按钮或 undefined。
137
+ */
138
+ function buttonByText(container, text) {
139
+ return [...container.querySelectorAll('button')].find((button) => (button.textContent ?? '').includes(text));
140
+ }
141
+
142
+ await test('未安装:显示安装命令、复制按钮与步骤', async () => {
143
+ const { container } = await mount(BASE);
144
+ const text = container.textContent ?? '';
145
+ assert.match(text, /未检测到 KimiCU/);
146
+ assert.match(text, /setup_macos\.sh/);
147
+ assert.match(text, /复制命令/);
148
+ assert.match(text, /安装官方 KimiCU 运行时。/);
149
+ assert.ok(buttonByText(container, '复制命令') !== undefined);
150
+ });
151
+
152
+ await test('已就绪:显示版本与恢复提示,不再展示安装命令', async () => {
153
+ const { container } = await mount({
154
+ ...BASE,
155
+ installed: true,
156
+ path: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu',
157
+ version: '0.6.8',
158
+ doctor: { ran: true, ok: true, checks: [{ id: 'accessibility', label: 'Accessibility (AX)', ok: true }], raw: '' },
159
+ });
160
+ const text = container.textContent ?? '';
161
+ assert.match(text, /KimiCU 已就绪/);
162
+ assert.match(text, /0\.6\.8/);
163
+ assert.match(text, /如果模型看不到/);
164
+ assert.doesNotMatch(text, /setup_macos\.sh/);
165
+ });
166
+
167
+ await test('已安装但未就绪:列出失败项与修复提示', async () => {
168
+ const { container } = await mount({
169
+ ...BASE,
170
+ installed: true,
171
+ path: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu',
172
+ version: '0.6.8',
173
+ doctor: {
174
+ ran: true,
175
+ ok: false,
176
+ checks: [{ id: 'screen-recording', label: 'Screen Recording', ok: false }],
177
+ raw: '',
178
+ },
179
+ });
180
+ const text = container.textContent ?? '';
181
+ assert.match(text, /已安装 KimiCU,但还没就绪/);
182
+ assert.match(text, /屏幕录制/);
183
+ assert.match(text, /打开系统设置/);
184
+ });
185
+
186
+ await test('平台不支持:只说明不支持,不展示安装命令', async () => {
187
+ const { container } = await mount({
188
+ ...BASE,
189
+ platform: 'other',
190
+ supported: false,
191
+ install: null,
192
+ mcp: null,
193
+ });
194
+ const text = container.textContent ?? '';
195
+ assert.match(text, /本插件只支持 macOS 与 Windows/);
196
+ assert.doesNotMatch(text, /setup_macos\.sh/);
197
+ });
198
+
199
+ await test('读取失败:显示错误与重试', async () => {
200
+ const { container } = await mount({ error: 'boom' }, { httpStatus: 500 });
201
+ const text = container.textContent ?? '';
202
+ assert.match(text, /读不到 KimiCU 的状态。/);
203
+ assert.ok(buttonByText(container, '重试') !== undefined);
204
+ });
205
+
206
+ await test('复制按钮把安装命令写进剪贴板', async () => {
207
+ const { container } = await mount(BASE);
208
+ copied = '';
209
+ const button = buttonByText(container, '复制命令');
210
+ assert.ok(button !== undefined);
211
+ await act(async () => {
212
+ button.click();
213
+ await new Promise((done) => setTimeout(done, 0));
214
+ });
215
+ assert.equal(copied, BASE.install.command);
216
+ });
217
+
218
+ await test('覆盖路径缺失:显示 override-missing 附注', async () => {
219
+ const { container } = await mount({ ...BASE, notes: ['override-missing'] });
220
+ const text = container.textContent ?? '';
221
+ assert.match(text, /KIMI_CU_WINDOWS_EXE 指向的文件不存在/);
222
+ });
223
+
224
+ await test('MCP 命令缺失:即使 doctor 全绿也显示未就绪', async () => {
225
+ const { container } = await mount({
226
+ ...BASE,
227
+ installed: true,
228
+ path: '/Applications/KimiCU.app/Contents/MacOS/kimi-cu',
229
+ version: '0.3.13',
230
+ doctor: { ran: true, ok: true, checks: [], raw: '' },
231
+ notes: ['legacy-install', 'mcp-command-missing'],
232
+ });
233
+ const text = container.textContent ?? '';
234
+ assert.match(text, /已安装 KimiCU,但还没就绪/);
235
+ assert.match(text, /MCP 行要启动的文件不存在/);
236
+ });
237
+
238
+ await test('重新检测请求带 refresh=1', async () => {
239
+ const { container } = await mount(BASE);
240
+ const button = buttonByText(container, '重新检测');
241
+ assert.ok(button !== undefined);
242
+ await act(async () => {
243
+ button.click();
244
+ await new Promise((done) => setTimeout(done, 0));
245
+ });
246
+ assert.match(fetchUrl, /refresh=1/);
247
+ });
248
+
249
+ console.log('\ntest-card: ' + passed + '/' + total + ' 条断言通过');