adb_cli/tests/uiPolish.test.tsx
2026-09-07 16:16:22 +08:00

88 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { 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 { fakeDevices } = vi.hoisted(() => ({
fakeDevices: [{ serial: 'serial-1', state: 'device', model: 'Pixel_7' }] as Device[],
}));
vi.mock('../src/adb/adbClient.js', async (importOriginal) => {
const actual = await importOriginal<typeof import('../src/adb/adbClient.js')>();
return { ...actual };
});
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} />);
}
/** 提取最后一次渲染的输入行内容(去掉提示符与光标)。 */
function inputLine(output: string): string {
const matches = output.match(/> ([^\n▌]*)▌/g);
return matches?.at(-1)?.replace(/^> /, '').replace(/▌$/, '') ?? '';
}
describe('UI 细节(spec v0.5:选择边界与底部提示行)', () => {
it('底部常驻提示行:包含 / 切换命令、/refresh、退出提示', async () => {
const app = makeApp();
await sleep(300);
const out = app.output();
expect(out).toContain('切换命令');
expect(out).toContain('/refresh');
expect(out).toContain('退出');
await app.stop();
});
it('下方向键在末项后 clamp:连续按 ↓ 超出数量仍停在最后一项(不循环)', async () => {
const app = makeApp();
await sleep(300);
app.write('/');
await sleep(300);
// 命令共 12 项;按 20 次 ↓(循环实现会落到第 8 项 install)
for (let i = 0; i < 20; i++) app.write('\x1b[B');
await sleep(400);
// 回车填入高亮项(quit 是最后一项,非精确匹配过滤文本,触发填入)
app.write('\r');
await sleep(300);
expect(inputLine(app.output())).toBe('/quit ');
await app.stop();
});
it('上方向键在首项后 clamp:按 ↑ 不从第一项跳到末项(不循环)', async () => {
const app = makeApp();
await sleep(300);
app.write('/');
await sleep(300);
for (let i = 0; i < 5; i++) app.write('\x1b[A');
await sleep(400);
app.write('\r');
await sleep(300);
// 循环实现会跳到倒敗第 5 项;clamp 应停在第一项 connect
expect(inputLine(app.output())).toBe('/connect ');
await app.stop();
});
it('滚动窗口:高亮移到第 9 项以后仍可见(面板跟随滚动)', async () => {
const app = makeApp();
await sleep(300);
app.write('/');
await sleep(300);
// 命令共 13 项(v0.7 含 shell);高亮移到第 10 项 devices(index 9,超出固定 8 行窗口)
for (let i = 0; i < 9; i++) app.write('\x1b[B');
await sleep(400);
// 面板文本应包含 devices 项(description 为“打开设备分屏,选择目标设备”)
expect(app.output()).toContain('打开设备分屏');
await app.stop();
});
});