Skip to content

Commit c3f2783

Browse files
committed
test: convert a panel and suggestion tests to use stub lsp
1 parent 67eebb0 commit c3f2783

8 files changed

+275
-54
lines changed

lua/copilot/suggestion/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ function M.accept(modifier)
581581
local ctx = get_ctx()
582582
logger.trace("suggestion accept", ctx)
583583

584-
if config.suggestion.trigger_on_accept and M.first_request_scheduled(ctx) then
584+
if config.suggestion.trigger_on_accept and not get_current_suggestion(ctx) and M.first_request_scheduled(ctx) then
585585
return
586586
end
587587

tests/child_helper.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,15 @@ function M.new_child_neovim(test_name)
5757
]])
5858
end
5959

60-
function child.run_pre_case()
60+
---@param mock_lsp_server? boolean
61+
function child.run_pre_case(mock_lsp_server)
6162
child.reset_config()
6263
child.restart({ "-u", "tests/scripts/minimal_init.lua" })
64+
65+
if mock_lsp_server then
66+
child.lua('package.loaded["copilot.lsp"] = require("tests.stubs.lsp_init")')
67+
end
68+
6369
if env.COPILOT_TOKEN and env.COPILOT_TOKEN ~= "" then
6470
child.fn.setenv("GITHUB_COPILOT_TOKEN", env.COPILOT_TOKEN)
6571
end

tests/screenshots/tests-test_suggestion.lua---suggestion()---accept_line,-1-line,-then-accept

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
--|---------|---------|---------|---------|
2-
01|# Numbers in a 3x3 grid, up to 63
3-
02|{
4-
03| 1,2,3
5-
04| 4,5,6
6-
05| 7,8,9
7-
06|}
8-
07|{
9-
08| 10,11,12
10-
09| 13,14,15
11-
10| 16,17,18
12-
11|}
13-
12|{
14-
13| 19,20,21
15-
14| 22,23,24
16-
15| 25,26,27
17-
16|}
18-
17|{
19-
18| 28,29,30
20-
19| 31,32,33
21-
20| 34,35,36
22-
21|}
23-
22|{
24-
23| 37,38,39
25-
24| 40,41,42
26-
25| 43,44,45
27-
26|}
28-
27|{
29-
28| 46,47,48
30-
29| 49,50,51
31-
30| 52,53,54
32-
31|}
33-
32|{
34-
33| 55,56,57
35-
34| 58,59,60
36-
35| 61,62,63
37-
36|}
2+
01|{
3+
02| 1,2,3
4+
03| 4,5,6
5+
04| 7,8,9
6+
05|}
7+
06|{
8+
07| 10,11,12
9+
08| 13,14,15
10+
09| 16,17,18
11+
10|}
12+
11|{
13+
12| 19,20,21
14+
13| 22,23,24
15+
14| 25,26,27
16+
15|}
17+
16|{
18+
17| 28,29,30
19+
18| 31,32,33
20+
19| 34,35,36
21+
20|}
22+
21|{
23+
22| 37,38,39
24+
23| 40,41,42
25+
24| 43,44,45
26+
25|}
27+
26|{
28+
27| 46,47,48
29+
28| 49,50,51
30+
29| 52,53,54
31+
30|}
32+
31|~
33+
32|~
34+
33|~
35+
34|~
36+
35|~
37+
36|~
3838
37|~
3939
38|~
4040
39|~
@@ -47,7 +47,7 @@
4747
46|~
4848
47|~
4949
48|~
50-
49|[No Name] [+] 36,2 All
50+
49|<rs_as_arrays.txt [+] 30,2 All
5151
50|-- INSERT --
5252

5353
--|---------|---------|---------|---------|
@@ -76,17 +76,17 @@
7676
23|0000000000000000000000000000000000000000
7777
24|0000000000000000000000000000000000000000
7878
25|0000000000000000000000000000000000000000
79-
26|0000000000000000000000000000000000000000
79+
26|1000000000000000000000000000000000000000
8080
27|0000000000000000000000000000000000000000
8181
28|0000000000000000000000000000000000000000
8282
29|0000000000000000000000000000000000000000
83-
30|0000000000000000000000000000000000000000
84-
31|0000000000000000000000000000000000000000
85-
32|1000000000000000000000000000000000000000
86-
33|0000000000000000000000000000000000000000
87-
34|0000000000000000000000000000000000000000
88-
35|0000000000000000000000000000000000000000
89-
36|1000000000000000000000000000000000000000
83+
30|1000000000000000000000000000000000000000
84+
31|2222222222222222222222222222222222222222
85+
32|2222222222222222222222222222222222222222
86+
33|2222222222222222222222222222222222222222
87+
34|2222222222222222222222222222222222222222
88+
35|2222222222222222222222222222222222222222
89+
36|2222222222222222222222222222222222222222
9090
37|2222222222222222222222222222222222222222
9191
38|2222222222222222222222222222222222222222
9292
39|2222222222222222222222222222222222222222

tests/screenshots/tests-test_suggestion.lua---suggestion()---duplicated-keymap-yields-correct-error-message

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
20|~
2222
21|~
2323
22|~
24-
23|[No Name] [+] 3,7-9 All
24+
23|numbers_with_spaces.txt [+] 3,7-9 All
2525
24|-- INSERT --
2626

2727
--|---------|---------|---------|---------|---------|---------|---------|---------|

tests/stubs/lsp_init.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
local M = {}
2+
3+
---@return boolean
4+
function M.initialization_failed()
5+
return false
6+
end
7+
8+
---@return boolean
9+
function M.init()
10+
return true
11+
end
12+
13+
---@param _ vim.lsp.Client|nil
14+
---@return string
15+
function M.get_server_info(_)
16+
return "mocked"
17+
end
18+
19+
function M.get_execute_command()
20+
return require("tests.stubs.lsp_server").server
21+
end
22+
23+
function M.setup(_, _)
24+
return true
25+
end
26+
27+
return M

tests/stubs/lsp_server.lua

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
local logger = require("copilot.logger")
2+
local M = {}
3+
4+
M.messages = {}
5+
M.completion_responses = {
6+
["numbers_with_spaces.txt"] = {
7+
completions = {
8+
{
9+
displayText = "8 9",
10+
docVersion = 7,
11+
position = {
12+
character = 2,
13+
line = 2,
14+
},
15+
range = {
16+
["end"] = {
17+
character = 2,
18+
line = 2,
19+
},
20+
start = {
21+
character = 0,
22+
line = 2,
23+
},
24+
},
25+
text = "7 8 9",
26+
uuid = "b7493391-cd5e-4c10-b0a6-2a844533fbed",
27+
},
28+
},
29+
},
30+
["numbers_as_arrays.txt"] = {
31+
completions = {
32+
{
33+
displayText = " 19,20,21\n 22,23,24\n 25,26,27\n}\n{\n 28,29,30\n 31,32,33\n 34,35,36\n}\n{\n 37,38,39\n 40,41,42\n 43,44,45\n}\n{\n 46,47,48\n 49,50,51\n 52,53,54\n}",
34+
docVersion = 30,
35+
position = {
36+
character = 0,
37+
line = 11,
38+
},
39+
range = {
40+
["end"] = {
41+
character = 0,
42+
line = 11,
43+
},
44+
start = {
45+
character = 0,
46+
line = 11,
47+
},
48+
},
49+
text = " 19,20,21\n 22,23,24\n 25,26,27\n}\n{\n 28,29,30\n 31,32,33\n 34,35,36\n}\n{\n 37,38,39\n 40,41,42\n 43,44,45\n}\n{\n 46,47,48\n 49,50,51\n 52,53,54\n}",
50+
uuid = "1df58ae9-3e93-4e6a-b514-218a9fe7e816",
51+
},
52+
},
53+
},
54+
}
55+
56+
---@param text_document lsp.VersionedTextDocumentIdentifier
57+
local function get_lsp_responses(text_document)
58+
local filename = vim.fs.basename(vim.uri_to_fname(text_document.uri))
59+
logger.trace("get_lsp_responses: " .. filename)
60+
61+
return M.completion_responses[filename]
62+
or {
63+
---@type copilot_get_completions_data
64+
completions = {
65+
{
66+
displayText = "89",
67+
docVersion = 7,
68+
position = {
69+
character = 1,
70+
line = 2,
71+
},
72+
range = {
73+
["end"] = {
74+
character = 1,
75+
line = 2,
76+
},
77+
start = {
78+
character = 0,
79+
line = 2,
80+
},
81+
},
82+
text = "789",
83+
uuid = "63f4ab66-e550-4313-9023-144a33254607",
84+
},
85+
},
86+
}
87+
end
88+
89+
function M.server()
90+
local closing = false
91+
local srv = {}
92+
-- local seen_files = {}
93+
94+
function srv.request(method, params, handler)
95+
logger.trace("lsp request: " .. method)
96+
table.insert(M.messages, { method = method, params = params })
97+
if method == "initialize" then
98+
handler(nil, {
99+
capabilities = {},
100+
})
101+
elseif method == "shutdown" then
102+
handler(nil, nil)
103+
elseif method == "getCompletions" then
104+
-- elseif method == "textDocument/copilotInlineEdit" then
105+
-- if not seen_files[params.textDocument.uri] then
106+
-- seen_files[params.textDocument.uri] = true
107+
local response = get_lsp_responses(params.textDocument)
108+
handler(nil, response)
109+
-- local empty_response = {
110+
-- edits = {},
111+
-- }
112+
-- handler(nil, empty_response)
113+
elseif method == "checkStatus" then
114+
handler(nil, { user = "someUser", status = "OK" })
115+
elseif method == "notifyAccepted" or method == "notifyRejected" or method == "notifyShown" then
116+
handler(nil, {})
117+
elseif method == "getPanelCompletions" then
118+
local params_panel = params or {}
119+
local panelId = params_panel.panelId or (params_panel.doc and params_panel.doc.panelId) or "test-panel-id"
120+
121+
handler(nil, {
122+
solutionCountTarget = 10,
123+
})
124+
125+
vim.defer_fn(function()
126+
local handlers = require("copilot.panel.handlers")
127+
128+
handlers.handlers.PanelSolution(nil, {
129+
completionText = "89\n0\n1",
130+
displayText = "789\n0\n1",
131+
panelId = panelId,
132+
range = {
133+
["end"] = {
134+
character = 1,
135+
line = 2,
136+
},
137+
start = {
138+
character = 1,
139+
line = 2,
140+
},
141+
},
142+
score = 0,
143+
solutionId = "213fc33d8f2dbde3207734e3097ea72a69fb8b009f2878468cdd9e74b70a1e59",
144+
})
145+
146+
handlers.handlers.PanelSolutionsDone(nil, {
147+
panelId = panelId,
148+
status = "OK",
149+
})
150+
end, 10)
151+
else
152+
assert(false, "Unhandled method: " .. method)
153+
end
154+
end
155+
156+
function srv.notify(method, params)
157+
logger.trace("lsp notify")
158+
table.insert(M.messages, { method = method, params = params })
159+
if method == "exit" then
160+
closing = true
161+
end
162+
end
163+
164+
function srv.is_closing()
165+
logger.trace("lsp closing")
166+
return closing
167+
end
168+
169+
function srv.terminate()
170+
logger.trace("lsp terminate")
171+
closing = true
172+
end
173+
174+
return srv
175+
end
176+
177+
function M.reset()
178+
M.messages = {}
179+
end
180+
181+
return M

tests/test_panel.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ local T = MiniTest.new_set({
77
hooks = {
88
pre_once = function() end,
99
pre_case = function()
10-
child.run_pre_case()
10+
child.run_pre_case(true)
1111
child.bo.readonly = false
1212
child.lua("p = require('copilot.panel')")
1313
end,

0 commit comments

Comments
 (0)