/** * plugins/computer_use/test/commands.test.ts * * 守两件事: * ① `/computeruse` 与 `/cu` 都能路由到同一个 runCommand(别名不是摆设) * ② 命令面**只读**:不平台依赖的子命令在任何机器上都给出可读输出, * 且不会因为拿不到守护进程就抛异常 */ import { describe, expect, test } from 'bun:test'; import { readdirSync } from 'node:fs'; import { join } from 'node:path'; import { initWorkingDir } from '../../../src/bootstrap/workingDir.js'; initWorkingDir(); const { handleComputerUseCommand } = await import('../src/commands.js'); describe('computer-use 命令面', () => { test('两个命令文件都存在 —— /cu 是声明出来的别名,不是隐式行为', () => { const commands = readdirSync(join(import.meta.dir, '..', 'commands')).sort(); expect(commands).toEqual(['computeruse.md', 'cu.md']); }); test('plugin.ts 对两个命令名都接管', async () => { const api = (await import('../plugin.js')).default; for (const name of ['computeruse', 'cu']) { const events: unknown[] = []; for await (const event of api.runCommand!(name, 'apps', { provider: { baseURL: '', apiKey: '', model: '' } as never, history: [], })) { events.push(event); } // 有实际输出,且以 turn_done 收尾(否则 UI 会一直等) expect(events.length).toBeGreaterThan(1); expect((events.at(-1) as { type: string }).type).toBe('turn_done'); } }); test('未知子命令给出可用列表,而不是静默或抛错', async () => { const out = await handleComputerUseCommand('nonsense'); expect(out).toContain('未知子命令'); expect(out).toContain('doctor'); }); test('apps 列出真实说明书,并如实报告拿不到前台应用', async () => { const out = await handleComputerUseCommand('apps'); expect(out).toContain('wechat'); expect(out).toContain('当前前台应用'); }); test('app 报告不可逆声明的可检查性 —— 不把 intent 说成拦截', async () => { const out = await handleComputerUseCommand('app wechat'); // 显示的是**引擎实际用来匹配**的形式(loadGuides 统一小写化), // 不是文件里的原始大小写 —— 诊断要照着引擎的眼睛看。 expect(out).toContain('com.tencent.xinwechat'); // 现状:微信的声明全是 intent 形式,必须如实说"拦不住" expect(out).toContain('模型漏报就拦不住'); }); test('app 名字不存在时列出已有的,不静默', async () => { const out = await handleComputerUseCommand('app 不存在的应用'); expect(out).toContain('已有:'); expect(out).toContain('finder'); }); test('report / audit 在没有任何记录时也不抛异常', async () => { expect(typeof await handleComputerUseCommand('report')).toBe('string'); expect(await handleComputerUseCommand('audit 3')).toContain('最近 3 条'); }); });