@@ -211,8 +211,8 @@ describe('ideSidebar', function()
211211 end )
212212 end )
213213
214- describe (' switchTerms method' , function ()
215- it (' should hide current terminal and show next one ' , function ()
214+ describe (' switchSidebar method' , function ()
215+ it (' should switch to the next active terminal ' , function ()
216216 -- Setup multiple commands to have multiple terminals to switch between
217217 ideSidebar .setup ({ cmds = { ' gemini' , ' qwen' }, port = 12345 })
218218
@@ -228,8 +228,8 @@ describe('ideSidebar', function()
228228 TERM_PROGRAM = ' vscode' ,
229229 }
230230
231- local geminiId = ideSidebar .createDeterministicId (' gemini' , geminiEnv )
232- local qwenId = ideSidebar .createDeterministicId (' qwen' , qwenEnv )
231+ local geminiId = ideSidebar .createDeterministicId (' gemini' , geminiEnv , 1 )
232+ local qwenId = ideSidebar .createDeterministicId (' qwen' , qwenEnv , 2 )
233233
234234 local geminiTerm = {
235235 hide = spy .new (function () end ),
@@ -247,22 +247,115 @@ describe('ideSidebar', function()
247247
248248 terminalMock .getActiveTerminals = function () return activeTerminals end
249249
250- -- Mock terminal.create to return the correct terminals
251- local originalCreate = terminalMock .create
252- terminalMock .create = spy .new (function (cmd , config )
253- if cmd == ' gemini' then
254- return geminiTerm
255- else
256- return qwenTerm
257- end
258- end )
250+ -- Spy on switchToCli to verify it's called with the correct parameters
251+ local switchToCliSpy = spy .new (function () end )
252+ local originalSwitchToCli = ideSidebar .switchToCli
253+ ideSidebar .switchToCli = switchToCliSpy
254+
255+ -- Call switchSidebar (with default 'next' direction)
256+ ideSidebar .switchSidebar ()
257+
258+ -- Check that switchToCli was called with the correct parameters
259+ assert .spy (switchToCliSpy ).was .called (1 )
260+ local args = switchToCliSpy .calls [1 ]
261+ assert .truthy (string.find (args .vals [1 ] or ' ' , ' sidebar' ))
262+
263+ -- Restore original function
264+ ideSidebar .switchToCli = originalSwitchToCli
265+ end )
266+
267+ it (
268+ ' should switch to the previous active terminal when direction is prev' ,
269+ function ()
270+ -- Setup multiple commands to have multiple terminals to switch between
271+ ideSidebar .setup ({ cmds = { ' gemini' , ' qwen' }, port = 12345 })
272+
273+ -- Create mock terminals
274+ local geminiEnv = {
275+ GEMINI_CLI_IDE_WORKSPACE_PATH = ' /test/dir' ,
276+ GEMINI_CLI_IDE_SERVER_PORT = ' 12345' ,
277+ TERM_PROGRAM = ' vscode' ,
278+ }
279+ local qwenEnv = {
280+ QWEN_CODE_IDE_WORKSPACE_PATH = ' /test/dir' ,
281+ QWEN_CODE_IDE_SERVER_PORT = ' 12345' ,
282+ TERM_PROGRAM = ' vscode' ,
283+ }
284+
285+ local geminiId =
286+ ideSidebar .createDeterministicId (' gemini' , geminiEnv , 1 )
287+ local qwenId = ideSidebar .createDeterministicId (' qwen' , qwenEnv , 2 )
288+
289+ local geminiTerm = {
290+ hide = spy .new (function () end ),
291+ show = spy .new (function () end ),
292+ }
293+
294+ local qwenTerm = {
295+ hide = spy .new (function () end ),
296+ show = spy .new (function () end ),
297+ }
298+
299+ local activeTerminals = {}
300+ activeTerminals [geminiId ] = geminiTerm
301+ activeTerminals [qwenId ] = qwenTerm
259302
260- -- Call switchTerms
261- ideSidebar .switchTerms ()
303+ terminalMock .getActiveTerminals = function () return activeTerminals end
262304
263- -- Check that hide was called on the first terminal and show on the second
264- assert .spy (geminiTerm .hide ).was .called (1 )
265- assert .spy (qwenTerm .show ).was .called (1 )
305+ -- Spy on switchToCli to verify it's called with the correct parameters
306+ local switchToCliSpy = spy .new (function () end )
307+ local originalSwitchToCli = ideSidebar .switchToCli
308+ ideSidebar .switchToCli = switchToCliSpy
309+
310+ -- Call switchSidebar with 'prev' direction
311+ ideSidebar .switchSidebar (' prev' )
312+
313+ -- Check that switchToCli was called with the correct parameters
314+ assert .spy (switchToCliSpy ).was .called (1 )
315+ local args = switchToCliSpy .calls [1 ]
316+ assert .truthy (string.find (args .vals [1 ] or ' ' , ' sidebar' ))
317+
318+ -- Restore original function
319+ ideSidebar .switchToCli = originalSwitchToCli
320+ end
321+ )
322+
323+ it (' should not switch if there are less than 2 active terminals' , function ()
324+ -- Setup multiple commands to have multiple terminals but only one active
325+ ideSidebar .setup ({ cmds = { ' gemini' , ' qwen' }, port = 12345 })
326+
327+ -- Create mock terminal for only one command
328+ local geminiEnv = {
329+ GEMINI_CLI_IDE_WORKSPACE_PATH = ' /test/dir' ,
330+ GEMINI_CLI_IDE_SERVER_PORT = ' 12345' ,
331+ TERM_PROGRAM = ' vscode' ,
332+ }
333+
334+ local geminiId = ideSidebar .createDeterministicId (' gemini' , geminiEnv )
335+
336+ local geminiTerm = {
337+ hide = spy .new (function () end ),
338+ show = spy .new (function () end ),
339+ }
340+
341+ local activeTerminals = {}
342+ activeTerminals [geminiId ] = geminiTerm
343+
344+ terminalMock .getActiveTerminals = function () return activeTerminals end
345+
346+ -- Spy on switchToCli to verify it's not called when less than 2 terminals are active
347+ local switchToCliSpy = spy .new (function () end )
348+ local originalSwitchToCli = ideSidebar .switchToCli
349+ ideSidebar .switchToCli = switchToCliSpy
350+
351+ -- Call switchSidebar
352+ ideSidebar .switchSidebar ()
353+
354+ -- Check that switchToCli was not called because only one terminal is active
355+ assert .spy (switchToCliSpy ).was_not_called ()
356+
357+ -- Restore original function
358+ ideSidebar .switchToCli = originalSwitchToCli
266359 end )
267360 end )
268361
0 commit comments