@@ -53,7 +53,68 @@ describe("account pool helper", () => {
5353 } ) ;
5454 } ) ;
5555
56- it ( "preserves lastUsed when updating an existing account" , async ( ) => {
56+ it ( "advances lastUsed when updating an existing account by default" , async ( ) => {
57+ const persist = vi . fn ( async ( ) => undefined ) ;
58+ const originalLastUsed = 456 ;
59+ const nowSpy = vi . spyOn ( Date , "now" ) . mockReturnValue ( 789 ) ;
60+
61+ try {
62+ await persistAccountPoolResults ( {
63+ results : [
64+ {
65+ type : "success" ,
66+ access : "access-token-next" ,
67+ refresh : "refresh-token" ,
68+ expires : 999 ,
69+ } ,
70+ ] ,
71+ replaceAll : false ,
72+ modelFamilies : [ "codex" ] ,
73+ withAccountStorageTransaction : async ( handler ) =>
74+ handler (
75+ {
76+ version : 3 ,
77+ activeIndex : 0 ,
78+ activeIndexByFamily : { codex : 0 } ,
79+ accounts : [
80+ {
81+ accountId : "acct_1" ,
82+ email : "user@example.com" ,
83+ refreshToken : "refresh-token" ,
84+ accessToken : "access-token-old" ,
85+ expiresAt : 123 ,
86+ addedAt : 111 ,
87+ lastUsed : originalLastUsed ,
88+ enabled : true ,
89+ } ,
90+ ] ,
91+ } ,
92+ persist ,
93+ ) ,
94+ findMatchingAccountIndex : ( ) => 0 ,
95+ extractAccountId : ( ) => "acct_1" ,
96+ extractAccountEmail : ( ) => "user@example.com" ,
97+ sanitizeEmail : ( email ) => email ,
98+ } ) ;
99+ } finally {
100+ nowSpy . mockRestore ( ) ;
101+ }
102+
103+ expect ( persist ) . toHaveBeenCalledWith (
104+ expect . objectContaining ( {
105+ accounts : [
106+ expect . objectContaining ( {
107+ refreshToken : "refresh-token" ,
108+ accessToken : "access-token-next" ,
109+ expiresAt : 999 ,
110+ lastUsed : 789 ,
111+ } ) ,
112+ ] ,
113+ } ) ,
114+ ) ;
115+ } ) ;
116+
117+ it ( "preserves lastUsed for report-style updates when requested" , async ( ) => {
57118 const persist = vi . fn ( async ( ) => undefined ) ;
58119 const originalLastUsed = 456 ;
59120
@@ -67,6 +128,7 @@ describe("account pool helper", () => {
67128 } ,
68129 ] ,
69130 replaceAll : false ,
131+ preserveLastUsedOnUpdate : true ,
70132 modelFamilies : [ "codex" ] ,
71133 withAccountStorageTransaction : async ( handler ) =>
72134 handler (
0 commit comments