@@ -137,56 +137,6 @@ function createOpenRouterOAuthContext(params: {
137137 return { ctx, progress, note, text, log, openUrl } ;
138138}
139139
140- async function startLocalOpenRouterOAuthLogin ( ) {
141- let markReady = ( ) => { } ;
142- const ready = new Promise < void > ( ( resolve ) => {
143- markReady = resolve ;
144- } ) ;
145- const controller = new AbortController ( ) ;
146- const fetchImpl = vi . fn < typeof fetch > ( async ( ) => jsonResponse ( { key : "sk-or-v1-test" } ) ) ;
147- const { ctx } = createOpenRouterOAuthContext ( {
148- isRemote : false ,
149- onProgress : ( message ) => {
150- if ( message . startsWith ( "Waiting for OpenRouter OAuth callback" ) ) {
151- markReady ( ) ;
152- }
153- } ,
154- signal : controller . signal ,
155- } ) ;
156- const login = loginOpenRouterOAuth ( ctx , {
157- createPkce : ( ) => ( { verifier : "verifier-1" , challenge : "challenge-1" } ) ,
158- createState : ( ) => "state-1" ,
159- fetchImpl,
160- } ) ;
161- login . catch ( ( ) => undefined ) ;
162- await Promise . race ( [
163- ready ,
164- login . then (
165- ( ) => {
166- throw new Error ( "OpenRouter OAuth completed before callback server started" ) ;
167- } ,
168- ( error : unknown ) => {
169- throw error ;
170- } ,
171- ) ,
172- ] ) ;
173-
174- return {
175- abort : ( ) => controller . abort ( ) ,
176- fetchImpl,
177- login,
178- request : async ( pathOrQuery : string , init ?: RequestInit ) => {
179- const url = pathOrQuery . startsWith ( "http" )
180- ? pathOrQuery
181- : `${ OPENROUTER_OAUTH_REDIRECT_URI } ?${ pathOrQuery } ` ;
182- const headers = new Headers ( init ?. headers ) ;
183- headers . set ( "Connection" , "close" ) ;
184- const response = await fetch ( url , { ...init , headers } ) ;
185- return { response, body : await response . text ( ) } ;
186- } ,
187- } ;
188- }
189-
190140function runRemoteOpenRouterOAuthRedirect ( redirectInput : string ) {
191141 const { ctx } = createOpenRouterOAuthContext ( { isRemote : true , redirectInput } ) ;
192142 return loginOpenRouterOAuth ( ctx , {
@@ -415,7 +365,7 @@ describe("OpenRouter OAuth", () => {
415365 expect ( progress . stop ) . toHaveBeenCalledWith ( "OpenRouter OAuth complete" ) ;
416366 } ) ;
417367
418- it ( "binds the local callback before opening the browser" , async ( ) => {
368+ it ( "binds the local callback before opening the browser and exchanges its accepted code " , async ( ) => {
419369 const fetchImpl = vi . fn < typeof fetch > ( async ( ) => jsonResponse ( { key : "sk-or-v1-test" } ) ) ;
420370 const waitForCallback = vi . fn ( async ( ) => ( {
421371 type : "authorization_code" as const ,
@@ -446,6 +396,41 @@ describe("OpenRouter OAuth", () => {
446396 expect ( close ) . toHaveBeenCalledTimes ( 1 ) ;
447397 expect ( openUrl ) . toHaveBeenCalledWith ( expect . stringContaining ( "https://openrouter.ai/auth?" ) ) ;
448398 expect ( text ) . not . toHaveBeenCalled ( ) ;
399+ expect ( fetchImpl ) . toHaveBeenCalledTimes ( 1 ) ;
400+ expect ( requestJsonBody ( fetchImpl . mock . calls [ 0 ] ?. [ 1 ] ) ) . toEqual ( {
401+ code : "AUTHCODE" ,
402+ code_verifier : "verifier-1" ,
403+ code_challenge_method : "S256" ,
404+ } ) ;
405+ } ) ;
406+
407+ it ( "closes a state-bound provider denial without exchanging a code" , async ( ) => {
408+ const fetchImpl = vi . fn < typeof fetch > ( async ( ) => jsonResponse ( { key : "sk-or-v1-test" } ) ) ;
409+ const waitForCallback = vi . fn ( async ( ) => ( {
410+ type : "oauth_error" as const ,
411+ error : "access_denied" ,
412+ errorDescription : "Denied" ,
413+ } ) ) ;
414+ const close = vi . fn ( async ( ) => undefined ) ;
415+ const startCallback = vi . fn ( async ( ) => ( { waitForCallback, close } ) ) ;
416+ const { ctx, text } = createOpenRouterOAuthContext ( { isRemote : false } ) ;
417+
418+ await expect (
419+ loginOpenRouterOAuth ( ctx , {
420+ createPkce : ( ) => ( { verifier : "verifier-1" , challenge : "challenge-1" } ) ,
421+ createState : ( ) => "state-1" ,
422+ fetchImpl,
423+ startCallback,
424+ } ) ,
425+ ) . rejects . toThrow ( "OpenRouter OAuth error: access_denied: Denied" ) ;
426+
427+ expect ( startCallback ) . toHaveBeenCalledWith (
428+ expect . objectContaining ( { expectedState : "state-1" } ) ,
429+ ) ;
430+ expect ( waitForCallback ) . toHaveBeenCalledTimes ( 1 ) ;
431+ expect ( close ) . toHaveBeenCalledTimes ( 1 ) ;
432+ expect ( fetchImpl ) . not . toHaveBeenCalled ( ) ;
433+ expect ( text ) . not . toHaveBeenCalled ( ) ;
449434 } ) ;
450435
451436 it ( "falls back to a pasted redirect when the local listener cannot start" , async ( ) => {
@@ -472,47 +457,6 @@ describe("OpenRouter OAuth", () => {
472457 expect ( fetchImpl ) . toHaveBeenCalledTimes ( 1 ) ;
473458 } ) ;
474459
475- it ( "keeps waiting after rejected callback candidates and exchanges one valid code" , async ( ) => {
476- const local = await startLocalOpenRouterOAuthLogin ( ) ;
477- try {
478- expect ( ( await local . request ( "http://localhost:3000/wrong" ) ) . response . status ) . toBe ( 404 ) ;
479- expect ( ( await local . request ( "" , { method : "POST" } ) ) . response . status ) . toBe ( 405 ) ;
480- expect ( ( await local . request ( "code=missing-state" ) ) . response . status ) . toBe ( 400 ) ;
481- expect ( ( await local . request ( "state=wrong&code=wrong-state" ) ) . response . status ) . toBe ( 400 ) ;
482- expect ( ( await local . request ( "state=state-1" ) ) . response . status ) . toBe ( 400 ) ;
483-
484- const validCallbacks = await Promise . allSettled ( [
485- local . request ( "state=state-1&code=AUTHCODE" ) ,
486- local . request ( "state=state-1&code=REPLAY" ) ,
487- ] ) ;
488- const statuses = validCallbacks . flatMap ( ( result ) =>
489- result . status === "fulfilled" ? [ result . value . response . status ] : [ ] ,
490- ) ;
491- expect ( statuses . filter ( ( status ) => status === 200 ) ) . toHaveLength ( 1 ) ;
492- await expect ( local . login ) . resolves . toMatchObject ( { defaultModel : "openrouter/auto" } ) ;
493- expect ( local . fetchImpl ) . toHaveBeenCalledTimes ( 1 ) ;
494- } finally {
495- local . abort ( ) ;
496- await local . login . catch ( ( ) => undefined ) ;
497- }
498- } ) ;
499-
500- it ( "terminates a state-bound provider denial without exchanging a code" , async ( ) => {
501- const local = await startLocalOpenRouterOAuthLogin ( ) ;
502- try {
503- const denied = await local . request (
504- "state=state-1&error=access_denied&error_description=Denied" ,
505- ) ;
506- expect ( denied . response . status ) . toBe ( 400 ) ;
507- expect ( denied . body ) . toBe ( "Authorization was not completed." ) ;
508- await expect ( local . login ) . rejects . toThrow ( "OpenRouter OAuth error: access_denied: Denied" ) ;
509- expect ( local . fetchImpl ) . not . toHaveBeenCalled ( ) ;
510- } finally {
511- local . abort ( ) ;
512- await local . login . catch ( ( ) => undefined ) ;
513- }
514- } ) ;
515-
516460 it ( "exposes stable auth choice metadata" , ( ) => {
517461 expect ( createOpenRouterOAuthAuthMethod ( ) . wizard ?. choiceId ) . toBe ( "openrouter-oauth" ) ;
518462 } ) ;
0 commit comments