@@ -12,6 +12,10 @@ import { installIsolatedCodexHome, type IsolatedCodexHome } from "./helpers/isol
1212
1313const TOKEN_ENDPOINT = "https://auth.x.ai/oauth/token" ;
1414const CHAT_ENDPOINT = `${ XAI_GROK_CLI_BASE_URL } /chat/completions` ;
15+ const PUBLIC_OAUTH_AUTHENTICATION_ERROR = "OAuth authentication failed. Check the OpenCodex account status and retry." ;
16+ const WINDOWS_PATH_CANARY = "C:\\Users\\Alice\\.opencodex\\auth.json.ocx-tmp" ;
17+ const UNC_PATH_CANARY = "\\\\server\\share\\opencodex\\auth.json.ocx-tmp" ;
18+ const POSIX_PATH_CANARY = "/home/alice/.opencodex/auth.json.ocx-tmp" ;
1519
1620let testDir = "" ;
1721let previousHome : string | undefined ;
@@ -35,11 +39,11 @@ afterEach(() => {
3539 if ( testDir ) rmSync ( testDir , { recursive : true , force : true } ) ;
3640} ) ;
3741
38- function seedOAuth ( ) : void {
39- saveCredential ( "xai" , {
42+ async function seedOAuth ( expires = Date . now ( ) + 3_600_000 ) : Promise < void > {
43+ await saveCredential ( "xai" , {
4044 access : "rejected-access" ,
4145 refresh : "initial-refresh" ,
42- expires : Date . now ( ) + 3_600_000 ,
46+ expires,
4347 accountId : "xai-test-account" ,
4448 source : "oauth" ,
4549 } ) ;
@@ -79,7 +83,10 @@ async function post(server: ReturnType<typeof startServer>): Promise<Response> {
7983 } ) ;
8084}
8185
82- function installOAuthFetch ( chatStatuses : number [ ] ) : { chatAuth : string [ ] ; counts : { refresh : number } } {
86+ function installOAuthFetch (
87+ chatStatuses : number [ ] ,
88+ options : { tokenErrorDescription ?: string } = { } ,
89+ ) : { chatAuth : string [ ] ; counts : { refresh : number } } {
8390 const chatAuth : string [ ] = [ ] ;
8491 const counts = { refresh : 0 } ;
8592 globalThis . fetch = ( async ( input , init ) => {
@@ -92,6 +99,15 @@ function installOAuthFetch(chatStatuses: number[]): { chatAuth: string[]; counts
9299 }
93100 if ( url === TOKEN_ENDPOINT ) {
94101 counts . refresh += 1 ;
102+ if ( options . tokenErrorDescription !== undefined ) {
103+ return new Response ( JSON . stringify ( {
104+ error : "temporarily_unavailable" ,
105+ error_description : options . tokenErrorDescription ,
106+ } ) , {
107+ status : 400 ,
108+ headers : { "content-type" : "application/json" } ,
109+ } ) ;
110+ }
95111 return new Response ( JSON . stringify ( {
96112 access_token : "fresh-access" ,
97113 refresh_token : "fresh-refresh" ,
@@ -115,8 +131,60 @@ function installOAuthFetch(chatStatuses: number[]): { chatAuth: string[]; counts
115131}
116132
117133describe ( "xAI OAuth upstream 401 replay" , ( ) => {
134+ test ( "initial OAuth refresh projects raw provider failures before responding" , async ( ) => {
135+ await seedOAuth ( 0 ) ;
136+ saveConfig ( xaiConfig ( ) ) ;
137+ const observed = installOAuthFetch ( [ ] , {
138+ tokenErrorDescription : `EACCES writing ${ WINDOWS_PATH_CANARY } , ${ UNC_PATH_CANARY } , or ${ POSIX_PATH_CANARY } ` ,
139+ } ) ;
140+ const server = startServer ( 0 ) ;
141+ try {
142+ const response = await post ( server ) ;
143+ const json = await response . json ( ) as { error ?: { code ?: string ; message ?: string ; type ?: string } } ;
144+ const message = json . error ?. message ?? "" ;
145+ expect ( response . status ) . toBe ( 401 ) ;
146+ expect ( json . error ?. type ) . toBe ( "authentication_error" ) ;
147+ expect ( json . error ?. code ) . toBe ( "invalid_api_key" ) ;
148+ expect ( message ) . toBe ( PUBLIC_OAUTH_AUTHENTICATION_ERROR ) ;
149+ expect ( message ) . not . toContain ( WINDOWS_PATH_CANARY ) ;
150+ expect ( message ) . not . toContain ( UNC_PATH_CANARY ) ;
151+ expect ( message ) . not . toContain ( POSIX_PATH_CANARY ) ;
152+ expect ( message ) . not . toContain ( "auth.json" ) ;
153+ expect ( observed . counts . refresh ) . toBe ( 1 ) ;
154+ expect ( observed . chatAuth ) . toEqual ( [ ] ) ;
155+ } finally {
156+ await server . stop ( true ) ;
157+ }
158+ } ) ;
159+
160+ test ( "OAuth 401 replay projects raw refresh failures before responding" , async ( ) => {
161+ await seedOAuth ( ) ;
162+ saveConfig ( xaiConfig ( ) ) ;
163+ const observed = installOAuthFetch ( [ 401 ] , {
164+ tokenErrorDescription : `EACCES writing ${ WINDOWS_PATH_CANARY } , ${ UNC_PATH_CANARY } , or ${ POSIX_PATH_CANARY } ` ,
165+ } ) ;
166+ const server = startServer ( 0 ) ;
167+ try {
168+ const response = await post ( server ) ;
169+ const json = await response . json ( ) as { error ?: { code ?: string ; message ?: string ; type ?: string } } ;
170+ const message = json . error ?. message ?? "" ;
171+ expect ( response . status ) . toBe ( 401 ) ;
172+ expect ( json . error ?. type ) . toBe ( "authentication_error" ) ;
173+ expect ( json . error ?. code ) . toBe ( "invalid_api_key" ) ;
174+ expect ( message ) . toBe ( PUBLIC_OAUTH_AUTHENTICATION_ERROR ) ;
175+ expect ( message ) . not . toContain ( WINDOWS_PATH_CANARY ) ;
176+ expect ( message ) . not . toContain ( UNC_PATH_CANARY ) ;
177+ expect ( message ) . not . toContain ( POSIX_PATH_CANARY ) ;
178+ expect ( message ) . not . toContain ( "auth.json" ) ;
179+ expect ( observed . counts . refresh ) . toBe ( 1 ) ;
180+ expect ( observed . chatAuth ) . toEqual ( [ "Bearer rejected-access" ] ) ;
181+ } finally {
182+ await server . stop ( true ) ;
183+ }
184+ } ) ;
185+
118186 test ( "401 then 200 performs one refresh and one replay" , async ( ) => {
119- seedOAuth ( ) ;
187+ await seedOAuth ( ) ;
120188 saveConfig ( xaiConfig ( ) ) ;
121189 const observed = installOAuthFetch ( [ 401 , 200 ] ) ;
122190 const server = startServer ( 0 ) ;
@@ -133,7 +201,7 @@ describe("xAI OAuth upstream 401 replay", () => {
133201 } ) ;
134202
135203 test ( "401 then 401 replays once and propagates the second error" , async ( ) => {
136- seedOAuth ( ) ;
204+ await seedOAuth ( ) ;
137205 saveConfig ( xaiConfig ( ) ) ;
138206 const observed = installOAuthFetch ( [ 401 , 401 ] ) ;
139207 const server = startServer ( 0 ) ;
@@ -181,7 +249,7 @@ describe("xAI OAuth upstream 401 replay", () => {
181249 } ) ;
182250
183251 test ( "concurrent 401 responses join one IdP refresh" , async ( ) => {
184- seedOAuth ( ) ;
252+ await seedOAuth ( ) ;
185253 saveConfig ( xaiConfig ( ) ) ;
186254 let refreshCalls = 0 ;
187255 let signalRefreshStarted ! : ( ) => void ;
0 commit comments