-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathat_mention_edge_cases_spec.lua
More file actions
381 lines (323 loc) · 12.2 KB
/
Copy pathat_mention_edge_cases_spec.lua
File metadata and controls
381 lines (323 loc) · 12.2 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
-- luacheck: globals expect
require("tests.busted_setup")
describe("At Mention Edge Cases", function()
local init_module
local mock_vim
local function setup_mocks()
package.loaded["claudecode.init"] = nil
package.loaded["claudecode.logger"] = nil
package.loaded["claudecode.config"] = nil
-- Mock logger
package.loaded["claudecode.logger"] = {
debug = function() end,
warn = function(component, ...)
local args = { ... }
local message = table.concat(args, " ")
_G.vim.notify(message, _G.vim.log.levels.WARN)
end,
error = function(component, ...)
local args = { ... }
local message = table.concat(args, " ")
_G.vim.notify(message, _G.vim.log.levels.ERROR)
end,
}
-- Mock config
package.loaded["claudecode.config"] = {
get = function()
return {
debounce_ms = 100,
visual_demotion_delay_ms = 50,
}
end,
}
-- Extend the existing vim mock
mock_vim = _G.vim or {}
-- Mock file system functions
mock_vim.fn = mock_vim.fn or {}
mock_vim.fn.isdirectory = function(path)
-- Simulate non-existent paths
if string.match(path, "nonexistent") or string.match(path, "invalid") then
return 0
end
if string.match(path, "/lua$") or string.match(path, "/tests$") or path == "/Users/test/project" then
return 1
end
return 0
end
mock_vim.fn.filereadable = function(path)
-- Simulate non-existent files
if string.match(path, "nonexistent") or string.match(path, "invalid") then
return 0
end
if string.match(path, "%.lua$") or string.match(path, "%.txt$") then
return 1
end
return 0
end
mock_vim.fn.getcwd = function()
return "/Users/test/project"
end
mock_vim.log = mock_vim.log or {}
mock_vim.log.levels = {
ERROR = 1,
WARN = 2,
INFO = 3,
}
mock_vim.notify = function(message, level)
-- Store notifications for testing
mock_vim._last_notification = { message = message, level = level }
end
_G.vim = mock_vim
end
before_each(function()
setup_mocks()
init_module = require("claudecode.init")
end)
describe("format_path_for_at_mention validation", function()
it("should reject nil file_path", function()
local success, error_msg = pcall(function()
return init_module._format_path_for_at_mention(nil)
end)
expect(success).to_be_false()
expect(error_msg).to_be_string()
assert_contains(error_msg, "non-empty string")
end)
it("should reject empty string file_path", function()
local success, error_msg = pcall(function()
return init_module._format_path_for_at_mention("")
end)
expect(success).to_be_false()
expect(error_msg).to_be_string()
assert_contains(error_msg, "non-empty string")
end)
it("should reject non-string file_path", function()
local success, error_msg = pcall(function()
return init_module._format_path_for_at_mention(123)
end)
expect(success).to_be_false()
expect(error_msg).to_be_string()
assert_contains(error_msg, "non-empty string")
end)
it("should reject nonexistent file_path in production", function()
-- Temporarily simulate production environment
local old_busted = package.loaded["busted"]
package.loaded["busted"] = nil
local success, error_msg = pcall(function()
return init_module._format_path_for_at_mention("/nonexistent/path.lua")
end)
expect(success).to_be_false()
expect(error_msg).to_be_string()
assert_contains(error_msg, "does not exist")
-- Restore test environment
package.loaded["busted"] = old_busted
end)
it("should handle valid file path", function()
local success, result = pcall(function()
return init_module._format_path_for_at_mention("/Users/test/project/config.lua")
end)
expect(success).to_be_true()
expect(result).to_be("config.lua")
end)
it("should handle valid directory path", function()
local success, result = pcall(function()
return init_module._format_path_for_at_mention("/Users/test/project/lua")
end)
expect(success).to_be_true()
expect(result).to_be("lua/")
end)
end)
describe("broadcast_at_mention error handling", function()
it("should handle format_path_for_at_mention errors gracefully", function()
-- Mock a running server
init_module.state = { server = {
broadcast = function()
return true
end,
} }
-- Temporarily simulate production environment
local old_busted = package.loaded["busted"]
package.loaded["busted"] = nil
local success, error_msg = init_module._broadcast_at_mention("/invalid/nonexistent/path.lua")
expect(success).to_be_false()
expect(error_msg).to_be_string()
assert_contains(error_msg, "does not exist")
-- Restore test environment
package.loaded["busted"] = old_busted
end)
it("should handle server not running", function()
init_module.state = { server = nil }
local success, error_msg = init_module._broadcast_at_mention("/Users/test/project/config.lua")
expect(success).to_be_false()
expect(error_msg).to_be_string()
assert_contains(error_msg, "not running")
end)
it("should handle broadcast failures", function()
-- Mock a server that fails to broadcast
init_module.state = { server = {
broadcast = function()
return false
end,
} }
local success, error_msg = init_module._broadcast_at_mention("/Users/test/project/config.lua")
expect(success).to_be_false()
expect(error_msg).to_be_string()
assert_contains(error_msg, "Failed to broadcast")
end)
end)
describe("add_paths_to_claude error scenarios", function()
it("should handle empty file list", function()
init_module.state = { server = {
broadcast = function()
return true
end,
} }
local success_count, total_count = init_module._add_paths_to_claude({})
expect(success_count).to_be(0)
expect(total_count).to_be(0)
end)
it("should handle nil file list", function()
init_module.state = { server = {
broadcast = function()
return true
end,
} }
local success_count, total_count = init_module._add_paths_to_claude(nil)
expect(success_count).to_be(0)
expect(total_count).to_be(0)
end)
it("should handle mixed success and failure", function()
init_module.state = {
server = {
broadcast = function(event, params)
-- Fail for files with "fail" in the name
return not string.match(params.filePath, "fail")
end,
},
}
local files = {
"/Users/test/project/success.lua",
"/invalid/fail/path.lua",
"/Users/test/project/another_success.lua",
}
local success_count, total_count = init_module._add_paths_to_claude(files, { show_summary = false })
expect(total_count).to_be(3)
expect(success_count).to_be(2) -- Two should succeed, one should fail
end)
it("should provide user notifications for mixed results", function()
init_module.state = {
server = {
broadcast = function(event, params)
return not string.match(params.filePath, "fail")
end,
},
}
local files = {
"/Users/test/project/success.lua",
"/invalid/fail/path.lua",
}
local success_count, total_count = init_module._add_paths_to_claude(files, { show_summary = true })
expect(total_count).to_be(2)
expect(success_count).to_be(1)
-- Check that a notification was generated
expect(mock_vim._last_notification).to_be_table()
expect(mock_vim._last_notification.message).to_be_string()
assert_contains(mock_vim._last_notification.message, "Added 1 file")
assert_contains(mock_vim._last_notification.message, "1 failed")
expect(mock_vim._last_notification.level).to_be(mock_vim.log.levels.WARN)
end)
it("should handle all failures", function()
init_module.state = { server = {
broadcast = function()
return false
end,
} }
local files = {
"/Users/test/project/file1.lua",
"/Users/test/project/file2.lua",
}
local success_count, total_count = init_module._add_paths_to_claude(files, { show_summary = true })
expect(total_count).to_be(2)
expect(success_count).to_be(0)
-- Check that a notification was generated with ERROR level
expect(mock_vim._last_notification).to_be_table()
expect(mock_vim._last_notification.level).to_be(mock_vim.log.levels.ERROR)
end)
end)
describe("special path edge cases", function()
it("should handle paths with spaces", function()
mock_vim.fn.filereadable = function(path)
return path == "/Users/test/project/file with spaces.lua" and 1 or 0
end
local success, result = pcall(function()
return init_module._format_path_for_at_mention("/Users/test/project/file with spaces.lua")
end)
expect(success).to_be_true()
expect(result).to_be("file with spaces.lua")
end)
it("should handle paths with special characters", function()
mock_vim.fn.filereadable = function(path)
return path == "/Users/test/project/file-name_test.lua" and 1 or 0
end
local success, result = pcall(function()
return init_module._format_path_for_at_mention("/Users/test/project/file-name_test.lua")
end)
expect(success).to_be_true()
expect(result).to_be("file-name_test.lua")
end)
it("should handle very long paths", function()
local long_path = "/Users/test/project/" .. string.rep("very_long_directory_name/", 10) .. "file.lua"
mock_vim.fn.filereadable = function(path)
return path == long_path and 1 or 0
end
local success, result = pcall(function()
return init_module._format_path_for_at_mention(long_path)
end)
expect(success).to_be_true()
expect(result).to_be_string()
assert_contains(result, "file.lua")
end)
end)
describe("custom base_cwd parameter", function()
it("should use custom base_cwd when provided", function()
mock_vim.fn.filereadable = function(path)
return path == "/git/repo/src/main.lua" and 1 or 0
end
-- Default behavior uses vim.fn.getcwd() which returns /Users/test/project
local result_default = init_module._format_path_for_at_mention("/git/repo/src/main.lua")
expect(result_default).to_be("/git/repo/src/main.lua") -- Not relative to /Users/test/project
-- With custom base_cwd, should be relative to /git/repo
local result_custom = init_module._format_path_for_at_mention("/git/repo/src/main.lua", "/git/repo")
expect(result_custom).to_be("src/main.lua")
end)
it("should use custom base_cwd for directories", function()
mock_vim.fn.isdirectory = function(path)
return path == "/git/repo/src" and 1 or 0
end
mock_vim.fn.filereadable = function()
return 0
end
-- With custom base_cwd, directory should be relative
local result_custom = init_module._format_path_for_at_mention("/git/repo/src", "/git/repo")
expect(result_custom).to_be("src/")
end)
it("should fall back to vim.fn.getcwd() when base_cwd is nil", function()
mock_vim.fn.filereadable = function(path)
return path == "/Users/test/project/config.lua" and 1 or 0
end
-- When base_cwd is nil, should use vim.fn.getcwd()
local result = init_module._format_path_for_at_mention("/Users/test/project/config.lua", nil)
expect(result).to_be("config.lua") -- Relative to /Users/test/project (getcwd)
end)
it("should handle root directory with custom base_cwd", function()
mock_vim.fn.isdirectory = function(path)
return path == "/git/repo" and 1 or 0
end
mock_vim.fn.filereadable = function()
return 0
end
-- Path is exactly the base_cwd
local result = init_module._format_path_for_at_mention("/git/repo", "/git/repo")
expect(result).to_be("./")
end)
end)
end)