Skip to content

Commit 08894e3

Browse files
committed
test: test improvements and conversion of test_client to stub LSP
1 parent c3f2783 commit 08894e3

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

tests/child_helper.lua

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local env = require("tests.env")
2-
local M = {}
2+
local M = {
3+
mock_lsp_server = false,
4+
}
35

46
if not _G.attach_debugger then
57
_G.attach_debugger = false
@@ -59,6 +61,7 @@ function M.new_child_neovim(test_name)
5961

6062
---@param mock_lsp_server? boolean
6163
function child.run_pre_case(mock_lsp_server)
64+
M.mock_lsp_server = mock_lsp_server or false
6265
child.reset_config()
6366
child.restart({ "-u", "tests/scripts/minimal_init.lua" })
6467

@@ -124,15 +127,35 @@ function M.new_child_neovim(test_name)
124127
end
125128

126129
function child.wait_for_suggestion()
127-
child.lua([[
130+
if M.mock_lsp_server then
131+
child.lua([[
132+
vim.wait(500, function()
133+
return M.suggested
134+
end, 10)
135+
]])
136+
else
137+
child.lua([[
128138
vim.wait(5000, function()
129139
return M.suggested
130140
end, 10)
131141
]])
142+
end
132143
end
133144

134145
function child.wait_for_panel_suggestion()
135-
child.lua([[
146+
if M.mock_lsp_server then
147+
child.lua([[
148+
local function suggestion_is_visible()
149+
lines = vim.api.nvim_buf_get_lines(2, 4, 5, false)
150+
return lines[1] and (lines[1] == "789" or lines[1] == "789\r")
151+
end
152+
153+
vim.wait(500, function()
154+
return suggestion_is_visible()
155+
end, 50)
156+
]])
157+
else
158+
child.lua([[
136159
local function suggestion_is_visible()
137160
lines = vim.api.nvim_buf_get_lines(2, 4, 5, false)
138161
return lines[1] and (lines[1] == "789" or lines[1] == "789\r")
@@ -142,6 +165,7 @@ function M.new_child_neovim(test_name)
142165
return suggestion_is_visible()
143166
end, 50)
144167
]])
168+
end
145169
end
146170

147171
return child

tests/stubs/lsp_server.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,15 @@ function M.server()
111111
-- }
112112
-- handler(nil, empty_response)
113113
elseif method == "checkStatus" then
114-
handler(nil, { user = "someUser", status = "OK" })
114+
vim.defer_fn(function()
115+
handler(nil, { user = "someUser", status = "OK" })
116+
local handlers = require("copilot.status").handlers
117+
handlers.statusNotification(
118+
nil,
119+
{ busy = false, kind = "Normal", message = "", status = "Normal" },
120+
{ client_id = 1, method = "statusNotification" }
121+
)
122+
end, 10)
115123
elseif method == "notifyAccepted" or method == "notifyRejected" or method == "notifyShown" then
116124
handler(nil, {})
117125
elseif method == "getPanelCompletions" then

tests/test_client.lua

Lines changed: 7 additions & 7 deletions
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.lua("s = require('copilot.status')")
1212
child.lua("c = require('copilot.client')")
1313
end,
@@ -40,7 +40,7 @@ T["client()"]["status info"] = function()
4040
end
4141
end
4242
43-
vim.wait(5000, function()
43+
vim.wait(500, function()
4444
return has_passed()
4545
end, 50)
4646
@@ -192,7 +192,7 @@ T["client()"]["manually detached buffer stays detached"] = function()
192192
end
193193
end
194194
195-
vim.wait(5000, function()
195+
vim.wait(500, function()
196196
return has_passed()
197197
end, 50)
198198
@@ -255,7 +255,7 @@ end
255255
-- end
256256
-- end
257257
--
258-
-- vim.wait(1000, function()
258+
-- vim.wait(500, function()
259259
-- return has_passed()
260260
-- end, 50)
261261
--
@@ -280,7 +280,7 @@ T["client()"]["auto_trigger off - will attach automatically"] = function()
280280
end
281281
end
282282
283-
vim.wait(5000, function()
283+
vim.wait(500, function()
284284
return has_passed()
285285
end, 50)
286286
@@ -306,7 +306,7 @@ T["client()"]["suggestion and panel off - will attach automatically"] = function
306306
end
307307
end
308308
309-
vim.wait(5000, function()
309+
vim.wait(500, function()
310310
return has_passed()
311311
end, 50)
312312
@@ -331,7 +331,7 @@ end
331331
-- end
332332
-- end
333333
--
334-
-- vim.wait(5000, function()
334+
-- vim.wait(500, function()
335335
-- return has_passed()
336336
-- end, 50)
337337
--

tests/test_command.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local child_helper = require("tests.child_helper")
2-
local child = child_helper.new_child_neovim("test_suggestion")
2+
local child = child_helper.new_child_neovim("test_command")
33
local u = require("tests.utils")
44
local reference_screenshot = MiniTest.expect.reference_screenshot
55

@@ -33,7 +33,7 @@ T["command()"]["panel toggle - close works"] = function()
3333
child.configure_copilot()
3434
child.cmd("Copilot panel toggle")
3535
child.cmd("Copilot panel toggle")
36-
reference_screenshot(child.get_screenshot())
36+
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 23, 24 }, ignore_attr = { 23, 24 } })
3737
end
3838

3939
T["command()"]["panel open - it works"] = function()

tests/test_suggestion.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ T["suggestion()"]["auto_trigger is false, will not show ghost test"] = function(
2929
child.o.lines, child.o.columns = 10, 15
3030
child.configure_copilot()
3131
child.type_keys("i123", "<Esc>", "o456", "<Esc>", "o7")
32-
vim.loop.sleep(50)
32+
child.wait_for_suggestion()
3333

3434
reference_screenshot(child.get_screenshot(), nil, { ignore_text = { 9, 10 }, ignore_attr = { 9, 10 } })
3535
end

0 commit comments

Comments
 (0)