-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutocomplete.test.tsx
More file actions
247 lines (224 loc) · 7.51 KB
/
Autocomplete.test.tsx
File metadata and controls
247 lines (224 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { render } from 'ink-testing-library';
import { tick } from '../utils/test';
vi.mock('../constants', async (importOriginal) => {
const actual = await importOriginal<typeof import('../constants')>();
return {
...actual,
COMMANDS: [
{ name: '/model', description: 'switch the model' },
{ name: '/mock', description: 'mock command' },
],
};
});
import { KEY } from '../constants';
import { Autocomplete } from './Autocomplete';
describe('Autocomplete', () => {
it('renders input prompt', () => {
const { lastFrame } = render(<Autocomplete onSubmit={vi.fn()} />);
expect(lastFrame()).toContain('>');
});
it('does not show suggestions on non-slash input', async () => {
const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />);
stdin.write('h');
await tick();
expect(lastFrame()).not.toContain('/model');
});
it('shows suggestions when typing /', async () => {
const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />);
stdin.write('/');
await tick();
expect(lastFrame()).toContain('/model');
expect(lastFrame()).toContain('switch the model');
});
it('filters suggestions as input narrows', async () => {
const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />);
stdin.write('/');
await tick();
stdin.write('m');
await tick();
expect(lastFrame()).toContain('/model');
stdin.write('x');
await tick();
expect(lastFrame()).not.toContain('/model');
}, 10000);
it('completes suggestion on Tab', async () => {
const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />);
stdin.write('/');
await tick();
stdin.write(KEY.TAB);
await tick();
expect(lastFrame()).toContain('/model');
});
it('submits completed value on Enter after Tab', async () => {
const onSubmit = vi.fn();
const { stdin } = render(<Autocomplete onSubmit={onSubmit} />);
stdin.write('/');
await tick();
stdin.write(KEY.TAB);
await tick();
stdin.write(KEY.ENTER);
await tick();
expect(onSubmit).toHaveBeenCalledWith('/model');
});
it('submits typed text on Enter without suggestion selected', async () => {
const onSubmit = vi.fn();
const { stdin } = render(<Autocomplete onSubmit={onSubmit} />);
stdin.write('h');
await tick();
stdin.write('i');
await tick();
stdin.write(KEY.ENTER);
await tick();
expect(onSubmit).toHaveBeenCalledWith('hi');
});
it('does not submit blank input', async () => {
const onSubmit = vi.fn();
const { stdin } = render(<Autocomplete onSubmit={onSubmit} />);
stdin.write(KEY.ENTER);
await tick();
expect(onSubmit).not.toHaveBeenCalled();
});
it('clears input after submit', async () => {
const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />);
stdin.write('h');
await tick();
stdin.write('i');
await tick();
stdin.write(KEY.ENTER);
await tick();
expect(lastFrame()).not.toContain('hi');
});
it('clears input and suggestions on Escape', async () => {
const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />);
stdin.write('/');
await tick();
expect(lastFrame()).toContain('/model');
stdin.write(KEY.ESCAPE);
await tick(50);
expect(lastFrame()).not.toContain('/model');
});
it('deletes last character on backspace', async () => {
const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />);
stdin.write('/');
await tick();
stdin.write('m');
await tick();
stdin.write(KEY.BACKSPACE);
await tick();
expect(lastFrame()).toContain('/model');
stdin.write(KEY.BACKSPACE);
await tick();
expect(lastFrame()).not.toContain('/model');
});
it('does not accept input when disabled', async () => {
const onSubmit = vi.fn();
const { lastFrame, stdin } = render(
<Autocomplete isDisabled onSubmit={onSubmit} />,
);
stdin.write('h');
await tick();
expect(lastFrame()).not.toContain('h');
stdin.write(KEY.ENTER);
await tick();
expect(onSubmit).not.toHaveBeenCalled();
});
it('moves highlight down with arrow keys', async () => {
const { stdin } = render(<Autocomplete onSubmit={vi.fn()} />);
stdin.write('/');
await tick();
stdin.write(KEY.DOWN);
await tick();
stdin.write(KEY.UP);
await tick();
});
it('submits highlighted suggestion on Enter', async () => {
const onSubmit = vi.fn();
const { stdin } = render(<Autocomplete onSubmit={onSubmit} />);
stdin.write('/');
await tick();
stdin.write(KEY.ENTER);
await tick();
expect(onSubmit).toHaveBeenCalledWith('/model');
});
it('shows non-highlighted suggestions when arrow down moves selection', async () => {
const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />);
stdin.write('/');
await tick();
expect(lastFrame()).toContain('/model');
expect(lastFrame()).toContain('/mock');
stdin.write(KEY.DOWN);
await tick();
expect(lastFrame()).toContain('/mock');
});
it('shows block cursor in output', async () => {
const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />);
stdin.write('hi');
await tick();
// The input should be visible with block cursor at end
expect(lastFrame()).toContain('hi');
});
it('cursor stays at end when typing', async () => {
const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />);
stdin.write('a');
await tick();
stdin.write('b');
await tick();
stdin.write('c');
await tick();
expect(lastFrame()).toContain('abc');
});
it('handles down arrow when no matches available', async () => {
const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />);
// Type /xyz to get zero matches
stdin.write('/xyz');
await tick();
expect(lastFrame()).not.toContain('/model');
// Down arrow with matches.length=0 triggers Math.min(-1, 1) = -1 (line 39-40)
stdin.write(KEY.DOWN);
await tick();
// Should not crash
expect(lastFrame()).toContain('/xyz');
});
it('uses fallback match when selectedIndex out of bounds', async () => {
const onSubmit = vi.fn();
const { stdin } = render(<Autocomplete onSubmit={onSubmit} />);
// Type / then Tab twice - first Tab sets value, second tests with changed state
stdin.write('/');
await tick();
// Move down to increase selectedIndex
stdin.write(KEY.DOWN);
await tick();
stdin.write(KEY.DOWN);
await tick();
// Tab should complete using fallback when selectedIndex >= matches.length (line 44)
stdin.write(KEY.TAB);
await tick();
stdin.write(KEY.ENTER);
await tick();
// Should submit successfully
expect(onSubmit).toHaveBeenCalled();
});
it('nullish coalescing when selectedIndex exceeds matches after filtering', async () => {
const { lastFrame, stdin } = render(<Autocomplete onSubmit={vi.fn()} />);
// Start with both matches showing
stdin.write('/');
await tick();
// Move down to select /mock (index 1)
stdin.write(KEY.DOWN);
await tick();
// Type 'mock' to narrow to just /mock - selectedIndex stays 1, matches.length becomes 1
stdin.write('m');
await tick();
stdin.write('o');
await tick();
stdin.write('c');
await tick();
stdin.write('k');
await tick();
// Tab should trigger ?? fallback since matches[1] is undefined
stdin.write(KEY.TAB);
await tick();
// Should complete to /mock using the fallback
expect(lastFrame()).toContain('/mock');
});
});