@@ -16,8 +16,25 @@ describe("ResponseHandler", function()
16
16
nvim_buf_set_lines = stub .new (),
17
17
nvim_buf_add_highlight = stub .new (),
18
18
nvim_win_get_cursor = stub .new ().returns ({ 1 , 0 }),
19
+ nvim_set_hl = stub .new (),
20
+ nvim_buf_clear_namespace = stub .new (),
21
+ nvim_buf_line_count = stub .new ().returns (10 ),
22
+ nvim_win_is_valid = stub .new ().returns (true ),
19
23
},
20
24
split = stub .new ().returns ({ " test" }),
25
+ list_slice = stub .new ().returns ({ " test" }),
26
+ tbl_map = stub .new ().returns ({ " test" }),
27
+ loop = {
28
+ new_timer = stub .new ().returns ({
29
+ start = stub .new (),
30
+ stop = stub .new (),
31
+ close = stub .new (),
32
+ }),
33
+ hrtime = stub .new ().returns (1000000000 ), -- 1 second in nanoseconds
34
+ },
35
+ schedule_wrap = function (fn )
36
+ return fn
37
+ end ,
21
38
-- Remove vim.cmd to avoid potential issues with Vim options
22
39
}
23
40
@@ -31,14 +48,21 @@ describe("ResponseHandler", function()
31
48
get = stub .new ().returns ({ ns_id = 1 , ex_id = 1 }),
32
49
}
33
50
51
+ -- Mock logger module
52
+ local mock_logger = {
53
+ debug = stub .new (),
54
+ }
55
+
34
56
-- Use package.loaded instead of _G.vim
35
57
package.loaded .vim = mock_vim
36
58
package.loaded [" parrot.utils" ] = mock_utils
59
+ package.loaded [" parrot.logger" ] = mock_logger
37
60
end )
38
61
39
62
after_each (function ()
40
63
package.loaded .vim = nil
41
64
package.loaded [" parrot.utils" ] = nil
65
+ package.loaded [" parrot.logger" ] = nil
42
66
end )
43
67
44
68
it (" should create a new ResponseHandler with default values" , function ()
@@ -80,7 +104,6 @@ describe("ResponseHandler", function()
80
104
assert .are .same (" " , handler .response )
81
105
end )
82
106
83
-
84
107
it (" should not move the cursor when cursor is false" , function ()
85
108
local handler = ResponseHandler :new (mock_queries )
86
109
handler .response = " line1\n line2"
@@ -93,4 +116,159 @@ describe("ResponseHandler", function()
93
116
local handler_func = handler :create_handler ()
94
117
assert .is_function (handler_func )
95
118
end )
119
+
120
+ it (" should schedule updates with timer" , function ()
121
+ local handler = ResponseHandler :new (mock_queries )
122
+ local mock_timer = {
123
+ start = stub .new (),
124
+ stop = stub .new (),
125
+ close = stub .new (),
126
+ }
127
+
128
+ -- Mock vim.loop.new_timer
129
+ local original_new_timer = vim .loop .new_timer
130
+ vim .loop .new_timer = stub .new ().returns (mock_timer )
131
+
132
+ handler :schedule_update (1 )
133
+
134
+ assert .stub (vim .loop .new_timer ).was_called ()
135
+ assert .stub (mock_timer .start ).was_called ()
136
+
137
+ -- Restore original function
138
+ vim .loop .new_timer = original_new_timer
139
+ end )
140
+
141
+ it (" should flush updates to buffer" , function ()
142
+ local handler = ResponseHandler :new (mock_queries )
143
+ handler .response = " test response"
144
+ handler .pending_chunks = true
145
+ handler .first_line = 1
146
+ handler .finished_lines = 0
147
+
148
+ -- Setup fresh mocks for this test
149
+ mock_queries .get .returns ({ ns_id = 1 , ex_id = 1 , response = " " })
150
+ mock_vim .split .returns ({ " test response" })
151
+ mock_vim .api .nvim_buf_get_extmark_by_id .returns ({ 1 , 0 })
152
+ mock_vim .api .nvim_buf_is_valid .returns (true )
153
+
154
+ handler :flush_updates (1 )
155
+
156
+ -- Test behavior rather than implementation
157
+ assert .are .same (false , handler .pending_chunks )
158
+ assert .are .same (" " , handler .chunk_buffer )
159
+ end )
160
+
161
+ it (" should update buffer with response lines" , function ()
162
+ local handler = ResponseHandler :new (mock_queries )
163
+ handler .response = " line1\n line2\n line3"
164
+ handler .first_line = 1
165
+ handler .finished_lines = 0
166
+
167
+ -- Setup mocks for this test
168
+ mock_vim .split .returns ({ " line1" , " line2" , " line3" })
169
+ mock_vim .tbl_map .returns ({ " line1" , " line2" , " line3" })
170
+ mock_vim .list_slice .returns ({ " line1" , " line2" , " line3" })
171
+
172
+ -- Call the method - we can't easily test the vim API calls due to mocking complexity
173
+ -- but we can test that the method doesn't crash
174
+ handler :update_buffer ()
175
+
176
+ -- Test that state is maintained
177
+ assert .are .same (" line1\n line2\n line3" , handler .response )
178
+ assert .are .same (1 , handler .first_line )
179
+ end )
180
+
181
+ it (" should update highlighting for new lines" , function ()
182
+ local handler = ResponseHandler :new (mock_queries )
183
+ handler .response = " line1\n line2\n line3"
184
+ handler .first_line = 1
185
+ handler .finished_lines = 0
186
+
187
+ -- Setup mocks for this test
188
+ mock_vim .split .returns ({ " line1" , " line2" , " line3" })
189
+
190
+ local qt = { ns_id = 1 }
191
+ handler :update_highlighting (qt )
192
+
193
+ -- Test that finished_lines is updated correctly
194
+ assert .are .same (2 , handler .finished_lines )
195
+ end )
196
+
197
+ it (" should update query object with line information" , function ()
198
+ local handler = ResponseHandler :new (mock_queries )
199
+ handler .response = " line1\n line2\n line3"
200
+ handler .first_line = 1
201
+
202
+ -- Mock vim.split to return the lines
203
+ mock_vim .split .returns ({ " line1" , " line2" , " line3" })
204
+
205
+ local qt = {}
206
+ handler :update_query_object (qt )
207
+
208
+ assert .are .same (1 , qt .first_line )
209
+ assert .are .same (3 , qt .last_line )
210
+ end )
211
+
212
+ it (" should move cursor when cursor is true" , function ()
213
+ local handler = ResponseHandler :new (mock_queries , nil , 1 , 1 , true , " " , true )
214
+ handler .response = " line1\n line2"
215
+ handler .first_line = 1
216
+
217
+ -- Setup mocks for this test
218
+ mock_vim .split .returns ({ " line1" , " line2" })
219
+
220
+ handler :move_cursor ()
221
+
222
+ -- Test that cursor property is maintained
223
+ assert .are .same (true , handler .cursor )
224
+ assert .are .same (1 , handler .first_line )
225
+ end )
226
+
227
+ it (" should cleanup timers properly" , function ()
228
+ local handler = ResponseHandler :new (mock_queries )
229
+ local mock_timer = {
230
+ stop = stub .new (),
231
+ close = stub .new (),
232
+ }
233
+ handler .update_timer = mock_timer
234
+
235
+ handler :cleanup ()
236
+
237
+ assert .stub (mock_timer .stop ).was_called ()
238
+ assert .stub (mock_timer .close ).was_called ()
239
+ assert .is_nil (handler .update_timer )
240
+ end )
241
+
242
+ it (" should not update buffer when first_line is nil" , function ()
243
+ local handler = ResponseHandler :new (mock_queries )
244
+ handler .first_line = nil
245
+ handler .response = " test"
246
+
247
+ handler :update_buffer ()
248
+
249
+ assert .stub (mock_vim .api .nvim_buf_set_lines ).was_not_called ()
250
+ end )
251
+
252
+ it (" should not update highlighting when first_line is nil" , function ()
253
+ local handler = ResponseHandler :new (mock_queries )
254
+ handler .first_line = nil
255
+ handler .response = " test"
256
+
257
+ local qt = { ns_id = 1 }
258
+ handler :update_highlighting (qt )
259
+
260
+ assert .stub (mock_vim .api .nvim_buf_clear_namespace ).was_not_called ()
261
+ end )
262
+
263
+ it (" should not update query object when first_line is nil" , function ()
264
+ local handler = ResponseHandler :new (mock_queries )
265
+ handler .first_line = nil
266
+ handler .response = " test"
267
+
268
+ local qt = {}
269
+ handler :update_query_object (qt )
270
+
271
+ assert .is_nil (qt .first_line )
272
+ assert .is_nil (qt .last_line )
273
+ end )
96
274
end )
0 commit comments