-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathdiff_inline_spec.lua
More file actions
504 lines (436 loc) · 17.1 KB
/
Copy pathdiff_inline_spec.lua
File metadata and controls
504 lines (436 loc) · 17.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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
-- luacheck: globals expect assert_contains
require("tests.busted_setup")
describe("Inline diff module", function()
local diff_inline
before_each(function()
-- Reset module cache
package.loaded["claudecode.diff_inline"] = nil
package.loaded["claudecode.diff"] = nil
package.loaded["claudecode.logger"] = nil
package.loaded["claudecode.config"] = nil
package.loaded["claudecode.terminal"] = nil
-- Stub logger
package.loaded["claudecode.logger"] = {
debug = function() end,
info = function() end,
warn = function() end,
error = function() end,
}
-- Stub terminal
package.loaded["claudecode.terminal"] = {
get_active_terminal_bufnr = function()
return nil
end,
ensure_visible = function() end,
}
diff_inline = require("claudecode.diff_inline")
end)
describe("compute_inline_diff", function()
it("should return empty arrays for identical content", function()
local lines, types = diff_inline.compute_inline_diff("hello\n", "hello\n")
assert.are.equal(1, #lines)
assert.are.equal(1, #types)
assert.are.equal("hello", lines[1])
assert.are.equal("unchanged", types[1])
end)
it("should handle pure addition (empty old)", function()
local lines, types = diff_inline.compute_inline_diff("", "line1\nline2\n")
assert.are.equal(2, #lines)
assert.are.equal(2, #types)
assert.are.equal("added", types[1])
assert.are.equal("added", types[2])
end)
it("should handle pure deletion (empty new)", function()
local lines, types = diff_inline.compute_inline_diff("line1\nline2\n", "")
assert.are.equal(2, #lines)
assert.are.equal(2, #types)
assert.are.equal("deleted", types[1])
assert.are.equal("deleted", types[2])
end)
it("should handle mixed changes", function()
local old = "line1\nline2\nline3\n"
local new = "line1\nmodified\nline3\n"
local lines, types = diff_inline.compute_inline_diff(old, new)
-- First line unchanged
assert.are.equal("line1", lines[1])
assert.are.equal("unchanged", types[1])
-- Find the deleted and added lines
local has_deleted = false
local has_added = false
for i, t in ipairs(types) do
if t == "deleted" then
assert.are.equal("line2", lines[i])
has_deleted = true
elseif t == "added" then
assert.are.equal("modified", lines[i])
has_added = true
end
end
assert.is_true(has_deleted)
assert.is_true(has_added)
-- Last line unchanged
assert.are.equal("line3", lines[#lines])
assert.are.equal("unchanged", types[#types])
end)
it("should handle new file (empty old text)", function()
local lines, types = diff_inline.compute_inline_diff("", "new content\n")
assert.are.equal(1, #lines)
assert.are.equal("new content", lines[1])
assert.are.equal("added", types[1])
end)
it("should handle nil old text", function()
local lines, types = diff_inline.compute_inline_diff(nil, "content\n")
assert.are.equal(1, #lines)
assert.are.equal("added", types[1])
end)
it("should handle content without trailing newline", function()
local _, types = diff_inline.compute_inline_diff("old", "new")
-- Should have at least one deleted and one added
local has_deleted = false
local has_added = false
for _, t in ipairs(types) do
if t == "deleted" then
has_deleted = true
end
if t == "added" then
has_added = true
end
end
assert.is_true(has_deleted)
assert.is_true(has_added)
end)
end)
describe("extract_new_content", function()
it("should keep unchanged and added lines, strip deleted", function()
local lines = { "unchanged1", "deleted1", "added1", "unchanged2" }
local types = { "unchanged", "deleted", "added", "unchanged" }
local result = diff_inline.extract_new_content(lines, types)
assert.are.equal("unchanged1\nadded1\nunchanged2", result)
end)
it("should handle all-unchanged content", function()
local lines = { "line1", "line2" }
local types = { "unchanged", "unchanged" }
local result = diff_inline.extract_new_content(lines, types)
assert.are.equal("line1\nline2", result)
end)
it("should handle all-added content", function()
local lines = { "new1", "new2" }
local types = { "added", "added" }
local result = diff_inline.extract_new_content(lines, types)
assert.are.equal("new1\nnew2", result)
end)
it("should handle all-deleted content", function()
local lines = { "old1", "old2" }
local types = { "deleted", "deleted" }
local result = diff_inline.extract_new_content(lines, types)
assert.are.equal("", result)
end)
it("should handle empty input", function()
local result = diff_inline.extract_new_content({}, {})
assert.are.equal("", result)
end)
end)
describe("render_diff_buffer", function()
it("should set buffer lines and extmarks", function()
local buf = vim.api.nvim_create_buf(false, true)
local lines = { "unchanged", "deleted_line", "added_line" }
local types = { "unchanged", "deleted", "added" }
diff_inline.render_diff_buffer(buf, lines, types)
-- Check lines were set
local buf_lines = vim._buffers[buf].lines
assert.are.equal(3, #buf_lines)
assert.are.equal("unchanged", buf_lines[1])
assert.are.equal("deleted_line", buf_lines[2])
assert.are.equal("added_line", buf_lines[3])
-- Check extmarks were created (2: one for deleted, one for added)
local extmarks = vim._buffers[buf].extmarks or {}
assert.are.equal(2, #extmarks)
end)
it("should not create extmarks for unchanged lines", function()
local buf = vim.api.nvim_create_buf(false, true)
local lines = { "line1", "line2" }
local types = { "unchanged", "unchanged" }
diff_inline.render_diff_buffer(buf, lines, types)
local extmarks = vim._buffers[buf].extmarks or {}
assert.are.equal(0, #extmarks)
end)
end)
describe("MCP response format", function()
it("should produce correct FILE_SAVED response", function()
-- Simulate the resolution flow
local callback_result = nil
local diff_data = {
lines = { "unchanged1", "deleted1", "added1" },
line_types = { "unchanged", "deleted", "added" },
new_file_contents = "unchanged1\nadded1\n",
status = "pending",
resolution_callback = function(result)
callback_result = result
end,
}
diff_inline.resolve_inline_as_saved("test_tab", diff_data)
assert.is_not_nil(callback_result)
assert.are.equal("saved", diff_data.status)
assert.are.equal(2, #callback_result.content)
assert.are.equal("text", callback_result.content[1].type)
assert.are.equal("FILE_SAVED", callback_result.content[1].text)
assert.are.equal("text", callback_result.content[2].type)
-- Content should be unchanged1\nadded1 with trailing newline preserved
assert.are.equal("unchanged1\nadded1\n", callback_result.content[2].text)
end)
it("should produce correct DIFF_REJECTED response", function()
local callback_result = nil
local diff_data = {
status = "pending",
resolution_callback = function(result)
callback_result = result
end,
}
diff_inline.resolve_inline_as_rejected("test_tab", diff_data)
assert.is_not_nil(callback_result)
assert.are.equal("rejected", diff_data.status)
assert.are.equal(2, #callback_result.content)
assert.are.equal("DIFF_REJECTED", callback_result.content[1].text)
assert.are.equal("test_tab", callback_result.content[2].text)
end)
it("should preserve trailing newline in saved content", function()
local callback_result = nil
local diff_data = {
lines = { "line1" },
line_types = { "added" },
new_file_contents = "line1\n", -- Has trailing newline
status = "pending",
resolution_callback = function(result)
callback_result = result
end,
}
diff_inline.resolve_inline_as_saved("test_tab", diff_data)
assert.are.equal("line1\n", callback_result.content[2].text)
end)
it("should not add trailing newline when original had none", function()
local callback_result = nil
local diff_data = {
lines = { "line1" },
line_types = { "added" },
new_file_contents = "line1", -- No trailing newline
status = "pending",
resolution_callback = function(result)
callback_result = result
end,
}
diff_inline.resolve_inline_as_saved("test_tab", diff_data)
assert.are.equal("line1", callback_result.content[2].text)
end)
it("should not resolve already-resolved diff", function()
local callback_count = 0
local diff_data = {
lines = { "line1" },
line_types = { "added" },
new_file_contents = "line1\n",
status = "saved", -- Already resolved
resolution_callback = function()
callback_count = callback_count + 1
end,
}
-- resolve_inline_as_saved doesn't check status (the dispatch in diff.lua does)
-- but resolve_inline_as_rejected is called from _resolve_diff_as_rejected which checks
-- So this test validates the upstream check behavior
assert.are.equal("saved", diff_data.status)
end)
end)
describe("new-tab inline placement", function()
it("should pick editor window from current tab, not global search", function()
-- Set up two tabs: tab 1 (original) and tab 2 (new tab for diff)
local old_tab = 1
local new_tab = 2
vim._tabs[new_tab] = true
-- Tab 1: one editor window
local old_win = vim._next_winid
vim._next_winid = vim._next_winid + 1
vim._windows[old_win] = { buf = vim.api.nvim_create_buf(false, true), width = 120 }
vim._win_tab[old_win] = old_tab
vim._tab_windows[old_tab] = { old_win }
-- Tab 2: terminal window + editor window
local term_win = vim._next_winid
vim._next_winid = vim._next_winid + 1
vim._windows[term_win] = { buf = vim.api.nvim_create_buf(false, true), width = 40 }
vim._win_tab[term_win] = new_tab
local new_tab_editor_win = vim._next_winid
vim._next_winid = vim._next_winid + 1
vim._windows[new_tab_editor_win] = { buf = vim.api.nvim_create_buf(false, true), width = 80 }
vim._win_tab[new_tab_editor_win] = new_tab
vim._tab_windows[new_tab] = { term_win, new_tab_editor_win }
vim._current_tabpage = new_tab
-- nvim_tabpage_list_wins(0) should return windows from new_tab
local tab_wins = vim.api.nvim_tabpage_list_wins(0)
assert.are.equal(2, #tab_wins)
-- Simulate the inline diff's window selection logic (from diff_inline.lua:233-245)
local terminal_win_in_new_tab = term_win
local editor_win = nil
for _, w in ipairs(tab_wins) do
if w ~= terminal_win_in_new_tab then
editor_win = w
break
end
end
-- Should pick the editor window from tab 2, NOT tab 1's window
assert.are.equal(new_tab_editor_win, editor_win)
assert.are_not.equal(old_win, editor_win)
end)
end)
describe("config validation", function()
it("should accept layout = 'unified'", function()
package.loaded["claudecode.config"] = nil
package.loaded["claudecode.terminal"] = nil
-- Stub terminal module with defaults
package.loaded["claudecode.terminal"] = {
defaults = {
split_side = "right",
split_width_percentage = 0.30,
provider = "auto",
show_native_term_exit_tip = true,
auto_close = true,
env = {},
snacks_win_opts = {},
},
get_active_terminal_bufnr = function()
return nil
end,
ensure_visible = function() end,
}
local config = require("claudecode.config")
local applied = config.apply({ diff_opts = { layout = "unified" } })
assert.are.equal("unified", applied.diff_opts.layout)
end)
it("should reject invalid layout values", function()
package.loaded["claudecode.config"] = nil
package.loaded["claudecode.terminal"] = nil
package.loaded["claudecode.terminal"] = {
defaults = {
split_side = "right",
split_width_percentage = 0.30,
provider = "auto",
show_native_term_exit_tip = true,
auto_close = true,
env = {},
snacks_win_opts = {},
},
get_active_terminal_bufnr = function()
return nil
end,
ensure_visible = function() end,
}
local config = require("claudecode.config")
local success, err = pcall(function()
config.apply({ diff_opts = { layout = "invalid" } })
end)
assert.is_false(success)
assert_contains(tostring(err), "'unified'")
end)
it("should reject the former layout = 'inline' (renamed to 'unified')", function()
package.loaded["claudecode.config"] = nil
package.loaded["claudecode.terminal"] = nil
package.loaded["claudecode.terminal"] = {
defaults = {
split_side = "right",
split_width_percentage = 0.30,
provider = "auto",
show_native_term_exit_tip = true,
auto_close = true,
env = {},
snacks_win_opts = {},
},
get_active_terminal_bufnr = function()
return nil
end,
ensure_visible = function() end,
}
local config = require("claudecode.config")
local success, err = pcall(function()
config.apply({ diff_opts = { layout = "inline" } })
end)
assert.is_false(success)
assert_contains(tostring(err), "'unified'")
end)
end)
describe("cleanup_inline_diff", function()
it("should delete autocmds", function()
local deleted_ids = {}
local original_del = vim.api.nvim_del_autocmd
vim.api.nvim_del_autocmd = function(id)
table.insert(deleted_ids, id)
end
local diff_data = {
autocmd_ids = { 10, 20, 30 },
created_new_tab = false,
new_window = nil,
new_buffer = nil,
}
diff_inline.cleanup_inline_diff("test_tab", diff_data)
assert.are.equal(3, #deleted_ids)
vim.api.nvim_del_autocmd = original_del
end)
it("should close diff window in new-tab mode", function()
-- Simulate a new tab (tab 2) with a terminal window and an editor window
local term_buf = vim.api.nvim_create_buf(false, true)
local editor_buf = vim.api.nvim_create_buf(false, true)
local diff_buf = vim.api.nvim_create_buf(false, true)
-- Create tab 2 with two windows: terminal and editor
local new_tab = 2
vim._tabs[new_tab] = true
vim._current_tabpage = new_tab
local term_win = vim._next_winid
vim._next_winid = vim._next_winid + 1
vim._windows[term_win] = { buf = term_buf, width = 40 }
vim._win_tab[term_win] = new_tab
local editor_win = vim._next_winid
vim._next_winid = vim._next_winid + 1
vim._windows[editor_win] = { buf = editor_buf, width = 80 }
vim._win_tab[editor_win] = new_tab
local diff_win = vim._next_winid
vim._next_winid = vim._next_winid + 1
vim._windows[diff_win] = { buf = diff_buf, width = 80 }
vim._win_tab[diff_win] = new_tab
vim._tab_windows[new_tab] = { term_win, editor_win, diff_win }
-- Also have a window in tab 1 (original tab) to catch wrong-tab bugs
local old_tab = 1
local old_win = vim._next_winid
vim._next_winid = vim._next_winid + 1
vim._windows[old_win] = { buf = vim.api.nvim_create_buf(false, true), width = 120 }
vim._win_tab[old_win] = old_tab
vim._tab_windows[old_tab] = { old_win }
local diff_data = {
autocmd_ids = {},
created_new_tab = true,
new_tab_number = new_tab,
original_tab_number = old_tab,
new_window = diff_win,
new_buffer = diff_buf,
}
diff_inline.cleanup_inline_diff("test_tab", diff_data)
-- Diff window should be closed, original tab window should remain
assert.is_nil(vim._windows[diff_win])
assert.is_not_nil(vim._windows[old_win])
end)
it("should close diff window when not in new tab", function()
-- Create a window for the diff
local buf = vim.api.nvim_create_buf(false, true)
local winid = vim._next_winid
vim._next_winid = vim._next_winid + 1
vim._windows[winid] = { buf = buf, width = 80 }
vim._win_tab[winid] = vim._current_tabpage
local tab_wins = vim._tab_windows[vim._current_tabpage] or {}
table.insert(tab_wins, winid)
vim._tab_windows[vim._current_tabpage] = tab_wins
local diff_data = {
autocmd_ids = {},
created_new_tab = false,
new_window = winid,
new_buffer = buf,
}
diff_inline.cleanup_inline_diff("test_tab", diff_data)
-- Window should be closed
assert.is_nil(vim._windows[winid])
end)
end)
end)