@@ -3,18 +3,45 @@ import { CodeIndexServiceFactory } from "../service-factory"
33import type { MockedClass } from "vitest"
44import * as path from "path"
55
6+ // Helper: create a mock vscode.Uri from an fsPath
7+ function mockUri ( fsPath : string , scheme = "file" ) {
8+ return {
9+ fsPath,
10+ scheme,
11+ authority : "" ,
12+ path : fsPath ,
13+ toString : ( skipEncoding ?: boolean ) => `${ scheme } ://${ fsPath } ` ,
14+ }
15+ }
16+
617// Mock vscode module
718vi . mock ( "vscode" , ( ) => {
819 const testPath = require ( "path" )
920 const testWorkspacePath = testPath . join ( testPath . sep , "test" , "workspace" )
1021 return {
22+ Uri : {
23+ file : ( p : string ) => ( {
24+ fsPath : p ,
25+ scheme : "file" ,
26+ authority : "" ,
27+ path : p ,
28+ toString : ( _skipEncoding ?: boolean ) => `file://${ p } ` ,
29+ } ) ,
30+ joinPath : vi . fn ( ( ...args : any [ ] ) => ( { fsPath : args . join ( "/" ) } ) ) ,
31+ } ,
1132 window : {
1233 activeTextEditor : null ,
1334 } ,
1435 workspace : {
1536 workspaceFolders : [
1637 {
17- uri : { fsPath : testWorkspacePath } ,
38+ uri : {
39+ fsPath : testWorkspacePath ,
40+ scheme : "file" ,
41+ authority : "" ,
42+ path : testWorkspacePath ,
43+ toString : ( _skipEncoding ?: boolean ) => `file://${ testWorkspacePath } ` ,
44+ } ,
1845 name : "test" ,
1946 index : 0 ,
2047 } ,
@@ -25,8 +52,9 @@ vi.mock("vscode", () => {
2552 onDidDelete : vi . fn ( ) . mockReturnValue ( { dispose : vi . fn ( ) } ) ,
2653 dispose : vi . fn ( ) ,
2754 } ) ,
55+ getWorkspaceFolder : vi . fn ( ) ,
2856 } ,
29- RelativePattern : vi . fn ( ) . mockImplementation ( ( base , pattern ) => ( { base, pattern } ) ) ,
57+ RelativePattern : vi . fn ( ) . mockImplementation ( ( base : any , pattern : any ) => ( { base, pattern } ) ) ,
3058 }
3159} )
3260
@@ -672,18 +700,59 @@ describe("CodeIndexManager - handleSettingsChange regression", () => {
672700 expect ( manager . isWorkspaceEnabled ) . toBe ( false )
673701 } )
674702
675- it ( "should store enablement per folder, not per window" , async ( ) => {
676- await manager . setAutoEnableDefault ( false )
703+ it ( "should store enablement per folder URI, not per window" , async ( ) => {
704+ CodeIndexManager . disposeAll ( )
705+
706+ const vscode = await import ( "vscode" )
707+
708+ const folderAPath = path . join ( path . sep , "test" , "folderA" )
709+ const folderBPath = path . join ( path . sep , "test" , "folderB" )
710+ const folderAUri = mockUri ( folderAPath )
711+ const folderBUri = mockUri ( folderBPath )
712+
713+ // Both folders share the same workspaceState (same window)
714+ const sharedStore : Record < string , any > = { }
715+ const sharedContext = {
716+ ...mockContext ,
717+ workspaceState : {
718+ get : vi . fn ( ( key : string , defaultValue ?: any ) => sharedStore [ key ] ?? defaultValue ) ,
719+ update : vi . fn ( async ( key : string , value : any ) => {
720+ sharedStore [ key ] = value
721+ } ) ,
722+ } as any ,
723+ globalState : {
724+ get : vi . fn ( ( _key : string , _defaultValue ?: any ) => false ) ,
725+ update : vi . fn ( ) ,
726+ } as any ,
727+ }
677728
678- // Enable indexing for the current manager's folder
679- await manager . setWorkspaceEnabled ( true )
680- expect ( manager . isWorkspaceEnabled ) . toBe ( true )
729+ // Patch workspaceFolders to include both folders
730+ ; ( vscode . workspace as any ) . workspaceFolders = [
731+ { uri : folderAUri , name : "folderA" , index : 0 } ,
732+ { uri : folderBUri , name : "folderB" , index : 1 } ,
733+ ]
734+
735+ const managerA = CodeIndexManager . getInstance ( sharedContext as any , folderAPath ) !
736+ const managerB = CodeIndexManager . getInstance ( sharedContext as any , folderBPath ) !
737+
738+ // Both start disabled (autoEnableDefault is false via globalState mock)
739+ expect ( managerA . isWorkspaceEnabled ) . toBe ( false )
740+ expect ( managerB . isWorkspaceEnabled ) . toBe ( false )
741+
742+ // Enable A only
743+ await managerA . setWorkspaceEnabled ( true )
744+
745+ expect ( managerA . isWorkspaceEnabled ) . toBe ( true )
746+ expect ( managerB . isWorkspaceEnabled ) . toBe ( false )
747+
748+ // Enable B, disable A
749+ await managerB . setWorkspaceEnabled ( true )
750+ await managerA . setWorkspaceEnabled ( false )
681751
682- // Create a second manager for a different folder path
683- const otherManager = CodeIndexManager . getInstance ( mockContext as any , "/other/workspace" ) !
752+ expect ( managerA . isWorkspaceEnabled ) . toBe ( false )
753+ expect ( managerB . isWorkspaceEnabled ) . toBe ( true )
684754
685- // The other folder should NOT be enabled — keys are per-folder
686- expect ( otherManager . isWorkspaceEnabled ) . toBe ( false )
755+ CodeIndexManager . disposeAll ( )
687756 } )
688757 } )
689758
0 commit comments