@@ -3365,6 +3365,81 @@ describe("codex-auth API", () => {
33653365 expect ( data . status ) . toBe ( "expired" ) ;
33663366 } ) ;
33673367
3368+ test ( "Codex OAuth login responses project raw provider errors" , async ( ) => {
3369+ const oauth = await import ( "../src/oauth" ) ;
3370+ const startSpy = spyOn ( oauth , "startLoginFlow" ) . mockImplementation ( async ( ) => {
3371+ throw new Error ( "already in progress at C:\\Users\\Alice\\.opencodex\\auth.json.ocx-tmp sk-secret-provider-key" ) ;
3372+ } ) ;
3373+ try {
3374+ const req = new Request ( "http://localhost/api/codex-auth/login" , {
3375+ method : "POST" ,
3376+ headers : { "content-type" : "application/json" } ,
3377+ body : "{}" ,
3378+ } ) ;
3379+ const resp = await handleCodexAuthAPI ( req , new URL ( req . url ) , makeConfig ( ) ) ;
3380+ const data = await resp ! . json ( ) as { error ?: string } ;
3381+
3382+ expect ( resp ! . status ) . toBe ( 500 ) ;
3383+ expect ( data . error ) . toBe ( "OAuth authentication failed. Check the OpenCodex account status and retry." ) ;
3384+ expect ( JSON . stringify ( data ) ) . not . toContain ( "Alice" ) ;
3385+ expect ( JSON . stringify ( data ) ) . not . toContain ( "sk-secret-provider-key" ) ;
3386+ } finally {
3387+ startSpy . mockRestore ( ) ;
3388+ }
3389+ } ) ;
3390+
3391+ test ( "Codex OAuth login status projects late provider errors" , async ( ) => {
3392+ const oauth = await import ( "../src/oauth" ) ;
3393+ const openUrlMod = await import ( "../src/lib/open-url" ) ;
3394+ const startSpy = spyOn ( oauth , "startLoginFlow" ) . mockResolvedValue ( { url : "https://example.test/oauth" } ) ;
3395+ const statusSpy = spyOn ( oauth , "getLoginStatus" ) . mockReturnValue ( {
3396+ done : true ,
3397+ loggedIn : false ,
3398+ error : "late failure at /home/alice/.opencodex/auth.json.ocx-tmp sk-secret-provider-key" ,
3399+ } as ReturnType < typeof oauth . getLoginStatus > ) ;
3400+ const openSpy = spyOn ( openUrlMod , "openUrl" ) . mockImplementation ( ( ) => { } ) ;
3401+ const timeoutSpy = spyOn ( globalThis , "setTimeout" ) . mockImplementation ( ( (
3402+ callback : ( ...args : unknown [ ] ) => void ,
3403+ delay ?: number ,
3404+ ...args : unknown [ ]
3405+ ) => {
3406+ if ( delay === 2_000 ) queueMicrotask ( ( ) => callback ( ...args ) ) ;
3407+ return 0 as unknown as ReturnType < typeof setTimeout > ;
3408+ } ) as typeof setTimeout ) ;
3409+ try {
3410+ const req = new Request ( "http://localhost/api/codex-auth/login" , {
3411+ method : "POST" ,
3412+ headers : { "content-type" : "application/json" } ,
3413+ body : "{}" ,
3414+ } ) ;
3415+ const startResponse = await handleCodexAuthAPI ( req , new URL ( req . url ) , makeConfig ( ) ) ;
3416+ const started = await startResponse ! . json ( ) as { flowId : string } ;
3417+ expect ( startResponse ! . status ) . toBe ( 200 ) ;
3418+
3419+ let state : { status ?: string ; error ?: string } = { } ;
3420+ for ( let attempt = 0 ; attempt < 50 && state . status !== "error" ; attempt += 1 ) {
3421+ const statusReq = new Request (
3422+ `http://localhost/api/codex-auth/login-status?flowId=${ encodeURIComponent ( started . flowId ) } ` ,
3423+ ) ;
3424+ const statusResponse = await handleCodexAuthAPI ( statusReq , new URL ( statusReq . url ) , makeConfig ( ) ) ;
3425+ state = await statusResponse ! . json ( ) as typeof state ;
3426+ if ( state . status !== "error" ) await new Promise < void > ( resolve => setImmediate ( resolve ) ) ;
3427+ }
3428+
3429+ expect ( state ) . toMatchObject ( {
3430+ status : "error" ,
3431+ error : "OAuth authentication failed. Check the OpenCodex account status and retry." ,
3432+ } ) ;
3433+ expect ( JSON . stringify ( state ) ) . not . toContain ( "/home/alice" ) ;
3434+ expect ( JSON . stringify ( state ) ) . not . toContain ( "sk-secret-provider-key" ) ;
3435+ } finally {
3436+ timeoutSpy . mockRestore ( ) ;
3437+ openSpy . mockRestore ( ) ;
3438+ statusSpy . mockRestore ( ) ;
3439+ startSpy . mockRestore ( ) ;
3440+ }
3441+ } ) ;
3442+
33683443 test ( "POST /api/codex-auth/login/cancel expires the pending flow" , async ( ) => {
33693444 const flowId = "flow-cancel-test" ;
33703445 const req = new Request ( "http://localhost/api/codex-auth/login/cancel" , {
0 commit comments