@@ -22,6 +22,7 @@ let calls: string[] = [];
2222let originalFetch : typeof globalThis . fetch ;
2323let accounts : unknown [ ] = [ ] ;
2424let threshold = 80 ;
25+ let nextAccountsResponseGate : Promise < void > | null = null ;
2526
2627beforeEach ( ( ) => {
2728 previous = Object . fromEntries ( globals . map ( ( k ) => [ k , Reflect . get ( globalThis , k ) ] ) ) as typeof previous ;
@@ -36,6 +37,7 @@ beforeEach(() => {
3637
3738 originalFetch = globalThis . fetch ;
3839 calls = [ ] ;
40+ nextAccountsResponseGate = null ;
3941 accounts = [ { id : "a1" , email : "account-one" , isMain : true , paused : false , hasCredential : true , quota : null } ] ;
4042 Object . defineProperty ( globalThis , "fetch" , {
4143 configurable : true ,
@@ -45,7 +47,8 @@ beforeEach(() => {
4547 if ( path === "codex-auth/accounts/pause" ) {
4648 const body = JSON . parse ( String ( init ?. body ) ) as { id : string ; paused : boolean } ;
4749 accounts = accounts . map ( account => (
48- typeof account === "object" && account !== null && "id" in account && account . id === body . id
50+ typeof account === "object" && account !== null && "id" in account
51+ && ( account . id === body . id || ( body . id === "__main__" && "isMain" in account && account . isMain === true ) )
4952 ? { ...account , paused : body . paused }
5053 : account
5154 ) ) ;
@@ -63,6 +66,9 @@ beforeEach(() => {
6366 } as unknown as Response ;
6467 }
6568 if ( path . startsWith ( "codex-auth/accounts" ) ) {
69+ const gate = nextAccountsResponseGate ;
70+ nextAccountsResponseGate = null ;
71+ if ( gate ) await gate ;
6672 return { ok : true , json : async ( ) => ( { accounts } ) } as unknown as Response ;
6773 }
6874 if ( path . startsWith ( "codex-auth/active" ) ) {
@@ -138,6 +144,25 @@ test("pausing an account writes the persisted endpoint and updates shared state"
138144 expect ( seen . current ! . activeId ) . toBeNull ( ) ;
139145} ) ;
140146
147+ test ( "pausing the main sentinel updates its distinct account row before reload" , async ( ) => {
148+ const seen = await mountController ( ) ;
149+ let releaseReload ! : ( ) => void ;
150+ nextAccountsResponseGate = new Promise < void > ( resolve => { releaseReload = resolve ; } ) ;
151+
152+ await act ( async ( ) => {
153+ expect ( await seen . current ! . setAccountPaused ( "__main__" , true ) ) . toEqual ( { ok : true } ) ;
154+ } ) ;
155+
156+ expect ( calls ) . toContain ( "PUT codex-auth/accounts/pause" ) ;
157+ expect ( seen . current ! . accounts . find ( account => account . isMain ) ?. id ) . toBe ( "a1" ) ;
158+ expect ( seen . current ! . accounts . find ( account => account . isMain ) ?. paused ) . toBe ( true ) ;
159+
160+ await act ( async ( ) => {
161+ releaseReload ( ) ;
162+ await new Promise ( ( resolve ) => setTimeout ( resolve , 30 ) ) ;
163+ } ) ;
164+ } ) ;
165+
141166test ( "bulk pausing writes one endpoint and updates every returned account" , async ( ) => {
142167 accounts = [
143168 { id : "a1" , email : "account-one" , isMain : true , paused : false , hasCredential : true , quota : null } ,
0 commit comments