forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_suggestion.lua
More file actions
343 lines (289 loc) · 14.3 KB
/
test_suggestion.lua
File metadata and controls
343 lines (289 loc) · 14.3 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
local reference_screenshot = MiniTest.expect.reference_screenshot
local child_helper = require("tests.child_helper")
local child = child_helper.new_child_neovim("test_suggestion")
local T = MiniTest.new_set({
hooks = {
pre_once = function() end,
pre_case = function()
child.run_pre_case(true)
child.bo.readonly = false
end,
post_once = child.stop,
},
})
T["suggestion()"] = MiniTest.new_set()
T["suggestion()"]["suggestion works"] = function()
child.o.lines, child.o.columns = 10, 15
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.configure_copilot()
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7")
child.wait_for_suggestion()
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["suggestion()"]["auto_trigger is false, will not show ghost test"] = function()
child.o.lines, child.o.columns = 10, 15
child.configure_copilot()
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7")
child.wait_for_suggestion()
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["suggestion()"]["accept keymap to trigger sugestion"] = function()
child.o.lines, child.o.columns = 10, 15
child.config.suggestion = child.config.suggestion .. "keymap = { accept = '<C-p>' },"
child.configure_copilot()
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7", "<C-p>")
child.wait_for_suggestion()
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["suggestion()"]["accept keymap to trigger sugestion (default)"] = function()
child.o.lines, child.o.columns = 10, 15
child.configure_copilot()
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7", "<M-l>")
child.wait_for_suggestion()
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["suggestion()"]["accept keymap to trigger suggestion (TAB)"] = function()
child.o.lines, child.o.columns = 10, 15
child.config.suggestion = child.config.suggestion .. "keymap = { accept = '<Tab>' },"
child.configure_copilot()
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7", "<Tab>")
child.wait_for_suggestion()
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["suggestion()"]["accept keymap to trigger suggestion - manual attach"] = function()
child.o.lines, child.o.columns = 10, 15
child.config.suggestion = child.config.suggestion .. "keymap = { accept = '<C-p>' },"
child.configure_copilot()
child.cmd("Copilot attach")
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7", "<C-p>")
child.wait_for_suggestion()
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["suggestion()"]["accept keymap, no suggestion, execute normal keystroke"] = function()
child.o.lines, child.o.columns = 10, 15
child.config.suggestion = child.config.suggestion .. "keymap = { accept = '<CR>' },\n" .. "trigger_on_accept = false,"
child.configure_copilot()
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7", "<CR>", "a")
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["suggestion()"]["accept line keymap, no suggestion, execute normal keystroke"] = function()
child.o.lines, child.o.columns = 10, 15
child.config.suggestion = child.config.suggestion
.. "keymap = { accept_line = '<CR>' },\n"
.. "trigger_on_accept = false,"
child.configure_copilot()
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7", "<CR>", "a")
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["suggestion()"]["accept word keymap, no suggestion, execute normal keystroke"] = function()
child.o.lines, child.o.columns = 10, 15
child.config.suggestion = child.config.suggestion
.. "keymap = { accept_word = '<CR>' },\n"
.. "trigger_on_accept = false,"
child.configure_copilot()
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7", "<CR>", "a")
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["suggestion()"]["accept_word, 1 word, works"] = function()
child.o.lines, child.o.columns = 10, 15
child.config.suggestion = child.config.suggestion .. "auto_trigger = true," .. "keymap = { accept_word = '<C-e>' },"
child.cmd("e numbers_with_spaces.txt")
child.configure_copilot()
child.type_keys("i1 2 3", "<Esc>", "o4 5 6", "<Esc>", "o7 ")
child.wait_for_suggestion()
child.type_keys("<C-e>", "<Esc>")
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["suggestion()"]["accept_word, 2 words, works"] = function()
child.o.lines, child.o.columns = 10, 15
child.config.suggestion = child.config.suggestion .. "auto_trigger = true," .. "keymap = { accept_word = '<C-e>' },"
child.cmd("e numbers_with_spaces.txt")
child.configure_copilot()
child.type_keys("i1 2 3", "<Esc>", "o4 5 6", "<Esc>", "o7 ")
child.wait_for_suggestion()
child.type_keys("<C-e>", "<C-e>", "<Esc>")
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
-- - accept_word, 1 word then next
-- - accept_word, 1 word then prev
T["suggestion()"]["accept_word, 1 word, then dismiss"] = function()
child.o.lines, child.o.columns = 10, 15
child.config.suggestion = child.config.suggestion
.. "auto_trigger = true,"
.. "keymap = { accept_word = '<C-e>', dismiss = '<Tab>' },"
child.cmd("e numbers_with_spaces.txt")
child.configure_copilot()
child.type_keys("i1 2 3", "<Esc>", "o4 5 6", "<Esc>", "o7 ")
child.wait_for_suggestion()
child.type_keys("<C-e>", "<Tab>")
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["suggestion()"]["accept_word, 1 word, then dismiss with Esc"] = function()
child.o.lines, child.o.columns = 10, 15
child.config.suggestion = child.config.suggestion
.. "auto_trigger = true,"
.. "keymap = { accept_word = '<C-e>', dismiss = '<Esc>' },"
child.cmd("e numbers_with_spaces.txt")
child.configure_copilot()
child.type_keys("i1 2 3", "<Esc>", "o4 5 6", "<Esc>", "o7 ")
child.wait_for_suggestion()
child.type_keys("<C-e>", "<Esc>")
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["suggestion()"]["accept_word, 1 word, then accept"] = function()
child.o.lines, child.o.columns = 10, 15
child.config.suggestion = child.config.suggestion
.. "auto_trigger = true,"
.. "keymap = { accept_word = '<C-e>', accept = '<Tab>' },"
child.cmd("e numbers_with_spaces.txt")
child.configure_copilot()
child.type_keys("i1 2 3", "<Esc>", "o4 5 6", "<Esc>", "o7 ")
child.wait_for_suggestion()
child.type_keys("<C-e>", "<Tab>")
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["suggestion()"]["accept_line, 1 line, works"] = function()
child.o.lines, child.o.columns = 30, 15
child.config.suggestion = child.config.suggestion .. "auto_trigger = true," .. "keymap = { accept_line = '<C-e>' },"
child.cmd("e numbers_as_arrays.txt")
child.configure_copilot()
child.type_keys("i{", "<Esc>o", " 1,2,3", "<Esc>o", "4,5,6", "<Esc>o", "7,8,9", "<Esc>o<bs>", "}", "<Esc>")
child.type_keys("o{", "<Esc>o", " 10,11,12", "<Esc>", "o13,14,15", "<Esc>", "o16,17,18", "<Esc>o<bs>", "}", "<Esc>")
child.type_keys("o{", "<Esc>o")
child.wait_for_suggestion()
child.type_keys("<C-e>", "<Esc>")
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 29, 30 }, ignore_attr = { 29, 30 } })
end
T["suggestion()"]["accept_line, 3 lines, works"] = function()
child.o.lines, child.o.columns = 50, 15
child.config.suggestion = child.config.suggestion .. "auto_trigger = true," .. "keymap = { accept_line = '<C-e>' },"
child.cmd("e numbers_as_arrays.txt")
child.configure_copilot()
child.type_keys("i{", "<Esc>o", " 1,2,3", "<Esc>o", "4,5,6", "<Esc>o", "7,8,9", "<Esc>o<bs>", "}", "<Esc>")
child.type_keys("o{", "<Esc>o", " 10,11,12", "<Esc>", "o13,14,15", "<Esc>", "o16,17,18", "<Esc>o<bs>", "}", "<Esc>")
child.type_keys("o{", "<Esc>o")
child.wait_for_suggestion()
child.type_keys("<C-e>", "<C-e>", "<C-e>", "<Esc>")
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 49, 50 }, ignore_attr = { 49, 50 } })
end
-- - accept_line, 1 line then next
-- - accept_line, 1 line then prev
T["suggestion()"]["accept_line, 1 line, then dismiss"] = function()
child.o.lines, child.o.columns = 30, 15
child.config.suggestion = child.config.suggestion
.. "auto_trigger = true,"
.. "keymap = { accept_line = '<C-e>', dismiss = '<Tab>' },"
child.cmd("e numbers_as_arrays.txt")
child.configure_copilot()
child.type_keys("i{", "<Esc>o", " 1,2,3", "<Esc>o", "4,5,6", "<Esc>o", "7,8,9", "<Esc>o<bs>", "}", "<Esc>")
child.type_keys("o{", "<Esc>o", " 10,11,12", "<Esc>", "o13,14,15", "<Esc>", "o16,17,18", "<Esc>o<bs>", "}", "<Esc>")
child.type_keys("o{", "<Esc>o")
child.wait_for_suggestion()
child.type_keys("<C-e>", "<Tab>")
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 29, 30 }, ignore_attr = { 29, 30 } })
end
T["suggestion()"]["accept_line, 1 line, then accept"] = function()
child.o.lines, child.o.columns = 50, 40
child.config.suggestion = child.config.suggestion
.. "auto_trigger = true,"
.. "keymap = { accept_line = '<C-e>', accept = '<Tab>' },"
child.cmd("e numbers_as_arrays.txt")
child.configure_copilot()
child.type_keys("i{", "<Esc>o", " 1,2,3", "<Esc>o", "4,5,6", "<Esc>o", "7,8,9", "<Esc>o<bs>", "}", "<Esc>")
child.type_keys("o{", "<Esc>o", " 10,11,12", "<Esc>", "o13,14,15", "<Esc>", "o16,17,18", "<Esc>o<bs>", "}", "<Esc>")
child.type_keys("o{", "<Esc>o")
child.wait_for_suggestion()
child.type_keys("<C-e>", "<Tab>")
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 49, 50 }, ignore_attr = { 49, 50 } })
end
T["suggestion()"]["duplicated keymap yields correct error message"] = function()
child.config.suggestion = child.config.suggestion .. "auto_trigger = true," .. "keymap = { accept = '<M-CR>' },"
child.cmd("e numbers_with_spaces.txt")
child.configure_copilot()
child.type_keys("i1 2 3", "<Esc>", "o4 5 6", "<Esc>", "o7 ")
child.wait_for_suggestion()
child.type_keys("<M-CR>", "<Tab>")
child.cmd_capture("Copilot disable")
local mess = child.cmd_capture("messages")
assert(mess:match("E31") == nil, "Error E31 should have been handled")
assert(mess:match("please review your configuration") ~= nil, "Should have logged a message about keymap conflict")
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 49, 50 }, ignore_attr = { 49, 50 } })
end
T["suggestion()"]["is_visible works"] = function()
child.o.lines, child.o.columns = 10, 15
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.configure_copilot()
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7")
child.wait_for_suggestion()
local is_visible = child.lua('return require("copilot.suggestion").is_visible()')
assert(is_visible, "is_visible should be true")
end
T["suggestion()"]["suggestion with indentation mismatch"] = function()
child.o.lines, child.o.columns = 10, 20
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.cmd("e indented_suggestion.txt")
child.configure_copilot()
child.type_keys("idef foo():", "<Esc>", "o ")
child.wait_for_suggestion()
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["suggestion()"]["suggestion with range offset"] = function()
child.o.lines, child.o.columns = 10, 20
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.cmd("e range_offset.txt")
child.configure_copilot()
child.type_keys("idef bar():", "<Esc>", "o i")
child.wait_for_suggestion()
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["suggestion()"]["is_visible returns nil after accept"] = function()
child.o.lines, child.o.columns = 10, 15
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.configure_copilot()
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7")
child.wait_for_suggestion()
local is_visible_before = child.lua('return require("copilot.suggestion").is_visible()')
assert(is_visible_before, "is_visible should be true before accept")
child.lua('require("copilot.suggestion").accept()')
-- Allow scheduled functions to complete
child.lua("vim.wait(200, function() return false end, 10)")
local is_visible_after = child.lua('return require("copilot.suggestion").is_visible()')
MiniTest.expect.equality(is_visible_after, vim.NIL)
end
T["suggestion()"]["next keymap triggers suggestion"] = function()
child.configure_copilot()
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7")
child.type_keys("<M-]>")
child.wait_for_suggestion()
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 49, 50 }, ignore_attr = { 49, 50 } })
end
T["suggestion()"]["accept does not use feedkeys autoindent hack"] = function()
child.o.lines, child.o.columns = 10, 15
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.configure_copilot()
-- Intercept nvim_feedkeys to detect the autoindent hack
child.lua([[
_G.feedkeys_calls = {}
local original_feedkeys = vim.api.nvim_feedkeys
vim.api.nvim_feedkeys = function(keys, mode, escape_ks)
table.insert(_G.feedkeys_calls, keys)
return original_feedkeys(keys, mode, escape_ks)
end
]])
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7")
child.wait_for_suggestion()
child.lua('require("copilot.suggestion").accept()')
child.lua("vim.wait(200, function() return false end, 10)")
-- Check that no feedkeys call contains the Space-Left-Del pattern
local has_hack = child.lua([[
local space_left_del = vim.api.nvim_replace_termcodes("<Space><Left><Del>", true, false, true)
for _, keys in ipairs(_G.feedkeys_calls) do
if keys == space_left_del then
return true
end
end
return false
]])
MiniTest.expect.equality(has_hack, false)
end
return T