|
| 1 | +-- luacheck: globals expect |
| 2 | +require("tests.busted_setup") |
| 3 | + |
| 4 | +describe("NvimTree Visual Selection", function() |
| 5 | + local visual_commands |
| 6 | + local mock_vim |
| 7 | + |
| 8 | + local function setup_mocks() |
| 9 | + package.loaded["claudecode.visual_commands"] = nil |
| 10 | + package.loaded["claudecode.logger"] = nil |
| 11 | + |
| 12 | + -- Mock logger |
| 13 | + package.loaded["claudecode.logger"] = { |
| 14 | + debug = function() end, |
| 15 | + warn = function() end, |
| 16 | + error = function() end, |
| 17 | + } |
| 18 | + |
| 19 | + mock_vim = { |
| 20 | + fn = { |
| 21 | + mode = function() |
| 22 | + return "V" -- Visual line mode |
| 23 | + end, |
| 24 | + getpos = function(mark) |
| 25 | + if mark == "'<" then |
| 26 | + return { 0, 2, 0, 0 } -- Start at line 2 |
| 27 | + elseif mark == "'>" then |
| 28 | + return { 0, 4, 0, 0 } -- End at line 4 |
| 29 | + elseif mark == "v" then |
| 30 | + return { 0, 2, 0, 0 } -- Anchor at line 2 |
| 31 | + end |
| 32 | + return { 0, 0, 0, 0 } |
| 33 | + end, |
| 34 | + }, |
| 35 | + api = { |
| 36 | + nvim_get_current_win = function() |
| 37 | + return 1002 |
| 38 | + end, |
| 39 | + nvim_get_mode = function() |
| 40 | + return { mode = "V" } |
| 41 | + end, |
| 42 | + nvim_get_current_buf = function() |
| 43 | + return 1 |
| 44 | + end, |
| 45 | + nvim_win_get_cursor = function() |
| 46 | + return { 4, 0 } -- Cursor at line 4 |
| 47 | + end, |
| 48 | + nvim_buf_get_lines = function(buf, start, end_line, strict) |
| 49 | + -- Return mock buffer lines for the visual selection |
| 50 | + return { |
| 51 | + " 📁 src/", |
| 52 | + " 📄 init.lua", |
| 53 | + " 📄 config.lua", |
| 54 | + } |
| 55 | + end, |
| 56 | + nvim_win_set_cursor = function(win, pos) |
| 57 | + -- Mock cursor setting |
| 58 | + end, |
| 59 | + nvim_replace_termcodes = function(keys, from_part, do_lt, special) |
| 60 | + return keys |
| 61 | + end, |
| 62 | + }, |
| 63 | + bo = { filetype = "NvimTree" }, |
| 64 | + schedule = function(fn) |
| 65 | + fn() |
| 66 | + end, |
| 67 | + } |
| 68 | + |
| 69 | + _G.vim = mock_vim |
| 70 | + end |
| 71 | + |
| 72 | + before_each(function() |
| 73 | + setup_mocks() |
| 74 | + end) |
| 75 | + |
| 76 | + describe("nvim-tree visual selection handling", function() |
| 77 | + before_each(function() |
| 78 | + visual_commands = require("claudecode.visual_commands") |
| 79 | + end) |
| 80 | + |
| 81 | + it("should extract files from visual selection in nvim-tree", function() |
| 82 | + -- Create a stateful mock that tracks cursor position |
| 83 | + local cursor_positions = {} |
| 84 | + local expected_nodes = { |
| 85 | + [2] = { type = "directory", absolute_path = "/Users/test/project/src" }, |
| 86 | + [3] = { type = "file", absolute_path = "/Users/test/project/init.lua" }, |
| 87 | + [4] = { type = "file", absolute_path = "/Users/test/project/config.lua" }, |
| 88 | + } |
| 89 | + |
| 90 | + mock_vim.api.nvim_win_set_cursor = function(win, pos) |
| 91 | + cursor_positions[#cursor_positions + 1] = pos[1] |
| 92 | + end |
| 93 | + |
| 94 | + local mock_nvim_tree_api = { |
| 95 | + tree = { |
| 96 | + get_node_under_cursor = function() |
| 97 | + local current_line = cursor_positions[#cursor_positions] or 2 |
| 98 | + return expected_nodes[current_line] |
| 99 | + end, |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + local visual_data = { |
| 104 | + tree_state = mock_nvim_tree_api, |
| 105 | + tree_type = "nvim-tree", |
| 106 | + start_pos = 2, |
| 107 | + end_pos = 4, |
| 108 | + } |
| 109 | + |
| 110 | + local files, err = visual_commands.get_files_from_visual_selection(visual_data) |
| 111 | + |
| 112 | + expect(err).to_be_nil() |
| 113 | + expect(files).to_be_table() |
| 114 | + expect(#files).to_be(3) |
| 115 | + expect(files[1]).to_be("/Users/test/project/src") |
| 116 | + expect(files[2]).to_be("/Users/test/project/init.lua") |
| 117 | + expect(files[3]).to_be("/Users/test/project/config.lua") |
| 118 | + end) |
| 119 | + |
| 120 | + it("should handle empty visual selection in nvim-tree", function() |
| 121 | + local mock_nvim_tree_api = { |
| 122 | + tree = { |
| 123 | + get_node_under_cursor = function() |
| 124 | + return nil -- No node found |
| 125 | + end, |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + local visual_data = { |
| 130 | + tree_state = mock_nvim_tree_api, |
| 131 | + tree_type = "nvim-tree", |
| 132 | + start_pos = 2, |
| 133 | + end_pos = 2, |
| 134 | + } |
| 135 | + |
| 136 | + local files, err = visual_commands.get_files_from_visual_selection(visual_data) |
| 137 | + |
| 138 | + expect(err).to_be_nil() |
| 139 | + expect(files).to_be_table() |
| 140 | + expect(#files).to_be(0) |
| 141 | + end) |
| 142 | + |
| 143 | + it("should filter out root-level files in nvim-tree", function() |
| 144 | + local mock_nvim_tree_api = { |
| 145 | + tree = { |
| 146 | + get_node_under_cursor = function() |
| 147 | + return { |
| 148 | + type = "file", |
| 149 | + absolute_path = "/root_file.txt", -- Root-level file should be filtered |
| 150 | + } |
| 151 | + end, |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + local visual_data = { |
| 156 | + tree_state = mock_nvim_tree_api, |
| 157 | + tree_type = "nvim-tree", |
| 158 | + start_pos = 1, |
| 159 | + end_pos = 1, |
| 160 | + } |
| 161 | + |
| 162 | + local files, err = visual_commands.get_files_from_visual_selection(visual_data) |
| 163 | + |
| 164 | + expect(err).to_be_nil() |
| 165 | + expect(files).to_be_table() |
| 166 | + expect(#files).to_be(0) -- Root-level file should be filtered out |
| 167 | + end) |
| 168 | + |
| 169 | + it("should remove duplicate files in visual selection", function() |
| 170 | + local call_count = 0 |
| 171 | + local mock_nvim_tree_api = { |
| 172 | + tree = { |
| 173 | + get_node_under_cursor = function() |
| 174 | + call_count = call_count + 1 |
| 175 | + -- Return the same file path twice to test deduplication |
| 176 | + return { |
| 177 | + type = "file", |
| 178 | + absolute_path = "/Users/test/project/duplicate.lua", |
| 179 | + } |
| 180 | + end, |
| 181 | + }, |
| 182 | + } |
| 183 | + |
| 184 | + local visual_data = { |
| 185 | + tree_state = mock_nvim_tree_api, |
| 186 | + tree_type = "nvim-tree", |
| 187 | + start_pos = 1, |
| 188 | + end_pos = 2, -- Two lines, same file |
| 189 | + } |
| 190 | + |
| 191 | + local files, err = visual_commands.get_files_from_visual_selection(visual_data) |
| 192 | + |
| 193 | + expect(err).to_be_nil() |
| 194 | + expect(files).to_be_table() |
| 195 | + expect(#files).to_be(1) -- Should have only one instance |
| 196 | + expect(files[1]).to_be("/Users/test/project/duplicate.lua") |
| 197 | + end) |
| 198 | + |
| 199 | + it("should handle mixed file and directory selection", function() |
| 200 | + local cursor_positions = {} |
| 201 | + local expected_nodes = { |
| 202 | + [1] = { type = "directory", absolute_path = "/Users/test/project/lib" }, |
| 203 | + [2] = { type = "file", absolute_path = "/Users/test/project/main.lua" }, |
| 204 | + [3] = { type = "directory", absolute_path = "/Users/test/project/tests" }, |
| 205 | + } |
| 206 | + |
| 207 | + mock_vim.api.nvim_win_set_cursor = function(win, pos) |
| 208 | + cursor_positions[#cursor_positions + 1] = pos[1] |
| 209 | + end |
| 210 | + |
| 211 | + local mock_nvim_tree_api = { |
| 212 | + tree = { |
| 213 | + get_node_under_cursor = function() |
| 214 | + local current_line = cursor_positions[#cursor_positions] or 1 |
| 215 | + return expected_nodes[current_line] |
| 216 | + end, |
| 217 | + }, |
| 218 | + } |
| 219 | + |
| 220 | + local visual_data = { |
| 221 | + tree_state = mock_nvim_tree_api, |
| 222 | + tree_type = "nvim-tree", |
| 223 | + start_pos = 1, |
| 224 | + end_pos = 3, |
| 225 | + } |
| 226 | + |
| 227 | + local files, err = visual_commands.get_files_from_visual_selection(visual_data) |
| 228 | + |
| 229 | + expect(err).to_be_nil() |
| 230 | + expect(files).to_be_table() |
| 231 | + expect(#files).to_be(3) |
| 232 | + expect(files[1]).to_be("/Users/test/project/lib") |
| 233 | + expect(files[2]).to_be("/Users/test/project/main.lua") |
| 234 | + expect(files[3]).to_be("/Users/test/project/tests") |
| 235 | + end) |
| 236 | + end) |
| 237 | +end) |
0 commit comments