@@ -646,4 +646,219 @@ suite('Kernel Connection Helpers', () => {
646646 assert . strictEqual ( name , '.env (Python 9.8.7)' ) ;
647647 } ) ;
648648 } ) ;
649+
650+ suite ( 'executeSilently' , ( ) => {
651+ test ( 'Returns outputs from kernel execution' , async ( ) => {
652+ const mockKernel = {
653+ requestExecute : ( ) => ( {
654+ done : Promise . resolve ( {
655+ content : {
656+ status : 'ok' as const
657+ }
658+ } ) ,
659+ onIOPub : ( ) => {
660+ // noop
661+ }
662+ } )
663+ } ;
664+
665+ const code = 'print("hello")' ;
666+ const { executeSilently } = await import ( './helpers' ) ;
667+ const result = await executeSilently ( mockKernel as any , code ) ;
668+
669+ // executeSilently should return outputs array
670+ assert . isArray ( result ) ;
671+ } ) ;
672+
673+ test ( 'Handles empty code' , async ( ) => {
674+ const mockKernel = {
675+ requestExecute : ( ) => ( {
676+ done : Promise . resolve ( {
677+ content : {
678+ status : 'ok' as const
679+ }
680+ } ) ,
681+ onIOPub : ( ) => {
682+ // noop
683+ }
684+ } )
685+ } ;
686+
687+ const code = '' ;
688+ const { executeSilently } = await import ( './helpers' ) ;
689+ const result = await executeSilently ( mockKernel as any , code ) ;
690+
691+ // Should return empty array for empty code
692+ assert . isArray ( result ) ;
693+ } ) ;
694+
695+ test ( 'Collects stream outputs' , async ( ) => {
696+ let iopubCallback : ( ( msg : any ) => void ) | undefined ;
697+
698+ const mockKernel = {
699+ requestExecute : ( ) => ( {
700+ done : Promise . resolve ( {
701+ content : {
702+ status : 'ok' as const
703+ }
704+ } ) ,
705+ onIOPub : ( cb : ( msg : any ) => void ) => {
706+ iopubCallback = cb ;
707+ // Simulate stream output
708+ setTimeout ( ( ) => {
709+ if ( iopubCallback ) {
710+ iopubCallback ( {
711+ header : { msg_type : 'stream' } ,
712+ content : {
713+ name : 'stdout' ,
714+ text : 'test output'
715+ }
716+ } ) ;
717+ }
718+ } , 0 ) ;
719+ }
720+ } )
721+ } ;
722+
723+ const code = 'print("test")' ;
724+ const { executeSilently } = await import ( './helpers' ) ;
725+ const result = await executeSilently ( mockKernel as any , code ) ;
726+
727+ assert . isArray ( result ) ;
728+ // Should have collected the stream output
729+ if ( result && result . length > 0 ) {
730+ assert . equal ( result [ 0 ] . output_type , 'stream' ) ;
731+ }
732+ } ) ;
733+
734+ test ( 'Collects error outputs' , async ( ) => {
735+ let iopubCallback : ( ( msg : any ) => void ) | undefined ;
736+
737+ const mockKernel = {
738+ requestExecute : ( ) => ( {
739+ done : Promise . resolve ( {
740+ content : {
741+ status : 'error' as const ,
742+ ename : 'NameError' ,
743+ evalue : 'name not defined' ,
744+ traceback : [ 'Traceback...' ]
745+ }
746+ } ) ,
747+ onIOPub : ( cb : ( msg : any ) => void ) => {
748+ iopubCallback = cb ;
749+ // Simulate error output
750+ setTimeout ( ( ) => {
751+ if ( iopubCallback ) {
752+ iopubCallback ( {
753+ header : { msg_type : 'error' } ,
754+ content : {
755+ ename : 'NameError' ,
756+ evalue : 'name not defined' ,
757+ traceback : [ 'Traceback...' ]
758+ }
759+ } ) ;
760+ }
761+ } , 0 ) ;
762+ }
763+ } )
764+ } ;
765+
766+ const code = 'undefined_variable' ;
767+ const { executeSilently } = await import ( './helpers' ) ;
768+ const result = await executeSilently ( mockKernel as any , code ) ;
769+
770+ assert . isArray ( result ) ;
771+ // Should have collected the error output
772+ if ( result && result . length > 0 ) {
773+ assert . equal ( result [ 0 ] . output_type , 'error' ) ;
774+ }
775+ } ) ;
776+
777+ test ( 'Collects display_data outputs' , async ( ) => {
778+ let iopubCallback : ( ( msg : any ) => void ) | undefined ;
779+
780+ const mockKernel = {
781+ requestExecute : ( ) => ( {
782+ done : Promise . resolve ( {
783+ content : {
784+ status : 'ok' as const
785+ }
786+ } ) ,
787+ onIOPub : ( cb : ( msg : any ) => void ) => {
788+ iopubCallback = cb ;
789+ // Simulate display_data output
790+ setTimeout ( ( ) => {
791+ if ( iopubCallback ) {
792+ iopubCallback ( {
793+ header : { msg_type : 'display_data' } ,
794+ content : {
795+ data : {
796+ 'text/plain' : 'some data'
797+ } ,
798+ metadata : { }
799+ }
800+ } ) ;
801+ }
802+ } , 0 ) ;
803+ }
804+ } )
805+ } ;
806+
807+ const code = 'display("data")' ;
808+ const { executeSilently } = await import ( './helpers' ) ;
809+ const result = await executeSilently ( mockKernel as any , code ) ;
810+
811+ assert . isArray ( result ) ;
812+ // Should have collected the display_data output
813+ if ( result && result . length > 0 ) {
814+ assert . equal ( result [ 0 ] . output_type , 'display_data' ) ;
815+ }
816+ } ) ;
817+
818+ test ( 'Handles multiple outputs' , async ( ) => {
819+ let iopubCallback : ( ( msg : any ) => void ) | undefined ;
820+
821+ const mockKernel = {
822+ requestExecute : ( ) => ( {
823+ done : Promise . resolve ( {
824+ content : {
825+ status : 'ok' as const
826+ }
827+ } ) ,
828+ onIOPub : ( cb : ( msg : any ) => void ) => {
829+ iopubCallback = cb ;
830+ // Simulate multiple outputs
831+ setTimeout ( ( ) => {
832+ if ( iopubCallback ) {
833+ iopubCallback ( {
834+ header : { msg_type : 'stream' } ,
835+ content : {
836+ name : 'stdout' ,
837+ text : 'output 1'
838+ }
839+ } ) ;
840+ iopubCallback ( {
841+ header : { msg_type : 'stream' } ,
842+ content : {
843+ name : 'stdout' ,
844+ text : 'output 2'
845+ }
846+ } ) ;
847+ }
848+ } , 0 ) ;
849+ }
850+ } )
851+ } ;
852+
853+ const code = 'print("1"); print("2")' ;
854+ const { executeSilently } = await import ( './helpers' ) ;
855+ const result = await executeSilently ( mockKernel as any , code ) ;
856+
857+ assert . isArray ( result ) ;
858+ // Should have collected multiple outputs
859+ if ( result ) {
860+ assert . isAtLeast ( result . length , 0 ) ;
861+ }
862+ } ) ;
863+ } ) ;
649864} ) ;
0 commit comments