@@ -69,7 +69,7 @@ vi.mock("@roo-code/telemetry", () => ({
6969 } ,
7070} ) )
7171
72- import type { ModelRecord } from "@roo-code/types"
72+ import type { ModelRecord , RooCodeSettings } from "@roo-code/types"
7373
7474import { webviewMessageHandler } from "../webviewMessageHandler"
7575import type { ClineProvider } from "../ClineProvider"
@@ -99,6 +99,7 @@ const mockFetchOpenAiCodexRateLimitInfo = vi.mocked(fetchOpenAiCodexRateLimitInf
9999const mockClineProvider = {
100100 getState : vi . fn ( ) ,
101101 postMessageToWebview : vi . fn ( ) ,
102+ saveViewState : vi . fn ( ) ,
102103 customModesManager : {
103104 getCustomModes : vi . fn ( ) ,
104105 deleteCustomMode : vi . fn ( ) ,
@@ -115,6 +116,16 @@ const mockClineProvider = {
115116 setValue : vi . fn ( ) ,
116117 getValue : vi . fn ( ) ,
117118 } ,
119+ // Delegates to contextProxy.setValue so existing assertions keep holding while
120+ // the updateSettings flow is exercised through the provider-level mutation path.
121+ setValue : vi
122+ . fn ( )
123+ . mockImplementation ( ( key : string , value : unknown ) =>
124+ mockClineProvider . contextProxy . setValue (
125+ key as keyof RooCodeSettings ,
126+ value as RooCodeSettings [ keyof RooCodeSettings ] ,
127+ ) ,
128+ ) ,
118129 log : vi . fn ( ) ,
119130 postStateToWebview : vi . fn ( ) ,
120131 resolveWebviewThemeFixtureProbe : vi . fn ( ) ,
@@ -261,6 +272,111 @@ import { Terminal } from "../../../integrations/terminal/Terminal"
261272import { TerminalRegistry } from "../../../integrations/terminal/TerminalRegistry"
262273import { providerIdentifiers , retiredProviderIdentifiers } from "@roo-code/types/provider-identifiers"
263274
275+ describe ( "webviewMessageHandler - webviewDidLaunch" , ( ) => {
276+ // Structural view of the provider members this suite reassigns at runtime: the
277+ // double literal does not declare them and some are readonly on the class, so a
278+ // cast of the mock target alone cannot express these reassignments without any.
279+ type LaunchProviderFixture = {
280+ setViewStateId : ( viewStateId : string ) => Promise < void >
281+ workspaceTracker : { initializeFilePaths : ( ) => Promise < void > }
282+ providerSettingsManager : {
283+ listConfig : ( ) => Promise < unknown [ ] >
284+ hasConfig : ( name : string ) => Promise < boolean >
285+ }
286+ activateProviderProfile : ( options : { name : string } ) => Promise < void >
287+ getMcpHub : ( ) => unknown
288+ getStateToPostToWebview : ( ) => Promise < { telemetrySetting : string } >
289+ }
290+ const double = mockClineProvider as unknown as LaunchProviderFixture
291+
292+ beforeEach ( ( ) => {
293+ vi . clearAllMocks ( )
294+ vi . mocked ( mockClineProvider . getState ) . mockResolvedValue ( {
295+ apiConfiguration : { apiProvider : providerIdentifiers . anthropic } ,
296+ currentApiConfigName : "view-local-profile" ,
297+ } as unknown as Awaited < ReturnType < typeof mockClineProvider . getState > > )
298+ double . setViewStateId = vi . fn ( ) . mockResolvedValue ( undefined )
299+ double . workspaceTracker = { initializeFilePaths : vi . fn ( ) . mockResolvedValue ( undefined ) }
300+ double . providerSettingsManager = {
301+ listConfig : vi
302+ . fn ( )
303+ . mockResolvedValue ( [ { name : "shared-profile" , apiProvider : providerIdentifiers . anthropic } ] ) ,
304+ hasConfig : vi . fn ( ) . mockResolvedValue ( false ) ,
305+ }
306+ double . activateProviderProfile = vi . fn ( ) . mockResolvedValue ( undefined )
307+ double . getMcpHub = vi . fn ( ) . mockReturnValue ( undefined )
308+ double . getStateToPostToWebview = vi . fn ( ) . mockResolvedValue ( { telemetrySetting : "disabled" } )
309+ vi . mocked ( mockClineProvider . customModesManager . getCustomModes ) . mockResolvedValue ( [ ] )
310+ // Key-aware so a mutated global-state key (e.g. "") resolves to nothing instead
311+ // of the canned value, keeping the re-pin branch's global lookup observable.
312+ vi . mocked ( mockClineProvider . contextProxy . getValue ) . mockImplementation ( ( key : string ) =>
313+ key === "currentApiConfigName" ? "shared-profile" : undefined ,
314+ )
315+ vi . mocked ( mockClineProvider . contextProxy . setValue ) . mockResolvedValue ( undefined )
316+ } )
317+
318+ it ( "validates the view-local currentApiConfigName on launch" , async ( ) => {
319+ await webviewMessageHandler ( mockClineProvider , { type : "webviewDidLaunch" , viewStateId : "view-1" } )
320+ await new Promise ( ( resolve ) => setImmediate ( resolve ) )
321+
322+ expect ( double . setViewStateId ) . toHaveBeenCalledWith ( "view-1" )
323+
324+ // The merged (view-local) name is validated first; the shared global is only
325+ // consulted when the view-local name is invalid.
326+ expect ( double . providerSettingsManager . hasConfig ) . toHaveBeenCalledWith ( "view-local-profile" )
327+ expect ( mockClineProvider . providerSettingsManager . hasConfig ) . toHaveBeenCalledWith ( "shared-profile" )
328+ // Both names are invalid in this setup, so the shared global is repaired.
329+ expect ( mockClineProvider . contextProxy . setValue ) . toHaveBeenCalledWith ( "currentApiConfigName" , "shared-profile" )
330+ expect ( mockClineProvider . activateProviderProfile ) . toHaveBeenCalledWith ( { name : "shared-profile" } )
331+ } )
332+
333+ it ( "re-pins only the view when its profile is missing but the shared global is still valid" , async ( ) => {
334+ vi . mocked ( mockClineProvider . providerSettingsManager . hasConfig ) . mockImplementation (
335+ async ( name : string ) => name === "shared-profile" ,
336+ )
337+ await webviewMessageHandler ( mockClineProvider , { type : "webviewDidLaunch" , viewStateId : "view-1" } )
338+ await new Promise ( ( resolve ) => setImmediate ( resolve ) )
339+ // The view pin is re-pinned to the first available profile,
340+ // and the shared global selection is left untouched: no global write, no global activation.
341+ expect ( mockClineProvider . saveViewState ) . toHaveBeenCalledWith ( "currentApiConfigName" , "shared-profile" )
342+ expect ( mockClineProvider . contextProxy . setValue ) . not . toHaveBeenCalledWith (
343+ "currentApiConfigName" ,
344+ "shared-profile" ,
345+ )
346+ expect ( mockClineProvider . activateProviderProfile ) . not . toHaveBeenCalled ( )
347+ } )
348+
349+ it ( "re-pins the view to the shared global profile rather than the first listed profile" , async ( ) => {
350+ double . providerSettingsManager . listConfig = vi . fn ( ) . mockResolvedValue ( [
351+ { name : "first-listed" , apiProvider : providerIdentifiers . anthropic } ,
352+ { name : "shared-profile" , apiProvider : providerIdentifiers . anthropic } ,
353+ ] )
354+ vi . mocked ( mockClineProvider . providerSettingsManager . hasConfig ) . mockImplementation (
355+ async ( name : string ) => name === "shared-profile" ,
356+ )
357+ await webviewMessageHandler ( mockClineProvider , { type : "webviewDidLaunch" , viewStateId : "view-1" } )
358+ await new Promise ( ( resolve ) => setImmediate ( resolve ) )
359+ // The view pin follows the still-valid shared global selection, not the first
360+ // profile in the list; the global selection is left untouched.
361+ expect ( mockClineProvider . saveViewState ) . toHaveBeenCalledWith ( "currentApiConfigName" , "shared-profile" )
362+ expect ( mockClineProvider . saveViewState ) . not . toHaveBeenCalledWith ( "currentApiConfigName" , "first-listed" )
363+ expect ( mockClineProvider . activateProviderProfile ) . not . toHaveBeenCalled ( )
364+ } )
365+
366+ it ( "records the legacy repair without activating a profile when no name is listed" , async ( ) => {
367+ double . providerSettingsManager . listConfig = vi
368+ . fn ( )
369+ . mockResolvedValue ( [ { apiProvider : providerIdentifiers . anthropic } ] )
370+ vi . mocked ( mockClineProvider . providerSettingsManager . hasConfig ) . mockResolvedValue ( false )
371+ await webviewMessageHandler ( mockClineProvider , { type : "webviewDidLaunch" , viewStateId : "view-1" } )
372+ await new Promise ( ( resolve ) => setImmediate ( resolve ) )
373+ // The legacy repair still records the (empty) selection, but does not activate a
374+ // profile that has no name.
375+ expect ( mockClineProvider . contextProxy . setValue ) . toHaveBeenCalledWith ( "currentApiConfigName" , undefined )
376+ expect ( mockClineProvider . activateProviderProfile ) . not . toHaveBeenCalled ( )
377+ } )
378+ } )
379+
264380describe ( "webviewMessageHandler - requestLmStudioModels" , ( ) => {
265381 beforeEach ( ( ) => {
266382 vi . clearAllMocks ( )
0 commit comments