@@ -31,6 +31,7 @@ import { generateSessionId, initSessionDir, sessionDir } from "./session/index.j
3131import { saveState } from "./session/state.js" ;
3232import { filterMcpServersForConnect } from "./trust/project-trust.js" ;
3333import { createExaMCPServerConfig } from "./mcp/exa.js" ;
34+ import { withFileLogSink } from "../tests/helpers/file-log-sink.js" ;
3435
3536const BUILTIN_EXA_MCP = createExaMCPServerConfig ( ) ;
3637
@@ -528,28 +529,18 @@ describe("loadConfig", () => {
528529 ) ;
529530 }
530531
531- const chunks : string [ ] = [ ] ;
532- const orig = process . stderr . write . bind ( process . stderr ) ;
533- process . stderr . write = ( ( chunk : string | Uint8Array , ...rest : unknown [ ] ) => {
534- chunks . push ( typeof chunk === "string" ? chunk : Buffer . from ( chunk ) . toString ( ) ) ;
535- return orig ( chunk , ...( rest as [ ] ) ) ;
536- } ) as typeof process . stderr . write ;
537532 let config : Awaited < ReturnType < typeof loadConfig > > ;
538- try {
533+ const logged = await withFileLogSink ( async ( ) => {
539534 config = await loadConfig ( [ "resume" , targetId , "--force" , "--cwd" , cwd ] , {
540535 globalSettingsPath : globalPath ,
541536 home,
542537 } ) ;
543- } finally {
544- process . stderr . write = orig ;
545- }
546- assertConfigured ( config ) ;
547- expect ( config . sessionId ) . toBe ( targetId ) ;
548- expect ( config . task ) . toBe ( "target failed session" ) ;
549- const text = chunks . join ( "" ) ;
550- expect ( text ) . not . toContain ( "ignoring unreadable" ) ;
551- expect ( text ) . not . toContain ( home ) ;
552- expect ( text ) . not . toContain ( "invalid shape" ) ;
538+ } ) ;
539+ assertConfigured ( config ! ) ;
540+ expect ( config ! . sessionId ) . toBe ( targetId ) ;
541+ expect ( config ! . task ) . toBe ( "target failed session" ) ;
542+ expect ( logged ) . not . toContain ( "unreadable session state" ) ;
543+ expect ( logged ) . not . toContain ( home ) ;
553544 } finally {
554545 await rm ( cwd , { recursive : true , force : true } ) ;
555546 await rm ( home , { recursive : true , force : true } ) ;
@@ -654,25 +645,20 @@ describe("loadConfig", () => {
654645 const globalPath = await writeGlobalSettings ( cwd ) ;
655646 const sessionId = generateSessionId ( ) ;
656647 await initSessionDir ( cwd , sessionId , home ) ;
657- await writeFile ( join ( sessionDir ( cwd , sessionId , home ) , "run.json" ) , "{ not json" ) ;
658-
659- const chunks : string [ ] = [ ] ;
660- const orig = process . stderr . write . bind ( process . stderr ) ;
661- process . stderr . write = ( ( chunk : string | Uint8Array , ...rest : unknown [ ] ) => {
662- chunks . push ( typeof chunk === "string" ? chunk : Buffer . from ( chunk ) . toString ( ) ) ;
663- return orig ( chunk , ...( rest as [ ] ) ) ;
664- } ) as typeof process . stderr . write ;
648+ const runPath = join ( sessionDir ( cwd , sessionId , home ) , "run.json" ) ;
649+ await writeFile ( runPath , "{ not json" ) ;
650+
665651 let thrown : unknown ;
666- try {
667- await loadConfig ( [ "resume" , sessionId , "--force" , "--cwd" , cwd ] , {
668- globalSettingsPath : globalPath ,
669- home ,
670- } ) ;
671- } catch ( err ) {
672- thrown = err ;
673- } finally {
674- process . stderr . write = orig ;
675- }
652+ const logged = await withFileLogSink ( async ( ) => {
653+ try {
654+ await loadConfig ( [ "resume" , sessionId , "--force" , "--cwd" , cwd ] , {
655+ globalSettingsPath : globalPath ,
656+ home ,
657+ } ) ;
658+ } catch ( err ) {
659+ thrown = err ;
660+ }
661+ } ) ;
676662
677663 expect ( thrown ) . toBeInstanceOf ( CliUserError ) ;
678664 const message = thrown instanceof Error ? thrown . message : String ( thrown ) ;
@@ -687,9 +673,8 @@ describe("loadConfig", () => {
687673 if ( thrown instanceof CliUserError ) {
688674 expect ( thrown . exitCode ) . toBe ( 1 ) ;
689675 }
690- const text = chunks . join ( "" ) ;
691- expect ( text ) . not . toContain ( "ignoring unreadable" ) ;
692- expect ( text ) . not . toContain ( home ) ;
676+ expect ( logged ) . toContain ( runPath ) ;
677+ expect ( logged ) . toContain ( "corrupt JSON" ) ;
693678 } finally {
694679 await rm ( cwd , { recursive : true , force : true } ) ;
695680 await rm ( home , { recursive : true , force : true } ) ;
0 commit comments