@@ -568,6 +568,7 @@ describe('CLI with yargs (index.ts)', () => {
568568 mockConsoleError . mockClear ( ) ; // Clear before run
569569
570570 await expect ( runMainWithArgs ( [ 'agent_query' , '{"query": "test"' ] ) ) . rejects . toThrowError (
571+ // This error is thrown by handleClientCommand and then re-thrown by yargs.fail()
571572 new Error ( "Invalid JSON parameters: Expected ',' or '}' after property value in JSON at position 16" )
572573 ) ;
573574
@@ -659,11 +660,10 @@ describe('CLI with yargs (index.ts)', () => {
659660 expect ( mockStartServerHandler ) . toHaveBeenCalled ( ) ;
660661
661662 // Check if the SUT's yargs middleware logged the port being observed.
662- expect ( currentMockLoggerInstance . info . mock . calls ) . toEqual (
663- expect . arrayContaining ( [
664- [ expect . stringContaining ( `[SUT_INDEX_TS_YARGS_MW] --port option value at middleware: ${ customPort } ` ) ]
665- ] )
663+ const portLogFound = currentMockLoggerInstance . info . mock . calls . some (
664+ call => typeof call [ 0 ] === 'string' && call [ 0 ] . includes ( `[SUT_INDEX_TS_YARGS_MW] --port option value at middleware: ${ customPort } ` )
666665 ) ;
666+ expect ( portLogFound , `Expected SUT log for --port middleware. Calls: ${ JSON . stringify ( currentMockLoggerInstance . info . mock . calls ) } ` ) . toBe ( true ) ;
667667
668668 // Restore original
669669 if ( originalHttpPort === undefined ) delete process . env . HTTP_PORT ;
@@ -778,7 +778,7 @@ describe('CLI with yargs (index.ts)', () => {
778778 mockConsoleError . mockClear ( ) ; // Clear before run
779779 mockProcessExit . mockClear ( ) ;
780780
781- const expectedErrorPattern = / U n k n o w n c o m m a n d : u n k n o w n c o m m a n d | Y o u m u s t p r o v i d e a c o m m a n d t o r u n | N o t e n o u g h n o n - o p t i o n a r g u m e n t s | U n k n o w n a r g u m e n t : u n k n o w n c o m m a n d / ; // Added Unknown argument
781+ const expectedErrorPattern = / U n k n o w n c o m m a n d : u n k n o w n c o m m a n d | Y o u m u s t p r o v i d e a c o m m a n d t o r u n | N o t e n o u g h n o n - o p t i o n a r g u m e n t s | U n k n o w n a r g u m e n t : u n k n o w n c o m m a n d / ;
782782 await expect ( runMainWithArgs ( [ 'unknowncommand' ] ) ) . rejects . toThrowError ( expectedErrorPattern ) ;
783783
784784 const consoleErrorCalls = mockConsoleError . mock . calls . map ( call => call . join ( ' ' ) ) . join ( '\n' ) ;
@@ -881,10 +881,15 @@ describe('CLI with yargs (index.ts)', () => {
881881
882882 process . env . VITEST_TESTING_FAIL_HANDLER = "true" ;
883883 await expect ( runMainWithArgs ( [ 'agent_query' , '{"query":"test_json_rpc_error"}' , '--json' ] ) )
884- . rejects . toThrow ( expect . objectContaining ( { // Check for an object that contains the jsonRpcError
885- message : `Tool 'agent_query' failed: ${ rpcErrorObject . message } ` ,
886- jsonRpcError : rpcErrorObject
887- } ) ) ;
884+ . rejects . toThrow (
885+ // The error thrown by yargs.fail() will be an Error instance.
886+ // If handleClientCommand throws an error with a jsonRpcError property,
887+ // yargs.fail() should re-throw that error object.
888+ expect . objectContaining ( {
889+ message : `Tool 'agent_query' failed: ${ rpcErrorObject . message } ` ,
890+ jsonRpcError : rpcErrorObject ,
891+ } )
892+ ) ;
888893
889894 // SUT's handleClientCommand logs the JSON error to console.error when --json is active
890895 // It logs the error object passed to it, which now includes jsonRpcError
@@ -906,7 +911,7 @@ describe('CLI with yargs (index.ts)', () => {
906911 call [ 0 ] === 'YARGS_FAIL_TEST_MODE_ERROR_OUTPUT:' &&
907912 typeof call [ 1 ] === 'string' && call [ 1 ] . includes ( `Tool 'agent_query' failed: ${ rpcErrorObject . message } ` )
908913 ) ;
909- expect ( yargsFailOutputLogged ) . toBe ( true ) ;
914+ expect ( yargsFailOutputLogged , `Expected yargs fail output. Calls: ${ JSON . stringify ( mockConsoleError . mock . calls ) } ` ) . toBe ( true ) ;
910915 expect ( mockProcessExit ) . not . toHaveBeenCalled ( ) ;
911916 delete process . env . VITEST_TESTING_FAIL_HANDLER ;
912917 } ) ;
@@ -935,7 +940,7 @@ describe('CLI with yargs (index.ts)', () => {
935940 call [ 0 ] === 'YARGS_FAIL_TEST_MODE_ERROR_OUTPUT:' &&
936941 typeof call [ 1 ] === 'string' && call [ 1 ] === genericError . message
937942 ) ;
938- expect ( yargsFailOutputLogged ) . toBe ( true ) ;
943+ expect ( yargsFailOutputLogged , `Expected yargs fail output. Calls: ${ JSON . stringify ( mockConsoleError . mock . calls ) } ` ) . toBe ( true ) ;
939944 expect ( mockProcessExit ) . not . toHaveBeenCalled ( ) ;
940945 delete process . env . VITEST_TESTING_FAIL_HANDLER ;
941946 } ) ;
0 commit comments