@@ -1159,6 +1159,76 @@ describe("AccountManager", () => {
11591159 expect ( account . consecutiveAuthFailures ) . toBe ( 0 ) ;
11601160 } ) ;
11611161
1162+ it ( "preserves unsaved live pool state when persisting refreshed auth" , async ( ) => {
1163+ const { withAccountStorageTransaction } = await import ( "../lib/storage.js" ) ;
1164+ const mockWithAccountStorageTransaction = vi . mocked (
1165+ withAccountStorageTransaction ,
1166+ ) ;
1167+ const now = Date . now ( ) ;
1168+ const stored = {
1169+ version : 3 as const ,
1170+ activeIndex : 0 ,
1171+ activeIndexByFamily : {
1172+ codex : 0 ,
1173+ } ,
1174+ accounts : [
1175+ {
1176+ refreshToken : "old-refresh-a" ,
1177+ accessToken : "old-access-a" ,
1178+ expiresAt : now ,
1179+ addedAt : now ,
1180+ lastUsed : now ,
1181+ email : "a@example.com" ,
1182+ } ,
1183+ {
1184+ refreshToken : "old-refresh-b" ,
1185+ accessToken : "old-access-b" ,
1186+ expiresAt : now ,
1187+ addedAt : now ,
1188+ lastUsed : now ,
1189+ email : "b@example.com" ,
1190+ } ,
1191+ ] ,
1192+ } ;
1193+ const manager = new AccountManager ( undefined , stored as any ) ;
1194+ const accountA = manager . getAccountByIndex ( 0 ) ! ;
1195+ const accountB = manager . getAccountByIndex ( 1 ) ! ;
1196+ manager . markSwitched ( accountB , "rotation" , "codex" ) ;
1197+ manager . markRateLimited ( accountB , 45_000 , "codex" ) ;
1198+ manager . markAccountCoolingDown ( accountB , 30_000 , "network-error" ) ;
1199+
1200+ const refreshedAuth : OAuthAuthDetails = {
1201+ type : "oauth" ,
1202+ access : "header.payload.signature" ,
1203+ refresh : "new-refresh-a" ,
1204+ expires : now + 3_600_000 ,
1205+ } ;
1206+
1207+ mockWithAccountStorageTransaction . mockImplementationOnce ( async ( handler ) => {
1208+ const persist = vi . fn ( ) . mockResolvedValue ( undefined ) ;
1209+ const result = await handler ( stored as any , persist ) ;
1210+
1211+ expect ( persist ) . toHaveBeenCalledTimes ( 1 ) ;
1212+ const persistedStorage = persist . mock . calls [ 0 ] ?. [ 0 ] ;
1213+ expect ( persistedStorage ?. activeIndex ) . toBe ( 1 ) ;
1214+ expect ( persistedStorage ?. activeIndexByFamily ?. codex ) . toBe ( 1 ) ;
1215+ expect ( persistedStorage ?. accounts [ 0 ] ?. refreshToken ) . toBe ( "new-refresh-a" ) ;
1216+ expect ( persistedStorage ?. accounts [ 1 ] ?. rateLimitResetTimes ?. codex ) . toBeGreaterThan (
1217+ now ,
1218+ ) ;
1219+ expect ( persistedStorage ?. accounts [ 1 ] ?. cooldownReason ) . toBe (
1220+ "network-error" ,
1221+ ) ;
1222+ expect ( persistedStorage ?. accounts [ 1 ] ?. coolingDownUntil ) . toBeGreaterThan (
1223+ now ,
1224+ ) ;
1225+
1226+ return result ;
1227+ } ) ;
1228+
1229+ await manager . commitRefreshedAuth ( accountA , refreshedAuth ) ;
1230+ } ) ;
1231+
11621232 it ( "propagates storage write failure as retryable CodexAuthError" , async ( ) => {
11631233 const { withAccountStorageTransaction } = await import ( "../lib/storage.js" ) ;
11641234 const mockWithAccountStorageTransaction = vi . mocked (
0 commit comments