forked from coder/claudecode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools_spec.lua
More file actions
320 lines (288 loc) · 11.1 KB
/
tools_spec.lua
File metadata and controls
320 lines (288 loc) · 11.1 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
-- luacheck: globals expect
require("tests.busted_setup")
describe("Tools Module", function()
local tools
local mock_vim
local spy -- For spying on functions
local function setup()
package.loaded["claudecode.tools.init"] = nil
package.loaded["claudecode.diff"] = nil
package.loaded["luassert.spy"] = nil -- Ensure spy is fresh
spy = require("luassert.spy")
mock_vim = {
fn = {
expand = function(path)
return path
end,
filereadable = function()
return 1
end,
fnameescape = function(path)
return path
end,
bufnr = function()
return 1
end,
buflisted = function()
return 1
end,
getcwd = function()
return "/test/workspace"
end,
fnamemodify = function(path, modifier)
if modifier == ":t" then
return "workspace"
end
return path
end,
},
cmd = function() end,
api = {
nvim_list_bufs = function()
return { 1, 2 }
end,
nvim_buf_is_loaded = function()
return true
end,
nvim_buf_get_name = function(bufnr)
if bufnr == 1 then
return "/test/file1.lua"
end
if bufnr == 2 then
return "/test/file2.lua"
end
return ""
end,
nvim_buf_get_option = function()
return false
end,
nvim_buf_call = function(bufnr, fn_to_call) -- Renamed to avoid conflict
fn_to_call()
end,
nvim_buf_delete = function() end,
},
lsp = {},
diagnostic = {
get = function()
return {
{
bufnr = 1,
lnum = 10,
col = 5,
severity = 1,
message = "Test error",
source = "test",
},
}
end,
},
json = {
encode = function(obj)
return vim.inspect(obj) -- Use the real vim.inspect if available, or our mock
end,
},
notify = function() end,
log = { -- Add mock for vim.log
levels = {
TRACE = 0,
DEBUG = 1,
ERROR = 2,
WARN = 3, -- Add other common levels for completeness if needed
INFO = 4,
},
},
inspect = function(obj) -- Keep the mock inspect for controlled output
if type(obj) == "string" then
return '"' .. obj .. '"'
elseif type(obj) == "table" then
local items = {}
for k, v in pairs(obj) do
table.insert(items, tostring(k) .. ": " .. mock_vim.inspect(v))
end
return "{" .. table.concat(items, ", ") .. "}"
else
return tostring(obj)
end
end,
}
_G.vim = mock_vim
tools = require("claudecode.tools.init")
-- Ensure tools are registered for testing handle_invoke
tools.register_all()
end
local function teardown()
_G.vim = nil
package.loaded["luassert.spy"] = nil
spy = nil
end
local function contains(str, pattern)
if type(str) ~= "string" or type(pattern) ~= "string" then
return false
end
return str:find(pattern, 1, true) ~= nil
end
before_each(function()
setup()
end)
after_each(function()
teardown()
end)
describe("Tool Registration", function()
it("should register all tools", function()
-- tools.register_all() is called in setup
expect(tools.tools).to_be_table()
expect(tools.tools.openFile).to_be_table()
expect(tools.tools.openFile.handler).to_be_function()
expect(tools.tools.getDiagnostics).to_be_table()
expect(tools.tools.getDiagnostics.handler).to_be_function()
expect(tools.tools.getOpenEditors).to_be_table()
expect(tools.tools.getOpenEditors.handler).to_be_function()
expect(tools.tools.openDiff).to_be_table()
expect(tools.tools.openDiff.handler).to_be_function()
-- Add more checks for other registered tools as needed
end)
it("should allow registering custom tools", function()
local custom_tool_handler = spy.new(function()
return "custom result"
end)
local custom_tool_module = {
name = "customTool",
schema = nil,
handler = custom_tool_handler,
}
tools.register(custom_tool_module)
expect(tools.tools.customTool.handler).to_be(custom_tool_handler)
end)
end)
describe("Tool Invocation Handler (handle_invoke)", function()
it("should handle valid tool invocation and return result (e.g., getOpenEditors)", function()
-- The 'tools' module and its handlers were loaded in setup() when _G.vim was 'mock_vim'.
-- So, we need to modify the spies on the 'mock_vim' instance directly.
mock_vim.api.nvim_list_bufs = spy.new(function()
return { 1 }
end)
mock_vim.api.nvim_buf_is_loaded = spy.new(function(b)
return b == 1
end)
mock_vim.fn.buflisted = spy.new(function(b) -- Ensure this is on mock_vim.fn
if b == 1 then
return 1
else
return 0
end -- Must return number 0 or 1
end)
mock_vim.api.nvim_buf_get_name = spy.new(function(b)
if b == 1 then
return "/test/file.lua"
else
return ""
end
end)
mock_vim.api.nvim_buf_get_option = spy.new(function(b, opt)
if b == 1 and opt == "modified" then
return false
else
return nil
end
end)
-- Re-register the specific tool to ensure its handler picks up the new spies
package.loaded["claudecode.tools.get_open_editors"] = nil -- Clear cache for the sub-tool
tools.register(require("claudecode.tools.get_open_editors"))
local params = {
name = "getOpenEditors",
arguments = {},
}
local result_obj = tools.handle_invoke(nil, params)
expect(result_obj.result).to_be_table() -- "Expected .result to be a table"
expect(result_obj.result.editors).to_be_table() -- "Expected .result.editors to be a table"
expect(#result_obj.result.editors).to_be(1)
expect(result_obj.result.editors[1].filePath).to_be("/test/file.lua")
expect(result_obj.error).to_be_nil() -- "Expected .error to be nil for successful call"
expect(mock_vim.api.nvim_list_bufs.calls).to_be_table() -- Check if .calls table exists
expect(#mock_vim.api.nvim_list_bufs.calls > 0).to_be_true() -- Then, check if called
expect(mock_vim.api.nvim_buf_is_loaded.calls[1].vals[1]).to_be(1) -- Check first arg of first call
expect(mock_vim.fn.buflisted.calls[1].vals[1]).to_be(1) -- Check first arg of first call
expect(mock_vim.api.nvim_buf_get_name.calls[1].vals[1]).to_be(1) -- Check first arg of first call
expect(mock_vim.api.nvim_buf_get_option.calls[1].vals[1]).to_be(1) -- Check first arg of first call
expect(mock_vim.api.nvim_buf_get_option.calls[1].vals[2]).to_be("modified") -- Check second arg of first call
end)
it("should handle unknown tool invocation with JSON-RPC error", function()
local params = {
name = "unknownTool",
arguments = {},
}
local result_obj = tools.handle_invoke(nil, params)
expect(result_obj.error).to_be_table()
expect(result_obj.error.code).to_be(-32601) -- Method not found
expect(contains(result_obj.error.message, "Tool not found: unknownTool")).to_be_true()
expect(result_obj.result).to_be_nil()
end)
it("should handle tool execution errors (structured error from handler) with JSON-RPC error", function()
local erroring_tool_handler = spy.new(function()
error({ code = -32001, message = "Specific tool error from handler", data = { detail = "some detail" } })
end)
tools.register({
name = "errorToolStructured",
schema = nil,
handler = erroring_tool_handler,
})
local params = { name = "errorToolStructured", arguments = {} }
local result_obj = tools.handle_invoke(nil, params)
expect(result_obj.error).to_be_table()
expect(result_obj.error.code).to_be(-32001)
expect(result_obj.error.message).to_be("Specific tool error from handler")
expect(result_obj.error.data).to_be_table()
expect(result_obj.error.data.detail).to_be("some detail")
expect(result_obj.result).to_be_nil()
expect(erroring_tool_handler.calls).to_be_table()
expect(#erroring_tool_handler.calls > 0).to_be_true()
end)
it("should handle tool execution errors (simple string error from handler) with JSON-RPC error", function()
local erroring_tool_handler_string = spy.new(function()
error("Simple string error from tool handler")
end)
tools.register({
name = "errorToolString",
schema = nil,
handler = erroring_tool_handler_string,
})
local params = { name = "errorToolString", arguments = {} }
local result_obj = tools.handle_invoke(nil, params)
expect(result_obj.error).to_be_table()
expect(result_obj.error.code).to_be(-32000) -- Default server error for unhandled/string errors
assert_contains(result_obj.error.message, "Simple string error from tool handler") -- Message includes traceback
assert_contains(result_obj.error.data, "Simple string error from tool handler") -- Original error string in data
expect(result_obj.result).to_be_nil()
expect(erroring_tool_handler_string.calls).to_be_table()
expect(#erroring_tool_handler_string.calls > 0).to_be_true()
end)
it("should handle tool execution errors (pcall/xpcall style error from handler) with JSON-RPC error", function()
local erroring_tool_handler_pcall = spy.new(function()
-- Simulate a tool that returns an error status and message, like from pcall
return false, "Pcall-style error message"
end)
tools.register({
name = "errorToolPcallStyle",
schema = nil,
handler = erroring_tool_handler_pcall,
})
local params = { name = "errorToolPcallStyle", arguments = {} }
local result_obj = tools.handle_invoke(nil, params)
expect(result_obj.error).to_be_table()
expect(result_obj.error.code).to_be(-32000) -- Default server error
expect(result_obj.error.message).to_be("Pcall-style error message") -- This should be exact as it's not passed through Lua's error()
expect(result_obj.error.data).not_to_be_nil() -- "error.data should not be nil for pcall-style string errors"
expect(type(result_obj.error.data)).to_be("string") -- Check type explicitly
assert_contains(result_obj.error.data, "Pcall-style error message")
expect(result_obj.result).to_be_nil()
expect(erroring_tool_handler_pcall.calls).to_be_table()
expect(#erroring_tool_handler_pcall.calls > 0).to_be_true()
end)
end)
-- All individual tool describe blocks (e.g., "Open File Tool", "Get Diagnostics Tool", etc.)
-- were removed from this file as of the refactoring on 2025-05-26.
-- Their functionality is now tested in their respective spec files
-- under tests/unit/tools/impl/.
-- This file now focuses on the tool registration and the generic handle_invoke logic.
teardown()
end)