forked from sudo-tee/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursor_tracking_spec.lua
More file actions
386 lines (310 loc) · 12 KB
/
cursor_tracking_spec.lua
File metadata and controls
386 lines (310 loc) · 12 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
local state = require('opencode.state')
local config = require('opencode.config')
local ui = require('opencode.ui.ui')
describe('cursor persistence (state)', function()
before_each(function()
state.set_cursor_position('input', nil)
state.set_cursor_position('output', nil)
end)
describe('set/get round-trip', function()
it('stores and retrieves input cursor', function()
state.set_cursor_position('input', { 5, 3 })
assert.same({ 5, 3 }, state.get_cursor_position('input'))
end)
it('stores and retrieves output cursor', function()
state.set_cursor_position('output', { 10, 0 })
assert.same({ 10, 0 }, state.get_cursor_position('output'))
end)
it('input and output are independent', function()
state.set_cursor_position('input', { 1, 0 })
state.set_cursor_position('output', { 99, 5 })
assert.same({ 1, 0 }, state.get_cursor_position('input'))
assert.same({ 99, 5 }, state.get_cursor_position('output'))
end)
it('returns nil for unknown win_type', function()
assert.is_nil(state.get_cursor_position('footer'))
end)
end)
describe('normalize_cursor edge cases', function()
it('clamps negative line to 1', function()
state.set_cursor_position('input', { -5, 3 })
local pos = state.get_cursor_position('input')
assert.equals(1, pos[1])
end)
it('clamps negative col to 0', function()
state.set_cursor_position('input', { 1, -1 })
local pos = state.get_cursor_position('input')
assert.equals(0, pos[2])
end)
it('floors fractional values', function()
state.set_cursor_position('input', { 3.7, 2.9 })
local pos = state.get_cursor_position('input')
assert.equals(3, pos[1])
assert.equals(2, pos[2])
end)
it('rejects non-table input', function()
state.set_cursor_position('input', 'bad')
assert.is_nil(state.get_cursor_position('input'))
end)
it('rejects table with fewer than 2 elements', function()
state.set_cursor_position('input', { 1 })
assert.is_nil(state.get_cursor_position('input'))
end)
it('rejects non-numeric elements', function()
state.set_cursor_position('input', { 'a', 'b' })
assert.is_nil(state.get_cursor_position('input'))
end)
it('clears position when set to nil', function()
state.set_cursor_position('input', { 5, 3 })
state.set_cursor_position('input', nil)
assert.is_nil(state.get_cursor_position('input'))
end)
end)
describe('save_cursor_position', function()
it('returns nil for invalid window', function()
assert.is_nil(state.save_cursor_position('input', nil))
assert.is_nil(state.save_cursor_position('input', 999999))
end)
it('captures and persists from a real window', function()
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { 'line1', 'line2', 'line3' })
local win = vim.api.nvim_open_win(buf, true, {
relative = 'editor', width = 40, height = 10, row = 0, col = 0,
})
vim.api.nvim_win_set_cursor(win, { 2, 3 })
local saved = state.save_cursor_position('output', win)
assert.same({ 2, 3 }, saved)
assert.same({ 2, 3 }, state.get_cursor_position('output'))
pcall(vim.api.nvim_win_close, win, true)
pcall(vim.api.nvim_buf_delete, buf, { force = true })
end)
end)
end)
describe('output_window.is_at_bottom', function()
local output_window = require('opencode.ui.output_window')
local buf, win
before_each(function()
config.setup({})
buf = vim.api.nvim_create_buf(false, true)
local lines = {}
for i = 1, 50 do
lines[i] = 'line ' .. i
end
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
win = vim.api.nvim_open_win(buf, true, {
relative = 'editor', width = 80, height = 10, row = 0, col = 0,
})
state.windows = { output_win = win, output_buf = buf }
end)
after_each(function()
pcall(vim.api.nvim_win_close, win, true)
pcall(vim.api.nvim_buf_delete, buf, { force = true })
state.windows = nil
end)
it('returns true when cursor is on last line', function()
vim.api.nvim_win_set_cursor(win, { 50, 0 })
assert.is_true(output_window.is_at_bottom(win))
end)
it('returns false when cursor is on second-to-last line', function()
vim.api.nvim_win_set_cursor(win, { 49, 0 })
assert.is_false(output_window.is_at_bottom(win))
end)
it('returns false when cursor is far from bottom', function()
vim.api.nvim_win_set_cursor(win, { 1, 0 })
assert.is_false(output_window.is_at_bottom(win))
end)
it('returns false when cursor is a few lines above bottom', function()
vim.api.nvim_win_set_cursor(win, { 45, 0 })
assert.is_false(output_window.is_at_bottom(win))
end)
it('returns true when always_scroll_to_bottom is enabled', function()
config.values.ui.output.always_scroll_to_bottom = true
vim.api.nvim_win_set_cursor(win, { 1, 0 })
assert.is_true(output_window.is_at_bottom(win))
config.values.ui.output.always_scroll_to_bottom = false
end)
it('returns true for invalid window', function()
assert.is_true(output_window.is_at_bottom(999999))
end)
it('returns true when no windows in state', function()
state.windows = nil
assert.is_true(output_window.is_at_bottom(win))
end)
it('returns true for empty buffer', function()
local empty_buf = vim.api.nvim_create_buf(false, true)
local empty_win = vim.api.nvim_open_win(empty_buf, true, {
relative = 'editor', width = 40, height = 5, row = 0, col = 0,
})
state.windows = { output_win = empty_win, output_buf = empty_buf }
assert.is_true(output_window.is_at_bottom(empty_win))
pcall(vim.api.nvim_win_close, empty_win, true)
pcall(vim.api.nvim_buf_delete, empty_buf, { force = true })
end)
it('cursor-based: scrolling viewport without moving cursor does NOT change result', function()
vim.api.nvim_win_set_cursor(win, { 50, 0 })
assert.is_true(output_window.is_at_bottom(win))
-- Scroll viewport up via winrestview, cursor stays at line 50
pcall(vim.api.nvim_win_call, win, function()
vim.fn.winrestview({ topline = 1 })
end)
-- Cursor is still at 50, so is_at_bottom should still be true
-- This is the key behavioral difference from viewport-based check
assert.is_true(output_window.is_at_bottom(win))
end)
end)
describe('renderer.scroll_to_bottom', function()
local renderer = require('opencode.ui.renderer')
local output_window = require('opencode.ui.output_window')
local buf, win
before_each(function()
config.setup({})
buf = vim.api.nvim_create_buf(false, true)
local lines = {}
for i = 1, 50 do
lines[i] = 'line ' .. i
end
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
win = vim.api.nvim_open_win(buf, true, {
relative = 'editor', width = 80, height = 10, row = 0, col = 0,
})
state.windows = { output_win = win, output_buf = buf }
renderer._prev_line_count = 50
end)
after_each(function()
pcall(vim.api.nvim_win_close, win, true)
pcall(vim.api.nvim_buf_delete, buf, { force = true })
state.windows = nil
renderer._prev_line_count = 0
output_window.viewport_at_bottom = nil
end)
it('does not force-scroll when user cursor is above previous bottom', function()
vim.api.nvim_win_set_cursor(win, { 10, 0 })
output_window.viewport_at_bottom = true
vim.api.nvim_buf_set_lines(buf, -1, -1, false, { 'line 51' })
renderer.scroll_to_bottom()
local cursor = vim.api.nvim_win_get_cursor(win)
assert.equals(10, cursor[1])
end)
it('still scrolls when always_scroll_to_bottom is enabled', function()
config.values.ui.output.always_scroll_to_bottom = true
vim.api.nvim_win_set_cursor(win, { 10, 0 })
vim.api.nvim_buf_set_lines(buf, -1, -1, false, { 'line 51' })
renderer.scroll_to_bottom()
local cursor = vim.api.nvim_win_get_cursor(win)
assert.equals(51, cursor[1])
config.values.ui.output.always_scroll_to_bottom = false
end)
end)
describe('ui.focus_input', function()
local input_buf, output_buf, input_win, output_win
before_each(function()
input_buf = vim.api.nvim_create_buf(false, true)
output_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(input_buf, 0, -1, false, { 'abcde' })
vim.api.nvim_buf_set_lines(output_buf, 0, -1, false, { 'output' })
output_win = vim.api.nvim_open_win(output_buf, true, {
relative = 'editor', width = 40, height = 5, row = 0, col = 0,
})
input_win = vim.api.nvim_open_win(input_buf, true, {
relative = 'editor', width = 40, height = 5, row = 6, col = 0,
})
state.windows = {
input_win = input_win,
output_win = output_win,
input_buf = input_buf,
output_buf = output_buf,
}
state.last_input_window_position = { 1, 4 }
end)
after_each(function()
pcall(vim.api.nvim_win_close, input_win, true)
pcall(vim.api.nvim_win_close, output_win, true)
pcall(vim.api.nvim_buf_delete, input_buf, { force = true })
pcall(vim.api.nvim_buf_delete, output_buf, { force = true })
state.windows = nil
state.last_input_window_position = nil
end)
it('does not restore cursor when already focused in input window', function()
vim.api.nvim_set_current_win(input_win)
vim.api.nvim_win_set_cursor(input_win, { 1, 2 })
ui.focus_input({ restore_position = true, start_insert = false })
assert.same({ 1, 2 }, vim.api.nvim_win_get_cursor(input_win))
end)
end)
describe('renderer._add_message_to_buffer scrolling', function()
local renderer = require('opencode.ui.renderer')
local formatter = require('opencode.ui.formatter')
local stub = require('luassert.stub')
local buf, win
before_each(function()
config.setup({})
buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { 'existing line' })
win = vim.api.nvim_open_win(buf, true, {
relative = 'editor', width = 80, height = 10, row = 0, col = 0,
})
state.windows = { output_win = win, output_buf = buf }
state.active_session = { id = 'test-session' }
state.messages = {}
renderer._prev_line_count = 1
renderer._render_state:reset()
end)
after_each(function()
pcall(vim.api.nvim_win_close, win, true)
pcall(vim.api.nvim_buf_delete, buf, { force = true })
state.windows = nil
state.active_session = nil
state.messages = nil
renderer._prev_line_count = 0
renderer._render_state:reset()
end)
it('scrolls to bottom when user message is added', function()
vim.api.nvim_win_set_cursor(win, { 1, 0 })
local user_message = {
info = {
id = 'msg-1',
sessionID = 'test-session',
role = 'user',
},
parts = {},
}
local scroll_called_with_force = false
stub(renderer, 'scroll_to_bottom').invokes(function(force)
scroll_called_with_force = force == true
end)
renderer._add_message_to_buffer(user_message)
assert.is_true(scroll_called_with_force)
assert.stub(renderer.scroll_to_bottom).was_called_with(true)
renderer.scroll_to_bottom:revert()
end)
it('does not scroll when assistant message is added', function()
vim.api.nvim_win_set_cursor(win, { 1, 0 })
local assistant_message = {
info = {
id = 'msg-2',
sessionID = 'test-session',
role = 'assistant',
},
parts = {},
}
stub(renderer, 'scroll_to_bottom')
renderer._add_message_to_buffer(assistant_message)
assert.stub(renderer.scroll_to_bottom).was_not_called()
renderer.scroll_to_bottom:revert()
end)
it('does not scroll when system message is added', function()
vim.api.nvim_win_set_cursor(win, { 1, 0 })
local system_message = {
info = {
id = 'msg-3',
sessionID = 'test-session',
role = 'system',
},
parts = {},
}
stub(renderer, 'scroll_to_bottom')
renderer._add_message_to_buffer(system_message)
assert.stub(renderer.scroll_to_bottom).was_not_called()
renderer.scroll_to_bottom:revert()
end)
end)