@@ -57,6 +57,30 @@ describe("Tool: open_file", function()
57
57
_G .vim .api .nvim_get_current_win = spy .new (function ()
58
58
return 1000
59
59
end )
60
+ _G .vim .api .nvim_get_current_buf = spy .new (function ()
61
+ return 1 -- Mock current buffer ID
62
+ end )
63
+ _G .vim .api .nvim_buf_get_name = spy .new (function (buf )
64
+ return " test.txt" -- Mock buffer name
65
+ end )
66
+ _G .vim .api .nvim_buf_line_count = spy .new (function (buf )
67
+ return 10 -- Mock line count
68
+ end )
69
+ _G .vim .api .nvim_buf_set_mark = spy .new (function (buf , name , line , col , opts )
70
+ -- Mock mark setting
71
+ end )
72
+ _G .vim .api .nvim_buf_get_lines = spy .new (function (buf , start , end_line , strict )
73
+ -- Mock buffer lines for search
74
+ return {
75
+ " local function test()" ,
76
+ " print('hello')" ,
77
+ " return true" ,
78
+ " end" ,
79
+ }
80
+ end )
81
+ _G .vim .api .nvim_win_set_cursor = spy .new (function (win , pos )
82
+ -- Mock cursor setting
83
+ end )
60
84
end )
61
85
62
86
after_each (function ()
@@ -129,10 +153,80 @@ describe("Tool: open_file", function()
129
153
expect (_G .vim .cmd_history [1 ]).to_be (" edit /Users/testuser/.config/nvim/init.lua" )
130
154
end )
131
155
132
- -- TODO: Add tests for selection by line numbers (params.startLine, params.endLine)
133
- -- This will require mocking vim.api.nvim_win_set_cursor or similar for selection
134
- -- and potentially vim.api.nvim_buf_get_lines if text content matters for selection.
156
+ it (" should handle makeFrontmost=false to return detailed JSON" , function ()
157
+ local params = { filePath = " test.txt" , makeFrontmost = false }
158
+ local success , result = pcall (open_file_handler , params )
159
+
160
+ expect (success ).to_be_true ()
161
+ expect (result .content ).to_be_table ()
162
+ expect (result .content [1 ].type ).to_be (" text" )
163
+
164
+ -- makeFrontmost=false should return JSON-encoded detailed info
165
+ local parsed_result = require (" tests.busted_setup" ).json_decode (result .content [1 ].text )
166
+ expect (parsed_result .success ).to_be_true ()
167
+ expect (parsed_result .filePath ).to_be (" test.txt" )
168
+ end )
169
+
170
+ it (" should handle preview mode parameter" , function ()
171
+ local params = { filePath = " test.txt" , preview = true }
172
+ local success , result = pcall (open_file_handler , params )
173
+
174
+ expect (success ).to_be_true ()
175
+ expect (result .content [1 ].text ).to_be (" Opened file: test.txt" )
176
+ -- Preview mode affects window behavior but basic functionality should work
177
+ end )
178
+
179
+ it (" should handle line selection parameters" , function ()
180
+ -- Mock additional functions needed for line selection
181
+ _G .vim .api .nvim_win_set_cursor = spy .new (function (win , pos )
182
+ -- Mock cursor setting
183
+ end )
184
+ _G .vim .fn .setpos = spy .new (function (mark , pos )
185
+ -- Mock position setting
186
+ end )
187
+
188
+ local params = { filePath = " test.txt" , startLine = 5 , endLine = 10 }
189
+ local success , result = pcall (open_file_handler , params )
135
190
136
- -- TODO: Add tests for selection by text patterns (params.startText, params.endText)
137
- -- This will require more complex mocking of buffer content and search functions.
191
+ expect (success ).to_be_true ()
192
+ expect (result .content ).to_be_table ()
193
+ expect (result .content [1 ].type ).to_be (" text" )
194
+ expect (result .content [1 ].text ).to_be (" Opened file and selected lines 5 to 10" )
195
+ end )
196
+
197
+ it (" should handle text pattern selection when pattern found" , function ()
198
+ local params = {
199
+ filePath = " test.txt" ,
200
+ startText = " function" ,
201
+ endText = " end" ,
202
+ selectToEndOfLine = true ,
203
+ }
204
+
205
+ local success , result = pcall (open_file_handler , params )
206
+
207
+ expect (success ).to_be_true ()
208
+ expect (result .content ).to_be_table ()
209
+ expect (result .content [1 ].type ).to_be (" text" )
210
+ -- Since the mock buffer contains "function" and "end", selection should work
211
+ expect (result .content [1 ].text ).to_be (' Opened file and selected text from "function" to "end"' )
212
+ end )
213
+
214
+ it (" should handle text pattern selection when pattern not found" , function ()
215
+ -- Mock search to return 0 (not found)
216
+ _G .vim .fn .search = spy .new (function (pattern )
217
+ return 0 -- Pattern not found
218
+ end )
219
+
220
+ local params = {
221
+ filePath = " test.txt" ,
222
+ startText = " nonexistent" ,
223
+ }
224
+
225
+ local success , result = pcall (open_file_handler , params )
226
+
227
+ expect (success ).to_be_true ()
228
+ expect (result .content ).to_be_table ()
229
+ expect (result .content [1 ].type ).to_be (" text" )
230
+ assert_contains (result .content [1 ].text , " not found" )
231
+ end )
138
232
end )
0 commit comments