-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands_spec.lua
More file actions
172 lines (147 loc) · 4.96 KB
/
commands_spec.lua
File metadata and controls
172 lines (147 loc) · 4.96 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
describe("peekstack.commands", function()
local commands = require("peekstack.commands")
local config = require("peekstack.config")
local peekstack = require("peekstack")
local persist = require("peekstack.persist")
local original_list_sessions = nil
local original_select = nil
local original_notify = nil
local original_strftime = nil
before_each(function()
config.setup({})
commands._reset()
original_list_sessions = persist.list_sessions
original_select = vim.ui.select
original_notify = vim.notify
original_strftime = vim.fn.strftime
end)
after_each(function()
config.setup({})
if original_list_sessions then
persist.list_sessions = original_list_sessions
end
if original_select then
vim.ui.select = original_select
end
if original_notify then
vim.notify = original_notify
end
if original_strftime then
vim.fn.strftime = original_strftime
end
commands._reset()
end)
it("returns session names without async list dependency", function()
local called_with_opts = false
persist.list_sessions = function(opts)
if opts ~= nil then
called_with_opts = true
end
return {
alpha = { items = {}, meta = { created_at = 1, updated_at = 1 } },
beta = { items = {}, meta = { created_at = 1, updated_at = 1 } },
}
end
commands.setup()
local names = vim.fn.getcompletion("PeekstackRestoreSession ", "cmdline")
table.sort(names)
assert.same({ "alpha", "beta" }, names)
assert.is_false(called_with_opts)
end)
it("handles missing session meta in list command", function()
local prompts = {}
persist.list_sessions = function(opts)
assert.is_truthy(opts)
assert.is_truthy(opts.on_done)
opts.on_done({
broken = {
items = { { uri = "file:///tmp/a.lua" } },
},
})
return {}
end
vim.ui.select = function(_items, opts, on_choice)
table.insert(prompts, opts.prompt)
if opts.prompt == "Select a session" then
on_choice("broken")
return
end
on_choice("Info only")
end
commands.setup()
vim.api.nvim_cmd({ cmd = "PeekstackListSessions" }, {})
assert.equals("Select a session", prompts[1])
assert.equals("broken: 1 items (updated: unknown)", prompts[2])
end)
it("notifies when list sessions is invoked while persist is disabled", function()
config.setup({ persist = { enabled = false } })
local messages = {}
vim.notify = function(msg)
table.insert(messages, msg)
end
commands.setup()
vim.api.nvim_cmd({ cmd = "PeekstackListSessions" }, {})
assert.is_true(vim.list_contains(messages, "peekstack.persist is disabled"))
end)
it("formats session updated_at with vim.fn.strftime", function()
local prompts = {}
local strftime_calls = {}
persist.list_sessions = function(opts)
assert.is_truthy(opts)
assert.is_truthy(opts.on_done)
opts.on_done({
alpha = {
items = {},
meta = { updated_at = 123 },
},
})
return {}
end
vim.fn.strftime = function(fmt, ts)
table.insert(strftime_calls, { fmt = fmt, ts = ts })
return "formatted-time"
end
vim.ui.select = function(_items, opts, on_choice)
table.insert(prompts, opts.prompt)
if opts.prompt == "Select a session" then
on_choice("alpha")
return
end
on_choice("Info only")
end
commands.setup()
vim.api.nvim_cmd({ cmd = "PeekstackListSessions" }, {})
assert.equals(1, #strftime_calls)
assert.equals("%Y-%m-%d %H:%M:%S", strftime_calls[1].fmt)
assert.equals(123, strftime_calls[1].ts)
assert.equals("alpha: 0 items (updated: formatted-time)", prompts[2])
end)
it("uses registered providers for quick peek completion", function()
peekstack.setup({})
local names = vim.fn.getcompletion("PeekstackQuickPeek ", "cmdline")
assert.is_true(vim.list_contains(names, "lsp.declaration"))
assert.is_true(vim.list_contains(names, "lsp.symbols_document"))
assert.is_true(vim.list_contains(names, "diagnostics.in_buffer"))
assert.is_false(vim.list_contains(names, "marks.buffer"))
assert.is_false(vim.list_contains(names, "marks.global"))
assert.is_false(vim.list_contains(names, "marks.all"))
end)
it("reflects provider registration changes in quick peek completion", function()
peekstack.setup({
providers = {
marks = {
enable = true,
},
},
})
local names = vim.fn.getcompletion("PeekstackQuickPeek ", "cmdline")
assert.is_true(vim.list_contains(names, "marks.buffer"))
assert.is_true(vim.list_contains(names, "marks.global"))
assert.is_true(vim.list_contains(names, "marks.all"))
peekstack.register_provider("custom.test", function(_ctx, cb)
cb({})
end)
names = vim.fn.getcompletion("PeekstackQuickPeek ", "cmdline")
assert.is_true(vim.list_contains(names, "custom.test"))
end)
end)