@@ -499,6 +499,13 @@ describe("ClineProvider", () => {
499499 let mockPostMessage : any
500500 let updateGlobalStateSpy : any
501501
502+ afterEach ( ( ) => {
503+ // Tests construct providers directly and many never dispose them; clear the
504+ // static registry so a leaked instance cannot claim stable view state ids
505+ // ahead of a later test.
506+ ClineProvider [ "activeInstances" ] = new Set ( )
507+ } )
508+
502509 beforeEach ( ( ) => {
503510 vi . clearAllMocks ( )
504511
@@ -2130,6 +2137,10 @@ describe("ClineProvider", () => {
21302137 } )
21312138 // @ts -ignore - Replace providerSettingsManager with a test double for the profile lookup.
21322139 provider . providerSettingsManager = { getProfile : getProfileSpy }
2140+ // The ownership check requires the writer to release the id before the
2141+ // next view adopts it; the durable entry it wrote stays in storage.
2142+ await writer . dispose ( )
2143+
21332144 await provider . contextProxy . setValue (
21342145 "viewStates" ,
21352146 mockContext . globalState . get < RooCodeSettings [ "viewStates" ] > ( "viewStates" ) ,
@@ -2142,7 +2153,6 @@ describe("ClineProvider", () => {
21422153 } )
21432154 expect ( getProfileSpy ) . toHaveBeenCalledWith ( { name : "my-profile" } )
21442155 expect ( logSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( "Loaded state for viewId" ) )
2145- await writer . dispose ( )
21462156 await provider . dispose ( )
21472157 } )
21482158
@@ -2270,6 +2280,10 @@ describe("ClineProvider", () => {
22702280 }
22712281 // A pre-load buffer write that was never persisted must not be merged over the load.
22722282 provider [ "viewLocalState" ] = { mode : "architect" }
2283+ // The ownership check requires the writer to release the id before the
2284+ // next view adopts it; the durable entry it wrote stays in storage.
2285+ await writer . dispose ( )
2286+
22732287 await provider . contextProxy . setValue (
22742288 "viewStates" ,
22752289 mockContext . globalState . get < RooCodeSettings [ "viewStates" ] > ( "viewStates" ) ,
@@ -2281,7 +2295,6 @@ describe("ClineProvider", () => {
22812295 apiProvider : providerIdentifiers . openrouter ,
22822296 openRouterModelId : "model-x" ,
22832297 } )
2284- await writer . dispose ( )
22852298 await provider . dispose ( )
22862299 } )
22872300
@@ -2693,6 +2706,10 @@ describe("ClineProvider", () => {
26932706 } ) ,
26942707 }
26952708 vi . spyOn ( provider , "postStateToWebview" ) . mockResolvedValue ( undefined )
2709+ // The ownership check requires the writer to release the id before the
2710+ // next view adopts it; the durable entry it wrote stays in storage.
2711+ await writer . dispose ( )
2712+
26962713 await provider . contextProxy . setValue (
26972714 "viewStates" ,
26982715 mockContext . globalState . get < RooCodeSettings [ "viewStates" ] > ( "viewStates" ) ,
@@ -2705,7 +2722,6 @@ describe("ClineProvider", () => {
27052722 // The buffer must track the activated profile so getValues() agrees with the proxy.
27062723 expect ( provider . getValues ( ) . currentApiConfigName ) . toBe ( "new-profile" )
27072724 expect ( provider . contextProxy . getValue ( "currentApiConfigName" ) ) . toBe ( "new-profile" )
2708- await writer . dispose ( )
27092725 await provider . dispose ( )
27102726 } )
27112727
@@ -5768,6 +5784,11 @@ describe("getTelemetryProperties", () => {
57685784 const { CloudService } = await import ( "@roo-code/cloud" )
57695785 const mockCloudService = {
57705786 isAuthenticated : vi . fn ( ) . mockReturnValue ( true ) ,
5787+
5788+ // Provider dispose detaches the settings listener; later tests construct
5789+ // and dispose providers against this surviving instance getter.
5790+ on : vi . fn ( ) ,
5791+ off : vi . fn ( ) ,
57715792 }
57725793
57735794 // Update the existing mock
@@ -5786,6 +5807,11 @@ describe("getTelemetryProperties", () => {
57865807 const { CloudService } = await import ( "@roo-code/cloud" )
57875808 const mockCloudService = {
57885809 isAuthenticated : vi . fn ( ) . mockReturnValue ( false ) ,
5810+
5811+ // Provider dispose detaches the settings listener; later tests construct
5812+ // and dispose providers against this surviving instance getter.
5813+ on : vi . fn ( ) ,
5814+ off : vi . fn ( ) ,
57895815 }
57905816
57915817 // Update the existing mock
@@ -5827,6 +5853,11 @@ describe("getTelemetryProperties", () => {
58275853 isAuthenticated : vi . fn ( ) . mockImplementation ( ( ) => {
58285854 throw new Error ( "Authentication check error" )
58295855 } ) ,
5856+
5857+ // Provider dispose detaches the settings listener; later tests construct
5858+ // and dispose providers against this surviving instance getter.
5859+ on : vi . fn ( ) ,
5860+ off : vi . fn ( ) ,
58305861 }
58315862
58325863 // Update the existing mock
@@ -7331,12 +7362,15 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
73317362 } ) ,
73327363 true ,
73337364 )
7334- expect ( saveConfig ) . toHaveBeenCalledWith (
7365+ // The non-active profile now goes through upsertProviderProfile so the views
7366+ // pinned to it are refreshed, not just the file on disk.
7367+ expect ( upsertSpy ) . toHaveBeenCalledWith (
73357368 "Backup Zoo" ,
73367369 expect . objectContaining ( {
73377370 zooSessionToken : "new-token" ,
73387371 zooGatewayBaseUrl : "https://www.zoocode.dev/api/gateway/v1" ,
73397372 } ) ,
7373+ false ,
73407374 )
73417375 } )
73427376
@@ -7433,4 +7467,59 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
74337467 } )
74347468 } )
74357469 } )
7470+ describe ( "profile save without activation and overlay completeness" , ( ) => {
7471+ it ( "refreshes views pinned to a profile saved without activation" , async ( ) => {
7472+ // Defensive: the doubles in this suite can leave console writes pending
7473+ // at worker teardown.
7474+ vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => { } )
7475+ vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => { } )
7476+ vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } )
7477+ const sibling = new ClineProvider (
7478+ mockContext ,
7479+ mockOutputChannel ,
7480+ "editor" ,
7481+ new ContextProxy ( mockContext ) ,
7482+ new WebviewFocusTracker ( ) ,
7483+ )
7484+ try {
7485+ // The sibling loaded the profile earlier: its buffer pins the name and
7486+ // carries the settings snapshot taken at that load.
7487+ sibling [ "viewLocalState" ] . currentApiConfigName = "saved-profile"
7488+ sibling [ "viewLocalState" ] . apiConfiguration = {
7489+ apiProvider : providerIdentifiers . anthropic ,
7490+ apiKey : "old-key" ,
7491+ }
7492+ vi . spyOn ( sibling , "postStateToWebview" ) . mockResolvedValue ( undefined )
7493+ vi . spyOn ( provider , "postStateToWebview" ) . mockResolvedValue ( undefined )
7494+ // @ts -ignore - Replace providerSettingsManager with a test double.
7495+ provider . providerSettingsManager = {
7496+ saveConfig : vi . fn ( ) . mockResolvedValue ( "saved-id" ) ,
7497+ listConfig : vi . fn ( ) . mockResolvedValue ( [ ] ) ,
7498+ setModeConfig : vi . fn ( ) ,
7499+ getProfile : vi
7500+ . fn ( )
7501+ . mockResolvedValue ( { name : "saved-profile" , apiProvider : providerIdentifiers . anthropic } ) ,
7502+ getModeConfigId : vi . fn ( ) . mockResolvedValue ( undefined ) ,
7503+ }
7504+
7505+ await provider . upsertProviderProfile (
7506+ "saved-profile" ,
7507+ { apiProvider : providerIdentifiers . anthropic , apiKey : "new-key" } ,
7508+ false ,
7509+ )
7510+
7511+ // A save that does not activate still rewrites the profile; the pinned
7512+ // sibling must not keep serving the pre-save snapshot.
7513+ expect ( sibling [ "viewLocalState" ] . apiConfiguration ) . toEqual ( {
7514+ apiProvider : providerIdentifiers . anthropic ,
7515+ apiKey : "new-key" ,
7516+ } )
7517+ // Let queued view-local persistence settle while the console spies are
7518+ // still installed, so teardown has no pending writes.
7519+ await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) )
7520+ } finally {
7521+ await sibling . dispose ( )
7522+ }
7523+ } )
7524+ } )
74367525} )
0 commit comments