forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.lua
More file actions
548 lines (451 loc) · 17 KB
/
test_client.lua
File metadata and controls
548 lines (451 loc) · 17 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
local reference_screenshot = MiniTest.expect.reference_screenshot
local child_helper = require("tests.child_helper")
local child = child_helper.new_child_neovim("test_client")
local u = require("tests.utils")
local T = MiniTest.new_set({
hooks = {
pre_once = function() end,
pre_case = function()
child.run_pre_case(true)
child.lua("s = require('copilot.status')")
child.lua("c = require('copilot.client')")
end,
post_once = child.stop,
},
})
T["client.config()"] = MiniTest.new_set()
T["client.config()"]["config, github-enterprise populated"] = function()
child.lua([[M.setup({
auth_provider_url = "https://someurl.com",
})]])
local settings = child.lua("return vim.inspect(c.config.settings)")
u.expect_match(settings, "{.*github%-enterprise.*{.*uri.*https://someurl%.com.*}.*}")
end
T["client()"] = MiniTest.new_set()
T["client()"]["status info"] = function()
child.configure_copilot()
child.cmd("Copilot status")
local messages = child.lua([[
local messages = ""
local function has_passed()
messages = vim.api.nvim_exec("messages", { output = true }) or ""
if messages:find(".*Online.*not yet requested.*") then
return true
end
end
vim.wait(500, function()
return has_passed()
end, 50)
return messages
]])
u.expect_match(messages, ".*Online.*attached.*")
end
T["client()"]["suggestions work when multiple files open with should_attach logic"] = function()
child.config.should_attach = [[function(bufnr, bufname)
local buffername = bufname:match("([^/\\]+)$") or ""
if not _G.bufnames then
_G.bufnames = ''
end
_G.bufnames = _G.bufnames .. ' ; ' .. buffername.. ' (' .. tostring(bufnr) .. ')'
if buffername == "file2.txt" then
return true
end
return false
end]]
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.configure_copilot()
child.cmd("e tests/files/file1.txt")
child.cmd("e tests/files/file2.txt")
child.type_keys("i")
child.type_keys("123", "<Esc>", "o456", "<Esc>", "o7")
child.wait_for_suggestion()
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["client()"]["suggestions off when previous file only should_attach"] = function()
child.config.should_attach = [[function(bufnr, bufname)
local buffername = bufname:match("([^/\\]+)$") or ""
if buffername == "file1.txt" then
return true
end
return false
end]]
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.configure_copilot()
child.cmd("e tests/files/file1.txt")
child.cmd("e tests/files/file2.txt")
child.type_keys("i")
child.type_keys("123", "<Esc>", "o456", "<Esc>", "o7")
child.wait_for_suggestion()
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["client()"]["suggestions off when previous file only should_attach - 2"] = function()
child.config.should_attach = [[function(bufnr, bufname)
local buffername = bufname:match("([^/\\]+)$") or ""
if buffername == "file2.txt" then
return true
end
return false
end]]
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.configure_copilot()
child.cmd("e tests/files/file1.txt")
child.cmd("e tests/files/file2.txt")
child.type_keys("i")
child.cmd("e tests/files/file3.txt")
child.type_keys("123", "<Esc>", "o456", "<Esc>", "o7")
child.wait_for_suggestion()
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["client()"]["suggestions work when already in insert mode and opening file - 3"] = function()
child.config.should_attach = [[function(bufnr, bufname)
local buffername = bufname:match("([^/\\]+)$") or ""
if buffername == "file3.txt" then
return true
end
return false
end]]
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.configure_copilot()
child.cmd("e tests/files/file1.txt")
child.cmd("e tests/files/file2.txt")
child.type_keys("i")
child.cmd("e tests/files/file3.txt")
child.type_keys("123", "<Esc>", "o456", "<Esc>", "o7")
child.wait_for_suggestion()
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
end
T["client()"]["suggestions work when attaching to second buffer in a row"] = function()
child.config.should_attach = [[function(bufnr, bufname)
local buffername = bufname:match("([^/\\]+)$") or ""
if buffername == "file3.txt" then
return true
end
return false
end]]
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.configure_copilot()
child.cmd("e tests/files/file1.txt")
child.cmd("e tests/files/file2.txt")
child.type_keys("i123", "<Esc>")
child.cmd("e tests/files/file3.txt")
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["client()"]["manually detached buffer stays detached"] = function()
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.configure_copilot()
child.type_keys("i123", "<Esc>")
child.cmd("Copilot detach")
child.cmd("e tests/files/file1.txt")
child.type_keys("i123", "<Esc>")
child.cmd("bp")
child.type_keys("i123", "<Esc>")
local filename = child.cmd_capture("echo expand('%:t')")
u.expect_match("", filename)
child.cmd("Copilot status")
local messages = child.lua([[
local messages = ""
local function has_passed()
messages = vim.api.nvim_exec("messages", { output = true }) or ""
if messages:find(".*Online.*attached.*") then
return true
end
end
vim.wait(500, function()
return has_passed()
end, 50)
return messages
]])
u.expect_match(messages, ".*Online.*manually detached.*")
end
T["client()"]["suggestions work when lazy is set to false"] = function()
child.config.should_attach = [[function(bufnr, bufname)
local buffername = bufname:match("([^/\\]+)$") or ""
if buffername == "file3.txt" then
return true
end
return false
end]]
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.configure_copilot()
child.cmd("e tests/files/file1.txt")
child.cmd("e tests/files/file2.txt")
child.type_keys("i123", "<Esc>")
child.cmd("e tests/files/file3.txt")
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["client()"]["will not attach to buffer due to filetype exclusion"] = function()
child.config.filetypes = [[
["*"] = false,
]]
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
-- Reenable with new config
-- T["client()"]["auto_trigger off - will not attach automatically"] = function()
-- child.configure_copilot()
-- child.cmd("e test.txt")
-- child.type_keys("i", "<Esc>")
-- child.cmd("Copilot status")
--
-- local messages = child.lua([[
-- local messages = ""
-- local function has_passed()
-- messages = vim.api.nvim_exec("messages", { output = true }) or ""
-- if messages:find(".*Online.*attached.*") then
-- return true
-- end
-- end
--
-- vim.wait(500, function()
-- return has_passed()
-- end, 50)
--
-- return messages
-- ]])
--
-- u.expect_no_match(messages, ".*Online.*attached.*")
-- end
T["client()"]["auto_trigger off - will attach automatically"] = function()
child.configure_copilot()
child.cmd("e test.txt")
child.type_keys("i", "<Esc>")
child.cmd("Copilot status")
local messages = child.lua([[
local messages = ""
local function has_passed()
messages = vim.api.nvim_exec("messages", { output = true }) or ""
if messages:find(".*Online.*attached.*") then
return true
end
end
vim.wait(500, function()
return has_passed()
end, 50)
return messages
]])
u.expect_match(messages, ".*Online.*attached.*")
end
T["client()"]["suggestion and panel off - will attach automatically"] = function()
child.config.suggestion = "enabled = false,"
child.configure_copilot()
child.cmd("e test.txt")
child.type_keys("i", "<Esc>")
child.cmd("Copilot status")
local messages = child.lua([[
local messages = ""
local function has_passed()
messages = vim.api.nvim_exec("messages", { output = true }) or ""
if messages:find(".*Online.*attached.*") then
return true
end
end
vim.wait(500, function()
return has_passed()
end, 50)
return messages
]])
u.expect_match(messages, ".*Online.*attached.*")
end
-- re-enable with added configuration
-- T["client()"]["auto_trigger off - will attach when requesting suggestion"] = function()
-- child.configure_copilot()
-- child.type_keys("i", "<M-l>", "<Esc>")
-- child.cmd("Copilot status")
--
-- local messages = child.lua([[
-- local messages = ""
-- local function has_passed()
-- messages = vim.api.nvim_exec("messages", { output = true }) or ""
-- if messages:find(".*Online.*attached.*") then
-- return true
-- end
-- end
--
-- vim.wait(500, function()
-- return has_passed()
-- end, 50)
--
-- return messages
-- ]])
--
-- u.expect_match(messages, ".*Online.*attached.*")
-- end
T["client()"]["saving file - will not yield URI not found error"] = function()
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.configure_copilot()
child.type_keys("i", "123", "<Esc>", "o456", "<Esc>", "o7")
child.wait_for_suggestion()
child.type_keys("<Esc>")
child.lua("M.suggested = false")
child.cmd("w! tests/files/test.txt")
child.type_keys("a8")
child.wait_for_suggestion()
local messages = child.cmd_capture("messages")
u.expect_no_match(messages, "RPC.*Document for URI could not be found")
end
T["client()"]["renaming buffer with :file - detaches and re-attaches"] = function()
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.configure_copilot()
child.type_keys("i", "123", "<Esc>", "o456", "<Esc>", "o7")
child.wait_for_suggestion()
child.type_keys("<Esc>")
child.lua("M.suggested = false")
-- Rename the buffer
child.cmd("file tests/files/renamed.txt")
child.lua("vim.wait(200, function() return false end, 10)")
-- After rename, the buffer should still be attached (re-attached by BufFilePost handler)
local is_attached = child.lua("return c.buf_is_attached(0)")
MiniTest.expect.equality(is_attached, true)
-- Verify the detach+re-attach happened by checking the log
local log_has_reattach = child.lua([[
local logfile = io.open("./tests/logs/test_client.log", "r")
if not logfile then return false end
local content = logfile:read("*a")
logfile:close()
return content:find("buffer filename changed") ~= nil
]])
MiniTest.expect.equality(log_has_reattach, true)
-- Verify suggestions still work after rename
child.type_keys("a8")
child.wait_for_suggestion()
local messages = child.cmd_capture("messages")
u.expect_no_match(messages, "RPC.*Document for URI could not be found")
end
T["client()"]["saving unnamed buffer with :w - detaches and re-attaches"] = function()
child.config.suggestion = child.config.suggestion .. "auto_trigger = true,"
child.configure_copilot()
child.type_keys("i", "123", "<Esc>", "o456", "<Esc>", "o7")
child.wait_for_suggestion()
child.type_keys("<Esc>")
child.lua("M.suggested = false")
-- Save unnamed buffer to a file (changes buffer name)
child.cmd("w! tests/files/saved_new.txt")
child.lua("vim.wait(200, function() return false end, 10)")
-- After save, the buffer should still be attached
local is_attached = child.lua("return c.buf_is_attached(0)")
MiniTest.expect.equality(is_attached, true)
-- Verify the detach+re-attach happened by checking the log
local log_has_reattach = child.lua([[
local logfile = io.open("./tests/logs/test_client.log", "r")
if not logfile then return false end
local content = logfile:read("*a")
logfile:close()
return content:find("buffer filename changed") ~= nil
]])
MiniTest.expect.equality(log_has_reattach, true)
-- Verify suggestions still work after save
child.type_keys("a8")
child.wait_for_suggestion()
local messages = child.cmd_capture("messages")
u.expect_no_match(messages, "RPC.*Document for URI could not be found")
end
T["client()"]["should_attach returns false prevents buffer attachment"] = function()
child.config.should_attach = [[function(bufnr, bufname)
return false
end]]
child.configure_copilot()
child.cmd("e test.txt")
child.type_keys("i", "<Esc>")
child.cmd("Copilot status")
local messages = child.lua([[
local messages = ""
local function has_passed()
messages = vim.api.nvim_exec("messages", { output = true }) or ""
if messages:find(".*Online.*") then
return true
end
end
vim.wait(500, function()
return has_passed()
end, 50)
return messages
]])
u.expect_match(messages, ".*Online.*should_attach.*")
end
T["client()"]["disabled copilot does not spam warnings on buffer enter"] = function()
-- Override lsp.setup to return false, simulating a failed initialization
-- (e.g. Node.js not found or wrong version). This sets is_disabled=true
-- in client.setup() but suggestion autocmds are still created.
child.lua([[
local lsp = require("copilot.lsp")
lsp.setup = function(_, _)
return false
end
]])
-- Intercept vim.notify to count "copilot is disabled" warnings
child.lua([[
_G.disabled_warning_count = 0
local original_notify = vim.notify
vim.notify = function(msg, level, opts)
if type(msg) == "string" and msg:find("copilot is disabled") then
_G.disabled_warning_count = _G.disabled_warning_count + 1
end
return original_notify(msg, level, opts)
end
]])
-- Setup copilot with auto_trigger - client will be disabled but suggestion
-- autocmds are still created, causing buf_attach to be called on every
-- insert mode action
child.lua([[
M.setup({
suggestion = { auto_trigger = true },
filetypes = { ["*"] = true },
})
]])
-- Trigger multiple insert mode entries across buffers.
-- Each InsertEnter and CursorMovedI fires the suggestion autocmd,
-- which calls buf_attach, which warns when client is disabled.
child.type_keys("i", "abc", "<Esc>")
child.cmd("e tests/files/file1.txt")
child.type_keys("i", "def", "<Esc>")
child.cmd("e tests/files/file2.txt")
child.type_keys("i", "ghi", "<Esc>")
-- Allow scheduled vim.notify calls to execute
child.lua("vim.wait(500, function() return false end, 50)")
local count = child.lua("return _G.disabled_warning_count")
-- The warning should appear at most once, not on every insert mode action.
-- See: https://github.com/zbirenbaum/copilot.lua/issues/629
MiniTest.expect.equality(count, 0)
end
T["client()"]["on_buf_enter skips filetype check for non-buflisted buffers"] = function()
child.configure_copilot()
-- Create a non-buflisted buffer with a filetype mismatch and trigger BufEnter.
-- This simulates floating windows / preview buffers that should not trigger
-- the filetype-change detach+re-attach cycle.
child.lua([[
local util = require("copilot.util")
local buf = vim.api.nvim_create_buf(false, true) -- nobuflisted, scratch
vim.api.nvim_set_option_value("filetype", "lua", { buf = buf })
-- Manually set previous_ft to simulate a buffer that was previously attached.
-- buf_attach won't succeed for non-buflisted buffers (should_attach rejects them),
-- so previous_ft wouldn't normally be set. In the real bug scenario, this state
-- can be reached through various pathways.
util.set_buffer_previous_ft(buf, "lua")
-- Change filetype on same non-buflisted buffer
vim.api.nvim_set_option_value("filetype", "python", { buf = buf })
-- Trigger BufEnter (simulating entering the buffer)
vim.api.nvim_exec_autocmds("BufEnter", { buffer = buf })
-- Wait for scheduled callbacks to execute
vim.wait(200, function() return false end, 10)
]])
-- The filetype change should NOT cause detach+re-attach for non-buflisted buffers
local detach_was_called = child.lua([[
local log_content = ""
local logfile = io.open("./tests/logs/test_client.log", "r")
if logfile then
log_content = logfile:read("*a")
logfile:close()
end
-- If the guard works, we should NOT see "filetype changed" for non-buflisted buffers
return log_content:find("filetype changed, detaching and re%-attaching") ~= nil
]])
MiniTest.expect.equality(detach_was_called, false)
end
return T