@@ -3,7 +3,7 @@ local config = require("peekstack.config")
33local events = require (" peekstack.core.events" )
44local helpers = require (" tests.helpers" )
55
6- describe (" stack.toggle_visibility " , function ()
6+ describe (" stack.toggle " , function ()
77 before_each (function ()
88 stack ._reset ()
99 config .setup ({})
@@ -14,7 +14,7 @@ describe("stack.toggle_visibility", function()
1414 local s = stack .current_stack ()
1515 -- Restore visibility so windows can be closed normally
1616 if s .hidden then
17- stack .toggle_visibility ()
17+ stack .toggle ()
1818 end
1919 for i = # s .popups , 1 , - 1 do
2020 stack .close (s .popups [i ].id )
@@ -23,7 +23,7 @@ describe("stack.toggle_visibility", function()
2323 end )
2424
2525 it (" returns false on empty stack" , function ()
26- assert .is_false (stack .toggle_visibility ())
26+ assert .is_false (stack .toggle ())
2727 assert .is_false (stack .is_hidden ())
2828 end )
2929
@@ -34,7 +34,7 @@ describe("stack.toggle_visibility", function()
3434 assert .is_not_nil (m1 )
3535 assert .is_not_nil (m2 )
3636
37- assert .is_true (stack .toggle_visibility ())
37+ assert .is_true (stack .toggle ())
3838 assert .is_true (stack .is_hidden ())
3939
4040 -- Windows should be gone but popups remain in the stack
@@ -53,11 +53,11 @@ describe("stack.toggle_visibility", function()
5353 assert .is_not_nil (m2 )
5454
5555 -- Hide
56- stack .toggle_visibility ()
56+ stack .toggle ()
5757 assert .is_true (stack .is_hidden ())
5858
5959 -- Show
60- assert .is_true (stack .toggle_visibility ())
60+ assert .is_true (stack .toggle ())
6161 assert .is_false (stack .is_hidden ())
6262
6363 local s = stack .current_stack ()
@@ -76,8 +76,8 @@ describe("stack.toggle_visibility", function()
7676 local id1 = m1 .id
7777 local id2 = m2 .id
7878
79- stack .toggle_visibility ()
80- stack .toggle_visibility ()
79+ stack .toggle ()
80+ stack .toggle ()
8181
8282 local s = stack .current_stack ()
8383 assert .equals (id1 , s .popups [1 ].id )
@@ -88,7 +88,7 @@ describe("stack.toggle_visibility", function()
8888 local loc = helpers .make_location ()
8989 stack .push (loc )
9090
91- stack .toggle_visibility ()
91+ stack .toggle ()
9292 assert .is_true (stack .is_hidden ())
9393
9494 -- Pushing should auto-show
@@ -109,7 +109,7 @@ describe("stack.toggle_visibility", function()
109109 stack .push (loc )
110110 stack .push (loc )
111111
112- stack .toggle_visibility ()
112+ stack .toggle ()
113113 assert .is_true (stack .is_hidden ())
114114
115115 stack .close_all ()
@@ -124,7 +124,7 @@ describe("stack.toggle_visibility", function()
124124 stack .push (loc )
125125 stack .push (loc )
126126
127- stack .toggle_visibility ()
127+ stack .toggle ()
128128 assert .is_true (stack .is_hidden ())
129129
130130 -- reflow_all should skip hidden popups (winid=nil) safely
@@ -140,13 +140,54 @@ describe("stack.toggle_visibility", function()
140140 end
141141 end )
142142
143+ it (" close by id works after hide/show cycle" , function ()
144+ local loc = helpers .make_location ()
145+ local m1 = stack .push (loc )
146+ local m2 = stack .push (loc )
147+
148+ -- Hide then show
149+ stack .toggle ()
150+ stack .toggle ()
151+
152+ -- close should find the popup by its original id
153+ assert .is_true (stack .close (m2 .id ))
154+ assert .is_true (stack .close (m1 .id ))
155+
156+ local s = stack .current_stack ()
157+ assert .equals (0 , # s .popups )
158+ end )
159+
160+ it (" keymaps reference correct popup id after hide/show cycle" , function ()
161+ local loc = helpers .make_location ()
162+ local m1 = stack .push (loc )
163+
164+ local original_id = m1 .id
165+
166+ -- Hide then show (recreates buffer + keymaps)
167+ stack .toggle ()
168+ stack .toggle ()
169+
170+ local s = stack .current_stack ()
171+ local restored = s .popups [1 ]
172+ assert .equals (original_id , restored .id )
173+
174+ -- The close keymap should work via the buffer variable
175+ assert .equals (original_id , vim .b [restored .bufnr ].peekstack_popup_id )
176+
177+ -- Simulate what the close keymap does: resolve + close by popup_id
178+ local found = stack .find_by_id (original_id )
179+ assert .is_not_nil (found )
180+ assert .equals (restored .bufnr , found .bufnr )
181+ assert .is_true (stack .close (original_id ))
182+ end )
183+
143184 it (" does not leak popups to history when hiding" , function ()
144185 local loc = helpers .make_location ()
145186 stack .push (loc )
146187 stack .push (loc )
147188
148189 local history_before = # stack .history_list ()
149- stack .toggle_visibility ()
190+ stack .toggle ()
150191 local history_after = # stack .history_list ()
151192
152193 assert .equals (history_before , history_after )
0 commit comments