@@ -9,12 +9,30 @@ import { ContextProxy } from "../ContextProxy"
99vi . mock ( "vscode" , ( ) => ( {
1010 Uri : {
1111 file : vi . fn ( ( path ) => ( { path } ) ) ,
12+ joinPath : vi . fn ( ( base , ...paths ) => ( { path : `${ base . path } /${ paths . join ( "/" ) } ` } ) ) ,
1213 } ,
1314 ExtensionMode : {
1415 Development : 1 ,
1516 Production : 2 ,
1617 Test : 3 ,
1718 } ,
19+ FileSystemError : class FileSystemError extends Error {
20+ code : string
21+ constructor ( message : string ) {
22+ super ( message )
23+ this . code = "FileNotFound"
24+ }
25+ } ,
26+ workspace : {
27+ fs : {
28+ readFile : vi . fn ( ) . mockRejectedValue ( Object . assign ( new Error ( "FileNotFound" ) , { code : "FileNotFound" } ) ) ,
29+ writeFile : vi . fn ( ) . mockResolvedValue ( undefined ) ,
30+ delete : vi . fn ( ) . mockResolvedValue ( undefined ) ,
31+ } ,
32+ } ,
33+ window : {
34+ showInformationMessage : vi . fn ( ) ,
35+ } ,
1836} ) )
1937
2038describe ( "ContextProxy" , ( ) => {
@@ -71,12 +89,14 @@ describe("ContextProxy", () => {
7189 describe ( "constructor" , ( ) => {
7290 it ( "should initialize state cache with all global state keys" , ( ) => {
7391 // +1 for the migration check of old nested settings
74- expect ( mockGlobalState . get ) . toHaveBeenCalledTimes ( GLOBAL_STATE_KEYS . length + 1 )
92+ // +1 for the taskHistory migration check
93+ expect ( mockGlobalState . get ) . toHaveBeenCalledTimes ( GLOBAL_STATE_KEYS . length + 2 )
7594 for ( const key of GLOBAL_STATE_KEYS ) {
7695 expect ( mockGlobalState . get ) . toHaveBeenCalledWith ( key )
7796 }
78- // Also check for migration call
97+ // Also check for migration calls
7998 expect ( mockGlobalState . get ) . toHaveBeenCalledWith ( "openRouterImageGenerationSettings" )
99+ expect ( mockGlobalState . get ) . toHaveBeenCalledWith ( "taskHistory" )
80100 } )
81101
82102 it ( "should initialize secret cache with all secret keys" , ( ) => {
@@ -99,8 +119,10 @@ describe("ContextProxy", () => {
99119 const result = proxy . getGlobalState ( "apiProvider" )
100120 expect ( result ) . toBe ( "deepseek" )
101121
102- // Original context should be called once during updateGlobalState (+1 for migration check)
103- expect ( mockGlobalState . get ) . toHaveBeenCalledTimes ( GLOBAL_STATE_KEYS . length + 1 ) // From initialization + migration check
122+ // Original context should be called during initialization
123+ // +1 for openRouterImageGenerationSettings migration check
124+ // +1 for taskHistory migration check
125+ expect ( mockGlobalState . get ) . toHaveBeenCalledTimes ( GLOBAL_STATE_KEYS . length + 2 )
104126 } )
105127
106128 it ( "should handle default values correctly" , async ( ) => {
@@ -109,23 +131,16 @@ describe("ContextProxy", () => {
109131 expect ( result ) . toBe ( "deepseek" )
110132 } )
111133
112- it ( "should bypass cache for pass-through state keys" , async ( ) => {
113- // Setup mock return value
114- mockGlobalState . get . mockReturnValue ( "pass-through-value" )
115-
116- // Use a pass-through key (taskHistory)
134+ it ( "should return value from cache for taskHistory" , async ( ) => {
135+ // taskHistory is now loaded from file and stored in cache
117136 const result = proxy . getGlobalState ( "taskHistory" )
118137
119- // Should get value directly from original context
120- expect ( result ) . toBe ( "pass-through-value" )
121- expect ( mockGlobalState . get ) . toHaveBeenCalledWith ( "taskHistory" )
138+ // Should return the cached value (empty array from file read)
139+ expect ( result ) . toEqual ( [ ] )
122140 } )
123141
124- it ( "should respect default values for pass-through state keys" , async ( ) => {
125- // Setup mock to return undefined
126- mockGlobalState . get . mockReturnValue ( undefined )
127-
128- // Use a pass-through key with default value
142+ it ( "should respect default values for taskHistory" , async ( ) => {
143+ // Use taskHistory with default value
129144 const historyItems = [
130145 {
131146 id : "1" ,
@@ -140,8 +155,8 @@ describe("ContextProxy", () => {
140155
141156 const result = proxy . getGlobalState ( "taskHistory" , historyItems )
142157
143- // Should return default value when original context returns undefined
144- expect ( result ) . toBe ( historyItems )
158+ // Should return cached value (empty array) since it exists
159+ expect ( result ) . toEqual ( [ ] )
145160 } )
146161 } )
147162
@@ -157,7 +172,7 @@ describe("ContextProxy", () => {
157172 expect ( storedValue ) . toBe ( "deepseek" )
158173 } )
159174
160- it ( "should bypass cache for pass-through state keys " , async ( ) => {
175+ it ( "should write taskHistory to file instead of global state " , async ( ) => {
161176 const historyItems = [
162177 {
163178 id : "1" ,
@@ -172,16 +187,15 @@ describe("ContextProxy", () => {
172187
173188 await proxy . updateGlobalState ( "taskHistory" , historyItems )
174189
175- // Should update original context
176- expect ( mockGlobalState . update ) . toHaveBeenCalledWith ( "taskHistory" , historyItems )
190+ // Should NOT update original context for taskHistory
191+ expect ( mockGlobalState . update ) . not . toHaveBeenCalledWith ( "taskHistory" , historyItems )
177192
178- // Setup mock for subsequent get
179- mockGlobalState . get . mockReturnValue ( historyItems )
193+ // Should write to file (mocked in vscode.workspace.fs.writeFile)
194+ expect ( vscode . workspace . fs . writeFile ) . toHaveBeenCalled ( )
180195
181- // Should get fresh value from original context
196+ // Should get value from cache
182197 const storedValue = proxy . getGlobalState ( "taskHistory" )
183- expect ( storedValue ) . toBe ( historyItems )
184- expect ( mockGlobalState . get ) . toHaveBeenCalledWith ( "taskHistory" )
198+ expect ( storedValue ) . toEqual ( historyItems )
185199 } )
186200 } )
187201
0 commit comments