@@ -530,11 +530,15 @@ describe("registerCommands handlers", () => {
530530 } )
531531 } )
532532
533- it ( "focusInput awaits postMessage on the registered provider when a sidebar panel is active " , async ( ) => {
533+ it ( "focusInput awaits postMessage on the instance resolved from the tracked sidebar view " , async ( ) => {
534534 const fakeSidebar = { } as vscode . WebviewView
535535 setPanel ( fakeSidebar , "sidebar" )
536- // The sidebar target is resolved through the live-instance lookup.
537- ; ( ClineProvider . getInstanceForView as Mock ) . mockReturnValue ( mockProvider )
536+ // The sidebar target is resolved through the live-instance lookup, so the lookup has to hand back
537+ // a provider that is NOT the one registered at activation: if both names refer to the same
538+ // double, a handler that posted to the registered provider instead of the looked-up instance
539+ // would pass either way.
540+ const sidebarProvider = { postMessageToWebview : vi . fn ( ) . mockResolvedValue ( undefined ) }
541+ ; ( ClineProvider . getInstanceForView as Mock ) . mockReturnValue ( sidebarProvider )
538542
539543 // Same deferred-promise pattern as above. focusInput first awaits
540544 // focusPanel() (mocked to resolve sync) and then awaits
@@ -545,7 +549,7 @@ describe("registerCommands handlers", () => {
545549 const postPromise = new Promise < void > ( ( resolve ) => {
546550 resolvePost = resolve
547551 } )
548- mockProvider . postMessageToWebview . mockReturnValueOnce ( postPromise )
552+ sidebarProvider . postMessageToWebview . mockReturnValueOnce ( postPromise )
549553
550554 const handlerPromise = handlers [ "zoo-code.focusInput" ] ( ) as Promise < unknown >
551555 let settled = false
@@ -559,10 +563,13 @@ describe("registerCommands handlers", () => {
559563 resolvePost ( )
560564 await handlerPromise
561565
562- expect ( mockProvider . postMessageToWebview ) . toHaveBeenCalledWith ( {
566+ expect ( ClineProvider . getInstanceForView as Mock ) . toHaveBeenCalledWith ( fakeSidebar )
567+ expect ( sidebarProvider . postMessageToWebview ) . toHaveBeenCalledWith ( {
563568 type : "action" ,
564569 action : "focusInput" ,
565570 } )
571+ // The registered provider is a different double: it must not receive the post.
572+ expect ( mockProvider . postMessageToWebview ) . not . toHaveBeenCalled ( )
566573 } )
567574
568575 it ( "focusInput does not post when no sidebar panel is tracked" , async ( ) => {
@@ -575,7 +582,8 @@ describe("registerCommands handlers", () => {
575582 } )
576583
577584 it ( "focusInput posts to the tab provider when a tab panel is tracked alongside the sidebar" , async ( ) => {
578- setPanel ( { } as vscode . WebviewView , "sidebar" )
585+ const sidebarView = { } as vscode . WebviewView
586+ setPanel ( sidebarView , "sidebar" )
579587 const tabPanel = { } as vscode . WebviewPanel
580588 setPanel ( tabPanel , "tab" )
581589 const tabProvider = { postMessageToWebview : vi . fn ( ) . mockResolvedValue ( undefined ) }
@@ -585,6 +593,9 @@ describe("registerCommands handlers", () => {
585593
586594 // focusPanel selects the tab when one is tracked, so the focus message follows that
587595 // selection: the tab webview is the one that became visible and must receive it.
596+ // Pin the arguments handed to focusPanel, not only the outcome: swapping them would make
597+ // focusPanel reveal the sidebar while the post still followed the tracked tab.
598+ expect ( focusPanel ) . toHaveBeenCalledWith ( tabPanel , sidebarView )
588599 expect ( ClineProvider . getInstanceForView as Mock ) . toHaveBeenCalledWith ( tabPanel )
589600 expect ( tabProvider . postMessageToWebview ) . toHaveBeenCalledWith ( { type : "action" , action : "focusInput" } )
590601 expect ( mockProvider . postMessageToWebview ) . not . toHaveBeenCalled ( )
@@ -596,10 +607,16 @@ describe("registerCommands handlers", () => {
596607 // that lookup finds no live instance (tab provider disposed or untracked), the
597608 // sidebar must not receive the post - focusPanel made the tab visible, so a
598609 // sidebar post would focus an input the user cannot see.
599- setPanel ( { } as vscode . WebviewView , "sidebar" )
610+ const sidebarView = { } as vscode . WebviewView
611+ setPanel ( sidebarView , "sidebar" )
600612 const tabPanel = { } as vscode . WebviewPanel
601613 setPanel ( tabPanel , "tab" )
602- ; ( ClineProvider . getInstanceForView as Mock ) . mockReturnValue ( undefined )
614+ // The lookup is view-specific on purpose: the tab lookup must miss while the sidebar stays live.
615+ // A blanket `undefined` cannot tell "no fallback" apart from "fell back to a sidebar that also
616+ // resolved to nothing", which is the defect these silence assertions exist to catch.
617+ ; ( ClineProvider . getInstanceForView as Mock ) . mockImplementation ( ( view : unknown ) =>
618+ view === tabPanel ? undefined : mockProvider ,
619+ )
603620
604621 await handlers [ "zoo-code.focusInput" ] ( )
605622
@@ -949,11 +966,12 @@ describe("openClineInNewTab", () => {
949966
950967 it ( "reveals the existing tab instead of creating a second panel" , async ( ) => {
951968 const mockExistingProvider = { postMessageToWebview : vi . fn ( ) . mockResolvedValue ( undefined ) }
969+ const revealMock = vi . fn ( ) . mockResolvedValue ( undefined )
952970 const mockPanel = {
953971 webview : { postMessage : vi . fn ( ) } ,
954972 onDidChangeViewState : vi . fn ( ) ,
955973 onDidDispose : vi . fn ( ) ,
956- reveal : vi . fn ( ) . mockResolvedValue ( undefined ) ,
974+ reveal : revealMock ,
957975 } as unknown as vscode . WebviewPanel
958976 setPanel ( mockPanel , "tab" )
959977 ; ( ClineProvider . getInstanceForView as Mock ) . mockReturnValue ( mockExistingProvider )
@@ -965,12 +983,18 @@ describe("openClineInNewTab", () => {
965983 } )
966984
967985 expect ( result ) . toBe ( mockExistingProvider )
968- expect ( mockPanel . reveal ) . toHaveBeenCalledTimes ( 1 )
986+ expect ( revealMock ) . toHaveBeenCalledTimes ( 1 )
969987 expect ( vscode . window . createWebviewPanel ) . not . toHaveBeenCalled ( )
970988 expect ( mockExistingProvider . postMessageToWebview ) . toHaveBeenCalledWith ( {
971989 type : "action" ,
972990 action : "didBecomeVisible" ,
973991 } )
992+ // Each assertion above is order-blind on its own. The contract is reveal first, then the
993+ // didBecomeVisible post: posting first would light up an input in a panel the user cannot
994+ // see yet, so the invocation order is pinned directly.
995+ expect ( revealMock . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
996+ mockExistingProvider . postMessageToWebview . mock . invocationCallOrder [ 0 ] ,
997+ )
974998 } )
975999
9761000 it ( "creates a new tab panel when the tracked tab's provider has been disposed" , async ( ) => {
@@ -993,6 +1017,40 @@ describe("openClineInNewTab", () => {
9931017 expect ( vscode . window . createWebviewPanel ) . toHaveBeenCalledTimes ( 1 )
9941018 } )
9951019
1020+ it ( "disposes the new panel and clears the tab ref when the first resolve rejects" , async ( ) => {
1021+ // The tab ref is stored before the resolve and the panel's own disposal handler is registered
1022+ // only after it, so a failed resolve used to leave a blank panel open and tracked: the next
1023+ // click would reveal a panel whose webview never resolved.
1024+ const failedPanel = {
1025+ webview : { postMessage : vi . fn ( ) } ,
1026+ onDidChangeViewState : vi . fn ( ) ,
1027+ onDidDispose : vi . fn ( ) ,
1028+ dispose : vi . fn ( ) ,
1029+ reveal : vi . fn ( ) . mockResolvedValue ( undefined ) ,
1030+ } as unknown as vscode . WebviewPanel
1031+ ; ( vscode . window . createWebviewPanel as Mock ) . mockReturnValue ( failedPanel )
1032+ ; ( ClineProvider . getInstanceForView as Mock ) . mockReturnValue ( undefined )
1033+ const ctor = ClineProvider as unknown as Mock
1034+ // A regular function, not an arrow: the class mock is constructed with new, and an arrow factory
1035+ // throws "is not a constructor". Once, so it is consumed by this test only.
1036+ ctor . mockImplementationOnce ( function ( this : unknown ) {
1037+ return {
1038+ resolveWebviewView : vi . fn ( ) . mockRejectedValue ( new Error ( "webview resolve failed" ) ) ,
1039+ } as unknown as ClineProvider
1040+ } )
1041+
1042+ await expect (
1043+ openClineInNewTab ( {
1044+ context : mockContext ,
1045+ outputChannel : mockOutputChannel ,
1046+ webviewFocusTracker : tabFocusTracker ,
1047+ } ) ,
1048+ ) . rejects . toThrow ( "webview resolve failed" )
1049+
1050+ expect ( failedPanel . dispose ) . toHaveBeenCalledTimes ( 1 )
1051+ expect ( getPanel ( ) ) . toBeUndefined ( )
1052+ } )
1053+
9961054 it ( "falls back to an undefined MdmService when MdmService.getInstance throws" , async ( ) => {
9971055 ; ( MdmService . getInstance as Mock ) . mockImplementation ( ( ) => {
9981056 throw new Error ( "MDM service not initialized" )
0 commit comments