@@ -496,6 +496,13 @@ describe("ClineProvider", () => {
496496 let mockPostMessage : any
497497 let updateGlobalStateSpy : any
498498
499+ afterEach ( ( ) => {
500+ // Tests construct providers directly and many never dispose them; clear the
501+ // static registry so a leaked instance cannot claim stable view state ids
502+ // ahead of a later test.
503+ ClineProvider [ "activeInstances" ] = new Set ( )
504+ } )
505+
499506 beforeEach ( ( ) => {
500507 vi . clearAllMocks ( )
501508
@@ -2123,6 +2130,10 @@ describe("ClineProvider", () => {
21232130 apiProvider : providerIdentifiers . openrouter ,
21242131 openRouterModelId : "model-x" ,
21252132 } )
2133+ // The ownership check requires the writer to release the id before the
2134+ // next view adopts it; the durable entry it wrote stays in storage.
2135+ await writer . dispose ( )
2136+
21262137 await provider . contextProxy . setValue (
21272138 "viewStates" ,
21282139 mockContext . globalState . get < RooCodeSettings [ "viewStates" ] > ( "viewStates" ) ,
@@ -2135,7 +2146,6 @@ describe("ClineProvider", () => {
21352146 } )
21362147 expect ( getProfileSpy ) . toHaveBeenCalledWith ( { name : "my-profile" } )
21372148 expect ( logSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( "Loaded state for viewId" ) )
2138- await writer . dispose ( )
21392149 await provider . dispose ( )
21402150 } )
21412151
@@ -2256,6 +2266,10 @@ describe("ClineProvider", () => {
22562266 } )
22572267 // A pre-load buffer write that was never persisted must not be merged over the load.
22582268 provider [ "viewLocalState" ] = { mode : "architect" }
2269+ // The ownership check requires the writer to release the id before the
2270+ // next view adopts it; the durable entry it wrote stays in storage.
2271+ await writer . dispose ( )
2272+
22592273 await provider . contextProxy . setValue (
22602274 "viewStates" ,
22612275 mockContext . globalState . get < RooCodeSettings [ "viewStates" ] > ( "viewStates" ) ,
@@ -2267,7 +2281,6 @@ describe("ClineProvider", () => {
22672281 apiProvider : providerIdentifiers . openrouter ,
22682282 openRouterModelId : "model-x" ,
22692283 } )
2270- await writer . dispose ( )
22712284 await provider . dispose ( )
22722285 } )
22732286
@@ -2652,6 +2665,10 @@ describe("ClineProvider", () => {
26522665 apiProvider : providerIdentifiers . anthropic ,
26532666 } )
26542667 vi . spyOn ( provider , "postStateToWebview" ) . mockResolvedValue ( undefined )
2668+ // The ownership check requires the writer to release the id before the
2669+ // next view adopts it; the durable entry it wrote stays in storage.
2670+ await writer . dispose ( )
2671+
26552672 await provider . contextProxy . setValue (
26562673 "viewStates" ,
26572674 mockContext . globalState . get < RooCodeSettings [ "viewStates" ] > ( "viewStates" ) ,
@@ -2664,7 +2681,6 @@ describe("ClineProvider", () => {
26642681 // The buffer must track the activated profile so getValues() agrees with the proxy.
26652682 expect ( provider . getValues ( ) . currentApiConfigName ) . toBe ( "new-profile" )
26662683 expect ( provider . contextProxy . getValue ( "currentApiConfigName" ) ) . toBe ( "new-profile" )
2667- await writer . dispose ( )
26682684 await provider . dispose ( )
26692685 } )
26702686
@@ -6579,6 +6595,11 @@ describe("getTelemetryProperties", () => {
65796595 const { CloudService } = await import ( "@roo-code/cloud" )
65806596 const mockCloudService = {
65816597 isAuthenticated : vi . fn ( ) . mockReturnValue ( true ) ,
6598+
6599+ // Provider dispose detaches the settings listener; later tests construct
6600+ // and dispose providers against this surviving instance getter.
6601+ on : vi . fn ( ) ,
6602+ off : vi . fn ( ) ,
65826603 }
65836604
65846605 // Update the existing mock
@@ -6597,6 +6618,11 @@ describe("getTelemetryProperties", () => {
65976618 const { CloudService } = await import ( "@roo-code/cloud" )
65986619 const mockCloudService = {
65996620 isAuthenticated : vi . fn ( ) . mockReturnValue ( false ) ,
6621+
6622+ // Provider dispose detaches the settings listener; later tests construct
6623+ // and dispose providers against this surviving instance getter.
6624+ on : vi . fn ( ) ,
6625+ off : vi . fn ( ) ,
66006626 }
66016627
66026628 // Update the existing mock
@@ -6638,6 +6664,11 @@ describe("getTelemetryProperties", () => {
66386664 isAuthenticated : vi . fn ( ) . mockImplementation ( ( ) => {
66396665 throw new Error ( "Authentication check error" )
66406666 } ) ,
6667+
6668+ // Provider dispose detaches the settings listener; later tests construct
6669+ // and dispose providers against this surviving instance getter.
6670+ on : vi . fn ( ) ,
6671+ off : vi . fn ( ) ,
66416672 }
66426673
66436674 // Update the existing mock
@@ -8247,4 +8278,89 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
82478278 } )
82488279 } )
82498280 } )
8281+ describe ( "profile save without activation and overlay completeness" , ( ) => {
8282+ it ( "refreshes views pinned to a profile saved without activation" , async ( ) => {
8283+ // Defensive: the doubles in this suite can leave console writes pending
8284+ // at worker teardown.
8285+ vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => { } )
8286+ vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => { } )
8287+ vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } )
8288+ const sibling = new ClineProvider (
8289+ mockContext ,
8290+ mockOutputChannel ,
8291+ "editor" ,
8292+ new ContextProxy ( mockContext ) ,
8293+ new WebviewFocusTracker ( ) ,
8294+ )
8295+ try {
8296+ // The sibling loaded the profile earlier: its buffer pins the name and
8297+ // carries the settings snapshot taken at that load.
8298+ sibling [ "viewLocalState" ] . currentApiConfigName = "saved-profile"
8299+ sibling [ "viewLocalState" ] . apiConfiguration = {
8300+ apiProvider : providerIdentifiers . anthropic ,
8301+ apiKey : "old-key" ,
8302+ }
8303+ vi . spyOn ( sibling , "postStateToWebview" ) . mockResolvedValue ( undefined )
8304+ vi . spyOn ( provider , "postStateToWebview" ) . mockResolvedValue ( undefined )
8305+ // @ts -ignore - Replace providerSettingsManager with a test double.
8306+ provider . providerSettingsManager = {
8307+ saveConfig : vi . fn ( ) . mockResolvedValue ( "saved-id" ) ,
8308+ listConfig : vi . fn ( ) . mockResolvedValue ( [ ] ) ,
8309+ setModeConfig : vi . fn ( ) ,
8310+ getProfile : vi
8311+ . fn ( )
8312+ . mockResolvedValue ( { name : "saved-profile" , apiProvider : providerIdentifiers . anthropic } ) ,
8313+ getModeConfigId : vi . fn ( ) . mockResolvedValue ( undefined ) ,
8314+ }
8315+
8316+ await provider . upsertProviderProfile (
8317+ "saved-profile" ,
8318+ { apiProvider : providerIdentifiers . anthropic , apiKey : "new-key" } ,
8319+ false ,
8320+ )
8321+
8322+ // A save that does not activate still rewrites the profile; the pinned
8323+ // sibling must not keep serving the pre-save snapshot.
8324+ expect ( sibling [ "viewLocalState" ] . apiConfiguration ) . toEqual ( {
8325+ apiProvider : providerIdentifiers . anthropic ,
8326+ apiKey : "new-key" ,
8327+ } )
8328+ // Let queued view-local persistence settle while the console spies are
8329+ // still installed, so teardown has no pending writes.
8330+ await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) )
8331+ } finally {
8332+ await sibling . dispose ( )
8333+ }
8334+ } )
8335+
8336+ it ( "does not leak shared provider keys into a pinned view's api configuration" , async ( ) => {
8337+ vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => { } )
8338+ vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => { } )
8339+ // The shared store sits on a profile with keys the pinned profile never
8340+ // sets; spreading the overlay over the shared keys would report a mixed
8341+ // configuration (e.g. another profile's model id and region).
8342+ await provider . contextProxy . setValue ( "apiProvider" , providerIdentifiers . bedrock )
8343+ await provider . contextProxy . setValue ( "apiModelId" , "anthropic.claude-v1" )
8344+ await provider . contextProxy . setValue ( "awsRegion" , "us-east-1" )
8345+ provider [ "viewLocalState" ] . currentApiConfigName = "pinned-profile"
8346+ provider [ "viewLocalState" ] . apiConfiguration = {
8347+ apiProvider : providerIdentifiers . anthropic ,
8348+ apiKey : "k" ,
8349+ }
8350+
8351+ // The cloud doubles in this suite make getState() log its defensive catch
8352+ // branches; silence them so teardown has no pending console writes.
8353+ vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } )
8354+
8355+ const state = await provider . getState ( )
8356+
8357+ expect ( state . apiConfiguration . apiProvider ) . toBe ( providerIdentifiers . anthropic )
8358+ expect ( state . apiConfiguration . apiKey ) . toBe ( "k" )
8359+ expect ( state . apiConfiguration . apiModelId ) . toBeUndefined ( )
8360+ expect ( state . apiConfiguration . awsRegion ) . toBeUndefined ( )
8361+
8362+ // Let any deferred logging settle while the spies are installed.
8363+ await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) )
8364+ } )
8365+ } )
82508366} )
0 commit comments