forked from sudo-tee/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore_spec.lua
More file actions
305 lines (275 loc) · 9.03 KB
/
core_spec.lua
File metadata and controls
305 lines (275 loc) · 9.03 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
local core = require('opencode.core')
local config_file = require('opencode.config_file')
local state = require('opencode.state')
local ui = require('opencode.ui.ui')
local session = require('opencode.session')
local Promise = require('opencode.promise')
local stub = require('luassert.stub')
-- Provide a mock api_client for tests that need it
local function mock_api_client()
state.api_client = {
create_session = function(_, params)
return Promise.new():resolve({ id = params and params.title or 'new-session' })
end,
create_message = function(_, sess_id, _params)
return Promise.new():resolve({ id = 'm1', sessionID = sess_id })
end,
abort_session = function(_, _id)
return Promise.new():resolve(true)
end,
get_current_project = function()
return Promise.new():resolve({ id = 'test-project-id' })
end,
get_config = function()
return Promise.new():resolve({ model = 'gpt-4' })
end,
}
end
describe('opencode.core', function()
local original_state
local original_system
local original_executable
local original_schedule
before_each(function()
original_state = vim.deepcopy(state)
original_system = vim.system
original_executable = vim.fn.executable
original_schedule = vim.schedule
vim.fn.executable = function(_)
return 1
end
vim.system = function(_cmd, _opts)
return {
wait = function()
return { stdout = 'opencode 0.6.3' }
end,
}
end
vim.schedule = function(fn)
fn()
end
stub(ui, 'create_windows').returns({
mock = 'windows',
input_buf = 1,
output_buf = 2,
input_win = 3,
output_win = 4,
})
stub(ui, 'clear_output')
stub(ui, 'render_output')
stub(ui, 'focus_input')
stub(ui, 'focus_output')
stub(ui, 'is_output_empty').returns(true)
stub(session, 'get_last_workspace_session').returns({ id = 'test-session' })
if session.get_by_id and type(session.get_by_id) == 'function' then
-- stub get_by_id to return a simple session object without filesystem access
stub(session, 'get_by_id').invokes(function(id)
if not id then
return nil
end
return { id = id, description = id, modified = os.time(), parentID = nil }
end)
-- stub get_by_name to return a simple session object without filesystem access
stub(session, 'get_by_name').invokes(function(name)
if not name then
return nil
end
return { id = name, description = name, modified = os.time(), parentID = nil }
end)
end
mock_api_client()
-- Mock server job to avoid trying to start real server
state.opencode_server = {
is_running = function()
return true
end,
shutdown = function() end,
url = 'http://127.0.0.1:4000',
}
-- Config is now loaded lazily, so no need to pre-seed promises
end)
after_each(function()
for k, v in pairs(original_state) do
state[k] = v
end
vim.system = original_system
vim.fn.executable = original_executable
vim.schedule = original_schedule
for _, fn in ipairs({
'create_windows',
'clear_output',
'render_output',
'focus_input',
'focus_output',
'is_output_empty',
}) do
if ui[fn] and ui[fn].revert then
ui[fn]:revert()
end
end
if session.get_last_workspace_session.revert then
session.get_last_workspace_session:revert()
end
if session.get_by_id and session.get_by_id.revert then
session.get_by_id:revert()
end
if session.get_by_name and session.get_by_name.revert then
session.get_by_name:revert()
end
end)
describe('open', function()
it("creates windows if they don't exist", function()
state.windows = nil
core.open({ new_session = false, focus = 'input' })
assert.truthy(state.windows)
assert.same({
mock = 'windows',
input_buf = 1,
output_buf = 2,
input_win = 3,
output_win = 4,
}, state.windows)
end)
it('handles new session properly', function()
state.windows = nil
state.active_session = { id = 'old-session' }
core.open({ new_session = true, focus = 'input' })
assert.truthy(state.active_session)
end)
it('focuses the appropriate window', function()
state.windows = nil
ui.focus_input:revert()
ui.focus_output:revert()
local input_focused, output_focused = false, false
stub(ui, 'focus_input').invokes(function()
input_focused = true
end)
stub(ui, 'focus_output').invokes(function()
output_focused = true
end)
core.open({ new_session = false, focus = 'input' })
assert.is_true(input_focused)
assert.is_false(output_focused)
input_focused, output_focused = false, false
core.open({ new_session = false, focus = 'output' })
assert.is_false(input_focused)
assert.is_true(output_focused)
end)
end)
describe('select_session', function()
it('filters sessions by description and parentID', function()
local mock_sessions = {
{ id = 'session1', description = 'First session', modified = 1, parentID = nil },
{ id = 'session2', description = '', modified = 2, parentID = nil },
{ id = 'session3', description = 'Third session', modified = 3, parentID = nil },
}
stub(session, 'get_all_workspace_sessions').returns(mock_sessions)
local passed
stub(ui, 'select_session').invokes(function(sessions, cb)
passed = sessions
cb(sessions[2]) -- expect session3 after filtering
end)
ui.render_output:revert()
stub(ui, 'render_output')
state.windows = { input_buf = 1, output_buf = 2 }
core.select_session(nil)
assert.equal(2, #passed)
assert.equal('session3', passed[2].id)
assert.truthy(state.active_session)
assert.equal('session3', state.active_session.id)
end)
end)
describe('send_message', function()
it('sends a message via api_client', function()
state.windows = { mock = 'windows' }
state.active_session = { id = 'sess1' }
local create_called = false
local orig = state.api_client.create_message
state.api_client.create_message = function(_, sid, params)
create_called = true
assert.equal('sess1', sid)
assert.truthy(params.parts)
return Promise.new():resolve({ id = 'm1' })
end
core.send_message('hello world')
vim.wait(50, function()
return create_called
end)
assert.True(create_called)
state.api_client.create_message = orig
end)
it('creates new session when none active', function()
state.windows = { mock = 'windows' }
state.active_session = nil
local created_session
local orig_session = state.api_client.create_session
state.api_client.create_session = function(_, _params)
created_session = true
return Promise.new():resolve({ id = 'sess-new' })
end
-- override create_new_session to use api_client path synchronously
local new = core.create_new_session('title')
assert.True(created_session)
assert.truthy(new)
assert.equal('sess-new', new.id)
state.api_client.create_session = orig_session
end)
end)
describe('opencode_ok (version checks)', function()
local original_system
local original_executable
local saved_cli
local function mock_vim_system(result)
return function(_cmd, _opts)
return {
wait = function()
return result
end,
}
end
end
before_each(function()
original_system = vim.system
original_executable = vim.fn.executable
saved_cli = state.opencode_cli_version
end)
after_each(function()
vim.system = original_system
vim.fn.executable = original_executable
state.opencode_cli_version = saved_cli
end)
it('returns false when opencode executable is missing', function()
vim.fn.executable = function(_)
return 0
end
assert.is_false(core.opencode_ok())
end)
it('returns false when version is below required', function()
vim.fn.executable = function(_)
return 1
end
vim.system = mock_vim_system({ stdout = 'opencode 0.4.1' })
state.opencode_cli_version = nil
state.required_version = '0.4.2'
assert.is_false(core.opencode_ok())
end)
it('returns true when version equals required', function()
vim.fn.executable = function(_)
return 1
end
vim.system = mock_vim_system({ stdout = 'opencode 0.4.2' })
state.opencode_cli_version = nil
state.required_version = '0.4.2'
assert.is_true(core.opencode_ok())
end)
it('returns true when version is above required', function()
vim.fn.executable = function(_)
return 1
end
vim.system = mock_vim_system({ stdout = 'opencode 0.5.0' })
state.opencode_cli_version = nil
state.required_version = '0.4.2'
assert.is_true(core.opencode_ok())
end)
end)
end)