@@ -500,6 +500,13 @@ describe("ClineProvider", () => {
500500 let mockPostMessage : any
501501 let updateGlobalStateSpy : any
502502
503+ afterEach ( ( ) => {
504+ // Tests construct providers directly and many never dispose them; clear the
505+ // static registry so a leaked instance cannot claim stable view state ids
506+ // ahead of a later test.
507+ ClineProvider [ "activeInstances" ] = new Set ( )
508+ } )
509+
503510 beforeEach ( ( ) => {
504511 vi . clearAllMocks ( )
505512
@@ -2129,6 +2136,10 @@ describe("ClineProvider", () => {
21292136 } )
21302137 // @ts -ignore - Replace providerSettingsManager with a test double for the profile lookup.
21312138 provider . providerSettingsManager = { getProfile : getProfileSpy }
2139+ // The ownership check requires the writer to release the id before the
2140+ // next view adopts it; the durable entry it wrote stays in storage.
2141+ await writer . dispose ( )
2142+
21322143 await provider . contextProxy . setValue (
21332144 "viewStates" ,
21342145 mockContext . globalState . get < RooCodeSettings [ "viewStates" ] > ( "viewStates" ) ,
@@ -2141,7 +2152,6 @@ describe("ClineProvider", () => {
21412152 } )
21422153 expect ( getProfileSpy ) . toHaveBeenCalledWith ( { name : "my-profile" } )
21432154 expect ( logSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( "Loaded state for viewId" ) )
2144- await writer . dispose ( )
21452155 await provider . dispose ( )
21462156 } )
21472157
@@ -2269,6 +2279,10 @@ describe("ClineProvider", () => {
22692279 }
22702280 // A pre-load buffer write that was never persisted must not be merged over the load.
22712281 provider [ "viewLocalState" ] = { mode : "architect" }
2282+ // The ownership check requires the writer to release the id before the
2283+ // next view adopts it; the durable entry it wrote stays in storage.
2284+ await writer . dispose ( )
2285+
22722286 await provider . contextProxy . setValue (
22732287 "viewStates" ,
22742288 mockContext . globalState . get < RooCodeSettings [ "viewStates" ] > ( "viewStates" ) ,
@@ -2280,7 +2294,6 @@ describe("ClineProvider", () => {
22802294 apiProvider : providerIdentifiers . openrouter ,
22812295 openRouterModelId : "model-x" ,
22822296 } )
2283- await writer . dispose ( )
22842297 await provider . dispose ( )
22852298 } )
22862299
@@ -2710,6 +2723,10 @@ describe("ClineProvider", () => {
27102723 } ) ,
27112724 }
27122725 vi . spyOn ( provider , "postStateToWebview" ) . mockResolvedValue ( undefined )
2726+ // The ownership check requires the writer to release the id before the
2727+ // next view adopts it; the durable entry it wrote stays in storage.
2728+ await writer . dispose ( )
2729+
27132730 await provider . contextProxy . setValue (
27142731 "viewStates" ,
27152732 mockContext . globalState . get < RooCodeSettings [ "viewStates" ] > ( "viewStates" ) ,
@@ -2722,7 +2739,6 @@ describe("ClineProvider", () => {
27222739 // The buffer must track the activated profile so getValues() agrees with the proxy.
27232740 expect ( provider . getValues ( ) . currentApiConfigName ) . toBe ( "new-profile" )
27242741 expect ( provider . contextProxy . getValue ( "currentApiConfigName" ) ) . toBe ( "new-profile" )
2725- await writer . dispose ( )
27262742 await provider . dispose ( )
27272743 } )
27282744
@@ -6774,6 +6790,11 @@ describe("getTelemetryProperties", () => {
67746790 const { CloudService } = await import ( "@roo-code/cloud" )
67756791 const mockCloudService = {
67766792 isAuthenticated : vi . fn ( ) . mockReturnValue ( true ) ,
6793+
6794+ // Provider dispose detaches the settings listener; later tests construct
6795+ // and dispose providers against this surviving instance getter.
6796+ on : vi . fn ( ) ,
6797+ off : vi . fn ( ) ,
67776798 }
67786799
67796800 // Update the existing mock
@@ -6792,6 +6813,11 @@ describe("getTelemetryProperties", () => {
67926813 const { CloudService } = await import ( "@roo-code/cloud" )
67936814 const mockCloudService = {
67946815 isAuthenticated : vi . fn ( ) . mockReturnValue ( false ) ,
6816+
6817+ // Provider dispose detaches the settings listener; later tests construct
6818+ // and dispose providers against this surviving instance getter.
6819+ on : vi . fn ( ) ,
6820+ off : vi . fn ( ) ,
67956821 }
67966822
67976823 // Update the existing mock
@@ -6833,6 +6859,11 @@ describe("getTelemetryProperties", () => {
68336859 isAuthenticated : vi . fn ( ) . mockImplementation ( ( ) => {
68346860 throw new Error ( "Authentication check error" )
68356861 } ) ,
6862+
6863+ // Provider dispose detaches the settings listener; later tests construct
6864+ // and dispose providers against this surviving instance getter.
6865+ on : vi . fn ( ) ,
6866+ off : vi . fn ( ) ,
68366867 }
68376868
68386869 // Update the existing mock
@@ -8337,12 +8368,15 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
83378368 } ) ,
83388369 true ,
83398370 )
8340- expect ( saveConfig ) . toHaveBeenCalledWith (
8371+ // The non-active profile now goes through upsertProviderProfile so the views
8372+ // pinned to it are refreshed, not just the file on disk.
8373+ expect ( upsertSpy ) . toHaveBeenCalledWith (
83418374 "Backup Zoo" ,
83428375 expect . objectContaining ( {
83438376 zooSessionToken : "new-token" ,
83448377 zooGatewayBaseUrl : "https://www.zoocode.dev/api/gateway/v1" ,
83458378 } ) ,
8379+ false ,
83468380 )
83478381 } )
83488382
@@ -8439,4 +8473,85 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
84398473 } )
84408474 } )
84418475 } )
8476+ describe ( "profile save without activation and overlay completeness" , ( ) => {
8477+ it ( "refreshes views pinned to a profile saved without activation" , async ( ) => {
8478+ // Defensive: the doubles in this suite can leave console writes pending
8479+ // at worker teardown.
8480+ vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => { } )
8481+ vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => { } )
8482+ vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } )
8483+ const sibling = new ClineProvider (
8484+ mockContext ,
8485+ mockOutputChannel ,
8486+ "editor" ,
8487+ new ContextProxy ( mockContext ) ,
8488+ new WebviewFocusTracker ( ) ,
8489+ )
8490+ try {
8491+ // The sibling loaded the profile earlier: its buffer pins the name and
8492+ // carries the settings snapshot taken at that load.
8493+ sibling [ "viewLocalState" ] . currentApiConfigName = "saved-profile"
8494+ sibling [ "viewLocalState" ] . apiConfiguration = {
8495+ apiProvider : providerIdentifiers . anthropic ,
8496+ apiKey : "old-key" ,
8497+ }
8498+ vi . spyOn ( sibling , "postStateToWebview" ) . mockResolvedValue ( undefined )
8499+ vi . spyOn ( provider , "postStateToWebview" ) . mockResolvedValue ( undefined )
8500+ // @ts -ignore - Replace providerSettingsManager with a test double.
8501+ provider . providerSettingsManager = {
8502+ saveConfig : vi . fn ( ) . mockResolvedValue ( "saved-id" ) ,
8503+ listConfig : vi . fn ( ) . mockResolvedValue ( [ ] ) ,
8504+ setModeConfig : vi . fn ( ) ,
8505+ }
8506+
8507+ await provider . upsertProviderProfile (
8508+ "saved-profile" ,
8509+ { apiProvider : providerIdentifiers . anthropic , apiKey : "new-key" } ,
8510+ false ,
8511+ )
8512+
8513+ // A save that does not activate still rewrites the profile; the pinned
8514+ // sibling must not keep serving the pre-save snapshot.
8515+ expect ( sibling [ "viewLocalState" ] . apiConfiguration ) . toEqual ( {
8516+ apiProvider : providerIdentifiers . anthropic ,
8517+ apiKey : "new-key" ,
8518+ } )
8519+ // Let queued view-local persistence settle while the console spies are
8520+ // still installed, so teardown has no pending writes.
8521+ await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) )
8522+ } finally {
8523+ await sibling . dispose ( )
8524+ }
8525+ } )
8526+
8527+ it ( "does not leak shared provider keys into a pinned view's api configuration" , async ( ) => {
8528+ vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => { } )
8529+ vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => { } )
8530+ // The shared store sits on a profile with keys the pinned profile never
8531+ // sets; spreading the overlay over the shared keys would report a mixed
8532+ // configuration (e.g. another profile's model id and region).
8533+ await provider . contextProxy . setValue ( "apiProvider" , providerIdentifiers . bedrock )
8534+ await provider . contextProxy . setValue ( "apiModelId" , "anthropic.claude-v1" )
8535+ await provider . contextProxy . setValue ( "awsRegion" , "us-east-1" )
8536+ provider [ "viewLocalState" ] . currentApiConfigName = "pinned-profile"
8537+ provider [ "viewLocalState" ] . apiConfiguration = {
8538+ apiProvider : providerIdentifiers . anthropic ,
8539+ apiKey : "k" ,
8540+ }
8541+
8542+ // The cloud doubles in this suite make getState() log its defensive catch
8543+ // branches; silence them so teardown has no pending console writes.
8544+ vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } )
8545+
8546+ const state = await provider . getState ( )
8547+
8548+ expect ( state . apiConfiguration . apiProvider ) . toBe ( providerIdentifiers . anthropic )
8549+ expect ( state . apiConfiguration . apiKey ) . toBe ( "k" )
8550+ expect ( state . apiConfiguration . apiModelId ) . toBeUndefined ( )
8551+ expect ( state . apiConfiguration . awsRegion ) . toBeUndefined ( )
8552+
8553+ // Let any deferred logging settle while the spies are installed.
8554+ await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) )
8555+ } )
8556+ } )
84428557} )
0 commit comments