120 lines
4.2 KiB
TypeScript
120 lines
4.2 KiB
TypeScript
import os from 'node:os';
|
|
import { describe, expect, it } from 'vitest';
|
|
import { expandLocalArgs, expandTilde, parseInput } from '../src/commands/parser.js';
|
|
|
|
describe('parseInput', () => {
|
|
it('解析 adb 命令与参数', () => {
|
|
const parsed = parseInput('/push ~/Documents/1.txt /sdcard/');
|
|
expect(parsed).toMatchObject({
|
|
kind: 'adb',
|
|
command: { name: 'push', deviceScoped: true },
|
|
args: ['~/Documents/1.txt', '/sdcard/'],
|
|
});
|
|
});
|
|
|
|
it('connect 命令 deviceScoped 为 false', () => {
|
|
const parsed = parseInput('/connect 192.168.1.8:5555');
|
|
expect(parsed).toMatchObject({
|
|
kind: 'adb',
|
|
command: { name: 'connect', deviceScoped: false },
|
|
});
|
|
});
|
|
|
|
it('解析内置命令', () => {
|
|
expect(parseInput('/devices')).toEqual({ kind: 'builtin', name: 'devices' });
|
|
expect(parseInput('/refresh')).toEqual({ kind: 'builtin', name: 'refresh' });
|
|
expect(parseInput('/exit')).toEqual({ kind: 'builtin', name: 'exit' });
|
|
expect(parseInput('/quit')).toEqual({ kind: 'builtin', name: 'quit' });
|
|
});
|
|
|
|
it('解析 /window 非负整数序号', () => {
|
|
expect(parseInput('/window 0')).toEqual({ kind: 'builtin', name: 'window', windowIndex: 0 });
|
|
expect(parseInput('/window 17')).toEqual({ kind: 'builtin', name: 'window', windowIndex: 17 });
|
|
});
|
|
|
|
it('/window 拒绝缺失、负数、非整数和多余参数', () => {
|
|
for (const input of ['/window', '/window -1', '/window 1.5', '/window abc', '/window 1 2']) {
|
|
const parsed = parseInput(input);
|
|
expect(parsed.kind).toBe('invalid');
|
|
if (parsed.kind === 'invalid') expect(parsed.message).toContain('/window');
|
|
}
|
|
});
|
|
|
|
it('解析 /mouse on|off', () => {
|
|
expect(parseInput('/mouse on')).toEqual({ kind: 'builtin', name: 'mouse', enabled: true });
|
|
expect(parseInput('/mouse off')).toEqual({ kind: 'builtin', name: 'mouse', enabled: false });
|
|
});
|
|
|
|
it('/mouse 拒绝缺失、未知值和多余参数', () => {
|
|
for (const input of ['/mouse', '/mouse yes', '/mouse ON', '/mouse off now']) {
|
|
const parsed = parseInput(input);
|
|
expect(parsed.kind).toBe('invalid');
|
|
if (parsed.kind === 'invalid') expect(parsed.message).toContain('/mouse <on|off>');
|
|
}
|
|
});
|
|
|
|
it('参数不足给出用法提示', () => {
|
|
const parsed = parseInput('/push a');
|
|
expect(parsed).toMatchObject({
|
|
kind: 'invalid',
|
|
});
|
|
if (parsed.kind === 'invalid') {
|
|
expect(parsed.message).toContain('用法: /push <local> <remote>');
|
|
}
|
|
});
|
|
|
|
it('未知斜杠命令给出不支持提示', () => {
|
|
const parsed = parseInput('/hello');
|
|
expect(parsed.kind).toBe('invalid');
|
|
if (parsed.kind === 'invalid') {
|
|
expect(parsed.message).toContain('不支持的命令: hello');
|
|
}
|
|
});
|
|
|
|
it('非斜杠输入给出不支持提示', () => {
|
|
const parsed = parseInput('push a b');
|
|
expect(parsed.kind).toBe('invalid');
|
|
if (parsed.kind === 'invalid') {
|
|
expect(parsed.message).toContain('不支持的命令: push');
|
|
}
|
|
});
|
|
|
|
it('空输入不产生提示', () => {
|
|
const parsed = parseInput(' ');
|
|
expect(parsed).toEqual({ kind: 'invalid', message: '' });
|
|
});
|
|
|
|
it('引号路径作为单个参数解析', () => {
|
|
expect(parseInput('/shell echo "hello world"')).toMatchObject({
|
|
kind: 'adb',
|
|
args: ['echo', 'hello world'],
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('expandTilde', () => {
|
|
it('展开 ~ 与 ~/ 前缀', () => {
|
|
expect(expandTilde('~')).toBe(os.homedir());
|
|
expect(expandTilde('~/Documents/1.txt')).toBe(`${os.homedir()}/Documents/1.txt`);
|
|
expect(expandTilde('/sdcard/')).toBe('/sdcard/');
|
|
expect(expandTilde('~user/x')).toBe('~user/x');
|
|
});
|
|
});
|
|
|
|
describe('expandLocalArgs', () => {
|
|
it('只展开 push/install 的本地源路径和 pull 的本地目标路径', () => {
|
|
const home = os.homedir();
|
|
expect(expandLocalArgs('push', ['~/file', '/sdcard/file'])).toEqual([
|
|
`${home}/file`, '/sdcard/file',
|
|
]);
|
|
expect(expandLocalArgs('pull', ['/sdcard/file', '~/Downloads'])).toEqual([
|
|
'/sdcard/file', `${home}/Downloads`,
|
|
]);
|
|
expect(expandLocalArgs('install', ['~/app.apk'])).toEqual([`${home}/app.apk`]);
|
|
});
|
|
|
|
it('shell 参数中的 ~ 保持设备端语义', () => {
|
|
expect(expandLocalArgs('shell', ['ls', '~/data'])).toEqual(['ls', '~/data']);
|
|
});
|
|
});
|