@@ -130,11 +130,6 @@ describe("discoverAuthStorage", () => {
130130 expect ( credentials . openai ) . toBeUndefined ( ) ;
131131 } ) ;
132132
133- // Regression: the oauth branch did not check past expiry at all, so an
134- // expired OAuth credential silently took the one-per-provider slot ahead
135- // of any valid profile. The conversion layer keeps expired-but-refreshable
136- // OAuth (the auth resolution path may refresh it), but the map builder now
137- // prefers a non-expired credential when one is available.
138133 it ( "keeps expired OAuth when it is the sole profile for a provider" , ( ) => {
139134 const credentials = resolveAgentCredentialMapFromStore ( {
140135 version : 1 ,
@@ -149,21 +144,23 @@ describe("discoverAuthStorage", () => {
149144 } ,
150145 } ) ;
151146
152- // Sole profile — the expired credential is kept so the refresh
153- // path has something to work with.
154- const openai = credentials . openai as
155- | { type ?: string ; access ?: string ; refresh ?: string }
156- | undefined ;
157- expect ( openai ?. type ) . toBe ( "oauth" ) ;
158- expect ( openai ?. access ) . toBe ( "sole-access" ) ;
147+ expect ( credentials . openai ) . toEqual ( {
148+ type : "oauth" ,
149+ access : "sole-access" ,
150+ refresh : "sole-refresh" ,
151+ expires : expect . any ( Number ) ,
152+ } ) ;
159153 } ) ;
160154
161- // A same-provider valid credential after an expired one must still fill
162- // the per-provider slot: the map skips expired entries and keeps scanning.
163- it ( "skips an expired first credential for a provider and picks the next valid one" , ( ) => {
155+ it ( "uses canonical mode and expiry ordering instead of profile insertion order" , ( ) => {
164156 const credentials = resolveAgentCredentialMapFromStore ( {
165157 version : 1 ,
166158 profiles : {
159+ "openai:key" : {
160+ type : "api_key" ,
161+ provider : "openai" ,
162+ key : "insertion-order-key" ,
163+ } ,
167164 "openai:expired" : {
168165 type : "oauth" ,
169166 provider : "openai" ,
@@ -181,11 +178,46 @@ describe("discoverAuthStorage", () => {
181178 } ,
182179 } ) ;
183180
184- const openai = credentials . openai as
185- | { type ?: string ; access ?: string ; refresh ?: string }
186- | undefined ;
187- expect ( openai ?. type ) . toBe ( "oauth" ) ;
188- expect ( openai ?. access ) . toBe ( "valid-access" ) ;
181+ expect ( credentials . openai ) . toEqual ( {
182+ type : "oauth" ,
183+ access : "valid-access" ,
184+ refresh : "valid-refresh" ,
185+ expires : expect . any ( Number ) ,
186+ } ) ;
187+ } ) ;
188+
189+ it ( "passes configured auth order through discovery selection" , async ( ) => {
190+ await withAgentDir ( async ( agentDir ) => {
191+ writeAuthProfilesSqlite ( agentDir , {
192+ version : 1 ,
193+ profiles : {
194+ "openai:oauth" : {
195+ type : "oauth" ,
196+ provider : "openai" ,
197+ access : "valid-access" ,
198+ refresh : "valid-refresh" ,
199+ expires : Date . now ( ) + 3600_000 ,
200+ } ,
201+ "openai:key" : {
202+ type : "api_key" ,
203+ provider : "openai" ,
204+ key : "configured-order-key" ,
205+ } ,
206+ } ,
207+ } ) ;
208+ const authStorage = discoverAuthStorage ( agentDir , {
209+ skipExternalAuthProfiles : true ,
210+ env : { } ,
211+ config : {
212+ auth : { order : { openai : [ "openai:key" , "openai:oauth" ] } } ,
213+ } ,
214+ } ) ;
215+
216+ expect ( authStorage . get ( "openai" ) ) . toEqual ( {
217+ type : "api_key" ,
218+ key : "configured-order-key" ,
219+ } ) ;
220+ } ) ;
189221 } ) ;
190222
191223 it ( "keeps keyRef and tokenRef profiles visible only for read-only agent discovery" , ( ) => {
0 commit comments