@@ -21,7 +21,7 @@ jest.mock("vscode", () => ({
2121 } ,
2222 env : {
2323 sessionId : "test-session-id" ,
24- machineId : "test-machine-id"
24+ machineId : "test-machine-id" ,
2525 } ,
2626 workspace : {
2727 name : "test-workspace-name" ,
@@ -130,9 +130,35 @@ describe("ContextProxy", () => {
130130 expect ( mockGlobalState . update ) . toHaveBeenCalledWith ( "test-key" , "new-value" )
131131
132132 // Should have stored the value in cache
133- const storedValue = await proxy . getGlobalState ( "test-key" )
133+ const storedValue = proxy . getGlobalState ( "test-key" )
134134 expect ( storedValue ) . toBe ( "new-value" )
135135 } )
136+
137+ it ( "should handle window-specific keys correctly" , async ( ) => {
138+ // Test with a window-specific key
139+ await proxy . updateGlobalState ( "mode" , "test-mode" )
140+
141+ // Should have called update with window-specific key
142+ expect ( mockGlobalState . update ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ w i n d o w : .+ : m o d e $ / ) , "test-mode" )
143+
144+ // Should have stored the value in cache with the original key
145+ const storedValue = proxy . getGlobalState ( "mode" )
146+ expect ( storedValue ) . toBe ( "test-mode" )
147+ } )
148+
149+ it ( "should throw and not update cache if storage update fails" , async ( ) => {
150+ // Mock a failure in the storage update
151+ mockGlobalState . update . mockRejectedValueOnce ( new Error ( "Storage update failed" ) )
152+
153+ // Set initial cache value
154+ proxy [ "stateCache" ] . set ( "error-key" , "initial-value" )
155+
156+ // Attempt to update should fail
157+ await expect ( proxy . updateGlobalState ( "error-key" , "new-value" ) ) . rejects . toThrow ( "Storage update failed" )
158+
159+ // Cache should still have the initial value
160+ expect ( proxy . getGlobalState ( "error-key" ) ) . toBe ( "initial-value" )
161+ } )
136162 } )
137163
138164 describe ( "getSecret" , ( ) => {
@@ -356,22 +382,22 @@ describe("ContextProxy", () => {
356382 it ( "should use window-specific key for mode" , async ( ) => {
357383 // Ensure 'mode' is in window specific keys
358384 expect ( WINDOW_SPECIFIC_KEYS ) . toContain ( "mode" )
359-
385+
360386 // Test update method with 'mode' key
361387 await proxy . updateGlobalState ( "mode" , "debug" )
362-
388+
363389 // Verify it's called with window-specific key
364390 expect ( mockGlobalState . update ) . toHaveBeenCalledWith ( expect . stringMatching ( / ^ w i n d o w : .+ : m o d e $ / ) , "debug" )
365391 } )
366-
392+
367393 it ( "should use regular key for non-window-specific state" , async ( ) => {
368394 // Test update method with a regular key
369395 await proxy . updateGlobalState ( "apiProvider" , "test-provider" )
370-
396+
371397 // Verify it's called with regular key
372398 expect ( mockGlobalState . update ) . toHaveBeenCalledWith ( "apiProvider" , "test-provider" )
373399 } )
374-
400+
375401 it ( "should consistently use same key format for get/update operations" , async ( ) => {
376402 // Set mock values for testing
377403 const windowKeyPattern = / ^ w i n d o w : .+ : m o d e $ /
@@ -380,20 +406,20 @@ describe("ContextProxy", () => {
380406 if ( key === "mode" ) return "global-debug-mode"
381407 return undefined
382408 } )
383-
409+
384410 // Update a window-specific value
385411 await proxy . updateGlobalState ( "mode" , "test-mode" )
386-
412+
387413 // The key used in update should match pattern
388414 const updateCallArg = mockGlobalState . update . mock . calls [ 0 ] [ 0 ]
389415 expect ( updateCallArg ) . toMatch ( windowKeyPattern )
390-
416+
391417 // Re-init to load values
392418 proxy [ "initializeStateCache" ] ( )
393-
419+
394420 // Verify we get the window-specific value back
395421 const value = proxy . getGlobalState ( "mode" )
396-
422+
397423 // We should get the window-specific value, not the global one
398424 expect ( mockGlobalState . get ) . toHaveBeenCalledWith ( expect . stringMatching ( windowKeyPattern ) )
399425 expect ( value ) . not . toBe ( "global-debug-mode" )
@@ -403,61 +429,62 @@ describe("ContextProxy", () => {
403429 describe ( "Enhanced window ID generation" , ( ) => {
404430 it ( "should generate a window ID that includes workspace name" , ( ) => {
405431 // Access the private method using type assertion
406- const generateWindowId = ( proxy as any ) . generateWindowId . bind ( proxy ) ;
407- const windowId = generateWindowId ( ) ;
408-
432+ const generateWindowId = ( proxy as any ) . generateWindowId . bind ( proxy )
433+ const windowId = generateWindowId ( )
434+
409435 // Should include the workspace name from our mock
410- expect ( windowId ) . toContain ( "test-workspace-name" ) ;
411- } ) ;
412-
436+ expect ( windowId ) . toContain ( "test-workspace-name" )
437+ } )
438+
413439 it ( "should generate a window ID that includes machine ID" , ( ) => {
414440 // Access the private method using type assertion
415- const generateWindowId = ( proxy as any ) . generateWindowId . bind ( proxy ) ;
416- const windowId = generateWindowId ( ) ;
417-
441+ const generateWindowId = ( proxy as any ) . generateWindowId . bind ( proxy )
442+ const windowId = generateWindowId ( )
443+
418444 // Should include the machine ID from our mock
419- expect ( windowId ) . toContain ( "test-machine-id" ) ;
420- } ) ;
421-
445+ expect ( windowId ) . toContain ( "test-machine-id" )
446+ } )
447+
422448 it ( "should use the fallback mechanism if generateWindowId fails" , ( ) => {
423449 // Create a proxy instance with a failing generateWindowId method
424- const spyOnGenerate = jest . spyOn ( ContextProxy . prototype as any , "generateWindowId" )
425- . mockImplementation ( ( ) => "" ) ;
426-
450+ const spyOnGenerate = jest
451+ . spyOn ( ContextProxy . prototype as any , "generateWindowId" )
452+ . mockImplementation ( ( ) => "" )
453+
427454 // Create a new proxy to trigger the constructor with our mock
428- const testProxy = new ContextProxy ( mockContext ) ;
429-
455+ const testProxy = new ContextProxy ( mockContext )
456+
430457 // Should have called ensureUniqueWindowId with a fallback
431- expect ( spyOnGenerate ) . toHaveBeenCalled ( ) ;
432-
458+ expect ( spyOnGenerate ) . toHaveBeenCalled ( )
459+
433460 // The windowId should use the fallback format (random ID)
434461 // We can't test the exact value, but we can verify it's not empty
435- expect ( ( testProxy as any ) . windowId ) . not . toBe ( "" ) ;
436-
462+ expect ( ( testProxy as any ) . windowId ) . not . toBe ( "" )
463+
437464 // Restore original implementation
438- spyOnGenerate . mockRestore ( ) ;
439- } ) ;
440-
465+ spyOnGenerate . mockRestore ( )
466+ } )
467+
441468 it ( "should create consistent session hash for same input" , ( ) => {
442469 // Access the private method using type assertion
443- const createSessionHash = ( proxy as any ) . createSessionHash . bind ( proxy ) ;
444-
445- const hash1 = createSessionHash ( "test-input" ) ;
446- const hash2 = createSessionHash ( "test-input" ) ;
447-
470+ const createSessionHash = ( proxy as any ) . createSessionHash . bind ( proxy )
471+
472+ const hash1 = createSessionHash ( "test-input" )
473+ const hash2 = createSessionHash ( "test-input" )
474+
448475 // Same input should produce same hash within the same session
449- expect ( hash1 ) . toBe ( hash2 ) ;
450- } ) ;
451-
476+ expect ( hash1 ) . toBe ( hash2 )
477+ } )
478+
452479 it ( "should create different session hashes for different inputs" , ( ) => {
453480 // Access the private method using type assertion
454- const createSessionHash = ( proxy as any ) . createSessionHash . bind ( proxy ) ;
455-
456- const hash1 = createSessionHash ( "test-input-1" ) ;
457- const hash2 = createSessionHash ( "test-input-2" ) ;
458-
481+ const createSessionHash = ( proxy as any ) . createSessionHash . bind ( proxy )
482+
483+ const hash1 = createSessionHash ( "test-input-1" )
484+ const hash2 = createSessionHash ( "test-input-2" )
485+
459486 // Different inputs should produce different hashes
460- expect ( hash1 ) . not . toBe ( hash2 ) ;
461- } ) ;
462- } ) ;
487+ expect ( hash1 ) . not . toBe ( hash2 )
488+ } )
489+ } )
463490} )
0 commit comments