|
| 1 | +import { describe, it, expect, mock, beforeEach, afterEach } from 'bun:test'; |
| 2 | + |
| 3 | +// Mock @clack/prompts and sisteransi before importing the wrapper. |
| 4 | +// These mocks are scoped to this file — placed under tests/unit/cli/ |
| 5 | +// to avoid leaking into src/cli/tui/__tests__/ where context.test.ts |
| 6 | +// imports real modules. |
| 7 | +const cancelSymbol = Symbol('clack:cancel'); |
| 8 | +const mockSelect = mock(() => Promise.resolve('value' as unknown)); |
| 9 | +const mockMultiselect = mock(() => Promise.resolve(['value'] as unknown)); |
| 10 | +const mockText = mock(() => Promise.resolve('text' as unknown)); |
| 11 | +const mockConfirm = mock(() => Promise.resolve(true as unknown)); |
| 12 | +const mockIsCancel = mock((v: unknown) => v === cancelSymbol); |
| 13 | + |
| 14 | +mock.module('@clack/prompts', () => ({ |
| 15 | + select: mockSelect, |
| 16 | + multiselect: mockMultiselect, |
| 17 | + text: mockText, |
| 18 | + confirm: mockConfirm, |
| 19 | + isCancel: mockIsCancel, |
| 20 | +})); |
| 21 | + |
| 22 | +const mockCursorMove = mock(() => '\x1b[MOVE]'); |
| 23 | +const mockEraseDown = mock(() => '\x1b[ERASE]'); |
| 24 | +mock.module('sisteransi', () => ({ |
| 25 | + cursor: { move: mockCursorMove }, |
| 26 | + erase: { down: mockEraseDown }, |
| 27 | +})); |
| 28 | + |
| 29 | +let stdoutWrites: string[] = []; |
| 30 | +const originalWrite = process.stdout.write; |
| 31 | + |
| 32 | +const { select, multiselect, text, confirm } = await import( |
| 33 | + '../../../src/cli/tui/prompts.js' |
| 34 | +); |
| 35 | + |
| 36 | +describe('TUI prompt wrappers', () => { |
| 37 | + beforeEach(() => { |
| 38 | + stdoutWrites = []; |
| 39 | + process.stdout.write = ((data: string) => { |
| 40 | + stdoutWrites.push(data); |
| 41 | + return true; |
| 42 | + }) as typeof process.stdout.write; |
| 43 | + }); |
| 44 | + |
| 45 | + afterEach(() => { |
| 46 | + process.stdout.write = originalWrite; |
| 47 | + mockSelect.mockReset(); |
| 48 | + mockMultiselect.mockReset(); |
| 49 | + mockText.mockReset(); |
| 50 | + mockConfirm.mockReset(); |
| 51 | + mockCursorMove.mockReset(); |
| 52 | + mockEraseDown.mockReset(); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('select', () => { |
| 56 | + it('returns value without erasing when not cancelled', async () => { |
| 57 | + mockSelect.mockResolvedValueOnce('chosen'); |
| 58 | + const result = await select({ message: 'Pick', options: [] }); |
| 59 | + expect(result).toBe('chosen'); |
| 60 | + expect(stdoutWrites).toHaveLength(0); |
| 61 | + }); |
| 62 | + |
| 63 | + it('erases prompt output when cancelled', async () => { |
| 64 | + mockSelect.mockResolvedValueOnce(cancelSymbol); |
| 65 | + const result = await select({ message: 'Pick', options: [] }); |
| 66 | + expect(result).toBe(cancelSymbol); |
| 67 | + expect(mockCursorMove).toHaveBeenCalledWith(0, -2); |
| 68 | + expect(mockEraseDown).toHaveBeenCalled(); |
| 69 | + expect(stdoutWrites.length).toBeGreaterThan(0); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('multiselect', () => { |
| 74 | + it('returns value without erasing when not cancelled', async () => { |
| 75 | + mockMultiselect.mockResolvedValueOnce(['a', 'b']); |
| 76 | + const result = await multiselect({ message: 'Pick', options: [] }); |
| 77 | + expect(result).toEqual(['a', 'b']); |
| 78 | + expect(stdoutWrites).toHaveLength(0); |
| 79 | + }); |
| 80 | + |
| 81 | + it('erases prompt output when cancelled', async () => { |
| 82 | + mockMultiselect.mockResolvedValueOnce(cancelSymbol); |
| 83 | + const result = await multiselect({ message: 'Pick', options: [] }); |
| 84 | + expect(result).toBe(cancelSymbol); |
| 85 | + expect(mockCursorMove).toHaveBeenCalledWith(0, -2); |
| 86 | + expect(mockEraseDown).toHaveBeenCalled(); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('text', () => { |
| 91 | + it('returns value without erasing when not cancelled', async () => { |
| 92 | + mockText.mockResolvedValueOnce('hello'); |
| 93 | + const result = await text({ message: 'Type' }); |
| 94 | + expect(result).toBe('hello'); |
| 95 | + expect(stdoutWrites).toHaveLength(0); |
| 96 | + }); |
| 97 | + |
| 98 | + it('erases prompt output when cancelled', async () => { |
| 99 | + mockText.mockResolvedValueOnce(cancelSymbol); |
| 100 | + const result = await text({ message: 'Type' }); |
| 101 | + expect(result).toBe(cancelSymbol); |
| 102 | + expect(mockCursorMove).toHaveBeenCalledWith(0, -2); |
| 103 | + expect(mockEraseDown).toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('confirm', () => { |
| 108 | + it('returns value without erasing when not cancelled', async () => { |
| 109 | + mockConfirm.mockResolvedValueOnce(true); |
| 110 | + const result = await confirm({ message: 'Sure?' }); |
| 111 | + expect(result).toBe(true); |
| 112 | + expect(stdoutWrites).toHaveLength(0); |
| 113 | + }); |
| 114 | + |
| 115 | + it('erases prompt output when cancelled', async () => { |
| 116 | + mockConfirm.mockResolvedValueOnce(cancelSymbol); |
| 117 | + const result = await confirm({ message: 'Sure?' }); |
| 118 | + expect(result).toBe(cancelSymbol); |
| 119 | + expect(mockCursorMove).toHaveBeenCalledWith(0, -2); |
| 120 | + expect(mockEraseDown).toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments