import { beforeEach, describe, expect, it, vi } from 'vitest'; import type { Device } from '../src/adb/deviceParser.js'; import { DevicePoller } from '../src/adb/devicePoller.js'; import { visibleOutputLines } from '../src/components/OutputArea.js'; import { App } from '../src/components/App.js'; import { renderHarness } from './harness.js'; const { execAdbMock } = vi.hoisted(() => ({ execAdbMock: vi.fn() })); vi.mock('../src/adb/adbClient.js', async (importOriginal) => { const actual = await importOriginal(); return { ...actual, execAdb: (args: string[], timeoutMs?: number) => execAdbMock(args, timeoutMs) }; }); const devices: Device[] = [{ serial: 'serial-1', state: 'device', model: 'Pixel_7' }]; const sleep = (ms = 150) => new Promise((resolve) => setTimeout(resolve, ms)); describe('主输出内部滚动(spec v1.5 A18)', () => { beforeEach(() => { execAdbMock.mockReset(); execAdbMock.mockResolvedValue({ code: 0, stdout: 'ok', stderr: '' }); }); it('按距离底部偏移切片并限制边界', () => { const lines = Array.from({ length: 5 }, (_, id) => ({ id, text: `line-${id}` })); expect(visibleOutputLines(lines, 3, 0).map((line) => line.text)).toEqual(['line-2', 'line-3', 'line-4']); expect(visibleOutputLines(lines, 3, 2).map((line) => line.text)).toEqual(['line-0', 'line-1', 'line-2']); expect(visibleOutputLines(lines, 3, 999).map((line) => line.text)).toEqual(['line-0', 'line-1', 'line-2']); }); it('浏览模式滚轮进入历史,End 返回底部', async () => { const output = Array.from({ length: 50 }, (_, index) => `output-${index}`).join('\n'); execAdbMock.mockResolvedValue({ code: 0, stdout: output, stderr: '' }); const poller = new DevicePoller(() => Promise.resolve(structuredClone(devices))); const app = renderHarness(); await sleep(300); app.write('/shell echo\r'); await sleep(400); expect(app.output()).toContain('输出:底部'); app.writeRaw('\x1b[<64;1;1M'); await sleep(250); expect(app.output()).toContain('输出:历史'); app.writeRaw('\x1b[F'); await sleep(250); expect(app.output()).toContain('输出:底部'); await app.stop(); }); it('浏览模式 PageUp/PageDown 可分页浏览主输出', async () => { const output = Array.from({ length: 50 }, (_, index) => `page-${index}`).join('\n'); execAdbMock.mockResolvedValue({ code: 0, stdout: output, stderr: '' }); const poller = new DevicePoller(() => Promise.resolve(structuredClone(devices))); const app = renderHarness(); await sleep(300); app.write('/shell echo\r'); await sleep(400); app.writeRaw('\x1b[5~'); await sleep(200); expect(app.output()).toContain('输出:历史'); app.writeRaw('\x1b[6~'); await sleep(200); expect(app.output()).toContain('输出:底部'); await app.stop(); }); it('/mouse off 后滚轮不改变内部历史状态', async () => { const output = Array.from({ length: 50 }, (_, index) => `output-${index}`).join('\n'); execAdbMock.mockResolvedValue({ code: 0, stdout: output, stderr: '' }); const poller = new DevicePoller(() => Promise.resolve(structuredClone(devices))); const app = renderHarness(); await sleep(300); app.write('/mouse off\r'); await sleep(250); app.write('/shell echo\r'); await sleep(400); app.writeRaw('\x1b[<64;1;1M'); await sleep(200); expect(app.output()).not.toContain('输出:历史'); await app.stop(); }); it('查看历史时新增输出不会把视口强制拉回底部', async () => { const output = Array.from({ length: 50 }, (_, index) => `stable-${index}`).join('\n'); execAdbMock .mockResolvedValueOnce({ code: 0, stdout: output, stderr: '' }) .mockResolvedValue({ code: 0, stdout: 'new-line', stderr: '' }); const poller = new DevicePoller(() => Promise.resolve(structuredClone(devices))); const app = renderHarness(); await sleep(300); app.write('/shell echo\r'); await sleep(400); app.writeRaw('\x1b[<64;1;1M'); await sleep(200); expect(app.output()).toContain('输出:历史'); app.write('/shell echo\r'); await sleep(400); expect(app.output()).toContain('输出:历史'); await app.stop(); }); });