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(); return { ...actual }; }); const sleep = (ms = 150) => new Promise((resolve) => setTimeout(resolve, ms)); function makeApp() { const poller = new DevicePoller(() => Promise.resolve(structuredClone(fakeDevices))); return renderHarness(); } /** 提取最后一次渲染的输入行内容(去掉提示符与光标)。 */ function inputLine(output: string): string { const matches = output.match(/> ([^\n▌]*)▌/g); return matches?.at(-1)?.replace(/^> /, '').replace(/▌$/, '') ?? ''; } describe('退格与回车兼容(F4 回归)', () => { it('标准退格(\\x7f)可删除 /', async () => { const app = makeApp(); await sleep(300); app.write('/'); await sleep(300); expect(inputLine(app.output())).toBe('/'); app.write('\x7f'); await sleep(300); expect(inputLine(app.output())).toBe(''); await app.stop(); }); it('ctrl+h(\\b)可删除字符', async () => { const app = makeApp(); await sleep(300); app.write('ab'); await sleep(300); app.write('\x08'); await sleep(300); expect(inputLine(app.output())).toBe('a'); await app.stop(); }); it('CSI-u 非标准退格序列(\\x1b[127;5u)可删除字符', async () => { const app = makeApp(); await sleep(300); app.write('/'); await sleep(300); expect(inputLine(app.output())).toBe('/'); // kitty/CSI-u 键盘协议的 backspace;Ink 无法解析出 backspace 标志, // 兜底逻辑应将其识别为退格而不是拼入文本 app.write('\x1b[127;5u'); await sleep(300); expect(inputLine(app.output())).toBe(''); expect(app.output()).not.toContain('127'); await app.stop(); }); it('回车以 \\n 到达时同样触发提交', async () => { const app = makeApp(); await sleep(300); app.write('ok'); await sleep(300); app.write('\n'); await sleep(300); expect(app.output()).toContain('不支持的命令: ok'); await app.stop(); }); it('粘贴含回车的整段文本(单个 data 事件)正常提交且不残留控制符', async () => { const app = makeApp(); await sleep(300); app.writeRaw('ok\r'); await sleep(400); expect(app.output()).toContain('不支持的命令: ok'); expect(inputLine(app.output())).not.toContain('\r'); await app.stop(); }); it('鼠标 SGR 滚轮/点击事件不进入输入行(T12/spec v0.4 A11)', async () => { const app = makeApp(); await sleep(300); // 滚轮上/下(64/65)与点击按下(0)的 SGR 编码;Ink 剥离块首 \x1b 后由 stripMouseSequences 过滤 app.writeRaw('\x1b[<64;10;5M'); await sleep(300); app.writeRaw('\x1b[<65;10;5M'); await sleep(300); app.writeRaw('\x1b[<0;10;5M'); await sleep(300); app.writeRaw('\x1b[<0;10;5m'); // 点击释放 await sleep(300); expect(inputLine(app.output())).toBe(''); expect(app.output()).not.toContain('<64'); expect(app.output()).not.toContain('65;10'); // 过滤后键盘输入仍正常 app.write('/ex'); await sleep(300); app.write('it'); await sleep(300); app.write('\r'); await sleep(400); expect(app.output()).toContain('再见'); await app.stop(); }); it('快速滚动的鼠标合并块(单个 data 事件)不进入输入行(用户复验缺陷)', async () => { const app = makeApp(); await sleep(300); // 快速滚动时终端合并多个事件为一个 data 块;Ink 整体投递且只剥离块首 \x1b // (use-input.js: input = sequence,slice 一次),形态为 `[<..M\x1b[<..M...` const merged = '\x1b[<64;48;14M'.repeat(6) + '\x1b[<65;48;14M'.repeat(6); app.writeRaw(merged); await sleep(300); expect(inputLine(app.output())).toBe(''); expect(app.output()).not.toContain(';14M'); expect(app.output()).not.toContain('<64'); // 剥离后键盘输入仍正常 app.write('a'); await sleep(300); expect(inputLine(app.output())).toBe('a'); await app.stop(); }); it('鼠标 X10 编码事件不进入输入行', async () => { const app = makeApp(); await sleep(300); // X10 编码:\x1b[M + 3 字节(button+32, col+32, row+32) app.writeRaw('\x1b[M !\"'); await sleep(300); expect(inputLine(app.output())).toBe(''); await app.stop(); }); });