|
| 1 | +import process from 'node:process'; |
| 2 | +import { Writable } from 'node:stream'; |
| 3 | +import { describe, expect, test, vi } from 'vitest'; |
| 4 | +import { createUI as createBrowserUI } from './index.browser.ts'; |
| 5 | +import { createUI } from './index.ts'; |
| 6 | +import { createNodePlatform } from './platform/node.ts'; |
| 7 | +import { createXtermPlatform, type XtermTerminal } from './platform/xterm.ts'; |
| 8 | +import type { Size } from './platform.ts'; |
| 9 | + |
| 10 | +describe('node platform', () => { |
| 11 | + test('reports the TTY size and follows resizes until aborted', () => { |
| 12 | + const output = tty(100, 30); |
| 13 | + const platform = createNodePlatform({ output, signal: AbortSignal.abort() }); |
| 14 | + const events = createEvents(); |
| 15 | + const controller = new AbortController(); |
| 16 | + |
| 17 | + platform.listen(events, controller.signal); |
| 18 | + expect(platform.size()).toEqual({ width: 100, height: 30 }); |
| 19 | + |
| 20 | + output.columns = 120; |
| 21 | + output.emit('resize'); |
| 22 | + expect(events.resize).toHaveBeenLastCalledWith({ width: 120, height: 30 }); |
| 23 | + |
| 24 | + controller.abort(); |
| 25 | + output.emit('resize'); |
| 26 | + expect(events.resize).toHaveBeenCalledTimes(1); |
| 27 | + }); |
| 28 | + |
| 29 | + test('prefers explicit width and height over the TTY size', () => { |
| 30 | + const platform = createNodePlatform({ output: tty(100, 30), width: 40, height: 10 }); |
| 31 | + expect(platform.size()).toEqual({ width: 40, height: 10 }); |
| 32 | + }); |
| 33 | + |
| 34 | + test('falls back to 80×24 when output is not a TTY', () => { |
| 35 | + const platform = createNodePlatform({ output: sink() }); |
| 36 | + expect(platform.size()).toEqual({ width: 80, height: 24 }); |
| 37 | + }); |
| 38 | + |
| 39 | + test('interrupts on SIGINT only when the caller passed no signal', () => { |
| 40 | + const before = process.listenerCount('SIGINT'); |
| 41 | + const controller = new AbortController(); |
| 42 | + |
| 43 | + createNodePlatform({ signal: controller.signal }).listen(createEvents(), controller.signal); |
| 44 | + expect(process.listenerCount('SIGINT')).toBe(before); |
| 45 | + |
| 46 | + const events = createEvents(); |
| 47 | + createNodePlatform({}).listen(events, controller.signal); |
| 48 | + process.emit('SIGINT', 'SIGINT'); |
| 49 | + expect(events.interrupt).toHaveBeenCalledTimes(1); |
| 50 | + |
| 51 | + controller.abort(); |
| 52 | + expect(process.listenerCount('SIGINT')).toBe(before); |
| 53 | + }); |
| 54 | + |
| 55 | + test('createUI() without a signal stops on SIGINT', async () => { |
| 56 | + const ui = await createUI({ output: tty(10, 1) }); |
| 57 | + const running = ui.main(); |
| 58 | + await new Promise((resolve) => setImmediate(resolve)); |
| 59 | + |
| 60 | + process.emit('SIGINT', 'SIGINT'); |
| 61 | + await running; |
| 62 | + expect(ui.main()).toBe(running); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe('xterm platform', () => { |
| 67 | + test('writes bytes and follows resizes until aborted', async () => { |
| 68 | + const terminal = new FakeXterm(80, 24); |
| 69 | + const platform = createXtermPlatform(terminal); |
| 70 | + const events = createEvents(); |
| 71 | + const controller = new AbortController(); |
| 72 | + |
| 73 | + platform.listen(events, controller.signal); |
| 74 | + await platform.write(new Uint8Array([104, 105])); |
| 75 | + expect(terminal.written).toEqual([new Uint8Array([104, 105])]); |
| 76 | + |
| 77 | + terminal.resize(100, 40); |
| 78 | + expect(events.resize).toHaveBeenLastCalledWith({ width: 100, height: 40 }); |
| 79 | + expect(platform.size()).toEqual({ width: 100, height: 40 }); |
| 80 | + |
| 81 | + controller.abort(); |
| 82 | + terminal.resize(10, 10); |
| 83 | + expect(events.resize).toHaveBeenCalledTimes(1); |
| 84 | + }); |
| 85 | + |
| 86 | + test('the browser createUI() renders into the terminal until aborted', async () => { |
| 87 | + const terminal = new FakeXterm(20, 2); |
| 88 | + const controller = new AbortController(); |
| 89 | + const ui = await createBrowserUI({ output: terminal, signal: controller.signal }); |
| 90 | + const running = ui.main(); |
| 91 | + |
| 92 | + ui.document.documentElement.appendChild(ui.document.createTextNode('hello')); |
| 93 | + await vi.waitFor(() => expect(terminal.text()).toContain('hello')); |
| 94 | + |
| 95 | + terminal.resize(30, 3); |
| 96 | + const frames = terminal.written.length; |
| 97 | + await vi.waitFor(() => expect(terminal.written.length).toBeGreaterThan(frames)); |
| 98 | + |
| 99 | + controller.abort(); |
| 100 | + await running; |
| 101 | + expect(terminal.listeners.size).toBe(0); |
| 102 | + }); |
| 103 | +}); |
| 104 | + |
| 105 | +function createEvents(): { |
| 106 | + resize: ReturnType<typeof vi.fn<(size: Size) => void>>; |
| 107 | + interrupt: ReturnType<typeof vi.fn<() => void>>; |
| 108 | +} { |
| 109 | + return { resize: vi.fn<(size: Size) => void>(), interrupt: vi.fn<() => void>() }; |
| 110 | +} |
| 111 | + |
| 112 | +function sink(): Writable { |
| 113 | + return new Writable({ write: (_chunk, _encoding, done) => done() }); |
| 114 | +} |
| 115 | + |
| 116 | +function tty(columns: number, rows: number): Writable & { columns: number; rows: number } { |
| 117 | + return Object.assign(sink(), { columns, rows }); |
| 118 | +} |
| 119 | + |
| 120 | +class FakeXterm implements XtermTerminal { |
| 121 | + cols: number; |
| 122 | + rows: number; |
| 123 | + written: Uint8Array[] = []; |
| 124 | + listeners = new Set<(size: { cols: number; rows: number }) => void>(); |
| 125 | + |
| 126 | + constructor(cols: number, rows: number) { |
| 127 | + this.cols = cols; |
| 128 | + this.rows = rows; |
| 129 | + } |
| 130 | + |
| 131 | + write(data: Uint8Array, callback?: () => void): void { |
| 132 | + this.written.push(data); |
| 133 | + queueMicrotask(() => callback?.()); |
| 134 | + } |
| 135 | + |
| 136 | + onResize(listener: (size: { cols: number; rows: number }) => void): { dispose(): void } { |
| 137 | + this.listeners.add(listener); |
| 138 | + return { dispose: () => this.listeners.delete(listener) }; |
| 139 | + } |
| 140 | + |
| 141 | + resize(cols: number, rows: number): void { |
| 142 | + this.cols = cols; |
| 143 | + this.rows = rows; |
| 144 | + for (const listener of this.listeners) listener({ cols, rows }); |
| 145 | + } |
| 146 | + |
| 147 | + text(): string { |
| 148 | + return this.written.map((bytes) => new TextDecoder().decode(bytes)).join(''); |
| 149 | + } |
| 150 | +} |
0 commit comments