@@ -3,6 +3,8 @@ import * as vscode from "vscode"
33import { createHash } from "crypto"
44import debounce from "lodash.debounce"
55import { CacheManager } from "../cache-manager"
6+ import * as path from "path"
7+ import * as os from "os"
68
79// Mock safeWriteJson utility
810vitest . mock ( "../../../utils/safeWriteJson" , ( ) => ( {
@@ -38,6 +40,11 @@ vitest.mock("@roo-code/telemetry", () => ({
3840 } ,
3941} ) )
4042
43+ // Mock os module
44+ vitest . mock ( "os" , ( ) => ( {
45+ homedir : vitest . fn ( ( ) => "/home/user" ) ,
46+ } ) )
47+
4148describe ( "CacheManager" , ( ) => {
4249 let mockContext : vscode . ExtensionContext
4350 let mockWorkspacePath : string
@@ -63,8 +70,18 @@ describe("CacheManager", () => {
6370 } )
6471
6572 describe ( "constructor" , ( ) => {
66- it ( "should correctly set up cachePath using Uri.joinPath and crypto.createHash" , ( ) => {
67- const expectedHash = createHash ( "sha256" ) . update ( mockWorkspacePath ) . digest ( "hex" )
73+ it ( "should correctly set up cachePath using Uri.joinPath with stable cache key" , ( ) => {
74+ // The cache key should be based on workspace name and relative path
75+ const workspaceName = path . basename ( mockWorkspacePath )
76+ const homedir = os . homedir ( )
77+ let relativePath = mockWorkspacePath
78+
79+ if ( mockWorkspacePath . startsWith ( homedir ) ) {
80+ relativePath = path . relative ( homedir , mockWorkspacePath )
81+ }
82+
83+ const compositeKey = `${ workspaceName } ::${ relativePath } `
84+ const expectedHash = createHash ( "sha256" ) . update ( compositeKey ) . digest ( "hex" )
6885
6986 expect ( vscode . Uri . joinPath ) . toHaveBeenCalledWith (
7087 mockContext . globalStorageUri ,
@@ -75,6 +92,36 @@ describe("CacheManager", () => {
7592 it ( "should set up debounced save function" , ( ) => {
7693 expect ( debounce ) . toHaveBeenCalledWith ( expect . any ( Function ) , 1500 )
7794 } )
95+
96+ it ( "should generate stable cache key for workspace under home directory" , ( ) => {
97+ // os.homedir is already mocked to return '/home/user'
98+ const workspaceUnderHome = "/home/user/projects/myproject"
99+ const cacheManagerHome = new CacheManager ( mockContext , workspaceUnderHome )
100+
101+ // Expected key should use relative path from home
102+ const expectedKey = `myproject::projects/myproject`
103+ const expectedHash = createHash ( "sha256" ) . update ( expectedKey ) . digest ( "hex" )
104+
105+ expect ( vscode . Uri . joinPath ) . toHaveBeenCalledWith (
106+ mockContext . globalStorageUri ,
107+ `roo-index-cache-${ expectedHash } .json` ,
108+ )
109+ } )
110+
111+ it ( "should generate stable cache key for workspace outside home directory" , ( ) => {
112+ // os.homedir is already mocked to return '/home/user'
113+ const workspaceOutsideHome = "/opt/projects/myproject"
114+ const cacheManagerOutside = new CacheManager ( mockContext , workspaceOutsideHome )
115+
116+ // Expected key should use full path since it's outside home
117+ const expectedKey = `myproject::/opt/projects/myproject`
118+ const expectedHash = createHash ( "sha256" ) . update ( expectedKey ) . digest ( "hex" )
119+
120+ expect ( vscode . Uri . joinPath ) . toHaveBeenCalledWith (
121+ mockContext . globalStorageUri ,
122+ `roo-index-cache-${ expectedHash } .json` ,
123+ )
124+ } )
78125 } )
79126
80127 describe ( "initialize" , ( ) => {
0 commit comments