@@ -17,11 +17,14 @@ import {
1717 resolveCodexAuthContext ,
1818} from "../src/codex/auth-context" ;
1919import { isCodexAccountUsable } from "../src/codex/account-usability" ;
20+ import { resetMainCodexAccountIdentityTrackingForTests } from "../src/codex/account-lifecycle" ;
2021import { MAIN_CODEX_ACCOUNT_ID , setMainAccountPlan } from "../src/codex/main-account" ;
2122import { saveCodexAccountCredential } from "../src/codex/account-store" ;
2223import {
2324 clearAccountNeedsReauth ,
2425 clearAccountQuota ,
26+ getAccountQuota ,
27+ isAccountNeedsReauth ,
2528 markAccountNeedsReauth ,
2629 updateAccountQuota ,
2730} from "../src/codex/auth-api" ;
@@ -74,6 +77,7 @@ describe("main account rotation (Option A)", () => {
7477 clearThreadAccountMap ( ) ;
7578 clearCodexUpstreamHealth ( ) ;
7679 clearAccountQuota ( ) ;
80+ resetMainCodexAccountIdentityTrackingForTests ( ) ;
7781 setMainAccountPlan ( null ) ;
7882 for ( const id of [ "a" , "b" , MAIN_CODEX_ACCOUNT_ID ] ) clearAccountNeedsReauth ( id ) ;
7983 saveCred ( "a" ) ;
@@ -85,6 +89,7 @@ describe("main account rotation (Option A)", () => {
8589 clearThreadAccountMap ( ) ;
8690 clearCodexUpstreamHealth ( ) ;
8791 clearAccountQuota ( ) ;
92+ resetMainCodexAccountIdentityTrackingForTests ( ) ;
8893 setMainAccountPlan ( null ) ;
8994 for ( const id of [ "a" , "b" , MAIN_CODEX_ACCOUNT_ID ] ) clearAccountNeedsReauth ( id ) ;
9095 for ( const d of [ STORE_DIR , CODEX_DIR ] ) if ( existsSync ( d ) ) rmSync ( d , { recursive : true } ) ;
@@ -147,6 +152,32 @@ describe("main account rotation (Option A)", () => {
147152 expect ( headers . get ( "chatgpt-account-id" ) ) . toBe ( "main_acct" ) ;
148153 } ) ;
149154
155+ test ( "switching the main auth identity discards runtime state from the previous account" , async ( ) => {
156+ const config = makeConfig ( { activeCodexAccountId : MAIN_CODEX_ACCOUNT_ID , autoSwitchThreshold : 0 , codexAccounts : [ ] } ) ;
157+ await expect ( resolveCodexAuthContext ( new Headers ( ) , config , "pool" ) ) . resolves . toMatchObject ( {
158+ kind : "main-pool" ,
159+ chatgptAccountId : "main_acct" ,
160+ } ) ;
161+
162+ recordCodexUpstreamOutcome ( config , MAIN_CODEX_ACCOUNT_ID , 429 , { retryAfter : "3600" } ) ;
163+ markAccountNeedsReauth ( MAIN_CODEX_ACCOUNT_ID ) ;
164+ updateAccountQuota ( MAIN_CODEX_ACCOUNT_ID , 100 , 0 ) ;
165+ writeFileSync (
166+ join ( CODEX_DIR , "auth.json" ) ,
167+ JSON . stringify ( { tokens : { access_token : "replacement_access" , account_id : "replacement_acct" } } ) ,
168+ ) ;
169+
170+ await expect ( resolveCodexAuthContext ( new Headers ( ) , config , "pool" ) ) . resolves . toEqual ( {
171+ kind : "main-pool" ,
172+ accountId : MAIN_CODEX_ACCOUNT_ID ,
173+ accessToken : "replacement_access" ,
174+ chatgptAccountId : "replacement_acct" ,
175+ } ) ;
176+ expect ( isCodexAccountInCooldown ( MAIN_CODEX_ACCOUNT_ID ) ) . toBe ( false ) ;
177+ expect ( isAccountNeedsReauth ( MAIN_CODEX_ACCOUNT_ID ) ) . toBe ( false ) ;
178+ expect ( getAccountQuota ( MAIN_CODEX_ACCOUNT_ID ) ) . toBeNull ( ) ;
179+ } ) ;
180+
150181 test ( "no active id selects from main plus added accounts and binds main affinity" , async ( ) => {
151182 const config = makeConfig ( { activeCodexAccountId : undefined , autoSwitchThreshold : 0 } ) ;
152183 updateAccountQuota ( MAIN_CODEX_ACCOUNT_ID , 5 , 0 ) ;
0 commit comments