@@ -2,7 +2,7 @@ import type { Mock } from "vitest"
22import * as vscode from "vscode"
33import { ClineProvider } from "../../core/webview/ClineProvider"
44
5- import { getVisibleProviderOrLog } from "../registerCommands"
5+ import { getVisibleProviderOrLog , registerCommands , setPanel } from "../registerCommands"
66
77vi . mock ( "execa" , ( ) => ( {
88 execa : vi . fn ( ) ,
@@ -25,10 +25,62 @@ vi.mock("vscode", () => ({
2525 } ,
2626 ] ,
2727 } ,
28+ commands : {
29+ registerCommand : vi . fn ( ) ,
30+ executeCommand : vi . fn ( ) ,
31+ } ,
2832} ) )
2933
3034vi . mock ( "../../core/webview/ClineProvider" )
3135
36+ vi . mock ( "../../shared/package" , ( ) => ( {
37+ Package : {
38+ name : "zoo-code" ,
39+ } ,
40+ } ) )
41+
42+ vi . mock ( "@roo-code/telemetry" , ( ) => ( {
43+ TelemetryService : {
44+ instance : {
45+ captureTitleButtonClicked : vi . fn ( ) ,
46+ } ,
47+ } ,
48+ } ) )
49+
50+ vi . mock ( "../../utils/focusPanel" , ( ) => ( {
51+ focusPanel : vi . fn ( ) . mockResolvedValue ( undefined ) ,
52+ } ) )
53+
54+ vi . mock ( "../handleTask" , ( ) => ( {
55+ handleNewTask : vi . fn ( ) ,
56+ } ) )
57+
58+ vi . mock ( "../../core/config/importExport" , ( ) => ( {
59+ importSettingsWithFeedback : vi . fn ( ) ,
60+ } ) )
61+
62+ vi . mock ( "../../services/code-index/manager" , ( ) => ( {
63+ CodeIndexManager : {
64+ getInstance : vi . fn ( ) ,
65+ } ,
66+ } ) )
67+
68+ vi . mock ( "../../services/mdm/MdmService" , ( ) => ( {
69+ MdmService : {
70+ getInstance : vi . fn ( ) ,
71+ } ,
72+ } ) )
73+
74+ vi . mock ( "../../core/config/ContextProxy" , ( ) => ( {
75+ ContextProxy : {
76+ getInstance : vi . fn ( ) ,
77+ } ,
78+ } ) )
79+
80+ vi . mock ( "../../i18n" , ( ) => ( {
81+ t : ( key : string ) => key ,
82+ } ) )
83+
3284describe ( "getVisibleProviderOrLog" , ( ) => {
3385 let mockOutputChannel : vscode . OutputChannel
3486
@@ -65,3 +117,133 @@ describe("getVisibleProviderOrLog", () => {
65117 expect ( mockOutputChannel . appendLine ) . toHaveBeenCalledWith ( "Cannot find any visible Roo Code instances." )
66118 } )
67119} )
120+
121+ describe ( "registerCommands handlers" , ( ) => {
122+ let mockOutputChannel : vscode . OutputChannel
123+ let mockContext : vscode . ExtensionContext
124+ let mockVisibleProvider : { postMessageToWebview : Mock }
125+ let mockProvider : { postMessageToWebview : Mock }
126+ let handlers : Record < string , ( ...args : unknown [ ] ) => unknown >
127+
128+ beforeEach ( ( ) => {
129+ vi . clearAllMocks ( )
130+ handlers = { }
131+
132+ mockOutputChannel = {
133+ appendLine : vi . fn ( ) ,
134+ append : vi . fn ( ) ,
135+ clear : vi . fn ( ) ,
136+ hide : vi . fn ( ) ,
137+ name : "mock" ,
138+ replace : vi . fn ( ) ,
139+ show : vi . fn ( ) ,
140+ dispose : vi . fn ( ) ,
141+ }
142+
143+ mockContext = {
144+ subscriptions : [ ] ,
145+ } as unknown as vscode . ExtensionContext
146+
147+ mockVisibleProvider = {
148+ postMessageToWebview : vi . fn ( ) . mockResolvedValue ( undefined ) ,
149+ }
150+
151+ mockProvider = {
152+ postMessageToWebview : vi . fn ( ) . mockResolvedValue ( undefined ) ,
153+ }
154+ ; ( ClineProvider . getVisibleInstance as Mock ) . mockReturnValue ( mockVisibleProvider )
155+ ; ( vscode . commands . registerCommand as Mock ) . mockImplementation (
156+ ( id : string , cb : ( ...args : unknown [ ] ) => unknown ) => {
157+ handlers [ id ] = cb
158+ return { dispose : vi . fn ( ) }
159+ } ,
160+ )
161+
162+ registerCommands ( {
163+ context : mockContext ,
164+ outputChannel : mockOutputChannel ,
165+ provider : mockProvider as unknown as ClineProvider ,
166+ } )
167+ } )
168+
169+ afterEach ( ( ) => {
170+ // Reset module-level panel state to prevent leakage between tests.
171+ setPanel ( undefined , "sidebar" )
172+ setPanel ( undefined , "tab" )
173+ } )
174+
175+ it ( "settingsButtonClicked posts both settingsButtonClicked and didBecomeVisible actions" , ( ) => {
176+ handlers [ "zoo-code.settingsButtonClicked" ] ( )
177+
178+ expect ( mockVisibleProvider . postMessageToWebview ) . toHaveBeenCalledWith ( {
179+ type : "action" ,
180+ action : "settingsButtonClicked" ,
181+ } )
182+ expect ( mockVisibleProvider . postMessageToWebview ) . toHaveBeenCalledWith ( {
183+ type : "action" ,
184+ action : "didBecomeVisible" ,
185+ } )
186+ expect ( mockVisibleProvider . postMessageToWebview ) . toHaveBeenCalledTimes ( 2 )
187+ } )
188+
189+ it ( "settingsButtonClicked is a no-op when no visible provider" , ( ) => {
190+ ; ( ClineProvider . getVisibleInstance as Mock ) . mockReturnValue ( undefined )
191+
192+ handlers [ "zoo-code.settingsButtonClicked" ] ( )
193+
194+ expect ( mockVisibleProvider . postMessageToWebview ) . not . toHaveBeenCalled ( )
195+ } )
196+
197+ it ( "historyButtonClicked posts historyButtonClicked action" , ( ) => {
198+ handlers [ "zoo-code.historyButtonClicked" ] ( )
199+
200+ expect ( mockVisibleProvider . postMessageToWebview ) . toHaveBeenCalledWith ( {
201+ type : "action" ,
202+ action : "historyButtonClicked" ,
203+ } )
204+ } )
205+
206+ it ( "marketplaceButtonClicked posts marketplaceButtonClicked action" , ( ) => {
207+ handlers [ "zoo-code.marketplaceButtonClicked" ] ( )
208+
209+ expect ( mockVisibleProvider . postMessageToWebview ) . toHaveBeenCalledWith ( {
210+ type : "action" ,
211+ action : "marketplaceButtonClicked" ,
212+ } )
213+ } )
214+
215+ it ( "acceptInput posts acceptInput message" , ( ) => {
216+ handlers [ "zoo-code.acceptInput" ] ( )
217+
218+ expect ( mockVisibleProvider . postMessageToWebview ) . toHaveBeenCalledWith ( {
219+ type : "acceptInput" ,
220+ } )
221+ } )
222+
223+ it ( "toggleAutoApprove awaits postMessage with toggleAutoApprove action" , async ( ) => {
224+ await handlers [ "zoo-code.toggleAutoApprove" ] ( )
225+
226+ expect ( mockVisibleProvider . postMessageToWebview ) . toHaveBeenCalledWith ( {
227+ type : "action" ,
228+ action : "toggleAutoApprove" ,
229+ } )
230+ } )
231+
232+ it ( "focusInput awaits postMessage on the registered provider when a sidebar panel is active" , async ( ) => {
233+ const fakeSidebar = { } as vscode . WebviewView
234+ setPanel ( fakeSidebar , "sidebar" )
235+
236+ await handlers [ "zoo-code.focusInput" ] ( )
237+
238+ expect ( mockProvider . postMessageToWebview ) . toHaveBeenCalledWith ( {
239+ type : "action" ,
240+ action : "focusInput" ,
241+ } )
242+ } )
243+
244+ it ( "focusInput does not post when no sidebar panel is active" , async ( ) => {
245+ await handlers [ "zoo-code.focusInput" ] ( )
246+
247+ expect ( mockProvider . postMessageToWebview ) . not . toHaveBeenCalled ( )
248+ } )
249+ } )
0 commit comments