Skip to content

Commit d06e18a

Browse files
committed
add tests for context commands
1 parent 4435552 commit d06e18a

2 files changed

Lines changed: 347 additions & 2 deletions

File tree

lua/eca/commands.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function M.setup()
108108
})
109109

110110
vim.api.nvim_create_user_command("EcaListContexts", function()
111-
Logger.notify("EcaListContexts is deprecated. Use EcaChatListContexts instead.")
111+
Logger.notify("EcaListContexts is deprecated. Use EcaChatListContexts instead.", vim.log.levels.WARN)
112112

113113
require("eca.api").list_contexts()
114114
end, {
@@ -122,7 +122,7 @@ function M.setup()
122122
})
123123

124124
vim.api.nvim_create_user_command("EcaClearContexts", function()
125-
Logger.notify("EcaClearContexts is deprecated. Use EcaChatClearContexts instead.")
125+
Logger.notify("EcaClearContexts is deprecated. Use EcaChatClearContexts instead.", vim.log.levels.WARN)
126126

127127
require("eca.api").clear_contexts()
128128
end, {

tests/test_context_commands.lua

Lines changed: 345 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
local MiniTest = require("mini.test")
2+
local eq = MiniTest.expect.equality
3+
local child = MiniTest.new_child_neovim()
4+
5+
local function flush(ms)
6+
vim.uv.sleep(ms or 100)
7+
child.api.nvim_eval("1")
8+
end
9+
10+
local function setup_test_environment()
11+
child.lua([[
12+
local Eca = require('eca')
13+
14+
-- Setup plugin with no auto server or keymaps so tests are
15+
-- deterministic and don't spawn external processes.
16+
Eca.setup({
17+
behavior = {
18+
auto_start_server = false,
19+
auto_set_keymaps = false,
20+
},
21+
})
22+
23+
-- Ensure we have a sidebar/mediator for the current tab
24+
local tab = vim.api.nvim_get_current_tabpage()
25+
Eca._init(tab)
26+
Eca.open_sidebar({})
27+
28+
-- Fake server so eca.api thinks it is running but we never
29+
-- actually start the external binary.
30+
if Eca.server then
31+
Eca.server.is_running = function()
32+
return true
33+
end
34+
else
35+
Eca.server = {
36+
is_running = function()
37+
return true
38+
end,
39+
}
40+
end
41+
42+
-- Clear any existing contexts before each test
43+
if Eca.mediator then
44+
Eca.mediator:clear_contexts()
45+
end
46+
47+
-- Capture Logger.notify calls (used by deprecated commands and
48+
-- some API helpers) so we can assert on deprecation messages.
49+
local Logger = require('eca.logger')
50+
_G.Logger = Logger
51+
_G.original_logger_notify = Logger.notify
52+
_G.captured_notifications = {}
53+
54+
Logger.notify = function(msg, level, opts)
55+
level = level or vim.log.levels.INFO
56+
opts = opts or {}
57+
58+
table.insert(_G.captured_notifications, {
59+
message = msg,
60+
level = level,
61+
opts = opts,
62+
})
63+
64+
if _G.original_logger_notify then
65+
_G.original_logger_notify(msg, level, opts)
66+
end
67+
end
68+
]])
69+
end
70+
71+
local T = MiniTest.new_set({
72+
hooks = {
73+
pre_case = function()
74+
child.restart({ "-u", "scripts/minimal_init.lua" })
75+
setup_test_environment()
76+
end,
77+
post_once = child.stop,
78+
},
79+
})
80+
81+
local function contexts_count()
82+
return child.lua_get("#require('eca').mediator:contexts()")
83+
end
84+
85+
local function get_contexts()
86+
return child.lua_get("require('eca').mediator:contexts()")
87+
end
88+
89+
-- EcaChatAddFile -----------------------------------------------------------
90+
91+
T["EcaChatAddFile"] = MiniTest.new_set()
92+
93+
T["EcaChatAddFile"]["command is registered"] = function()
94+
local commands = child.lua_get("vim.api.nvim_get_commands({})")
95+
eq(type(commands.EcaChatAddFile), "table")
96+
eq(commands.EcaChatAddFile.name, "EcaChatAddFile")
97+
end
98+
99+
T["EcaChatAddFile"]["adds current file as context when no args"] = function()
100+
child.cmd("edit README.md")
101+
local abs = child.lua_get("vim.fn.fnamemodify('README.md', ':p')")
102+
103+
eq(contexts_count(), 0)
104+
105+
child.cmd("EcaChatAddFile")
106+
flush()
107+
108+
local contexts = get_contexts()
109+
eq(#contexts, 1)
110+
eq(contexts[1].type, "file")
111+
eq(contexts[1].data.path, abs)
112+
end
113+
114+
T["EcaChatAddFile"]["adds provided path as context when args are given"] = function()
115+
local filename = "README.md"
116+
local expected_abs = child.lua_get("vim.fn.fnamemodify(..., ':p')", { filename })
117+
118+
eq(contexts_count(), 0)
119+
120+
child.cmd("EcaChatAddFile " .. filename)
121+
flush()
122+
123+
local contexts = get_contexts()
124+
eq(#contexts, 1)
125+
eq(contexts[1].type, "file")
126+
eq(contexts[1].data.path, expected_abs)
127+
end
128+
129+
-- Deprecated EcaAddFile ----------------------------------------------------
130+
131+
T["EcaAddFile"] = MiniTest.new_set()
132+
133+
T["EcaAddFile"]["command is registered"] = function()
134+
local commands = child.lua_get("vim.api.nvim_get_commands({})")
135+
eq(type(commands.EcaAddFile), "table")
136+
eq(commands.EcaAddFile.name, "EcaAddFile")
137+
end
138+
139+
T["EcaAddFile"]["shows deprecation notice when called"] = function()
140+
child.cmd("EcaAddFile")
141+
flush()
142+
143+
local notifications = child.lua_get("_G.captured_notifications")
144+
eq(#notifications > 0, true)
145+
eq(notifications[1].message, "EcaAddFile is deprecated. Use EcaChatAddFile instead.")
146+
eq(notifications[1].level, child.lua_get("vim.log.levels.WARN"))
147+
end
148+
149+
-- EcaChatRemoveFile --------------------------------------------------------
150+
151+
T["EcaChatRemoveFile"] = MiniTest.new_set()
152+
153+
T["EcaChatRemoveFile"]["command is registered"] = function()
154+
local commands = child.lua_get("vim.api.nvim_get_commands({})")
155+
eq(type(commands.EcaChatRemoveFile), "table")
156+
eq(commands.EcaChatRemoveFile.name, "EcaChatRemoveFile")
157+
end
158+
159+
T["EcaChatRemoveFile"]["removes current file context when no args"] = function()
160+
child.cmd("edit README.md")
161+
162+
child.cmd("EcaChatAddFile")
163+
flush()
164+
eq(contexts_count(), 1)
165+
166+
child.cmd("EcaChatRemoveFile")
167+
flush()
168+
169+
eq(contexts_count(), 0)
170+
end
171+
172+
T["EcaChatRemoveFile"]["removes context for provided path when args are given"] = function()
173+
local filename = "README.md"
174+
175+
child.cmd("edit README.md")
176+
child.cmd("EcaChatAddFile")
177+
flush()
178+
eq(contexts_count(), 1)
179+
180+
child.cmd("EcaChatRemoveFile " .. filename)
181+
flush()
182+
183+
eq(contexts_count(), 0)
184+
end
185+
186+
-- Deprecated EcaRemoveContext ---------------------------------------------
187+
188+
T["EcaRemoveContext"] = MiniTest.new_set()
189+
190+
T["EcaRemoveContext"]["command is registered"] = function()
191+
local commands = child.lua_get("vim.api.nvim_get_commands({})")
192+
eq(type(commands.EcaRemoveContext), "table")
193+
eq(commands.EcaRemoveContext.name, "EcaRemoveContext")
194+
end
195+
196+
T["EcaRemoveContext"]["shows deprecation notice when called"] = function()
197+
child.cmd("EcaRemoveContext")
198+
flush()
199+
200+
local notifications = child.lua_get("_G.captured_notifications")
201+
eq(#notifications > 0, true)
202+
eq(notifications[1].message, "EcaRemoveContext is deprecated. Use EcaChatRemoveFile instead.")
203+
eq(notifications[1].level, child.lua_get("vim.log.levels.WARN"))
204+
end
205+
206+
T["EcaChatAddSelection"] = MiniTest.new_set()
207+
208+
T["EcaChatAddSelection"]["command is registered"] = function()
209+
local commands = child.lua_get("vim.api.nvim_get_commands({})")
210+
eq(type(commands.EcaChatAddSelection), "table")
211+
eq(commands.EcaChatAddSelection.name, "EcaChatAddSelection")
212+
end
213+
214+
T["EcaChatAddSelection"]["adds a ranged file context based on visual selection"] = function()
215+
child.cmd("edit README.md")
216+
local abs = child.lua_get("vim.fn.fnamemodify('README.md', ':p')")
217+
218+
-- Manually set visual selection marks for lines 1-2 to avoid headless
219+
-- visual-mode quirks
220+
child.lua([[
221+
local bufnr = vim.api.nvim_get_current_buf()
222+
vim.fn.setpos("'<", {bufnr, 1, 1, 0})
223+
vim.fn.setpos("'>", {bufnr, 2, 1, 0})
224+
]])
225+
226+
eq(contexts_count(), 0)
227+
228+
child.cmd("EcaChatAddSelection")
229+
flush(200)
230+
231+
local contexts = get_contexts()
232+
eq(#contexts, 1)
233+
eq(contexts[1].type, "file")
234+
eq(contexts[1].data.path, abs)
235+
eq(contexts[1].data.lines_range.line_start, 1)
236+
eq(contexts[1].data.lines_range.line_end, 2)
237+
end
238+
239+
-- Deprecated EcaAddSelection -----------------------------------------------
240+
241+
T["EcaAddSelection"] = MiniTest.new_set()
242+
243+
T["EcaAddSelection"]["command is registered"] = function()
244+
local commands = child.lua_get("vim.api.nvim_get_commands({})")
245+
eq(type(commands.EcaAddSelection), "table")
246+
eq(commands.EcaAddSelection.name, "EcaAddSelection")
247+
end
248+
249+
T["EcaAddSelection"]["shows deprecation notice when called"] = function()
250+
child.cmd("EcaAddSelection")
251+
flush(200)
252+
253+
local notifications = child.lua_get("_G.captured_notifications")
254+
eq(#notifications > 0, true)
255+
eq(notifications[1].message, "EcaAddSelection is deprecated. Use EcaChatAddSelection instead.")
256+
eq(notifications[1].level, child.lua_get("vim.log.levels.WARN"))
257+
end
258+
259+
T["EcaChatListContexts"] = MiniTest.new_set()
260+
261+
T["EcaChatListContexts"]["command is registered"] = function()
262+
local commands = child.lua_get("vim.api.nvim_get_commands({})")
263+
eq(type(commands.EcaChatListContexts), "table")
264+
eq(commands.EcaChatListContexts.name, "EcaChatListContexts")
265+
end
266+
267+
T["EcaChatListContexts"]["runs without modifying contexts"] = function()
268+
child.cmd("edit README.md")
269+
child.cmd("EcaChatAddFile")
270+
flush()
271+
272+
local before = contexts_count()
273+
child.cmd("EcaChatListContexts")
274+
flush()
275+
local after = contexts_count()
276+
277+
eq(after, before)
278+
end
279+
280+
-- Deprecated EcaListContexts -----------------------------------------------
281+
282+
T["EcaListContexts"] = MiniTest.new_set()
283+
284+
T["EcaListContexts"]["command is registered"] = function()
285+
local commands = child.lua_get("vim.api.nvim_get_commands({})")
286+
eq(type(commands.EcaListContexts), "table")
287+
eq(commands.EcaListContexts.name, "EcaListContexts")
288+
end
289+
290+
T["EcaListContexts"]["shows deprecation notice when called"] = function()
291+
child.cmd("EcaListContexts")
292+
flush()
293+
294+
local notifications = child.lua_get("_G.captured_notifications")
295+
eq(#notifications > 0, true)
296+
eq(notifications[1].message, "EcaListContexts is deprecated. Use EcaChatListContexts instead.")
297+
eq(notifications[1].level, child.lua_get("vim.log.levels.WARN"))
298+
-- No explicit level is passed in the command for this deprecation,
299+
-- so we only assert on the message.
300+
end
301+
302+
T["EcaChatClearContexts"] = MiniTest.new_set()
303+
304+
T["EcaChatClearContexts"]["command is registered"] = function()
305+
local commands = child.lua_get("vim.api.nvim_get_commands({})")
306+
eq(type(commands.EcaChatClearContexts), "table")
307+
eq(commands.EcaChatClearContexts.name, "EcaChatClearContexts")
308+
end
309+
310+
T["EcaChatClearContexts"]["clears all contexts"] = function()
311+
child.cmd("edit README.md")
312+
child.cmd("EcaChatAddFile")
313+
child.cmd("EcaChatAddFile")
314+
flush()
315+
eq(contexts_count() > 0, true)
316+
317+
child.cmd("EcaChatClearContexts")
318+
flush()
319+
320+
eq(contexts_count(), 0)
321+
end
322+
323+
-- Deprecated EcaClearContexts ----------------------------------------------
324+
325+
T["EcaClearContexts"] = MiniTest.new_set()
326+
327+
T["EcaClearContexts"]["command is registered"] = function()
328+
local commands = child.lua_get("vim.api.nvim_get_commands({})")
329+
eq(type(commands.EcaClearContexts), "table")
330+
eq(commands.EcaClearContexts.name, "EcaClearContexts")
331+
end
332+
333+
T["EcaClearContexts"]["shows deprecation notice when called"] = function()
334+
child.cmd("EcaClearContexts")
335+
flush()
336+
337+
local notifications = child.lua_get("_G.captured_notifications")
338+
eq(#notifications > 0, true)
339+
eq(notifications[1].message, "EcaClearContexts is deprecated. Use EcaChatClearContexts instead.")
340+
eq(notifications[1].level, child.lua_get("vim.log.levels.WARN"))
341+
-- No explicit level is passed in the command for this deprecation,
342+
-- so we only assert on the message.
343+
end
344+
345+
return T

0 commit comments

Comments
 (0)