@@ -2,7 +2,14 @@ import { screen } from "@testing-library/react"
22
33import { renderWithExtensionState } from "@/utils/test-utils"
44
5- import { providerIdentifiers , type ProviderSettings , type OrganizationAllowList } from "@roo-code/types"
5+ import {
6+ providerIdentifiers ,
7+ geminiDefaultModelId ,
8+ vertexDefaultModelId ,
9+ type ModelInfo ,
10+ type ProviderSettings ,
11+ type OrganizationAllowList ,
12+ } from "@roo-code/types"
613
714import { useExtensionState } from "@src/context/ExtensionStateContext"
815import { useRouterModels } from "@src/components/ui/hooks/useRouterModels"
@@ -11,13 +18,26 @@ import { useSelectedModel } from "@src/components/ui/hooks/useSelectedModel"
1118import ApiOptions from "../ApiOptions"
1219import { MODELS_BY_PROVIDER , PROVIDERS } from "../constants"
1320
14- // Mock the extension state context
21+ // Mock the extension state context. The hoisted state holder lets the dynamic
22+ // catalog suite reset the allow list to the unfiltered default without
23+ // re-typing the full ExtensionStateContextType (previous suites pin
24+ // mockReturnValue via `as any`, and vi.clearAllMocks() does not undo that).
25+ const { useExtensionStateMock, setOrganizationAllowList } = vi . hoisted ( ( ) => {
26+ const mock = vi . fn ( ( ) => ( {
27+ organizationAllowList : undefined as OrganizationAllowList | undefined ,
28+ cloudIsAuthenticated : false ,
29+ } ) )
30+ return {
31+ useExtensionStateMock : mock ,
32+ setOrganizationAllowList : ( list : OrganizationAllowList | undefined ) => {
33+ mock . mockReturnValue ( { organizationAllowList : list , cloudIsAuthenticated : false } )
34+ } ,
35+ }
36+ } )
37+
1538vi . mock ( "@src/context/ExtensionStateContext" , ( ) => ( {
1639 ExtensionStateContextProvider : ( { children } : any ) => children ,
17- useExtensionState : vi . fn ( ( ) => ( {
18- organizationAllowList : undefined ,
19- cloudIsAuthenticated : false ,
20- } ) ) ,
40+ useExtensionState : useExtensionStateMock ,
2141} ) )
2242
2343// Mock the translation hook
@@ -34,12 +54,20 @@ vi.mock("@src/utils/vscode", () => ({
3454 } ,
3555} ) )
3656
37- // Mock the router models hook
57+ // Mock the router models hook. The hoisted state holder lets tests drive `data`
58+ // without re-typing the full react-query UseQueryResult at each call site.
59+ const { useRouterModelsMock, setRouterModelsData } = vi . hoisted ( ( ) => {
60+ const state = { data : undefined as Record < string , Record < string , unknown > > | undefined }
61+ return {
62+ useRouterModelsMock : vi . fn ( ( ) => ( { data : state . data , refetch : vi . fn ( ) } ) ) ,
63+ setRouterModelsData : ( data : typeof state . data ) => {
64+ state . data = data
65+ } ,
66+ }
67+ } )
68+
3869vi . mock ( "@src/components/ui/hooks/useRouterModels" , ( ) => ( {
39- useRouterModels : vi . fn ( ( ) => ( {
40- data : null ,
41- refetch : vi . fn ( ) ,
42- } ) ) ,
70+ useRouterModels : useRouterModelsMock ,
4371} ) )
4472
4573// Mock the selected model hook
@@ -147,6 +175,29 @@ describe("ApiOptions Provider Filtering", () => {
147175 expect ( useRouterModels ) . toHaveBeenCalledWith ( { enabled : true , provider : providerIdentifiers . kenari } )
148176 } )
149177
178+ it . each ( [ providerIdentifiers . gemini , providerIdentifiers . vertex ] ) (
179+ "requests router models for the dynamic catalog provider %s" ,
180+ ( provider ) => {
181+ vi . mocked ( useSelectedModel ) . mockReturnValue ( {
182+ provider,
183+ id : "model" ,
184+ info : undefined ,
185+ isLoading : false ,
186+ isError : false ,
187+ } )
188+
189+ renderWithProviders ( {
190+ ...defaultProps ,
191+ apiConfiguration : { apiProvider : provider } as ProviderSettings ,
192+ } )
193+
194+ // Both the automatic fetch gate and the readiness gate in useSelectedModel
195+ // key off isDynamicProvider, so no wiring change is needed here; this pins
196+ // the gating now that these providers are dynamic.
197+ expect ( useRouterModels ) . toHaveBeenCalledWith ( { enabled : true , provider } )
198+ } ,
199+ )
200+
150201 it . each ( [ providerIdentifiers . ollama , providerIdentifiers . lmstudio ] ) (
151202 "does not make an aggregate router request for local provider %s" ,
152203 ( provider ) => {
@@ -343,4 +394,77 @@ describe("ApiOptions Provider Filtering", () => {
343394 delete ( MODELS_BY_PROVIDER as any ) . testEmptyProvider
344395 PROVIDERS . pop ( )
345396 } )
397+
398+ describe . each ( [
399+ [ providerIdentifiers . gemini , geminiDefaultModelId ] ,
400+ [ providerIdentifiers . vertex , vertexDefaultModelId ] ,
401+ ] as const ) ( "dynamic model catalog (ApiOptions -> ModelPicker wiring) for %s" , ( provider , defaultModelId ) => {
402+ // Regression guard: the generic ModelPicker must receive the host-fetched router
403+ // catalog for this provider merged on top of the static MODELS_BY_PROVIDER fallback
404+ // in ApiOptions. ModelPicker itself performs no router merging, so if the merge in
405+ // ApiOptions regressed to static-only, router-only models newer than the shipped
406+ // catalog would silently become unselectable and no other suite would catch it.
407+ const routerOnlyModelId = `${ provider } -router-only-preview`
408+
409+ const routerOnlyModelInfo : ModelInfo = {
410+ maxTokens : 131_072 ,
411+ contextWindow : 262_144 ,
412+ supportsPromptCache : false ,
413+ }
414+
415+ const renderWithProviderSelected = ( routerData : Record < string , Record < string , ModelInfo > > | undefined ) => {
416+ // Earlier allow-list suites leave a restrictive organizationAllowList on this
417+ // mock (clearAllMocks keeps mockReturnValue); reset to the unfiltered default
418+ // so the catalog reaches the picker in full.
419+ setOrganizationAllowList ( undefined )
420+ setRouterModelsData ( routerData )
421+ vi . mocked ( useSelectedModel ) . mockReturnValue ( {
422+ provider,
423+ id : defaultModelId ,
424+ info : undefined ,
425+ isLoading : false ,
426+ isError : false ,
427+ } )
428+
429+ return renderWithProviders ( {
430+ ...defaultProps ,
431+ apiConfiguration : {
432+ apiProvider : provider ,
433+ apiModelId : defaultModelId ,
434+ } as ProviderSettings ,
435+ } )
436+ }
437+
438+ afterEach ( ( ) => {
439+ // Restore the default (no fetched models) for any suites that run later.
440+ setRouterModelsData ( undefined )
441+ } )
442+
443+ it ( "passes both static and router-fetched models to the generic ModelPicker" , ( ) => {
444+ // The router payload contains ONLY a model id that does not exist in the
445+ // shipped static catalog; it can reach the picker solely via the merge.
446+ renderWithProviderSelected ( { [ provider ] : { [ routerOnlyModelId ] : routerOnlyModelInfo } } )
447+
448+ // Static default from getStaticModelsForProvider(provider) / MODELS_BY_PROVIDER.
449+ expect ( screen . getByTestId ( `model-option-${ defaultModelId } ` ) ) . toBeInTheDocument ( )
450+ // Router-only model: present in the picker only if ApiOptions merged routerModels[provider].
451+ expect ( screen . getByTestId ( `model-option-${ routerOnlyModelId } ` ) ) . toBeInTheDocument ( )
452+ } )
453+
454+ it ( "keeps the static catalog selectable when no router models are available" , ( ) => {
455+ // useRouterModels data is undefined while the fetch is pending/failed; the
456+ // spread of routerModels?.[provider] must not crash or drop the static defaults.
457+ renderWithProviderSelected ( undefined )
458+
459+ expect ( screen . getByTestId ( `model-option-${ defaultModelId } ` ) ) . toBeInTheDocument ( )
460+ expect ( screen . queryByTestId ( `model-option-${ routerOnlyModelId } ` ) ) . not . toBeInTheDocument ( )
461+ } )
462+
463+ it ( "keeps the static catalog selectable when the router catalog is empty" , ( ) => {
464+ renderWithProviderSelected ( { } )
465+
466+ expect ( screen . getByTestId ( `model-option-${ defaultModelId } ` ) ) . toBeInTheDocument ( )
467+ expect ( screen . queryByTestId ( `model-option-${ routerOnlyModelId } ` ) ) . not . toBeInTheDocument ( )
468+ } )
469+ } )
346470} )
0 commit comments