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 sleep = (ms = 150) => new Promise((resolve) => setTimeout(resolve, ms)); // harness rows=40 → 设备分屏固定高度 = floor(40/2) = 20 行(含上下边框) const EXPECTED_PANEL_ROWS = 20; function makeDevices(count: number): Device[] { return Array.from({ length: count }, (_, i) => ({ serial: `127.0.0.1:${40000 + i}`, state: 'device', model: `Model_${i}`, })); } function makeApp(devices: Device[]) { const poller = new DevicePoller(() => Promise.resolve(structuredClone(devices))); return renderHarness(); } /** * 测量设备分屏高度:从最后一帧中找圆角边框上沿(╭)到下沿(╰)的行数。 * Ink 用 log-update 擦除重绘,输出末段即最新帧。 */ function panelHeight(output: string): number { const lines = output.split('\n'); let top = -1; for (let i = lines.length - 1; i >= 0; i--) { if (lines[i]!.includes('╭')) { top = i; break; } } if (top < 0) return -1; for (let j = top + 1; j < lines.length; j++) { if (lines[j]!.includes('╰')) return j - top + 1; } return -1; } describe('设备分屏固定高度(spec v0.6:终端高度 1/2,不随补全面板变化)', () => { it('单设备:补全面板展开/收起时分屏高度保持不变', async () => { const app = makeApp(makeDevices(1)); await sleep(300); // 无补全面板 const before = panelHeight(app.output()); expect(before).toBe(EXPECTED_PANEL_ROWS); // 输入 / 展开补全面板(输入区高度从 2 变 10) app.write('/'); await sleep(400); expect(panelHeight(app.output())).toBe(EXPECTED_PANEL_ROWS); // 退格收起补全面板 app.write('\x7f'); await sleep(400); expect(panelHeight(app.output())).toBe(EXPECTED_PANEL_ROWS); await app.stop(); }); it('设备数超出面板容量时截断显示,面板高度不变', async () => { // 容量 = 20 - 2(边框) - 1(标题) - 1(提示) = 16;截断提示占 1 行 → 显示前 15 台 const app = makeApp(makeDevices(30)); await sleep(300); expect(panelHeight(app.output())).toBe(EXPECTED_PANEL_ROWS); const out = app.output(); expect(out).toContain('Devices (30)'); expect(out).toContain('仅显示15台'); expect(out).not.toContain('127.0.0.1:40015'); await app.stop(); }); it('无设备时分屏隐藏(既有行为不受影响)', async () => { const app = makeApp([]); await sleep(300); expect(app.output()).not.toContain('Devices ('); expect(panelHeight(app.output())).toBe(-1); await app.stop(); }); });