@@ -1229,6 +1229,59 @@ describe("AccountManager", () => {
12291229 await manager . commitRefreshedAuth ( accountA , refreshedAuth ) ;
12301230 } ) ;
12311231
1232+ it ( "keeps trimmed token accountId identical in memory and persisted storage" , async ( ) => {
1233+ const { withAccountStorageTransaction } = await import ( "../lib/storage.js" ) ;
1234+ const mockWithAccountStorageTransaction = vi . mocked (
1235+ withAccountStorageTransaction ,
1236+ ) ;
1237+ const now = Date . now ( ) ;
1238+ const payload = Buffer . from (
1239+ JSON . stringify ( {
1240+ "https://api.openai.com/auth" : {
1241+ chatgpt_account_id : " matching-account-id " ,
1242+ } ,
1243+ } ) ,
1244+ ) . toString ( "base64url" ) ;
1245+ const stored = {
1246+ version : 3 as const ,
1247+ activeIndex : 0 ,
1248+ accounts : [
1249+ {
1250+ refreshToken : "old-refresh" ,
1251+ accessToken : "old-access" ,
1252+ expiresAt : now ,
1253+ addedAt : now ,
1254+ lastUsed : now ,
1255+ accountId : "matching-account-id" ,
1256+ accountIdSource : "token" as const ,
1257+ } ,
1258+ ] ,
1259+ } ;
1260+ const manager = new AccountManager ( undefined , stored as any ) ;
1261+ const account = manager . getAccountByIndex ( 0 ) ! ;
1262+ const refreshedAuth : OAuthAuthDetails = {
1263+ type : "oauth" ,
1264+ access : `header.${ payload } .signature` ,
1265+ refresh : "new-refresh" ,
1266+ expires : now + 3_600_000 ,
1267+ } ;
1268+
1269+ mockWithAccountStorageTransaction . mockImplementationOnce ( async ( handler ) => {
1270+ const persist = vi . fn ( ) . mockResolvedValue ( undefined ) ;
1271+ const result = await handler ( stored as any , persist ) ;
1272+
1273+ expect ( persist ) . toHaveBeenCalledTimes ( 1 ) ;
1274+ const persistedStorage = persist . mock . calls [ 0 ] ?. [ 0 ] ;
1275+ expect ( persistedStorage ?. accounts [ 0 ] ?. accountId ) . toBe ( "matching-account-id" ) ;
1276+
1277+ return result ;
1278+ } ) ;
1279+
1280+ await manager . commitRefreshedAuth ( account , refreshedAuth ) ;
1281+
1282+ expect ( account . accountId ) . toBe ( "matching-account-id" ) ;
1283+ } ) ;
1284+
12321285 it ( "propagates storage write failure as retryable CodexAuthError" , async ( ) => {
12331286 const { withAccountStorageTransaction } = await import ( "../lib/storage.js" ) ;
12341287 const mockWithAccountStorageTransaction = vi . mocked (
0 commit comments