|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { enterAltScreen } from './alt-screen'; |
| 3 | + |
| 4 | +const ENTER = '\x1b[?1049h'; |
| 5 | +const LEAVE = '\x1b[?1049l'; |
| 6 | + |
| 7 | +function fakeStream(): { writes: string[]; stream: { write(text: string): void } } { |
| 8 | + const writes: string[] = []; |
| 9 | + return { writes, stream: { write: (text: string) => writes.push(text) } }; |
| 10 | +} |
| 11 | + |
| 12 | +describe('enterAltScreen', () => { |
| 13 | + it('enter で ?1049h を書き、leave で ?1049l を書く', () => { |
| 14 | + const { writes, stream } = fakeStream(); |
| 15 | + const leave = enterAltScreen(stream); |
| 16 | + expect(writes).toEqual([ENTER]); |
| 17 | + leave(); |
| 18 | + expect(writes).toEqual([ENTER, LEAVE]); |
| 19 | + }); |
| 20 | + |
| 21 | + it('leave は冪等(2回呼んでも ?1049l は1回だけ)', () => { |
| 22 | + const { writes, stream } = fakeStream(); |
| 23 | + const leave = enterAltScreen(stream); |
| 24 | + leave(); |
| 25 | + leave(); |
| 26 | + expect(writes).toEqual([ENTER, LEAVE]); |
| 27 | + }); |
| 28 | + |
| 29 | + it('クラッシュ保険の exit フックを登録し、leave で解除する', () => { |
| 30 | + const before = process.listenerCount('exit'); |
| 31 | + const { stream } = fakeStream(); |
| 32 | + const leave = enterAltScreen(stream); |
| 33 | + expect(process.listenerCount('exit')).toBe(before + 1); |
| 34 | + leave(); |
| 35 | + expect(process.listenerCount('exit')).toBe(before); |
| 36 | + }); |
| 37 | + |
| 38 | + it('process の exit イベントで leave される(明示 leave を忘れても復元)', () => { |
| 39 | + const { writes, stream } = fakeStream(); |
| 40 | + enterAltScreen(stream); |
| 41 | + process.emit('exit', 0); |
| 42 | + expect(writes).toEqual([ENTER, LEAVE]); |
| 43 | + // exit フック経由でも解除・冪等化されている |
| 44 | + process.emit('exit', 0); |
| 45 | + expect(writes).toEqual([ENTER, LEAVE]); |
| 46 | + }); |
| 47 | +}); |
0 commit comments