@@ -662,63 +662,69 @@ suite('Kernel Connection Helpers', () => {
662662 }
663663
664664 function createMockKernel ( options : MockKernelOptions ) {
665- let iopubCallback : ( ( msg : any ) => void ) | undefined ;
666-
667665 return {
668- requestExecute : ( ) => ( {
669- done : Promise . resolve ( {
670- content :
671- options . status === 'ok'
672- ? { status : 'ok' as const }
673- : {
674- status : 'error' as const ,
675- ...options . errorContent
676- }
677- } ) ,
678- onIOPub : ( cb : ( msg : any ) => void ) => {
679- iopubCallback = cb ;
666+ requestExecute : ( ) => {
667+ let iopubCallback : ( ( msg : any ) => void ) | undefined ;
668+
669+ // Create a promise that resolves after IOPub messages are dispatched
670+ const donePromise = new Promise < any > ( ( resolve ) => {
680671 // Dispatch messages asynchronously to preserve async behavior
681- if ( options . messages && options . messages . length > 0 ) {
682- setTimeout ( ( ) => {
683- if ( iopubCallback ) {
684- options . messages ! . forEach ( ( msg ) => {
685- iopubCallback ! ( {
686- header : { msg_type : msg . msg_type } ,
687- content : msg . content
688- } ) ;
672+ setTimeout ( ( ) => {
673+ if ( iopubCallback && options . messages && options . messages . length > 0 ) {
674+ options . messages . forEach ( ( msg ) => {
675+ iopubCallback ! ( {
676+ header : { msg_type : msg . msg_type } ,
677+ content : msg . content
689678 } ) ;
690- }
691- } , 0 ) ;
679+ } ) ;
680+ }
681+ // Resolve the done promise after messages are dispatched
682+ resolve ( {
683+ content :
684+ options . status === 'ok'
685+ ? { status : 'ok' as const }
686+ : {
687+ status : 'error' as const ,
688+ ...options . errorContent
689+ }
690+ } ) ;
691+ } , 0 ) ;
692+ } ) ;
693+
694+ return {
695+ done : donePromise ,
696+ set onIOPub ( cb : ( msg : any ) => void ) {
697+ iopubCallback = cb ;
692698 }
693- }
694- } )
699+ } ;
700+ }
695701 } ;
696702 }
697703
698704 test ( 'Returns outputs from kernel execution' , async ( ) => {
699705 const mockKernel = createMockKernel ( {
700- status : 'ok'
706+ status : 'ok' ,
707+ messages : [
708+ {
709+ msg_type : 'stream' ,
710+ content : {
711+ name : 'stdout' ,
712+ text : 'hello\n'
713+ }
714+ }
715+ ]
701716 } ) ;
702717
703718 const code = 'print("hello")' ;
704719 const { executeSilently } = await import ( './helpers' ) ;
705720 const result = await executeSilently ( mockKernel as any , code ) ;
706721
707- // executeSilently should return outputs array
708- assert . isArray ( result ) ;
709- } ) ;
710-
711- test ( 'Handles empty code' , async ( ) => {
712- const mockKernel = createMockKernel ( {
713- status : 'ok'
714- } ) ;
715-
716- const code = '' ;
717- const { executeSilently } = await import ( './helpers' ) ;
718- const result = await executeSilently ( mockKernel as any , code ) ;
719-
720- // Should return empty array for empty code
722+ // executeSilently should return outputs array with collected stream output
721723 assert . isArray ( result ) ;
724+ assert . equal ( result . length , 1 ) ;
725+ assert . equal ( result [ 0 ] . output_type , 'stream' ) ;
726+ assert . equal ( ( result [ 0 ] as any ) . name , 'stdout' ) ;
727+ assert . equal ( ( result [ 0 ] as any ) . text , 'hello\n' ) ;
722728 } ) ;
723729
724730 test ( 'Collects stream outputs' , async ( ) => {
@@ -740,10 +746,10 @@ suite('Kernel Connection Helpers', () => {
740746 const result = await executeSilently ( mockKernel as any , code ) ;
741747
742748 assert . isArray ( result ) ;
743- // Should have collected the stream output
744- if ( result && result . length > 0 ) {
745- assert . equal ( result [ 0 ] . output_type , 'stream ' ) ;
746- }
749+ assert . equal ( result . length , 1 ) ;
750+ assert . equal ( result [ 0 ] . output_type , 'stream' ) ;
751+ assert . equal ( ( result [ 0 ] as any ) . name , 'stdout ' ) ;
752+ assert . equal ( ( result [ 0 ] as any ) . text , 'test output' ) ;
747753 } ) ;
748754
749755 test ( 'Collects error outputs' , async ( ) => {
@@ -771,10 +777,11 @@ suite('Kernel Connection Helpers', () => {
771777 const result = await executeSilently ( mockKernel as any , code ) ;
772778
773779 assert . isArray ( result ) ;
774- // Should have collected the error output
775- if ( result && result . length > 0 ) {
776- assert . equal ( result [ 0 ] . output_type , 'error' ) ;
777- }
780+ assert . equal ( result . length , 1 ) ;
781+ assert . equal ( result [ 0 ] . output_type , 'error' ) ;
782+ assert . equal ( ( result [ 0 ] as any ) . ename , 'NameError' ) ;
783+ assert . equal ( ( result [ 0 ] as any ) . evalue , 'name not defined' ) ;
784+ assert . deepStrictEqual ( ( result [ 0 ] as any ) . traceback , [ 'Traceback...' ] ) ;
778785 } ) ;
779786
780787 test ( 'Collects display_data outputs' , async ( ) => {
@@ -798,10 +805,10 @@ suite('Kernel Connection Helpers', () => {
798805 const result = await executeSilently ( mockKernel as any , code ) ;
799806
800807 assert . isArray ( result ) ;
801- // Should have collected the display_data output
802- if ( result && result . length > 0 ) {
803- assert . equal ( result [ 0 ] . output_type , 'display_data' ) ;
804- }
808+ assert . equal ( result . length , 1 ) ;
809+ assert . equal ( result [ 0 ] . output_type , 'display_data' ) ;
810+ assert . deepStrictEqual ( ( result [ 0 ] as any ) . data , { 'text/plain' : 'some data' } ) ;
811+ assert . deepStrictEqual ( ( result [ 0 ] as any ) . metadata , { } ) ;
805812 } ) ;
806813
807814 test ( 'Handles multiple outputs' , async ( ) => {
@@ -830,10 +837,11 @@ suite('Kernel Connection Helpers', () => {
830837 const result = await executeSilently ( mockKernel as any , code ) ;
831838
832839 assert . isArray ( result ) ;
833- // Should have collected multiple outputs
834- if ( result ) {
835- assert . isAtLeast ( result . length , 0 ) ;
836- }
840+ // Consecutive stream messages with the same name are concatenated
841+ assert . equal ( result . length , 1 ) ;
842+ assert . equal ( result [ 0 ] . output_type , 'stream' ) ;
843+ assert . equal ( ( result [ 0 ] as any ) . name , 'stdout' ) ;
844+ assert . equal ( ( result [ 0 ] as any ) . text , 'output 1output 2' ) ;
837845 } ) ;
838846 } ) ;
839847} ) ;
0 commit comments