@@ -793,3 +793,60 @@ func TestNewRunCommandJsonOutput(t *testing.T) {
793793 t .Errorf ("unexpected scripts order or names: %v" , output )
794794 }
795795}
796+
797+ func TestNewRunCommandJsonOutputEmpty (t * testing.T ) {
798+ f := newFakeKoolRun (nil , nil )
799+ f .parser .(* parser.FakeParser ).MockScriptDetails = []parser.ScriptDetail {}
800+
801+ cmd := NewRunCommand (f )
802+ cmd .SetArgs ([]string {"--json" })
803+
804+ if err := cmd .Execute (); err != nil {
805+ t .Errorf ("unexpected error executing run command with --json; error: %v" , err )
806+ }
807+
808+ fakeShell := f .shell .(* shell.FakeShell )
809+
810+ if len (fakeShell .OutLines ) == 0 {
811+ t .Error ("expected JSON output" )
812+ return
813+ }
814+
815+ // Should output empty array, not null
816+ if fakeShell .OutLines [0 ] != "[]" {
817+ t .Errorf ("expected empty JSON array '[]', got '%s'" , fakeShell .OutLines [0 ])
818+ }
819+ }
820+
821+ func TestNewRunCommandJsonOutputNullSafety (t * testing.T ) {
822+ f := newFakeKoolRun (nil , nil )
823+ f .parser .(* parser.FakeParser ).MockScriptDetails = []parser.ScriptDetail {
824+ {
825+ Name : "test" ,
826+ Comments : nil , // nil comments
827+ Commands : nil , // nil commands
828+ },
829+ }
830+
831+ cmd := NewRunCommand (f )
832+ cmd .SetArgs ([]string {"--json" })
833+
834+ if err := cmd .Execute (); err != nil {
835+ t .Errorf ("unexpected error executing run command with --json; error: %v" , err )
836+ }
837+
838+ fakeShell := f .shell .(* shell.FakeShell )
839+
840+ var output []parser.ScriptDetail
841+ if err := json .Unmarshal ([]byte (fakeShell .OutLines [0 ]), & output ); err != nil {
842+ t .Fatalf ("failed to parse json output: %v" , err )
843+ }
844+
845+ // Verify nil values are converted to empty arrays
846+ if output [0 ].Comments == nil {
847+ t .Error ("Comments should not be nil in JSON output" )
848+ }
849+ if output [0 ].Commands == nil {
850+ t .Error ("Commands should not be nil in JSON output" )
851+ }
852+ }
0 commit comments