refactor: llm tools (#1675)
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
local stub = require("luassert.stub")
|
||||
local LlmTools = require("avante.llm_tools")
|
||||
local LlmToolHelpers = require("avante.llm_tools.helpers")
|
||||
local Config = require("avante.config")
|
||||
local Utils = require("avante.utils")
|
||||
local ls = require("avante.llm_tools.ls")
|
||||
local grep = require("avante.llm_tools.grep")
|
||||
local glob = require("avante.llm_tools.glob")
|
||||
local read_file = require("avante.llm_tools.read_file")
|
||||
local bash = require("avante.llm_tools.bash")
|
||||
|
||||
LlmTools.confirm = function(msg, cb) return cb(true) end
|
||||
LlmToolHelpers.confirm = function(msg, cb) return cb(true) end
|
||||
|
||||
describe("llm_tools", function()
|
||||
local test_dir = "/tmp/test_llm_tools"
|
||||
@@ -44,23 +50,23 @@ describe("llm_tools", function()
|
||||
Utils.get_project_root:revert()
|
||||
end)
|
||||
|
||||
describe("list_files", function()
|
||||
describe("ls", function()
|
||||
it("should list files in directory", function()
|
||||
local result, err = LlmTools.list_files({ rel_path = ".", max_depth = 1 })
|
||||
local result, err = ls({ rel_path = ".", max_depth = 1 })
|
||||
assert.is_nil(err)
|
||||
assert.falsy(result:find("avante.nvim"))
|
||||
assert.truthy(result:find("test.txt"))
|
||||
assert.falsy(result:find("test1.txt"))
|
||||
end)
|
||||
it("should list files in directory with depth", function()
|
||||
local result, err = LlmTools.list_files({ rel_path = ".", max_depth = 2 })
|
||||
local result, err = ls({ rel_path = ".", max_depth = 2 })
|
||||
assert.is_nil(err)
|
||||
assert.falsy(result:find("avante.nvim"))
|
||||
assert.truthy(result:find("test.txt"))
|
||||
assert.truthy(result:find("test1.txt"))
|
||||
end)
|
||||
it("should list files respecting gitignore", function()
|
||||
local result, err = LlmTools.list_files({ rel_path = ".", max_depth = 2 })
|
||||
local result, err = ls({ rel_path = ".", max_depth = 2 })
|
||||
assert.is_nil(err)
|
||||
assert.falsy(result:find("avante.nvim"))
|
||||
assert.truthy(result:find("test.txt"))
|
||||
@@ -71,13 +77,13 @@ describe("llm_tools", function()
|
||||
|
||||
describe("read_file", function()
|
||||
it("should read file content", function()
|
||||
local content, err = LlmTools.read_file({ rel_path = "test.txt" })
|
||||
local content, err = read_file({ rel_path = "test.txt" })
|
||||
assert.is_nil(err)
|
||||
assert.equals("test content", content)
|
||||
end)
|
||||
|
||||
it("should return error for non-existent file", function()
|
||||
local content, err = LlmTools.read_file({ rel_path = "non_existent.txt" })
|
||||
local content, err = read_file({ rel_path = "non_existent.txt" })
|
||||
assert.truthy(err)
|
||||
assert.equals("", content)
|
||||
end)
|
||||
@@ -119,15 +125,7 @@ describe("llm_tools", function()
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("search_files", function()
|
||||
it("should find files matching pattern", function()
|
||||
local result, err = LlmTools.search_files({ rel_path = ".", keyword = "test" })
|
||||
assert.is_nil(err)
|
||||
assert.truthy(result:find("test.txt"))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("grep_search", function()
|
||||
describe("grep", function()
|
||||
local original_exepath = vim.fn.exepath
|
||||
|
||||
after_each(function() vim.fn.exepath = original_exepath end)
|
||||
@@ -150,27 +148,27 @@ describe("llm_tools", function()
|
||||
file:write("this is nothing")
|
||||
file:close()
|
||||
|
||||
local result, err = LlmTools.grep_search({ rel_path = ".", query = "Searchable", case_sensitive = false })
|
||||
local result, err = grep({ rel_path = ".", query = "Searchable", case_sensitive = false })
|
||||
assert.is_nil(err)
|
||||
assert.truthy(result:find("searchable.txt"))
|
||||
assert.falsy(result:find("nothing.txt"))
|
||||
|
||||
local result2, err2 = LlmTools.grep_search({ rel_path = ".", query = "searchable", case_sensitive = true })
|
||||
local result2, err2 = grep({ rel_path = ".", query = "searchable", case_sensitive = true })
|
||||
assert.is_nil(err2)
|
||||
assert.truthy(result2:find("searchable.txt"))
|
||||
assert.falsy(result2:find("nothing.txt"))
|
||||
|
||||
local result3, err3 = LlmTools.grep_search({ rel_path = ".", query = "Searchable", case_sensitive = true })
|
||||
local result3, err3 = grep({ rel_path = ".", query = "Searchable", case_sensitive = true })
|
||||
assert.is_nil(err3)
|
||||
assert.falsy(result3:find("searchable.txt"))
|
||||
assert.falsy(result3:find("nothing.txt"))
|
||||
|
||||
local result4, err4 = LlmTools.grep_search({ rel_path = ".", query = "searchable", case_sensitive = false })
|
||||
local result4, err4 = grep({ rel_path = ".", query = "searchable", case_sensitive = false })
|
||||
assert.is_nil(err4)
|
||||
assert.truthy(result4:find("searchable.txt"))
|
||||
assert.falsy(result4:find("nothing.txt"))
|
||||
|
||||
local result5, err5 = LlmTools.grep_search({
|
||||
local result5, err5 = grep({
|
||||
rel_path = ".",
|
||||
query = "searchable",
|
||||
case_sensitive = false,
|
||||
@@ -194,7 +192,7 @@ describe("llm_tools", function()
|
||||
file:write("content for ag test")
|
||||
file:close()
|
||||
|
||||
local result, err = LlmTools.grep_search({ rel_path = ".", query = "ag test" })
|
||||
local result, err = grep({ rel_path = ".", query = "ag test" })
|
||||
assert.is_nil(err)
|
||||
assert.is_string(result)
|
||||
assert.truthy(result:find("ag_test.txt"))
|
||||
@@ -218,27 +216,27 @@ describe("llm_tools", function()
|
||||
file:write("this is nothing")
|
||||
file:close()
|
||||
|
||||
local result, err = LlmTools.grep_search({ rel_path = ".", query = "Searchable", case_sensitive = false })
|
||||
local result, err = grep({ rel_path = ".", query = "Searchable", case_sensitive = false })
|
||||
assert.is_nil(err)
|
||||
assert.truthy(result:find("searchable.txt"))
|
||||
assert.falsy(result:find("nothing.txt"))
|
||||
|
||||
local result2, err2 = LlmTools.grep_search({ rel_path = ".", query = "searchable", case_sensitive = true })
|
||||
local result2, err2 = grep({ rel_path = ".", query = "searchable", case_sensitive = true })
|
||||
assert.is_nil(err2)
|
||||
assert.truthy(result2:find("searchable.txt"))
|
||||
assert.falsy(result2:find("nothing.txt"))
|
||||
|
||||
local result3, err3 = LlmTools.grep_search({ rel_path = ".", query = "Searchable", case_sensitive = true })
|
||||
local result3, err3 = grep({ rel_path = ".", query = "Searchable", case_sensitive = true })
|
||||
assert.is_nil(err3)
|
||||
assert.falsy(result3:find("searchable.txt"))
|
||||
assert.falsy(result3:find("nothing.txt"))
|
||||
|
||||
local result4, err4 = LlmTools.grep_search({ rel_path = ".", query = "searchable", case_sensitive = false })
|
||||
local result4, err4 = grep({ rel_path = ".", query = "searchable", case_sensitive = false })
|
||||
assert.is_nil(err4)
|
||||
assert.truthy(result4:find("searchable.txt"))
|
||||
assert.falsy(result4:find("nothing.txt"))
|
||||
|
||||
local result5, err5 = LlmTools.grep_search({
|
||||
local result5, err5 = grep({
|
||||
rel_path = ".",
|
||||
query = "searchable",
|
||||
case_sensitive = false,
|
||||
@@ -253,18 +251,18 @@ describe("llm_tools", function()
|
||||
-- Mock exepath to return nothing
|
||||
vim.fn.exepath = function() return "" end
|
||||
|
||||
local result, err = LlmTools.grep_search({ rel_path = ".", query = "test" })
|
||||
local result, err = grep({ rel_path = ".", query = "test" })
|
||||
assert.equals("", result)
|
||||
assert.equals("No search command found", err)
|
||||
end)
|
||||
|
||||
it("should respect path permissions", function()
|
||||
local result, err = LlmTools.grep_search({ rel_path = "../outside_project", query = "test" })
|
||||
local result, err = grep({ rel_path = "../outside_project", query = "test" })
|
||||
assert.truthy(err:find("No permission to access path"))
|
||||
end)
|
||||
|
||||
it("should handle non-existent paths", function()
|
||||
local result, err = LlmTools.grep_search({ rel_path = "non_existent_dir", query = "test" })
|
||||
local result, err = grep({ rel_path = "non_existent_dir", query = "test" })
|
||||
assert.equals("", result)
|
||||
assert.truthy(err)
|
||||
assert.truthy(err:find("No such file or directory"))
|
||||
@@ -273,14 +271,14 @@ describe("llm_tools", function()
|
||||
|
||||
describe("bash", function()
|
||||
it("should execute command and return output", function()
|
||||
LlmTools.bash({ rel_path = ".", command = "echo 'test'" }, nil, function(result, err)
|
||||
bash({ rel_path = ".", command = "echo 'test'" }, nil, function(result, err)
|
||||
assert.is_nil(err)
|
||||
assert.equals("test\n", result)
|
||||
end)
|
||||
end)
|
||||
|
||||
it("should return error when running outside current directory", function()
|
||||
LlmTools.bash({ rel_path = "../outside_project", command = "echo 'test'" }, nil, function(result, err)
|
||||
bash({ rel_path = "../outside_project", command = "echo 'test'" }, nil, function(result, err)
|
||||
assert.is_false(result)
|
||||
assert.truthy(err)
|
||||
assert.truthy(err:find("No permission to access path"))
|
||||
@@ -373,7 +371,7 @@ describe("llm_tools", function()
|
||||
os.execute("touch " .. test_dir .. "/nested/file4.lua")
|
||||
|
||||
-- Test for lua files in the root
|
||||
local result, err = LlmTools.glob({ rel_path = ".", pattern = "*.lua" })
|
||||
local result, err = glob({ rel_path = ".", pattern = "*.lua" })
|
||||
assert.is_nil(err)
|
||||
local files = vim.json.decode(result)
|
||||
assert.equals(2, #files)
|
||||
@@ -383,7 +381,7 @@ describe("llm_tools", function()
|
||||
assert.falsy(vim.tbl_contains(files, test_dir .. "/nested/file4.lua"))
|
||||
|
||||
-- Test with recursive pattern
|
||||
local result2, err2 = LlmTools.glob({ rel_path = ".", pattern = "**/*.lua" })
|
||||
local result2, err2 = glob({ rel_path = ".", pattern = "**/*.lua" })
|
||||
assert.is_nil(err2)
|
||||
local files2 = vim.json.decode(result2)
|
||||
assert.equals(3, #files2)
|
||||
@@ -393,13 +391,13 @@ describe("llm_tools", function()
|
||||
end)
|
||||
|
||||
it("should respect path permissions", function()
|
||||
local result, err = LlmTools.glob({ rel_path = "../outside_project", pattern = "*.txt" })
|
||||
local result, err = glob({ rel_path = "../outside_project", pattern = "*.txt" })
|
||||
assert.equals("", result)
|
||||
assert.truthy(err:find("No permission to access path"))
|
||||
end)
|
||||
|
||||
it("should handle patterns without matches", function()
|
||||
local result, err = LlmTools.glob({ rel_path = ".", pattern = "*.nonexistent" })
|
||||
local result, err = glob({ rel_path = ".", pattern = "*.nonexistent" })
|
||||
assert.is_nil(err)
|
||||
local files = vim.json.decode(result)
|
||||
assert.equals(0, #files)
|
||||
@@ -414,7 +412,7 @@ describe("llm_tools", function()
|
||||
os.execute("touch " .. test_dir .. "/test_dir1/notignored1.lua")
|
||||
os.execute("touch " .. test_dir .. "/test_dir1/notignored2.lua")
|
||||
|
||||
local result, err = LlmTools.glob({ rel_path = ".", pattern = "**/*.lua" })
|
||||
local result, err = glob({ rel_path = ".", pattern = "**/*.lua" })
|
||||
assert.is_nil(err)
|
||||
local files = vim.json.decode(result)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user