@@ -149,14 +149,14 @@ async function sendInitializeResult(
149149 harness : ReturnType < typeof createClientHarness > ,
150150 userAgent : string ,
151151) : Promise < void > {
152- await vi . waitFor ( ( ) => expect ( harness . writes . length ) . toBeGreaterThanOrEqual ( 1 ) ) ;
153- const initialize = JSON . parse ( harness . writes [ 0 ] ?? "{}" ) as { id ?: number } ;
152+ const initialize = JSON . parse ( await harness . waitForWrite ( 0 ) ) as { id : number ; method : string } ;
153+ expect ( initialize . method ) . toBe ( "initialize" ) ;
154154 harness . send ( { id : initialize . id , result : { userAgent } } ) ;
155155}
156156
157157async function sendEmptyModelList ( harness : ReturnType < typeof createClientHarness > ) : Promise < void > {
158- await vi . waitFor ( ( ) => expect ( harness . writes . length ) . toBeGreaterThanOrEqual ( 3 ) ) ;
159- const modelList = JSON . parse ( harness . writes [ 2 ] ?? "{}" ) as { id ?: number } ;
158+ const modelList = JSON . parse ( await harness . waitForWrite ( 2 ) ) as { id : number ; method : string } ;
159+ expect ( modelList . method ) . toBe ( "model/list" ) ;
160160 harness . send ( { id : modelList . id , result : { data : [ ] } } ) ;
161161}
162162
@@ -411,8 +411,8 @@ describe("shared Codex app-server client", () => {
411411 secretFreeCacheKey : "replacement-account" ,
412412 } ) ;
413413 const nextAcquire = getLeasedSharedCodexAppServerClient ( options ) ;
414- await vi . waitFor ( ( ) => expect ( startSpy ) . toHaveBeenCalledTimes ( 2 ) ) ;
415414 await sendInitializeResult ( replacement , "openclaw/0.149.0 (Linux; test)" ) ;
415+ expect ( startSpy ) . toHaveBeenCalledTimes ( 2 ) ;
416416 await expect ( nextAcquire ) . resolves . toBe ( replacement . client ) ;
417417 releaseLeasedSharedCodexAppServerClient ( replacement . client ) ;
418418 expect ( mocks . applyCodexAppServerAuthProfile ) . toHaveBeenLastCalledWith (
@@ -1374,25 +1374,33 @@ describe("shared Codex app-server client", () => {
13741374 chatgptPlanType : null ,
13751375 } ) ;
13761376 }
1377+ const responseIndex = first . writes . length ;
13771378 first . send ( {
13781379 id : "failed-refresh" ,
13791380 method : "account/chatgptAuthTokens/refresh" ,
13801381 params : { reason : "unauthorized" , previousAccountId : "original-account" } ,
13811382 } ) ;
1382- await vi . waitFor ( ( ) =>
1383- expect ( first . writes . map ( ( line ) => JSON . parse ( line ) ) ) . toContainEqual (
1384- expect . objectContaining ( { id : "failed-refresh" , error : expect . any ( Object ) } ) ,
1385- ) ,
1386- ) ;
1383+ expect ( JSON . parse ( await first . waitForWrite ( responseIndex ) ) ) . toEqual ( {
1384+ id : "failed-refresh" ,
1385+ error : {
1386+ code : - 32603 ,
1387+ message :
1388+ failure === "failure"
1389+ ? "refresh failed"
1390+ : "ChatGPT workspace changed during Codex token refresh. Retry to start a client for the selected workspace." ,
1391+ } ,
1392+ } ) ;
13871393 expect ( first . stdinDestroyed ) . toBe ( false ) ;
13881394 const nextAcquire = getLeasedSharedCodexAppServerClient ( options ) ;
1389- await vi . waitFor ( ( ) => expect ( startSpy ) . toHaveBeenCalledTimes ( 2 ) ) ;
13901395 await sendInitializeResult ( replacement , "openclaw/0.149.0 (Linux; test)" ) ;
1396+ expect ( startSpy ) . toHaveBeenCalledTimes ( 2 ) ;
13911397 await expect ( nextAcquire ) . resolves . toBe ( replacement . client ) ;
13921398 expect ( releaseLeasedSharedCodexAppServerClient ( first . client ) ) . toBe ( true ) ;
13931399 expect ( first . stdinDestroyed ) . toBe ( true ) ;
13941400 expect ( replacement . stdinDestroyed ) . toBe ( false ) ;
1395- releaseLeasedSharedCodexAppServerClient ( replacement . client ) ;
1401+ expect ( releaseLeasedSharedCodexAppServerClient ( replacement . client ) ) . toBe ( true ) ;
1402+ expect ( clearSharedCodexAppServerClientIfCurrent ( replacement . client ) ) . toBe ( true ) ;
1403+ expect ( replacement . stdinDestroyed ) . toBe ( true ) ;
13961404 } ,
13971405 ) ;
13981406
@@ -1543,8 +1551,8 @@ describe("shared Codex app-server client", () => {
15431551 }
15441552 : { authProfileId : "openai:scoped" , authProfileStore : secondStore } ) ,
15451553 } ) ;
1546- await vi . waitFor ( ( ) => expect ( startSpy ) . toHaveBeenCalledTimes ( 2 ) ) ;
15471554 await sendInitializeResult ( secondHarness , "openclaw/0.149.0 (macOS; test)" ) ;
1555+ expect ( startSpy ) . toHaveBeenCalledTimes ( 2 ) ;
15481556 await expect ( secondPromise ) . resolves . toBe ( secondHarness . client ) ;
15491557
15501558 expect ( resolvedCacheKeys ) . toEqual ( [ "account:sha256:first" , "account:sha256:second" ] ) ;
@@ -1693,8 +1701,8 @@ describe("shared Codex app-server client", () => {
16931701 timeoutMs : 1000 ,
16941702 preparedAuth : { kind : "api-key" , apiKey : "second-platform-key" } ,
16951703 } ) ;
1696- await vi . waitFor ( ( ) => expect ( startSpy ) . toHaveBeenCalledTimes ( 2 ) ) ;
16971704 await sendInitializeResult ( secondHarness , "openclaw/0.149.0 (macOS; test)" ) ;
1705+ expect ( startSpy ) . toHaveBeenCalledTimes ( 2 ) ;
16981706 await expect ( secondPromise ) . resolves . toBe ( secondHarness . client ) ;
16991707
17001708 expect ( cacheKeys ) . toEqual ( [ "api_key:sha256:first" , "api_key:sha256:second" ] ) ;
0 commit comments