278 lines
8.4 KiB
TypeScript
278 lines
8.4 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||
import type { Device } from '../src/adb/deviceParser.js';
|
||
import { DevicePoller } from '../src/adb/devicePoller.js';
|
||
import { App } from '../src/components/App.js';
|
||
import { renderHarness } from './harness.js';
|
||
|
||
const { execAdbMock, fakeDevices } = vi.hoisted(() => ({
|
||
execAdbMock: vi.fn(),
|
||
fakeDevices: [
|
||
{ serial: 'serial-1', state: 'device', model: 'Pixel_7' },
|
||
{ serial: 'serial-2', state: 'device', model: 'Pixel_8' },
|
||
] as Device[],
|
||
}));
|
||
|
||
// 仅 mock execAdb(拦截真实 adb 调用并捕获参数),其余保留真实实现
|
||
vi.mock('../src/adb/adbClient.js', async (importOriginal) => {
|
||
const actual = await importOriginal<typeof import('../src/adb/adbClient.js')>();
|
||
return {
|
||
...actual,
|
||
execAdb: (args: string[], timeoutMs?: number) => execAdbMock(args, timeoutMs),
|
||
};
|
||
});
|
||
|
||
const sleep = (ms = 150) => new Promise((resolve) => setTimeout(resolve, ms));
|
||
|
||
function makeApp() {
|
||
const poller = new DevicePoller(() => Promise.resolve(structuredClone(fakeDevices)));
|
||
return renderHarness(<App adbVersion="1.0.41" poller={poller} />);
|
||
}
|
||
|
||
beforeEach(() => {
|
||
execAdbMock.mockReset();
|
||
execAdbMock.mockResolvedValue({ code: 0, stdout: 'ok', stderr: '' });
|
||
});
|
||
|
||
describe('App(集成验证 spec A1–A10)', () => {
|
||
afterEach(() => {
|
||
execAdbMock.mockReset();
|
||
});
|
||
|
||
it('启动渲染 banner 并默认选中第一台设备(A6/A3)', async () => {
|
||
const app = makeApp();
|
||
await sleep(300);
|
||
|
||
const output = app.output();
|
||
expect(output).toContain('adb-cli v0.1.0 (adb 1.0.41)');
|
||
expect(output).toContain('已选择目标设备: serial-1');
|
||
expect(output).toContain('Devices (2)');
|
||
expect(output).toContain('serial-1');
|
||
|
||
await app.stop();
|
||
});
|
||
|
||
it('输入 / 触发补全面板,回车填入命令并显示用法提示(A1)', async () => {
|
||
const app = makeApp();
|
||
await sleep(300);
|
||
|
||
app.write('/pu');
|
||
await sleep(300);
|
||
expect(app.output()).toContain('/push');
|
||
|
||
app.write('\r');
|
||
await sleep(300);
|
||
expect(app.output()).toContain('用法: /push <local> <remote>');
|
||
|
||
await app.stop();
|
||
});
|
||
|
||
it('deviceScoped 命令自动注入 -s 目标设备(A5/D2)', async () => {
|
||
const app = makeApp();
|
||
await sleep(300);
|
||
|
||
app.write('/push ~/Documents/1.txt /sdcard/\r');
|
||
await sleep(300);
|
||
|
||
const home = (await import('node:os')).homedir();
|
||
expect(execAdbMock).toHaveBeenCalledWith(
|
||
['-s', 'serial-1', 'push', `${home}/Documents/1.txt`, '/sdcard/'],
|
||
undefined,
|
||
);
|
||
expect(app.output()).toContain('ok');
|
||
// 退出码不再打印到界面(spec v0.7)
|
||
expect(app.output()).not.toContain('[退出码');
|
||
|
||
await app.stop();
|
||
});
|
||
|
||
it('/shell 命令整行透传并注入 -s(spec v0.7)', async () => {
|
||
const app = makeApp();
|
||
await sleep(300);
|
||
|
||
app.write('/shell ls -l /sdcard/\r');
|
||
await sleep(300);
|
||
|
||
expect(execAdbMock).toHaveBeenCalledWith(['-s', 'serial-1', 'shell', 'ls', '-l', '/sdcard/'], undefined);
|
||
expect(app.output()).toContain('$ /shell ls -l /sdcard/');
|
||
expect(app.output()).toContain('ok');
|
||
expect(app.output()).not.toContain('[退出码');
|
||
|
||
await app.stop();
|
||
});
|
||
|
||
it('/shell 无参数时提示用法(spec v0.7)', async () => {
|
||
const app = makeApp();
|
||
await sleep(300);
|
||
|
||
app.write('/shell\r');
|
||
await sleep(300);
|
||
|
||
expect(execAdbMock).not.toHaveBeenCalled();
|
||
expect(app.output()).toContain('用法: /shell <命令>');
|
||
|
||
await app.stop();
|
||
});
|
||
|
||
it('connect 不注入 -s(D2)', async () => {
|
||
const app = makeApp();
|
||
await sleep(300);
|
||
|
||
app.write('/connect 192.168.1.8:5555\r');
|
||
await sleep(300);
|
||
|
||
expect(execAdbMock).toHaveBeenCalledWith(['connect', '192.168.1.8:5555'], undefined);
|
||
|
||
await app.stop();
|
||
});
|
||
|
||
it('无设备时分屏隐藏,deviceScoped 命令给出提示(A3/V5)', async () => {
|
||
const poller = new DevicePoller(() => Promise.resolve([]));
|
||
const app = renderHarness(<App adbVersion="1.0.41" poller={poller} />);
|
||
await sleep(300);
|
||
expect(app.output()).not.toContain('Devices (');
|
||
|
||
app.write('/root\r');
|
||
await sleep(300);
|
||
expect(app.output()).toContain('未选择目标设备');
|
||
expect(execAdbMock).not.toHaveBeenCalled();
|
||
|
||
await app.stop();
|
||
});
|
||
|
||
it('/devices 进入分屏,上下选择,回车确认切换目标设备(A4)', async () => {
|
||
const app = makeApp();
|
||
await sleep(300);
|
||
|
||
app.write('/devices\r');
|
||
await sleep(400);
|
||
// 注:提示文本在 25% 宽面板内可能被换行拆断,断言用单行内子串
|
||
expect(app.output()).toContain('回车确认');
|
||
|
||
// ↓ 选中第二台,回车确认
|
||
app.write('\x1b[B');
|
||
await sleep(300);
|
||
app.write('\r');
|
||
await sleep(300);
|
||
expect(app.output()).toContain('目标设备已切换: serial-2');
|
||
|
||
// 后续命令作用于新目标设备
|
||
execAdbMock.mockClear();
|
||
app.write('/root\r');
|
||
await sleep(400);
|
||
expect(execAdbMock).toHaveBeenCalledWith(['-s', 'serial-2', 'root'], undefined);
|
||
|
||
await app.stop();
|
||
});
|
||
|
||
it('q 退出分屏且不改变选中设备(A4)', async () => {
|
||
const app = makeApp();
|
||
await sleep(300);
|
||
|
||
app.write('/devices\r');
|
||
await sleep(400);
|
||
app.write('\x1b[B'); // 浏览到第二台但不确认
|
||
await sleep(300);
|
||
app.write('q');
|
||
await sleep(300);
|
||
expect(app.output()).not.toContain('目标设备已切换');
|
||
|
||
execAdbMock.mockClear();
|
||
app.write('/remount\r');
|
||
await sleep(400);
|
||
expect(execAdbMock).toHaveBeenCalledWith(['-s', 'serial-1', 'remount'], undefined);
|
||
|
||
await app.stop();
|
||
});
|
||
|
||
it('非白名单输入给出不支持提示(A9)', async () => {
|
||
const app = makeApp();
|
||
await sleep(300);
|
||
|
||
app.write('hello\r');
|
||
await sleep(400);
|
||
expect(app.output()).toContain('不支持的命令: hello');
|
||
|
||
await app.stop();
|
||
});
|
||
|
||
it('Ctrl+C 清空输入行不退出(A10/D8)', async () => {
|
||
const app = makeApp();
|
||
await sleep(300);
|
||
|
||
app.write('abc');
|
||
await sleep(150);
|
||
app.write('\x03');
|
||
await sleep(150);
|
||
// 若未清空,后续 /exit 会拼成 "abc/exit" 而无法执行
|
||
app.write('/exit\r');
|
||
await sleep(300);
|
||
expect(app.output()).toContain('再见');
|
||
expect(app.output()).not.toContain('不支持的命令: abc');
|
||
|
||
await app.stop();
|
||
});
|
||
|
||
it('/mouse off|on 切换捕获并在底部显示当前模式(A17)', async () => {
|
||
const onMouseModeChange = vi.fn();
|
||
const poller = new DevicePoller(() => Promise.resolve(structuredClone(fakeDevices)));
|
||
const app = renderHarness(
|
||
<App adbVersion="1.0.41" poller={poller} onMouseModeChange={onMouseModeChange} />,
|
||
);
|
||
await sleep(300);
|
||
expect(app.output()).toContain('模式:浏览 · 输出:底部');
|
||
|
||
app.write('/mouse off\r');
|
||
await sleep(400);
|
||
expect(onMouseModeChange).toHaveBeenCalledWith(false);
|
||
expect(app.output()).toContain('鼠标捕获已关闭');
|
||
expect(app.output()).toContain('模式:复制 · 输出:底部');
|
||
|
||
app.write('/mouse maybe\r');
|
||
await sleep(400);
|
||
expect(onMouseModeChange).toHaveBeenCalledTimes(1);
|
||
expect(app.output()).toContain('用法: /mouse <on|off>');
|
||
|
||
app.write('/mouse on\r');
|
||
await sleep(400);
|
||
expect(onMouseModeChange).toHaveBeenLastCalledWith(true);
|
||
expect(app.output().lastIndexOf('模式:浏览 · 输出:底部')).toBeGreaterThan(
|
||
app.output().lastIndexOf('模式:复制 · 输出:底部'),
|
||
);
|
||
|
||
await app.stop();
|
||
});
|
||
|
||
it('目标设备从列表消失时清空选中并提示(D3)', async () => {
|
||
let online = true;
|
||
const poller = new DevicePoller(() =>
|
||
Promise.resolve(online ? structuredClone(fakeDevices) : []),
|
||
);
|
||
const app = renderHarness(<App adbVersion="1.0.41" poller={poller} />);
|
||
await sleep(300);
|
||
expect(app.output()).toContain('已选择目标设备: serial-1');
|
||
|
||
online = false; // 模拟设备断开
|
||
await sleep(1500);
|
||
expect(app.output()).toContain('目标设备已断开连接 (serial-1)');
|
||
expect(app.output()).not.toContain('Devices (1)'); // 分屏隐藏
|
||
|
||
// 选中已清空,后续 deviceScoped 命令给出提示
|
||
app.write('/root\r');
|
||
await sleep(400);
|
||
expect(app.output()).toContain('未选择目标设备');
|
||
|
||
await app.stop();
|
||
});
|
||
|
||
it('/exit 输出再见并退出(A10)', async () => {
|
||
const app = makeApp();
|
||
await sleep(300);
|
||
|
||
app.write('/exit\r');
|
||
await sleep(300);
|
||
expect(app.output()).toContain('再见');
|
||
|
||
await app.stop();
|
||
});
|
||
});
|