11// npx vitest run __tests__/extension.spec.ts
22
33import type * as vscode from "vscode"
4+ import { makeUri } from "../test-utils/vscode"
45
56vi . mock ( "vscode" , ( ) => ( {
67 window : {
@@ -15,6 +16,7 @@ vi.mock("vscode", () => ({
1516 onDidChangeActiveTextEditor : vi . fn ( ) ,
1617 } ,
1718 workspace : {
19+ workspaceFolders : undefined ,
1820 registerTextDocumentContentProvider : vi . fn ( ) ,
1921 getConfiguration : vi . fn ( ) . mockReturnValue ( {
2022 get : vi . fn ( ) . mockReturnValue ( [ ] ) ,
@@ -139,10 +141,10 @@ vi.mock("../services/mcp/McpServerManager", () => ({
139141 } ,
140142} ) )
141143
142- vi . mock ( "../services/code-index/code-index- manager-registry " , ( ) => ( {
143- CodeIndexManagerRegistry : {
144- getInstance : vi . fn ( ) . mockReturnValue ( null ) ,
145- } ,
144+ vi . mock ( "../services/code-index/manager" , ( ) => ( {
145+ CodeIndexManager : vi . fn ( ) . mockImplementation ( function ( ) {
146+ return { initialize : vi . fn ( ) . mockResolvedValue ( { requiresRestart : false } ) , dispose : vi . fn ( ) }
147+ } ) ,
146148} ) )
147149
148150vi . mock ( "../services/mdm/MdmService" , ( ) => ( {
@@ -267,6 +269,189 @@ describe("extension.ts", () => {
267269 expect ( dotenv . config ) . toHaveBeenCalledTimes ( 1 )
268270 } )
269271
272+ describe ( "code index workspace activation" , ( ) => {
273+ beforeEach ( ( ) => {
274+ vi . resetModules ( )
275+ } )
276+
277+ afterEach ( async ( ) => {
278+ const vscode = await import ( "vscode" )
279+ vi . mocked ( vscode . workspace ) . workspaceFolders = undefined
280+ const { codeIndexWorkspaceScopeRegistry } =
281+ await import ( "../services/code-index/code-index-workspace-scope-registry" )
282+ codeIndexWorkspaceScopeRegistry . disposeAll ( )
283+ } )
284+
285+ test ( "initializes every workspace once with the shared context without blocking activation" , async ( ) => {
286+ const vscode = await import ( "vscode" )
287+ const folders = [ "/first" , "/second" ] . map ( ( name , index ) => ( { name, index, uri : makeUri ( name ) } ) )
288+ vi . mocked ( vscode . workspace ) . workspaceFolders = folders
289+ const { codeIndexWorkspaceScopeRegistry : registry } =
290+ await import ( "../services/code-index/code-index-workspace-scope-registry" )
291+ const scopes = folders . map ( ( folder ) => registry . getScope ( mockContext , folder . uri . fsPath ) ! )
292+ let release ! : ( ) => void
293+ const pending = new Promise < void > ( ( resolve ) => {
294+ release = resolve
295+ } )
296+ for ( const scope of scopes ) {
297+ vi . mocked ( scope . codeIndexManager . initialize ) . mockImplementationOnce ( async ( ) => {
298+ await pending
299+ return { requiresRestart : false }
300+ } )
301+ }
302+ const { ContextProxy } = await import ( "../core/config/ContextProxy" )
303+ const { activate } = await import ( "../extension" )
304+ let activated = false
305+ const activation = activate ( mockContext ) . then ( ( ) => {
306+ activated = true
307+ } )
308+ try {
309+ await vi . waitFor ( ( ) => expect ( activated ) . toBe ( true ) )
310+ const contextProxy = await ContextProxy . getInstance ( mockContext )
311+ for ( const scope of scopes ) {
312+ expect ( scope . codeIndexManager . initialize ) . toHaveBeenCalledExactlyOnceWith ( contextProxy )
313+ expect ( scope . codeIndexManager . dispose ) . not . toHaveBeenCalled ( )
314+ }
315+ expect ( vscode . commands . executeCommand ) . toHaveBeenCalledWith ( "test-extension.activationCompleted" )
316+ } finally {
317+ release ( )
318+ await activation
319+ }
320+ } )
321+
322+ test . each ( [ new Error ( "configuration failed" ) , "configuration failed" ] ) (
323+ "logs background rejection %s with its workspace and continues other initialization and cleanup" ,
324+ async ( error ) => {
325+ const vscode = await import ( "vscode" )
326+ vi . mocked ( vscode . workspace ) . workspaceFolders = [ "/broken" , "/healthy" ] . map ( ( name , index ) => ( {
327+ name,
328+ index,
329+ uri : makeUri ( name ) ,
330+ } ) )
331+ const { codeIndexWorkspaceScopeRegistry : registry } =
332+ await import ( "../services/code-index/code-index-workspace-scope-registry" )
333+ const broken = registry . getScope ( mockContext , "/broken" ) !
334+ const healthy = registry . getScope ( mockContext , "/healthy" ) !
335+ vi . mocked ( broken . codeIndexManager . initialize ) . mockRejectedValueOnce ( error )
336+ const { activate } = await import ( "../extension" )
337+ await expect ( activate ( mockContext ) ) . resolves . toBeDefined ( )
338+
339+ expect ( healthy . codeIndexManager . initialize ) . toHaveBeenCalledTimes ( 1 )
340+ const channel = vi . mocked ( vscode . window . createOutputChannel ) . mock . results . at ( - 1 ) ?. value
341+ expect ( channel . appendLine ) . toHaveBeenCalledWith (
342+ "[CodeIndexManager] Error during background CodeIndexManager configuration/indexing for /broken: configuration failed" ,
343+ )
344+ await Promise . all ( mockContext . subscriptions . map ( ( subscription ) => subscription ?. dispose ?.( ) ) )
345+ expect ( broken . codeIndexManager . dispose ) . toHaveBeenCalledTimes ( 1 )
346+ expect ( healthy . codeIndexManager . dispose ) . toHaveBeenCalledTimes ( 1 )
347+ expect ( registry . getAllScopes ( ) ) . toEqual ( [ ] )
348+ } ,
349+ )
350+
351+ test . each ( [ undefined , [ ] ] ) (
352+ "activation without workspace folders (%s) still owns lazy cleanup" ,
353+ async ( folders ) => {
354+ const vscode = await import ( "vscode" )
355+ vi . mocked ( vscode . workspace ) . workspaceFolders = folders
356+ const { CodeIndexManager } = await import ( "../services/code-index/manager" )
357+ const { activate } = await import ( "../extension" )
358+ await expect ( activate ( mockContext ) ) . resolves . toBeDefined ( )
359+ expect ( CodeIndexManager ) . not . toHaveBeenCalled ( )
360+
361+ vi . mocked ( vscode . workspace ) . workspaceFolders = [ { name : "late" , index : 0 , uri : makeUri ( "/late" ) } ]
362+ const { codeIndexWorkspaceScopeRegistry : registry } =
363+ await import ( "../services/code-index/code-index-workspace-scope-registry" )
364+ const lazy = registry . getScope ( mockContext , "/late" ) !
365+ await Promise . all ( mockContext . subscriptions . map ( ( subscription ) => subscription ?. dispose ?.( ) ) )
366+ expect ( lazy . codeIndexManager . dispose ) . toHaveBeenCalledTimes ( 1 )
367+ expect ( registry . getAllScopes ( ) ) . toEqual ( [ ] )
368+ } ,
369+ )
370+
371+ test ( "one registry cleanup owner disposes startup and lazy scopes exactly once" , async ( ) => {
372+ const vscode = await import ( "vscode" )
373+ const first = { name : "first" , index : 0 , uri : makeUri ( "/first" ) }
374+ const late = { name : "late" , index : 1 , uri : makeUri ( "/late" ) }
375+ vi . mocked ( vscode . workspace ) . workspaceFolders = [ first ]
376+ const { codeIndexWorkspaceScopeRegistry : registry } =
377+ await import ( "../services/code-index/code-index-workspace-scope-registry" )
378+ const { activate, deactivate } = await import ( "../extension" )
379+ await activate ( mockContext )
380+ const startup = registry . getScope ( mockContext , first . uri . fsPath ) !
381+ vi . mocked ( vscode . workspace ) . workspaceFolders = [ first , late ]
382+ const lazy = registry . getScope ( mockContext , late . uri . fsPath ) !
383+
384+ await deactivate ( )
385+ for ( const subscription of mockContext . subscriptions ) {
386+ // Unrelated activation mocks do not all return a disposable.
387+ await subscription ?. dispose ?.( )
388+ }
389+
390+ expect ( startup . codeIndexManager . dispose ) . toHaveBeenCalledTimes ( 1 )
391+ expect ( lazy . codeIndexManager . dispose ) . toHaveBeenCalledTimes ( 1 )
392+ expect ( registry . getAllScopes ( ) ) . toEqual ( [ ] )
393+ expect ( mockContext . subscriptions ) . not . toContain ( startup )
394+ expect ( mockContext . subscriptions ) . not . toContain ( lazy )
395+ registry . disposeAll ( )
396+ expect ( startup . codeIndexManager . dispose ) . toHaveBeenCalledTimes ( 1 )
397+ expect ( lazy . codeIndexManager . dispose ) . toHaveBeenCalledTimes ( 1 )
398+ } )
399+
400+ test . each ( [ false , true ] ) (
401+ "cleanup waits for pending initialization (rejects=%s) before disposal" ,
402+ async ( rejects ) => {
403+ const vscode = await import ( "vscode" )
404+ vi . mocked ( vscode . workspace ) . workspaceFolders = [ { name : "first" , index : 0 , uri : makeUri ( "/first" ) } ]
405+ const { codeIndexWorkspaceScopeRegistry : registry } =
406+ await import ( "../services/code-index/code-index-workspace-scope-registry" )
407+ const scope = registry . getScope ( mockContext , "/first" ) !
408+ let release ! : ( ) => void
409+ const pending = new Promise < void > ( ( resolve ) => {
410+ release = resolve
411+ } )
412+ vi . mocked ( scope . codeIndexManager . initialize ) . mockImplementationOnce ( async ( ) => {
413+ await pending
414+ if ( rejects ) throw new Error ( "late initialization failure" )
415+ return { requiresRestart : false }
416+ } )
417+ const { activate, deactivate } = await import ( "../extension" )
418+ await activate ( mockContext )
419+ const cleanup = Promise . all ( mockContext . subscriptions . map ( ( subscription ) => subscription ?. dispose ?.( ) ) )
420+ const disposedBeforeInitialization = vi . mocked ( scope . codeIndexManager . dispose ) . mock . calls . length
421+ release ( )
422+ await cleanup
423+ await deactivate ( )
424+
425+ expect ( disposedBeforeInitialization ) . toBe ( 0 )
426+ expect ( scope . codeIndexManager . dispose ) . toHaveBeenCalledTimes ( 1 )
427+ expect ( registry . getAllScopes ( ) ) . toEqual ( [ ] )
428+ } ,
429+ )
430+
431+ test ( "skips an unavailable scope without skipping later workspace initialization" , async ( ) => {
432+ const vscode = await import ( "vscode" )
433+ vi . mocked ( vscode . workspace ) . workspaceFolders = [ "/unavailable" , "/healthy" ] . map ( ( name , index ) => ( {
434+ name,
435+ index,
436+ uri : makeUri ( name ) ,
437+ } ) )
438+ const { codeIndexWorkspaceScopeRegistry : registry } =
439+ await import ( "../services/code-index/code-index-workspace-scope-registry" )
440+ const getScope = vi . spyOn ( registry , "getScope" ) . mockReturnValueOnce ( undefined )
441+ try {
442+ const { activate } = await import ( "../extension" )
443+ await expect ( activate ( mockContext ) ) . resolves . toBeDefined ( )
444+ expect ( getScope ) . toHaveBeenCalledWith ( mockContext , "/unavailable" )
445+ expect ( getScope ) . toHaveBeenCalledWith ( mockContext , "/healthy" )
446+ const scopes = registry . getAllScopes ( )
447+ expect ( scopes ) . toHaveLength ( 1 )
448+ expect ( scopes [ 0 ] . codeIndexManager . initialize ) . toHaveBeenCalledTimes ( 1 )
449+ } finally {
450+ getScope . mockRestore ( )
451+ }
452+ } )
453+ } )
454+
270455 describe ( "cloud organization settings handling" , ( ) => {
271456 beforeEach ( ( ) => {
272457 vi . resetModules ( )
0 commit comments