import { describe, expect, it, vi } from 'vitest'; import { completeRemotePath, completeLocalPath, getCompletionContext, insertCompletion, shellCommandSuggestions, } from '../src/commands/shellCompletion.js'; const { execAdbMock } = vi.hoisted(() => ({ execAdbMock: vi.fn() })); vi.mock('../src/adb/adbClient.js', async (importOriginal) => { const actual = await importOriginal(); return { ...actual, execAdb: execAdbMock }; }); describe('shell/path completion', () => { it('过滤静态 Android shell 命令', () => { expect(shellCommandSuggestions('p').map((item) => item.value)).toEqual(['pm', 'ps']); expect(shellCommandSuggestions('dump')[0]?.value).toBe('dumpsys'); }); it('识别 shell 命令和设备/本地路径上下文', () => { expect(getCompletionContext('/shell p')).toMatchObject({ kind: 'shell-command', prefix: 'p', commandArgs: [], }); expect(getCompletionContext('/shell ls /sd')).toMatchObject({ kind: 'remote-path', prefix: '/sd', commandArgs: ['ls'], }); expect(getCompletionContext('/pull /sdcard/file ~/Do')).toMatchObject({ kind: 'local-path', prefix: '~/Do', commandArgs: ['/sdcard/file'], }); expect(getCompletionContext('/shell ls ~/Do')?.kind).toBe('remote-path'); expect(getCompletionContext('/push "My Documents/Do')).toMatchObject({ kind: 'local-path', prefix: 'My Documents/Do', }); }); it('查询设备目录并保留输入目录前缀', async () => { execAdbMock.mockResolvedValue({ code: 0, stdout: '/sdcard/Documents/\n/sdcard/Download/\n/sdcard/file.txt\n', stderr: '', }); await expect(completeRemotePath('serial-1', '/sdcard/Do')).resolves.toEqual([ { value: '/sdcard/Documents/', description: '设备路径' }, { value: '/sdcard/Download/', description: '设备路径' }, ]); expect(execAdbMock).toHaveBeenCalledWith( ['-s', 'serial-1', 'shell', 'ls', '-1', '-p', '/sdcard/'], 1500, ); }); it('带空格建议自动加引号并可在引号内插入', () => { const context = getCompletionContext('/push ~/My')!; expect(insertCompletion('/push ~/My', context, { value: '~/My Documents/file.txt', description: '文件', })).toBe('/push "~/My Documents/file.txt"'); const quoted = getCompletionContext('/push "~/My')!; expect(insertCompletion('/push "~/My', quoted, { value: '~/My Documents/file.txt', description: '文件', })).toBe('/push "~/My Documents/file.txt"'); }); it('本地 ~ 联想保持别名形式', async () => { await expect(completeLocalPath('~')).resolves.toEqual([ { value: '~/', description: '目录' }, ]); }); });