@@ -12,7 +12,7 @@ vi.mock("vscode", () => ({
1212 tabGroups : {
1313 onDidChangeTabs : vi . fn ( ) ,
1414 } ,
15- onDidChangeActiveTextEditor : vi . fn ( ) ,
15+ onDidChangeActiveTextEditor : vi . fn ( ) . mockReturnValue ( { dispose : vi . fn ( ) } ) ,
1616 } ,
1717 workspace : {
1818 registerTextDocumentContentProvider : vi . fn ( ) ,
@@ -205,6 +205,7 @@ vi.mock("../core/webview/ClineProvider", async () => {
205205 {
206206 // Static method used by extension.ts
207207 getVisibleInstance : vi . fn ( ) . mockReturnValue ( mockInstance ) ,
208+ getAllInstances : vi . fn ( ) . mockReturnValue ( [ ] ) ,
208209 sideBarId : "zoo-code.SidebarProvider" ,
209210 } ,
210211 ) ,
@@ -238,6 +239,129 @@ describe("extension.ts", () => {
238239 settingsUpdatedHandler = undefined
239240 } )
240241
242+ test ( "initializes the code index scope and registers it for extension cleanup" , async ( ) => {
243+ vi . resetModules ( )
244+ const { CodeIndexScope } = await import ( "../services/code-index/code-index-scope" )
245+ const init = vi . spyOn ( CodeIndexScope . prototype , "init" )
246+ const dispose = vi . spyOn ( CodeIndexScope . prototype , "dispose" )
247+ try {
248+ const { activate } = await import ( "../extension" )
249+ await activate ( mockContext )
250+
251+ const scopes = mockContext . subscriptions . filter ( ( entry ) => entry instanceof CodeIndexScope )
252+ expect ( scopes ) . toHaveLength ( 1 )
253+ expect ( init ) . toHaveBeenCalledExactlyOnceWith ( )
254+ expect ( init . mock . contexts [ 0 ] ) . toBe ( scopes [ 0 ] )
255+ expect ( dispose ) . not . toHaveBeenCalled ( )
256+ scopes [ 0 ] . dispose ( )
257+ expect ( dispose ) . toHaveBeenCalledExactlyOnceWith ( )
258+ } finally {
259+ init . mockRestore ( )
260+ dispose . mockRestore ( )
261+ }
262+ } )
263+
264+ test ( "publishes indexing status to matching providers and logs delivery failures" , async ( ) => {
265+ vi . resetModules ( )
266+ const { CodeIndexScope } = await import ( "../services/code-index/code-index-scope" )
267+ const { ClineProvider } = await import ( "../core/webview/ClineProvider" )
268+ const log = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } )
269+ const provider = ClineProvider . getVisibleInstance ( ) !
270+ Object . defineProperty ( provider , "workspacePath" , { configurable : true , value : "/workspace" } )
271+ const getAll = vi . mocked ( ClineProvider . getAllInstances )
272+ const post = vi . mocked ( provider . postMessageToWebview )
273+ getAll . mockReturnValue ( [ provider , provider ] )
274+ post . mockRejectedValueOnce ( new Error ( "delivery failed" ) ) . mockResolvedValue ( undefined )
275+ try {
276+ const { activate } = await import ( "../extension" )
277+ await activate ( mockContext )
278+ const scope = mockContext . subscriptions . find ( ( entry ) => entry instanceof CodeIndexScope ) !
279+ const status = {
280+ systemStatus : "Standby" as const ,
281+ message : "Ready" ,
282+ processedItems : 0 ,
283+ totalItems : 0 ,
284+ currentItemUnit : "blocks" ,
285+ workspacePath : "/workspace" ,
286+ workspaceEnabled : true ,
287+ autoEnableDefault : true ,
288+ }
289+ scope [ "statusManager" ] ! [ "publishStatus" ] ( status )
290+ await Promise . resolve ( )
291+ expect ( post ) . toHaveBeenCalledTimes ( 2 )
292+ expect ( post ) . toHaveBeenCalledWith ( { type : "indexingStatusUpdate" , values : status } )
293+ expect ( log ) . toHaveBeenCalledWith (
294+ "[CodeIndexStatusManager] Failed to publish indexing status:" ,
295+ expect . objectContaining ( { message : "delivery failed" } ) ,
296+ )
297+ scope . dispose ( )
298+ } finally {
299+ log . mockRestore ( )
300+ getAll . mockReturnValue ( [ ] )
301+ post . mockReset ( )
302+ Reflect . deleteProperty ( provider , "workspacePath" )
303+ }
304+ } )
305+
306+ test . each ( [ "/other-workspace" , undefined , "" ] ) (
307+ "does not publish workspace metadata to a provider bound to %s" ,
308+ async ( workspacePath ) => {
309+ vi . resetModules ( )
310+ const { CodeIndexScope } = await import ( "../services/code-index/code-index-scope" )
311+ const { ClineProvider } = await import ( "../core/webview/ClineProvider" )
312+ const provider = ClineProvider . getVisibleInstance ( ) !
313+ Object . defineProperty ( provider , "workspacePath" , { configurable : true , value : workspacePath } )
314+ const getAll = vi . mocked ( ClineProvider . getAllInstances )
315+ getAll . mockReturnValue ( [ provider ] )
316+ try {
317+ const { activate } = await import ( "../extension" )
318+ await activate ( mockContext )
319+ const scope = mockContext . subscriptions . find ( ( entry ) => entry instanceof CodeIndexScope ) !
320+ const status = {
321+ systemStatus : "Indexing" as const ,
322+ message : "Processing confidential.ts" ,
323+ processedItems : 1 ,
324+ totalItems : 2 ,
325+ currentItemUnit : "files" ,
326+ workspacePath : "/workspace" ,
327+ workspaceEnabled : true ,
328+ autoEnableDefault : true ,
329+ }
330+ scope [ "statusManager" ] ! [ "publishStatus" ] ( status )
331+ await Promise . resolve ( )
332+ expect ( provider . postMessageToWebview ) . not . toHaveBeenCalled ( )
333+ scope . dispose ( )
334+ } finally {
335+ getAll . mockReturnValue ( [ ] )
336+ Reflect . deleteProperty ( provider , "workspacePath" )
337+ }
338+ } ,
339+ )
340+
341+ test . each ( [ new Error ( "scope initialization failed" ) , "scope initialization failed" ] ) (
342+ "continues activation and logs code index scope initialization failure: %s" ,
343+ async ( error ) => {
344+ vi . resetModules ( )
345+ const { CodeIndexScope } = await import ( "../services/code-index/code-index-scope" )
346+ const init = vi . spyOn ( CodeIndexScope . prototype , "init" ) . mockImplementationOnce ( ( ) => {
347+ throw error
348+ } )
349+ try {
350+ const { activate } = await import ( "../extension" )
351+ await expect ( activate ( mockContext ) ) . resolves . toBeDefined ( )
352+
353+ const vscode = await import ( "vscode" )
354+ const channel = vi . mocked ( vscode . window . createOutputChannel ) . mock . results . at ( - 1 ) ?. value
355+ expect ( channel ?. appendLine ) . toHaveBeenCalledWith (
356+ "[CodeIndexScope] Failed to initialize: scope initialization failed" ,
357+ )
358+ expect ( mockContext . subscriptions . filter ( ( entry ) => entry instanceof CodeIndexScope ) ) . toHaveLength ( 1 )
359+ } finally {
360+ init . mockRestore ( )
361+ }
362+ } ,
363+ )
364+
241365 test ( "does not call dotenv.config when optional .env does not exist" , async ( ) => {
242366 vi . resetModules ( )
243367 vi . clearAllMocks ( )
0 commit comments