@@ -4,14 +4,47 @@ import {
44 ProviderInstanceId ,
55 type ServerProvider ,
66} from "@t3tools/contracts" ;
7+ import { RegistryContext , useAtomSet , useAtomValue } from "@effect/atom-react" ;
8+ import { Atom , AtomRegistry } from "effect/reactivity" ;
79import { act , type ReactNode } from "react" ;
8- import { createRoot } from "react-dom/client" ;
10+ import { createRoot , type Root } from "react-dom/client" ;
911import { afterEach , describe , expect , it , vi } from "vite-plus/test" ;
12+ import { page , userEvent } from "vite-plus/test/browser" ;
13+
14+ import "~/index.css" ;
1015
1116import { deriveProviderInstanceEntries } from "../../providerInstances" ;
1217import { ProviderModelPicker } from "./ProviderModelPicker" ;
1318import type { ModelEsque } from "./providerIconUtils" ;
1419
20+ const favoritesAtom = Atom . make < ReadonlyArray < { provider : ProviderInstanceId ; model : string } > > (
21+ [ ] ,
22+ ) . pipe ( Atom . keepAlive ) ;
23+
24+ vi . mock ( import ( "~/hooks/useSettings" ) , async ( original ) => ( {
25+ ...( await original ( ) ) ,
26+ useEnvironmentModelPreferences : ( ) => ( {
27+ favorites : useAtomValue ( favoritesAtom ) ,
28+ providerModelPreferences : { } ,
29+ } ) ,
30+ useToggleEnvironmentModelFavorite : ( ) => {
31+ const setFavorites = useAtomSet ( favoritesAtom ) ;
32+ return async ( provider : ProviderInstanceId , model : string ) => {
33+ setFavorites ( ( favorites ) =>
34+ favorites . filter ( ( favorite ) => favorite . provider !== provider || favorite . model !== model ) ,
35+ ) ;
36+ } ;
37+ } ,
38+ } ) ) ;
39+
40+ vi . mock ( import ( "@tanstack/react-router" ) , async ( original ) => ( {
41+ ...( await original ( ) ) ,
42+ useNavigate : ( ) => vi . fn ( ) ,
43+ } ) ) ;
44+
45+ let pickerRoot : Root | undefined ;
46+ let pickerRegistry : AtomRegistry . AtomRegistry | undefined ;
47+
1548function providerEntry ( instanceId : string , driver : string ) {
1649 const provider : ServerProvider = {
1750 instanceId : ProviderInstanceId . make ( instanceId ) ,
@@ -29,7 +62,11 @@ function providerEntry(instanceId: string, driver: string) {
2962 return deriveProviderInstanceEntries ( [ provider ] ) [ 0 ] ! ;
3063}
3164
32- afterEach ( ( ) => {
65+ afterEach ( async ( ) => {
66+ if ( pickerRoot ) await act ( async ( ) => pickerRoot ! . unmount ( ) ) ;
67+ pickerRoot = undefined ;
68+ pickerRegistry ?. dispose ( ) ;
69+ pickerRegistry = undefined ;
3370 document . body . replaceChildren ( ) ;
3471 vi . unstubAllGlobals ( ) ;
3572} ) ;
@@ -70,7 +107,92 @@ function renderPicker(input: {
70107 ) ;
71108}
72109
110+ async function click ( target : Parameters < typeof userEvent . click > [ 0 ] ) {
111+ await act ( async ( ) => userEvent . click ( target ) ) ;
112+ }
113+
114+ async function openLegacyPicker ( activeModel : string ) {
115+ vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
116+ const entry = providerEntry ( "codex" , "codex" ) ;
117+ pickerRegistry = AtomRegistry . make ( ) ;
118+ pickerRegistry . set ( favoritesAtom , [
119+ { provider : entry . instanceId , model : "current-favorite" } ,
120+ { provider : entry . instanceId , model : "legacy-favorite" } ,
121+ ] ) ;
122+ pickerRoot = createRoot ( document . body . appendChild ( document . createElement ( "div" ) ) ) ;
123+ await act ( async ( ) =>
124+ pickerRoot ! . render (
125+ < RegistryContext . Provider value = { pickerRegistry ! } >
126+ < ProviderModelPicker
127+ environmentId = { null }
128+ activeInstanceId = { entry . instanceId }
129+ model = { activeModel }
130+ lockedProvider = { null }
131+ instanceEntries = { [ entry ] }
132+ modelOptionsByInstance = {
133+ new Map ( [
134+ [
135+ entry . instanceId ,
136+ [
137+ { slug : "current" , name : "Current model" } ,
138+ { slug : "legacy-favorite" , name : "Legacy favorite" , isLegacy : true } ,
139+ { slug : "current-favorite" , name : "Current favorite" } ,
140+ { slug : "other-legacy" , name : "Other legacy" , isLegacy : true } ,
141+ ] ,
142+ ] ,
143+ ] )
144+ }
145+ onInstanceModelChange = { ( ) => { } }
146+ />
147+ </ RegistryContext . Provider > ,
148+ ) ,
149+ ) ;
150+ await click ( document . querySelector ( "button" ) ! ) ;
151+ await click ( page . getByRole ( "button" , { name : entry . displayName , exact : true } ) ) ;
152+ }
153+
73154describe ( "ProviderModelPicker" , ( ) => {
155+ it ( "hoists legacy favorites in provider order and returns an unfavorited model to legacy" , async ( ) => {
156+ await openLegacyPicker ( "current" ) ;
157+ const legacyFavorite = page . getByRole ( "option" , { name : / L e g a c y f a v o r i t e / } ) ;
158+ const currentFavorite = page . getByRole ( "option" , { name : / C u r r e n t f a v o r i t e / } ) ;
159+ const current = page . getByRole ( "option" , { name : / C u r r e n t m o d e l / } ) ;
160+ const legacySection = page . getByRole ( "option" , { name : / L e g a c y m o d e l s / } ) ;
161+
162+ await expect . element ( legacyFavorite ) . toBeVisible ( ) ;
163+ await expect . element ( currentFavorite ) . toBeVisible ( ) ;
164+ await expect . element ( current ) . toBeVisible ( ) ;
165+ expect ( legacyFavorite . element ( ) . getBoundingClientRect ( ) . top ) . toBeLessThan (
166+ currentFavorite . element ( ) . getBoundingClientRect ( ) . top ,
167+ ) ;
168+ expect ( currentFavorite . element ( ) . getBoundingClientRect ( ) . top ) . toBeLessThan (
169+ current . element ( ) . getBoundingClientRect ( ) . top ,
170+ ) ;
171+
172+ await click ( legacyFavorite . getByRole ( "button" , { name : "Remove from favorites" } ) ) ;
173+ await expect . element ( legacyFavorite ) . not . toBeInTheDocument ( ) ;
174+ await click ( legacySection ) ;
175+ await expect . element ( legacyFavorite ) . toBeVisible ( ) ;
176+ await expect . element ( page . getByRole ( "option" , { name : / O t h e r l e g a c y / } ) ) . toBeVisible ( ) ;
177+ expect ( legacyFavorite . element ( ) . getBoundingClientRect ( ) . top ) . toBeGreaterThan (
178+ legacySection . element ( ) . getBoundingClientRect ( ) . top ,
179+ ) ;
180+ await expect
181+ . element ( legacyFavorite . getByRole ( "button" , { name : "Add to favorites" } ) )
182+ . toBeVisible ( ) ;
183+ } ) ;
184+
185+ it ( "keeps unrelated legacy models collapsed when the active model is a favorite" , async ( ) => {
186+ await openLegacyPicker ( "legacy-favorite" ) ;
187+
188+ await expect . element ( page . getByRole ( "option" , { name : / L e g a c y f a v o r i t e / } ) ) . toBeVisible ( ) ;
189+ await expect
190+ . element ( page . getByRole ( "option" , { name : / O t h e r l e g a c y / } ) )
191+ . not . toBeInTheDocument ( ) ;
192+ await click ( page . getByRole ( "option" , { name : / L e g a c y m o d e l s / } ) ) ;
193+ await expect . element ( page . getByRole ( "option" , { name : / O t h e r l e g a c y / } ) ) . toBeVisible ( ) ;
194+ } ) ;
195+
74196 it ( "shows a neutral aggregate value without a representative model or availability badge" , async ( ) => {
75197 const label = await renderPicker ( {
76198 instanceId : "codex_personal" ,
0 commit comments