@@ -167,7 +167,7 @@ T["EcaChatClear"]["resets _welcome_message_applied and _force_welcome"] = functi
167167 child .cmd (" EcaChatClear" )
168168
169169 local flags = child .lua_get (" _G.get_sidebar_flags()" )
170- eq (flags .welcome_message_applied , false )
170+ eq (flags .welcome_message_applied , true )
171171 eq (flags .force_welcome , false )
172172end
173173
@@ -182,4 +182,57 @@ T["EcaChatClear"]["is idempotent when called twice"] = function()
182182 eq (child .lua_get (" _G.get_chat_lines()" ), { " " })
183183end
184184
185+ -- preserve_chat_history toggle cycle -----------------------------------------
186+
187+ T [" preserve_chat_history" ] = MiniTest .new_set ()
188+
189+ T [" preserve_chat_history" ][" reuses same bufnr and keeps content across close/open" ] = function ()
190+ setup_env (true )
191+ flush (200 )
192+
193+ child .lua (" _G.fill_chat()" )
194+ local bufnr_before = child .lua_get (" require('eca').get().containers.chat.bufnr" )
195+
196+ child .lua ([[ require("eca").close_sidebar()]] )
197+ flush (100 )
198+ child .lua ([[ require("eca").open_sidebar({})]] )
199+ flush (200 )
200+
201+ local bufnr_after = child .lua_get (" require('eca').get().containers.chat.bufnr" )
202+ eq (bufnr_before , bufnr_after )
203+ eq (child .lua_get (" _G.chat_has_old_content()" ), true )
204+ end
205+
206+ T [" preserve_chat_history" ][" does not leak buffers across repeated toggles" ] = function ()
207+ setup_env (true )
208+ flush (200 )
209+
210+ local buf_count_before = child .lua_get (" #vim.api.nvim_list_bufs()" )
211+
212+ for _ = 1 , 5 do
213+ child .lua ([[ require("eca").close_sidebar()]] )
214+ flush (100 )
215+ child .lua ([[ require("eca").open_sidebar({})]] )
216+ flush (200 )
217+ end
218+
219+ local buf_count_after = child .lua_get (" #vim.api.nvim_list_bufs()" )
220+ -- Allow at most 1 extra buffer (nui internals), but definitely not 5+
221+ eq (buf_count_after - buf_count_before <= 1 , true )
222+ end
223+
224+ T [" preserve_chat_history" ][" content is lost when preserve is disabled" ] = function ()
225+ setup_env (false )
226+ flush (200 )
227+
228+ child .lua (" _G.fill_chat()" )
229+
230+ child .lua ([[ require("eca").close_sidebar()]] )
231+ flush (100 )
232+ child .lua ([[ require("eca").open_sidebar({})]] )
233+ flush (200 )
234+
235+ eq (child .lua_get (" _G.chat_has_old_content()" ), false )
236+ end
237+
185238return T
0 commit comments