@@ -109,11 +109,13 @@ beforeEach(() => {
109109 process . env . OPENCODEX_HOME = TEST_DIR ;
110110 process . env . CODEX_HOME = TEST_CODEX_HOME ;
111111 delete process . env [ MANUAL_IMPORT_ENV ] ;
112+ clearAccountNeedsReauth ( "__main__" ) ;
112113 clearAccountQuota ( ) ;
113114 clearCodexWebSocketRegistry ( ) ;
114115} ) ;
115116
116117afterEach ( ( ) => {
118+ clearAccountNeedsReauth ( "__main__" ) ;
117119 clearAccountQuota ( ) ;
118120 clearCodexWebSocketRegistry ( ) ;
119121 globalThis . fetch = previousFetch ;
@@ -138,6 +140,38 @@ describe("codex-auth API", () => {
138140 expect ( main ) . toBeTruthy ( ) ;
139141 } ) ;
140142
143+ test ( "BUG-R327: main account exposes and updates needsReauth from WHAM auth responses" , async ( ) => {
144+ writeFileSync ( join ( TEST_CODEX_HOME , "auth.json" ) , JSON . stringify ( {
145+ tokens : {
146+ access_token : "main-access" ,
147+ account_id : "main-account" ,
148+ } ,
149+ } ) ) ;
150+ let status = 401 ;
151+ globalThis . fetch = ( async ( input : RequestInfo | URL ) => {
152+ expect ( String ( input ) ) . toBe ( "https://chatgpt.com/backend-api/wham/usage" ) ;
153+ return status === 200
154+ ?
new Response ( JSON . stringify ( { email :
"[email protected] " , plan_type :
"plus" } ) , { 155+ status,
156+ headers : { "Content-Type" : "application/json" } ,
157+ } )
158+ : new Response ( null , { status } ) ;
159+ } ) as typeof fetch ;
160+
161+ const listMain = async ( ) : Promise < { needsReauth : boolean } > => {
162+ const req = new Request ( "http://localhost/api/codex-auth/accounts?refresh=1" , { method : "GET" } ) ;
163+ const resp = await handleCodexAuthAPI ( req , new URL ( req . url ) , makeConfig ( ) ) ;
164+ const data = await resp ! . json ( ) as { accounts : Array < { id : string ; needsReauth : boolean } > } ;
165+ return data . accounts . find ( account => account . id === "__main__" ) ! ;
166+ } ;
167+
168+ expect ( ( await listMain ( ) ) . needsReauth ) . toBe ( true ) ;
169+ status = 403 ;
170+ expect ( ( await listMain ( ) ) . needsReauth ) . toBe ( true ) ;
171+ status = 200 ;
172+ expect ( ( await listMain ( ) ) . needsReauth ) . toBe ( false ) ;
173+ } ) ;
174+
141175 test ( "maskEmail hides local account names" , ( ) => {
142176 expect ( maskEmail ( "[email protected] " ) ) . toBe ( "*@example.test" ) ; 143177 expect ( maskEmail ( "[email protected] " ) ) . toBe ( "a*@example.test" ) ;
0 commit comments