|
1 | 1 | local comp_utils = require("parrot.completion.utils")
|
| 2 | +local mock = require("luassert.mock") |
2 | 3 | local async = require("plenary.async")
|
3 | 4 |
|
4 | 5 | describe("completion.utils", function()
|
5 | 6 | describe("resolve_path", function()
|
6 | 7 | it("should resolve relative paths correctly", function()
|
7 |
| - async.run(function() |
8 |
| - local cwd = vim.fn.getcwd() |
9 |
| - local result = comp_utils.resolve_path("test/path", cwd) |
10 |
| - assert.are.equal(cwd .. "/test", result) |
11 |
| - end) |
| 8 | + local cwd = vim.fn.getcwd() |
| 9 | + local result = comp_utils.resolve_path("test/path", cwd) |
| 10 | + assert.are.equal(cwd .. "/test", result) |
12 | 11 | end)
|
13 | 12 |
|
14 | 13 | it("should handle absolute paths correctly", function()
|
15 |
| - async.run(function() |
16 |
| - local result = comp_utils.resolve_path("/absolute/path/file.txt", "") |
17 |
| - assert.are.equal("/absolute/path", result) |
18 |
| - end) |
| 14 | + local result = comp_utils.resolve_path("/absolute/path/file.txt", "") |
| 15 | + assert.are.equal("/absolute/path", result) |
19 | 16 | end)
|
20 | 17 |
|
21 | 18 | it("should handle paths with trailing slashes", function()
|
22 |
| - async.run(function() |
23 |
| - local result = comp_utils.resolve_path("test/path/", "") |
24 |
| - assert.are.equal("test/path/", result) |
25 |
| - end) |
| 19 | + local result = comp_utils.resolve_path("test/path/", "") |
| 20 | + assert.are.equal("test/path/", result) |
| 21 | + end) |
| 22 | + |
| 23 | + it("should handle empty path with cwd", function() |
| 24 | + local cwd = vim.fn.getcwd() |
| 25 | + local result = comp_utils.resolve_path("", cwd) |
| 26 | + assert.are.equal(cwd, result) |
26 | 27 | end)
|
27 | 28 | end)
|
28 | 29 |
|
29 | 30 | describe("get_command_documentation", function()
|
30 | 31 | it("should return documentation for known commands", function()
|
31 |
| - assert.is_string(comp_utils.get_command_documentation("file")) |
32 |
| - assert.is_string(comp_utils.get_command_documentation("buffer")) |
33 |
| - assert.is_string(comp_utils.get_command_documentation("directory")) |
| 32 | + local file_doc = comp_utils.get_command_documentation("file") |
| 33 | + assert.is_string(file_doc) |
| 34 | + assert.is_true(file_doc:find("@file:") > 0) |
| 35 | + |
| 36 | + local buffer_doc = comp_utils.get_command_documentation("buffer") |
| 37 | + assert.is_string(buffer_doc) |
| 38 | + assert.is_true(buffer_doc:find("@buffer:") > 0) |
| 39 | + |
| 40 | + local dir_doc = comp_utils.get_command_documentation("directory") |
| 41 | + assert.is_string(dir_doc) |
| 42 | + assert.is_true(dir_doc:find("@directory:") > 0) |
34 | 43 | end)
|
35 | 44 |
|
36 | 45 | it("should return empty string for unknown commands", function()
|
37 | 46 | assert.are.equal("", comp_utils.get_command_documentation("unknown"))
|
38 | 47 | end)
|
39 | 48 | end)
|
| 49 | + |
| 50 | + describe("is_completion_available", function() |
| 51 | + it("should return true in parrot chat files", function() |
| 52 | + local config_mock = mock(require("parrot.config"), true) |
| 53 | + config_mock.loaded = true |
| 54 | + config_mock.options = { chat_dir = "/mock/chat/dir" } |
| 55 | + |
| 56 | + local utils_mock = mock(require("parrot.utils"), true) |
| 57 | + utils_mock.is_chat.returns(true) |
| 58 | + |
| 59 | + local api_mock = mock(vim.api, true) |
| 60 | + api_mock.nvim_get_current_buf.returns(1) |
| 61 | + api_mock.nvim_buf_get_name.returns("/mock/chat/dir/test.txt") |
| 62 | + |
| 63 | + assert.is_true(comp_utils.is_completion_available()) |
| 64 | + |
| 65 | + mock.revert(config_mock) |
| 66 | + mock.revert(utils_mock) |
| 67 | + mock.revert(api_mock) |
| 68 | + end) |
| 69 | + |
| 70 | + it("should return true in UI input buffers", function() |
| 71 | + local api_mock = mock(vim.api, true) |
| 72 | + api_mock.nvim_get_current_buf.returns(1) |
| 73 | + api_mock.nvim_buf_get_name.returns("") |
| 74 | + api_mock.nvim_get_option_value.returns("nofile") |
| 75 | + api_mock.nvim_get_namespaces.returns({ 1 }) |
| 76 | + api_mock.nvim_buf_get_extmarks.returns({ |
| 77 | + { 1, 0, 0, { virt_text = { { "Enter text here", "Comment" } } } }, |
| 78 | + }) |
| 79 | + |
| 80 | + assert.is_true(comp_utils.is_completion_available()) |
| 81 | + |
| 82 | + mock.revert(api_mock) |
| 83 | + end) |
| 84 | + |
| 85 | + it("should return false in regular files", function() |
| 86 | + local api_mock = mock(vim.api, true) |
| 87 | + api_mock.nvim_get_current_buf.returns(1) |
| 88 | + api_mock.nvim_buf_get_name.returns("somefile.txt") |
| 89 | + api_mock.nvim_get_option_value.returns("file") |
| 90 | + api_mock.nvim_get_namespaces.returns({}) |
| 91 | + |
| 92 | + assert.is_false(comp_utils.is_completion_available()) |
| 93 | + |
| 94 | + mock.revert(api_mock) |
| 95 | + end) |
| 96 | + end) |
| 97 | + |
| 98 | + -- describe("read_file_async", function() |
| 99 | + -- it("should read file content asynchronously", function() |
| 100 | + -- local uv_mock = mock(vim.uv, true) |
| 101 | + -- uv_mock.fs_open.returns(1) |
| 102 | + -- uv_mock.fs_read.returns("test content") |
| 103 | + -- uv_mock.fs_close.returns(true) |
| 104 | + |
| 105 | + -- local content |
| 106 | + -- async.run(function() |
| 107 | + -- comp_utils.read_file_async("test.txt", 1024, async).map(function(data) |
| 108 | + -- content = data |
| 109 | + -- end) |
| 110 | + -- end) |
| 111 | + |
| 112 | + -- vim.wait(100, function() return content ~= nil end) |
| 113 | + -- assert.are.equal("test content", content) |
| 114 | + |
| 115 | + -- mock.revert(uv_mock) |
| 116 | + -- end) |
| 117 | + -- end) |
| 118 | + |
40 | 119 | end)
|
0 commit comments