Skip to content

Commit 89cdac8

Browse files
author
Bassam Data
committed
tests: updated tests for the history feature
1 parent 711b37a commit 89cdac8

File tree

1 file changed

+66
-186
lines changed

1 file changed

+66
-186
lines changed

tests/nes/test_ui_preview.lua

Lines changed: 66 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -312,120 +312,72 @@ T["ui_preview"]["cursor_aware_suggestion_clearing"] = function()
312312
ref(child.get_screenshot())
313313
end
314314

315-
T["ui_preview"]["suggestion_preserves_on_movement_towards"] = function()
316-
set_content("line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8")
317-
ref(child.get_screenshot())
318-
319-
-- Position cursor at line 8
320-
child.cmd("normal! gg7j")
321-
322-
-- Create a suggestion at line 3
323-
local edit = {
324-
range = {
325-
start = { line = 2, character = 0 },
326-
["end"] = { line = 2, character = 0 },
327-
},
328-
newText = "suggested text ",
329-
}
330-
331-
-- Display suggestion
332-
child.g.test_edit = edit
333-
child.lua_func(function()
334-
local ns_id = vim.api.nvim_create_namespace("nes_test")
335-
local edits = { vim.g.test_edit }
336-
require("copilot-lsp.nes.ui")._display_next_suggestion(0, ns_id, edits)
337-
end)
338-
ref(child.get_screenshot())
339-
340-
-- Test: Moving cursor towards the suggestion (even outside buffer zone) shouldn't clear it
341-
child.cmd("normal! 4k") -- Move to line 4, moving towards the suggestion
342-
child.lua_func(function()
343-
vim.uv.sleep(500)
344-
end)
345-
346-
-- Verify suggestion still exists
347-
local suggestion_exists = child.lua_func(function()
348-
return vim.b[0].nes_state ~= nil
349-
end)
350-
eq(suggestion_exists, true)
351-
ref(child.get_screenshot())
352-
end
353-
354315
T["ui_preview"]["suggestion_history_basic_cycle"] = function()
355316
set_content("line1\nline2\nline3")
356-
357-
-- Create first suggestion
317+
-- Create first suggestion and display it
358318
local edit1 = {
359-
range = {
360-
start = { line = 1, character = 0 },
361-
["end"] = { line = 1, character = 0 },
362-
},
319+
range = { start = { line = 1, character = 0 }, ["end"] = { line = 1, character = 0 } },
363320
newText = "-- first suggestion",
364321
}
365-
366-
-- Display first suggestion
367322
child.g.test_edit = edit1
368323
child.lua_func(function()
369324
local ns_id = vim.api.nvim_create_namespace("nes_test")
370-
local edits = { vim.g.test_edit }
371-
require("copilot-lsp.nes.ui")._display_next_suggestion(0, ns_id, edits)
325+
local bufnr = vim.api.nvim_get_current_buf()
326+
require("copilot-lsp.nes.ui")._display_next_suggestion(bufnr, ns_id, { vim.g.test_edit })
372327
vim.uv.sleep(300)
373328
end)
374329

375-
-- Create and display second suggestion (should store first in history)
330+
-- Create and display second suggestion
376331
local edit2 = {
377-
range = {
378-
start = { line = 2, character = 0 },
379-
["end"] = { line = 2, character = 0 },
380-
},
332+
range = { start = { line = 2, character = 0 }, ["end"] = { line = 2, character = 0 } },
381333
newText = "-- second suggestion",
382334
}
383-
384335
child.g.test_edit = edit2
385336
child.lua_func(function()
386337
local ns_id = vim.api.nvim_create_namespace("nes_test")
387-
local edits = { vim.g.test_edit }
388-
require("copilot-lsp.nes.ui")._display_next_suggestion(0, ns_id, edits)
338+
local bufnr = vim.api.nvim_get_current_buf()
339+
require("copilot-lsp.nes.ui")._display_next_suggestion(bufnr, ns_id, { vim.g.test_edit })
389340
vim.uv.sleep(300)
390341
end)
391342

392-
-- Clear current suggestion (second should now be in history too)
393343
child.lua_func(function()
394344
local ns_id = vim.api.nvim_create_namespace("nes_test")
395-
require("copilot-lsp.nes.ui").clear_suggestion(0, ns_id)
345+
local bufnr = vim.api.nvim_get_current_buf()
346+
require("copilot-lsp.nes.ui").clear_suggestion(bufnr, ns_id)
396347
vim.uv.sleep(300)
397348
end)
398349

399-
-- First restore should show second suggestion (most recent)
350+
local has_history = child.lua_func(function()
351+
local bufnr = vim.api.nvim_get_current_buf()
352+
return require("copilot-lsp.nes.ui").has_history(bufnr)
353+
end)
354+
eq(has_history, true)
355+
356+
-- Test cycling through suggestions
400357
local restored1 = child.lua_func(function()
401358
local ns_id = vim.api.nvim_create_namespace("nes_test")
402-
local result = require("copilot-lsp.nes.ui").restore_suggestion(0, ns_id)
359+
local bufnr = vim.api.nvim_get_current_buf()
360+
local result = require("copilot-lsp.nes.ui").restore_suggestion(bufnr, ns_id)
403361
vim.uv.sleep(300)
404362
return result
405363
end)
406364
eq(restored1, true)
407365

408-
-- Verify we can check history content
409-
local history_size = child.lua_func(function()
410-
return #(vim.b[0].copilotlsp_nes_history or {})
411-
end)
412-
eq(history_size, 2)
413-
414-
-- Second restore should show first suggestion
415366
local restored2 = child.lua_func(function()
416367
local ns_id = vim.api.nvim_create_namespace("nes_test")
417-
local restored = require("copilot-lsp.nes.ui").restore_suggestion(0, ns_id)
368+
local bufnr = vim.api.nvim_get_current_buf()
369+
local result = require("copilot-lsp.nes.ui").restore_suggestion(bufnr, ns_id)
418370
vim.uv.sleep(300)
419-
return restored
371+
return result
420372
end)
421373
eq(restored2, true)
422374

423-
-- Third restore should cycle back to second suggestion
424375
local restored3 = child.lua_func(function()
425376
local ns_id = vim.api.nvim_create_namespace("nes_test")
426-
local restored = require("copilot-lsp.nes.ui").restore_suggestion(0, ns_id)
377+
local bufnr = vim.api.nvim_get_current_buf()
378+
local result = require("copilot-lsp.nes.ui").restore_suggestion(bufnr, ns_id)
427379
vim.uv.sleep(300)
428-
return restored
380+
return result
429381
end)
430382
eq(restored3, true)
431383
end
@@ -452,37 +404,39 @@ T["ui_preview"]["suggestion_history_max_two_items"] = function()
452404
child.g.test_edit = edit
453405
child.lua_func(function()
454406
local ns_id = vim.api.nvim_create_namespace("nes_test")
455-
local edits = { vim.g.test_edit }
456-
require("copilot-lsp.nes.ui")._display_next_suggestion(0, ns_id, edits)
407+
local bufnr = vim.api.nvim_get_current_buf()
408+
require("copilot-lsp.nes.ui")._display_next_suggestion(bufnr, ns_id, { vim.g.test_edit })
457409
vim.uv.sleep(300)
458410
end)
459411
end
460412

461-
-- Clear current suggestion
462413
child.lua_func(function()
463414
local ns_id = vim.api.nvim_create_namespace("nes_test")
464-
require("copilot-lsp.nes.ui").clear_suggestion(0, ns_id)
415+
local bufnr = vim.api.nvim_get_current_buf()
416+
require("copilot-lsp.nes.ui").clear_suggestion(bufnr, ns_id)
465417
vim.uv.sleep(300)
466418
end)
467419

468-
-- Verify history only keeps 2 most recent
469-
local history_size = child.lua_func(function()
470-
return #(vim.b[0].copilotlsp_nes_history or {})
420+
local has_history = child.lua_func(function()
421+
local bufnr = vim.api.nvim_get_current_buf()
422+
return require("copilot-lsp.nes.ui").has_history(bufnr)
471423
end)
472-
eq(history_size, 2)
424+
eq(has_history, true)
473425

474-
-- Verify we can only cycle between 2 suggestions
426+
-- Verify we can cycle through suggestions (should only have 2 most recent)
475427
local restore_results = {}
476-
for _ = 1, 4 do -- Try 4 restores to test cycling
428+
for i = 1, 4 do -- Try 4 restores to test cycling
477429
local restored = child.lua_func(function()
478430
local ns_id = vim.api.nvim_create_namespace("nes_test")
479-
return require("copilot-lsp.nes.ui").restore_suggestion(0, ns_id)
431+
local bufnr = vim.api.nvim_get_current_buf()
432+
local result = require("copilot-lsp.nes.ui").restore_suggestion(bufnr, ns_id)
433+
vim.uv.sleep(300)
434+
return result
480435
end)
481436
table.insert(restore_results, restored)
482-
vim.uv.sleep(300)
483437
end
484438

485-
-- All restores should succeed
439+
-- All restores should succeed (cycling between 2 items)
486440
for _, result in ipairs(restore_results) do
487441
eq(result, true)
488442
end
@@ -491,146 +445,72 @@ end
491445
T["ui_preview"]["suggestion_history_invalid_after_text_changes"] = function()
492446
set_content("line1\nline2\nline3\nline4\nline5")
493447

494-
-- Create suggestion on line 4 (0-indexed)
495448
local edit = {
496-
range = {
497-
start = { line = 4, character = 0 },
498-
["end"] = { line = 4, character = 0 },
499-
},
449+
range = { start = { line = 4, character = 0 }, ["end"] = { line = 4, character = 0 } },
500450
newText = "-- comment on line 5",
501451
}
502452

503453
child.g.test_edit = edit
504454
child.lua_func(function()
505455
local ns_id = vim.api.nvim_create_namespace("nes_test")
506-
local edits = { vim.g.test_edit }
507-
require("copilot-lsp.nes.ui")._display_next_suggestion(0, ns_id, edits)
456+
local bufnr = vim.api.nvim_get_current_buf()
457+
require("copilot-lsp.nes.ui")._display_next_suggestion(bufnr, ns_id, { vim.g.test_edit })
508458
vim.uv.sleep(300)
509459
end)
510460

511461
-- Clear suggestion to store in history
512462
child.lua_func(function()
513463
local ns_id = vim.api.nvim_create_namespace("nes_test")
514-
require("copilot-lsp.nes.ui").clear_suggestion(0, ns_id)
464+
local bufnr = vim.api.nvim_get_current_buf()
465+
require("copilot-lsp.nes.ui").clear_suggestion(bufnr, ns_id)
515466
vim.uv.sleep(300)
516467
end)
517468

518-
-- Verify history exists
519-
local history_size_before = child.lua_func(function()
520-
return #(vim.b[0].copilotlsp_nes_history or {})
469+
-- Verify history exists before deletion
470+
local has_history_before = child.lua_func(function()
471+
local bufnr = vim.api.nvim_get_current_buf()
472+
return require("copilot-lsp.nes.ui").has_history(bufnr)
521473
end)
522-
eq(history_size_before, 1)
474+
eq(has_history_before, true)
523475

524-
-- Delete lines to make history invalid (keep only first 3 lines)
476+
-- Delete lines to make history invalid
525477
child.api.nvim_buf_set_lines(0, 3, -1, false, {})
478+
child.lua_func(function()
479+
vim.uv.sleep(300)
480+
end)
526481

527482
-- Try to restore (should fail and clear history)
528483
local restored = child.lua_func(function()
529484
local ns_id = vim.api.nvim_create_namespace("nes_test")
530-
local result = require("copilot-lsp.nes.ui").restore_suggestion(0, ns_id)
485+
local bufnr = vim.api.nvim_get_current_buf()
486+
local result = require("copilot-lsp.nes.ui").restore_suggestion(bufnr, ns_id)
531487
vim.uv.sleep(300)
532488
return result
533489
end)
534490
eq(restored, false)
535491

536-
-- Verify history was cleared
537-
local history_size_after = child.lua_func(function()
538-
return #(vim.b[0].copilotlsp_nes_history or {})
539-
end)
540-
eq(history_size_after, 0)
541-
end
542-
543-
T["ui_preview"]["suggestion_history_restore_index_reset"] = function()
544-
set_content("line1\nline2\nline3")
545-
-- Create and display two suggestions to build history
546-
local edit1 = {
547-
range = {
548-
start = { line = 1, character = 0 },
549-
["end"] = { line = 1, character = 0 },
550-
},
551-
newText = "-- first",
552-
}
553-
local edit2 = {
554-
range = {
555-
start = { line = 2, character = 0 },
556-
["end"] = { line = 2, character = 0 },
557-
},
558-
newText = "-- second",
559-
}
560-
561-
-- Display first, then second (first goes to history)
562-
child.g.test_edit = edit1
563-
child.lua_func(function()
564-
local ns_id = vim.api.nvim_create_namespace("nes_test")
565-
require("copilot-lsp.nes.ui")._display_next_suggestion(0, ns_id, { vim.g.test_edit })
566-
vim.uv.sleep(300)
567-
end)
568-
569-
child.g.test_edit = edit2
570-
child.lua_func(function()
571-
local ns_id = vim.api.nvim_create_namespace("nes_test")
572-
require("copilot-lsp.nes.ui")._display_next_suggestion(0, ns_id, { vim.g.test_edit })
573-
vim.uv.sleep(300)
574-
end)
575-
576-
-- Clear to add second to history
577-
child.lua_func(function()
578-
local ns_id = vim.api.nvim_create_namespace("nes_test")
579-
require("copilot-lsp.nes.ui").clear_suggestion(0, ns_id)
580-
vim.uv.sleep(300)
581-
end)
582-
583-
-- Restore once (should show second, index becomes 1)
584-
child.lua_func(function()
585-
local ns_id = vim.api.nvim_create_namespace("nes_test")
586-
require("copilot-lsp.nes.ui").restore_suggestion(0, ns_id)
587-
vim.uv.sleep(300)
492+
local has_history_after = child.lua_func(function()
493+
local bufnr = vim.api.nvim_get_current_buf()
494+
return require("copilot-lsp.nes.ui").has_history(bufnr)
588495
end)
589-
590-
-- Get restore index
591-
local index_before = child.lua_func(function()
592-
return vim.b[0].copilotlsp_nes_restore_index or 0
593-
end)
594-
eq(index_before, 1)
595-
596-
-- Display new suggestion (should reset index)
597-
local edit3 = {
598-
range = {
599-
start = { line = 0, character = 0 },
600-
["end"] = { line = 0, character = 0 },
601-
},
602-
newText = "-- third",
603-
}
604-
605-
child.g.test_edit = edit3
606-
child.lua_func(function()
607-
local ns_id = vim.api.nvim_create_namespace("nes_test")
608-
require("copilot-lsp.nes.ui")._display_next_suggestion(0, ns_id, { vim.g.test_edit })
609-
vim.uv.sleep(300)
610-
end)
611-
612-
-- Verify index was reset
613-
local index_after = child.lua_func(function()
614-
return vim.b[0].copilotlsp_nes_restore_index or 0
615-
end)
616-
eq(index_after, 0)
496+
eq(has_history_after, false)
617497
end
618498

619499
T["ui_preview"]["suggestion_history_no_restore_when_empty"] = function()
620500
set_content("line1\nline2\nline3")
621-
622501
-- Try to restore when no history exists
623502
local restored = child.lua_func(function()
624503
local ns_id = vim.api.nvim_create_namespace("nes_test")
625-
return require("copilot-lsp.nes.ui").restore_suggestion(0, ns_id)
504+
local bufnr = vim.api.nvim_get_current_buf()
505+
return require("copilot-lsp.nes.ui").restore_suggestion(bufnr, ns_id)
626506
end)
627507
eq(restored, false)
628508

629-
-- Verify no history exists
630-
local history_size = child.lua_func(function()
631-
return #(vim.b[0].copilotlsp_nes_history or {})
509+
local has_history = child.lua_func(function()
510+
local bufnr = vim.api.nvim_get_current_buf()
511+
return require("copilot-lsp.nes.ui").has_history(bufnr)
632512
end)
633-
eq(history_size, 0)
513+
eq(has_history, false)
634514
end
635515

636516
return T

0 commit comments

Comments
 (0)