import { EventEmitter } from 'node:events'; import { describe, expect, it, vi } from 'vitest'; import { createAltScreen } from '../src/term/altScreen.js'; const ENTER_SEQ = '\x1b[?1049h\x1b[H\x1b[?1000h\x1b[?1006h'; const EXIT_SEQ = '\x1b[?1006l\x1b[?1000l\x1b[?1049l'; const MOUSE_ENABLE_SEQ = '\x1b[?1000h\x1b[?1006h'; const MOUSE_DISABLE_SEQ = '\x1b[?1006l\x1b[?1000l'; function makeFixtures() { const writes: string[] = []; const stream = { write: (data: string) => void writes.push(data) }; const emitter = new EventEmitter(); const exitSpy = vi.fn(() => undefined as never); const target = { once: emitter.once.bind(emitter), removeListener: emitter.removeListener.bind(emitter), exit: exitSpy, }; return { stream, target, writes, emitter, exitSpy }; } describe('createAltScreen(备用屏幕 + 鼠标捕获,spec v0.3/v0.4 A11)', () => { it('enter 写入 1049h+1000h+1006h,exit 反序写入恢复序列', () => { const { stream, target, writes } = makeFixtures(); const alt = createAltScreen(stream, target); alt.enter(); expect(writes).toEqual([ENTER_SEQ]); alt.exit(); expect(writes).toEqual([ENTER_SEQ, EXIT_SEQ]); }); it('enter/exit 幂等:重复调用不重复写入', () => { const { stream, target, writes } = makeFixtures(); const alt = createAltScreen(stream, target); alt.enter(); alt.enter(); alt.exit(); alt.exit(); expect(writes).toEqual([ENTER_SEQ, EXIT_SEQ]); }); it('活动期间即时切换鼠标捕获,重复设置幂等', () => { const { stream, target, writes } = makeFixtures(); const alt = createAltScreen(stream, target); alt.enter(); alt.setMouseEnabled(false); alt.setMouseEnabled(false); alt.setMouseEnabled(true); alt.setMouseEnabled(true); expect(writes).toEqual([ENTER_SEQ, MOUSE_DISABLE_SEQ, MOUSE_ENABLE_SEQ]); alt.exit(); expect(writes.at(-1)).toBe(EXIT_SEQ); }); it('进入备用屏前关闭鼠标模式时不写启用序列', () => { const { stream, target, writes } = makeFixtures(); const alt = createAltScreen(stream, target); alt.setMouseEnabled(false); alt.enter(); expect(writes).toEqual(['\x1b[?1049h\x1b[H']); alt.exit(); expect(writes.at(-1)).toBe(EXIT_SEQ); }); it('exit 清理 exit/信号钩子,进程正常退出时不重复写入恢复序列', () => { const { stream, target, writes, emitter } = makeFixtures(); const alt = createAltScreen(stream, target); alt.enter(); alt.exit(); emitter.emit('exit'); expect(writes).toEqual([ENTER_SEQ, EXIT_SEQ]); }); it('未手动 exit 时,进程 exit 事件兜底恢复主屏', () => { const { stream, target, writes, emitter } = makeFixtures(); const alt = createAltScreen(stream, target); alt.enter(); emitter.emit('exit'); expect(writes).toEqual([ENTER_SEQ, EXIT_SEQ]); }); it('SIGTERM 等信号:先恢复主屏再退出进程', () => { const { stream, target, writes, emitter, exitSpy } = makeFixtures(); const alt = createAltScreen(stream, target); alt.enter(); emitter.emit('SIGTERM'); expect(writes).toEqual([ENTER_SEQ, EXIT_SEQ]); expect(exitSpy).toHaveBeenCalledTimes(1); emitter.emit('exit'); expect(writes).toEqual([ENTER_SEQ, EXIT_SEQ]); }); it('信号监听覆盖 SIGINT/SIGTERM/SIGHUP', () => { const { stream, target, writes, emitter } = makeFixtures(); const alt = createAltScreen(stream, target); alt.enter(); for (const sig of ['SIGINT', 'SIGHUP'] as const) { emitter.emit(sig); } expect(writes.filter((w) => w === EXIT_SEQ).length).toBe(1); }); });