| |
| |
| |
| |
| |
|
|
| import { vi, describe, expect, it, beforeEach, afterEach } from 'vitest'; |
| import { readStdin } from './readStdin.js'; |
|
|
| |
| const mockStdin = { |
| setEncoding: vi.fn(), |
| read: vi.fn(), |
| on: vi.fn(), |
| removeListener: vi.fn(), |
| destroy: vi.fn(), |
| }; |
|
|
| describe('readStdin', () => { |
| let originalStdin: typeof process.stdin; |
| let onReadableHandler: () => void; |
| let onEndHandler: () => void; |
|
|
| beforeEach(() => { |
| vi.clearAllMocks(); |
| originalStdin = process.stdin; |
|
|
| |
| Object.defineProperty(process, 'stdin', { |
| value: mockStdin, |
| writable: true, |
| configurable: true, |
| }); |
|
|
| |
| mockStdin.on.mockImplementation((event: string, handler: () => void) => { |
| if (event === 'readable') onReadableHandler = handler; |
| if (event === 'end') onEndHandler = handler; |
| }); |
| }); |
|
|
| afterEach(() => { |
| vi.restoreAllMocks(); |
| Object.defineProperty(process, 'stdin', { |
| value: originalStdin, |
| writable: true, |
| configurable: true, |
| }); |
| }); |
|
|
| it('should read and accumulate data from stdin', async () => { |
| mockStdin.read |
| .mockReturnValueOnce('I love ') |
| .mockReturnValueOnce('Gemini!') |
| .mockReturnValueOnce(null); |
|
|
| const promise = readStdin(); |
|
|
| |
| onReadableHandler(); |
|
|
| |
| onEndHandler(); |
|
|
| await expect(promise).resolves.toBe('I love Gemini!'); |
| }); |
|
|
| it('should handle empty stdin input', async () => { |
| mockStdin.read.mockReturnValue(null); |
|
|
| const promise = readStdin(); |
|
|
| |
| onEndHandler(); |
|
|
| await expect(promise).resolves.toBe(''); |
| }); |
|
|
| |
| it('should timeout and resolve with empty string when no input is available', async () => { |
| vi.useFakeTimers(); |
|
|
| const promise = readStdin(); |
|
|
| |
| vi.advanceTimersByTime(500); |
|
|
| await expect(promise).resolves.toBe(''); |
|
|
| vi.useRealTimers(); |
| }); |
|
|
| it('should clear timeout once when data is received and resolve with data', async () => { |
| const clearTimeoutSpy = vi.spyOn(global, 'clearTimeout'); |
| mockStdin.read |
| .mockReturnValueOnce('chunk1') |
| .mockReturnValueOnce('chunk2') |
| .mockReturnValueOnce(null); |
|
|
| const promise = readStdin(); |
|
|
| |
| onReadableHandler(); |
|
|
| expect(clearTimeoutSpy).toHaveBeenCalledOnce(); |
|
|
| |
| onEndHandler(); |
|
|
| await expect(promise).resolves.toBe('chunk1chunk2'); |
| }); |
| }); |
|
|