@@ -168,6 +168,11 @@ describe("registerCommands handlers", () => {
168168
169169 beforeEach ( ( ) => {
170170 vi . clearAllMocks ( )
171+
172+ // vi.clearAllMocks drops call history but keeps implementations, so a per-view
173+ // lookup installed by an earlier test would still be in place here. Clear it so
174+ // each test decides its own routing instead of inheriting the previous one.
175+ ; ( ClineProvider . getInstanceForView as Mock ) . mockReset ( )
171176 handlers = { }
172177
173178 mockOutputChannel = {
@@ -685,9 +690,7 @@ describe("registerCommands handlers", () => {
685690 it ( "focusInput logs the drop when a tab panel is tracked without a live tab instance" , async ( ) => {
686691 setPanel ( { } as vscode . WebviewView , "sidebar" )
687692 setPanel ( { } as vscode . WebviewPanel , "tab" )
688- // No live instance for the tracked panel: clear the return value the previous
689- // InTab test left on this mock so the tab path resolves no provider.
690- ; ( ClineProvider . getInstanceForView as Mock ) . mockReturnValue ( undefined )
693+ // No live instance for the tracked panel, so the tab path resolves no provider.
691694
692695 await handlers [ "zoo-code.focusInput" ] ( )
693696
@@ -699,6 +702,14 @@ describe("registerCommands handlers", () => {
699702 "focusInput: no live provider for the tracked tab panel; action dropped" ,
700703 )
701704 } )
705+
706+ // Determinism guard: vi.clearAllMocks keeps mockImplementation, so an implementation
707+ // installed by an earlier handler test would decide which provider a later test
708+ // resolves. The lookup must enter every test unset - this case fails on the residue of
709+ // the tests above it, not on any behaviour of its own.
710+ it ( "enters each test with no installed per-view provider lookup" , ( ) => {
711+ expect ( ( ClineProvider . getInstanceForView as Mock ) . getMockImplementation ( ) ) . toBeUndefined ( )
712+ } )
702713} )
703714
704715describe ( "openClineInNewTab" , ( ) => {
@@ -1130,6 +1141,52 @@ describe("openClineInNewTab", () => {
11301141 expect ( getPanel ( ) ) . toBeUndefined ( )
11311142 } )
11321143
1144+ it ( "keeps a hidden live tab as the tracked ref when the tracked panel closes" , async ( ) => {
1145+ // A Zoo Code tab that sits open behind another editor in its group is neither active nor
1146+ // visible. It is still a live tab, so the tracked ref must keep resolving the tab-scoped
1147+ // commands and focusInput at it - clearing the reference is for when nothing remains.
1148+ const panelA = Object . assign ( { } as vscode . WebviewPanel , {
1149+ marker : "panel-A" ,
1150+ webview : { postMessage : vi . fn ( ) } ,
1151+ onDidChangeViewState : vi . fn ( ) ,
1152+ onDidDispose : vi . fn ( ) ,
1153+ } )
1154+ ; ( vscode . window . createWebviewPanel as Mock ) . mockReturnValueOnce ( panelA )
1155+ await openClineInNewTab ( {
1156+ context : mockContext ,
1157+ outputChannel : mockOutputChannel ,
1158+ webviewFocusTracker : new WebviewFocusTracker ( ) ,
1159+ } )
1160+
1161+ const panelB = Object . assign ( { } as vscode . WebviewPanel , {
1162+ marker : "panel-B" ,
1163+ webview : { postMessage : vi . fn ( ) } ,
1164+ onDidChangeViewState : vi . fn ( ) ,
1165+ onDidDispose : vi . fn ( ) ,
1166+ } )
1167+ ; ( vscode . window . createWebviewPanel as Mock ) . mockReturnValueOnce ( panelB )
1168+ await openClineInNewTab ( {
1169+ context : mockContext ,
1170+ outputChannel : mockOutputChannel ,
1171+ webviewFocusTracker : new WebviewFocusTracker ( ) ,
1172+ } )
1173+ Object . assign ( panelB , { active : true , visible : true } )
1174+ ; ( panelB . onDidChangeViewState as Mock ) . mock . calls [ 0 ] ! [ 0 ] ! ( { webviewPanel : panelB } )
1175+ expect ( getPanel ( ) ) . toBe ( panelB )
1176+
1177+ // Closing the tracked panel leaves only the hidden tab: neither flag is set on it.
1178+ const disposeB = ( panelB . onDidDispose as Mock ) . mock . calls [ 0 ] ! [ 0 ] as ( ) => void
1179+ disposeB ( )
1180+ expect ( panelA . active ) . toBeUndefined ( )
1181+ expect ( panelA . visible ) . toBeUndefined ( )
1182+ expect ( getPanel ( ) ) . toBe ( panelA )
1183+
1184+ // Closing that tab too leaves nothing live, and only then is the ref cleared.
1185+ const disposeA = ( panelA . onDidDispose as Mock ) . mock . calls [ 0 ] ! [ 0 ] as ( ) => void
1186+ disposeA ( )
1187+ expect ( getPanel ( ) ) . toBeUndefined ( )
1188+ } )
1189+
11331190 it ( "prefers the active panel over a merely visible one when re-pointing the tracked tab ref" , async ( ) => {
11341191 // Panel A is created and tracked first and stays merely visible...
11351192 const panelA = Object . assign ( { } as vscode . WebviewPanel , {
@@ -1720,53 +1777,6 @@ describe("openClineInNewTab", () => {
17201777 __resetLiveTabPanelsForTests ( )
17211778 } )
17221779
1723- it . each ( [ false , true ] ) (
1724- "clears a closed tab reference only if it is still current (replacement: %s)" ,
1725- async ( replaced ) => {
1726- await openClineInNewTab ( {
1727- context : mockContext ,
1728- outputChannel : mockOutputChannel ,
1729- webviewFocusTracker : new WebviewFocusTracker ( ) ,
1730- } )
1731- const panel = vi . mocked ( vscode . window . createWebviewPanel ) . mock . results [ 0 ] . value
1732- const onDispose = vi . mocked ( panel . onDidDispose ) . mock . calls [ 0 ] [ 0 ]
1733- const replacement = { ...panel }
1734- if ( replaced ) setPanel ( replacement , "tab" )
1735-
1736- onDispose ( )
1737-
1738- expect ( getPanel ( ) ) . toBe ( replaced ? replacement : undefined )
1739- } ,
1740- )
1741-
1742- it ( "creates a webview panel with title 'Zoo Code'" , async ( ) => {
1743- // Pin the MdmService sentinel locally: the module mock's implementation leaks
1744- // across describes once another test set it, so the constructor's sixth
1745- // argument must be asserted against a deterministic double.
1746- const mockMdm = Object . assign ( { } as MdmService , { name : "mock-mdm" } )
1747- ; ( MdmService . getInstance as Mock ) . mockReturnValue ( mockMdm )
1748- const webviewFocusTracker = new WebviewFocusTracker ( )
1749- await openClineInNewTab ( { context : mockContext , outputChannel : mockOutputChannel , webviewFocusTracker } )
1750- expect ( ClineProvider ) . toHaveBeenCalledWith (
1751- mockContext ,
1752- mockOutputChannel ,
1753- "editor" ,
1754- await ContextProxy . getInstance ( mockContext ) ,
1755- webviewFocusTracker ,
1756- mockMdm ,
1757- )
1758-
1759- expect ( vscode . window . createWebviewPanel ) . toHaveBeenCalledWith (
1760- "zoo-code.TabPanelProvider" ,
1761- "Zoo Code" ,
1762- expect . any ( Number ) ,
1763- expect . objectContaining ( {
1764- enableScripts : true ,
1765- retainContextWhenHidden : true ,
1766- } ) ,
1767- )
1768- } )
1769-
17701780 it ( "cleans up the tracked refs and disposes the provider when initialization fails" , async ( ) => {
17711781 const resolveSpy = vi
17721782 . spyOn ( ClineProvider . prototype , "resolveWebviewView" )
@@ -1808,35 +1818,6 @@ describe("openClineInNewTab", () => {
18081818 disposeSpy . mockRestore ( )
18091819 } )
18101820
1811- it ( "preserves the initialization error when provider cleanup also fails" , async ( ) => {
1812- const resolveSpy = vi
1813- . spyOn ( ClineProvider . prototype , "resolveWebviewView" )
1814- . mockRejectedValue ( new Error ( "init failed" ) )
1815- // A throwing MCP disposable inside dispose() rejects the cleanup await;
1816- // that rejection must not replace the initialization error the caller
1817- // sees (deregistration itself already ran in dispose()'s finally path).
1818- const disposeSpy = vi . spyOn ( ClineProvider . prototype , "dispose" ) . mockRejectedValue ( new Error ( "cleanup boom" ) )
1819- ; ( vscode . window . createWebviewPanel as Mock ) . mockReturnValue (
1820- Object . assign ( { } as vscode . WebviewPanel , {
1821- webview : { postMessage : vi . fn ( ) } ,
1822- onDidChangeViewState : vi . fn ( ) ,
1823- onDidDispose : vi . fn ( ) ,
1824- dispose : vi . fn ( ) ,
1825- } ) ,
1826- )
1827-
1828- await expect (
1829- openClineInNewTab ( {
1830- context : mockContext ,
1831- outputChannel : mockOutputChannel ,
1832- webviewFocusTracker : new WebviewFocusTracker ( ) ,
1833- } ) ,
1834- ) . rejects . toThrow ( "init failed" )
1835-
1836- resolveSpy . mockRestore ( )
1837- disposeSpy . mockRestore ( )
1838- } )
1839-
18401821 it ( "re-points the tracked tab at the surviving panel when a new panel's initialization fails" , async ( ) => {
18411822 // Panel A opens fully and becomes the tracked tab.
18421823 const panelA = Object . assign ( { } as vscode . WebviewPanel , {
0 commit comments