|
| 1 | +import { render } from 'ink-testing-library'; |
| 2 | + |
| 3 | +import { tick } from '../utils/test'; |
| 4 | + |
| 5 | +vi.mock('../constants', async (importOriginal) => { |
| 6 | + const actual = await importOriginal<typeof import('../constants')>(); |
| 7 | + return { |
| 8 | + ...actual, |
| 9 | + COMMANDS: [ |
| 10 | + { name: '/model', description: 'Switch the active model' }, |
| 11 | + { name: '/mock', description: 'Mock command' }, |
| 12 | + ], |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +import { KEY } from '../constants'; |
| 17 | +import { Autocomplete } from './Autocomplete'; |
| 18 | + |
| 19 | +describe('Autocomplete', () => { |
| 20 | + it('renders input prompt', () => { |
| 21 | + const { lastFrame } = render(<Autocomplete onSubmit={vi.fn()} />); |
| 22 | + expect(lastFrame()).toContain('>'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('does not show suggestions on non-slash input', async () => { |
| 26 | + const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />); |
| 27 | + stdin.write('h'); |
| 28 | + await tick(); |
| 29 | + expect(lastFrame()).not.toContain('/model'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('shows suggestions when typing /', async () => { |
| 33 | + const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />); |
| 34 | + stdin.write('/'); |
| 35 | + await tick(); |
| 36 | + expect(lastFrame()).toContain('/model'); |
| 37 | + expect(lastFrame()).toContain('Switch the active model'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('filters suggestions as input narrows', async () => { |
| 41 | + const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />); |
| 42 | + stdin.write('/'); |
| 43 | + await tick(); |
| 44 | + stdin.write('m'); |
| 45 | + await tick(); |
| 46 | + expect(lastFrame()).toContain('/model'); |
| 47 | + stdin.write('x'); |
| 48 | + await tick(); |
| 49 | + expect(lastFrame()).not.toContain('/model'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('completes suggestion on Tab', async () => { |
| 53 | + const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />); |
| 54 | + stdin.write('/'); |
| 55 | + await tick(); |
| 56 | + stdin.write(KEY.TAB); |
| 57 | + await tick(); |
| 58 | + expect(lastFrame()).toContain('/model'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('submits completed value on Enter after Tab', async () => { |
| 62 | + const onSubmit = vi.fn(); |
| 63 | + const { stdin } = render(<Autocomplete onSubmit={onSubmit} />); |
| 64 | + stdin.write('/'); |
| 65 | + await tick(); |
| 66 | + stdin.write(KEY.TAB); |
| 67 | + await tick(); |
| 68 | + stdin.write(KEY.ENTER); |
| 69 | + await tick(); |
| 70 | + expect(onSubmit).toHaveBeenCalledWith('/model'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('submits typed text on Enter without suggestion selected', async () => { |
| 74 | + const onSubmit = vi.fn(); |
| 75 | + const { stdin } = render(<Autocomplete onSubmit={onSubmit} />); |
| 76 | + stdin.write('h'); |
| 77 | + await tick(); |
| 78 | + stdin.write('i'); |
| 79 | + await tick(); |
| 80 | + stdin.write(KEY.ENTER); |
| 81 | + await tick(); |
| 82 | + expect(onSubmit).toHaveBeenCalledWith('hi'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('does not submit blank input', async () => { |
| 86 | + const onSubmit = vi.fn(); |
| 87 | + const { stdin } = render(<Autocomplete onSubmit={onSubmit} />); |
| 88 | + stdin.write(KEY.ENTER); |
| 89 | + await tick(); |
| 90 | + expect(onSubmit).not.toHaveBeenCalled(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('clears input after submit', async () => { |
| 94 | + const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />); |
| 95 | + stdin.write('h'); |
| 96 | + await tick(); |
| 97 | + stdin.write('i'); |
| 98 | + await tick(); |
| 99 | + stdin.write(KEY.ENTER); |
| 100 | + await tick(); |
| 101 | + expect(lastFrame()).not.toContain('hi'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('clears input and suggestions on Escape', async () => { |
| 105 | + const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />); |
| 106 | + stdin.write('/'); |
| 107 | + await tick(); |
| 108 | + expect(lastFrame()).toContain('/model'); |
| 109 | + stdin.write(KEY.ESCAPE); |
| 110 | + await tick(50); |
| 111 | + expect(lastFrame()).not.toContain('/model'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('deletes last character on backspace', async () => { |
| 115 | + const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />); |
| 116 | + stdin.write('/'); |
| 117 | + await tick(); |
| 118 | + stdin.write('m'); |
| 119 | + await tick(); |
| 120 | + stdin.write(KEY.BACKSPACE); |
| 121 | + await tick(); |
| 122 | + expect(lastFrame()).toContain('/model'); |
| 123 | + stdin.write(KEY.BACKSPACE); |
| 124 | + await tick(); |
| 125 | + expect(lastFrame()).not.toContain('/model'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('does not accept input when disabled', async () => { |
| 129 | + const onSubmit = vi.fn(); |
| 130 | + const { lastFrame, stdin } = render( |
| 131 | + <Autocomplete isDisabled onSubmit={onSubmit} />, |
| 132 | + ); |
| 133 | + stdin.write('h'); |
| 134 | + await tick(); |
| 135 | + expect(lastFrame()).not.toContain('h'); |
| 136 | + stdin.write(KEY.ENTER); |
| 137 | + await tick(); |
| 138 | + expect(onSubmit).not.toHaveBeenCalled(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('moves highlight down with arrow keys', async () => { |
| 142 | + const { stdin } = render(<Autocomplete onSubmit={vi.fn()} />); |
| 143 | + stdin.write('/'); |
| 144 | + await tick(); |
| 145 | + stdin.write(KEY.DOWN); |
| 146 | + await tick(); |
| 147 | + stdin.write(KEY.UP); |
| 148 | + await tick(); |
| 149 | + }); |
| 150 | + |
| 151 | + it('submits highlighted suggestion on Enter', async () => { |
| 152 | + const onSubmit = vi.fn(); |
| 153 | + const { stdin } = render(<Autocomplete onSubmit={onSubmit} />); |
| 154 | + stdin.write('/'); |
| 155 | + await tick(); |
| 156 | + stdin.write(KEY.ENTER); |
| 157 | + await tick(); |
| 158 | + expect(onSubmit).toHaveBeenCalledWith('/model'); |
| 159 | + }); |
| 160 | + |
| 161 | + it('shows non-highlighted suggestions when arrow down moves selection', async () => { |
| 162 | + const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />); |
| 163 | + stdin.write('/'); |
| 164 | + await tick(); |
| 165 | + expect(lastFrame()).toContain('/model'); |
| 166 | + expect(lastFrame()).toContain('/mock'); |
| 167 | + stdin.write(KEY.DOWN); |
| 168 | + await tick(); |
| 169 | + expect(lastFrame()).toContain('/mock'); |
| 170 | + }); |
| 171 | +}); |
0 commit comments