-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprinter_test.go
More file actions
1530 lines (1377 loc) · 37.2 KB
/
printer_test.go
File metadata and controls
1530 lines (1377 loc) · 37.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package probe
import (
"fmt"
"strings"
"testing"
"time"
"github.com/fatih/color"
)
// Color function tests
func TestColorFunctions(t *testing.T) {
// Disable color output for consistent testing
color.NoColor = true
defer func() { color.NoColor = false }()
tests := []struct {
name string
colorFn func() *color.Color
text string
expected string
}{
{
name: "colorSuccess",
colorFn: colorSuccess,
text: "success",
expected: "success",
},
{
name: "colorError",
colorFn: colorError,
text: "error",
expected: "error",
},
{
name: "colorInfo",
colorFn: colorInfo,
text: "info",
expected: "info",
},
{
name: "colorWarning",
colorFn: colorWarning,
text: "warning",
expected: "warning",
},
{
name: "colorSkipped",
colorFn: colorSkipped,
text: "skipped",
expected: "skipped",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test Sprintf
result := tt.colorFn().Sprintf("%s", tt.text)
if result != tt.expected {
t.Errorf("Sprintf: expected %s, got %s", tt.expected, result)
}
// Test SprintFunc
sprintFunc := tt.colorFn().SprintFunc()
result2 := sprintFunc(tt.text)
if result2 != tt.expected {
t.Errorf("SprintFunc: expected %s, got %s", tt.expected, result2)
}
})
}
}
func TestColorSuccess_RGB(t *testing.T) {
// Test that colorSuccess returns the correct RGB color
c := colorSuccess()
// The color should contain RGB values for 0,175,0
// We can't directly test RGB values, but we can test the function returns a valid color
if c == nil {
t.Error("colorSuccess() should return a non-nil *color.Color")
}
// Test that it can format text
result := c.Sprintf("test")
if !strings.Contains(result, "test") {
t.Errorf("colorSuccess().Sprintf should contain 'test', got %s", result)
}
}
func TestRepeatNoTestDisplay(t *testing.T) {
// Test the "no test" display format
totalCount := 1000
actual := colorInfo().Sprintf("⏺") + " " +
colorInfo().Sprintf("%d/%d completed (no test)", totalCount, totalCount)
// Check that the format contains expected parts
if !strings.Contains(actual, "1000/1000 completed (no test)") {
t.Errorf("Expected format to contain '1000/1000 completed (no test)', got %s", actual)
}
}
// Printer interface tests
func TestNewPrinter(t *testing.T) {
printer := NewPrinter(false, []string{})
if printer == nil {
t.Error("NewPrinter() should return a non-nil Printer")
return
}
if printer.verbose {
t.Error("NewPrinter(false) should set verbose to false")
}
verbosePrinter := NewPrinter(true, []string{})
if !verbosePrinter.verbose {
t.Error("NewPrinter(true) should set verbose to true")
}
}
func TestNewPrinter_BufferInitialization(t *testing.T) {
tests := []struct {
name string
bufferIDs []string
}{
{
name: "empty buffer IDs",
bufferIDs: []string{},
},
{
name: "single buffer ID",
bufferIDs: []string{"job1"},
},
{
name: "multiple buffer IDs",
bufferIDs: []string{"job1", "job2", "job3"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
printer := NewPrinter(false, tt.bufferIDs)
if printer.Buffer == nil {
t.Error("Buffer should not be nil")
return
}
// Check BufferIDs are stored correctly
if len(printer.BufferIDs) != len(tt.bufferIDs) {
t.Errorf("Expected %d BufferIDs, got %d", len(tt.bufferIDs), len(printer.BufferIDs))
}
for i, expectedID := range tt.bufferIDs {
if i >= len(printer.BufferIDs) || printer.BufferIDs[i] != expectedID {
t.Errorf("BufferIDs[%d]: expected '%s', got '%s'", i, expectedID, printer.BufferIDs[i])
}
}
// Check that all provided IDs have initialized buffers
for _, id := range tt.bufferIDs {
if _, exists := printer.Buffer[id]; !exists {
t.Errorf("Buffer for ID '%s' should be initialized", id)
}
if printer.Buffer[id] == nil {
t.Errorf("Buffer for ID '%s' should not be nil", id)
}
}
// Check that buffer count matches expected
if len(printer.Buffer) != len(tt.bufferIDs) {
t.Errorf("Expected %d buffers, got %d", len(tt.bufferIDs), len(printer.Buffer))
}
})
}
}
func TestStatusType(t *testing.T) {
tests := []struct {
status StatusType
expected string
}{
{StatusSuccess, "success"},
{StatusError, "error"},
{StatusWarning, "warning"},
{StatusSkipped, "skipped"},
}
for _, tt := range tests {
t.Run(tt.expected, func(t *testing.T) {
// Test that the status type constants are properly defined
if int(tt.status) < 0 {
t.Errorf("StatusType %s should have a valid integer value", tt.expected)
}
})
}
}
// Test StatusType constants have expected values
func TestStatusTypeConstants(t *testing.T) {
// Test that status constants are assigned in expected order
if StatusSuccess != 0 {
t.Errorf("StatusSuccess should be 0, got %d", StatusSuccess)
}
if StatusError != 1 {
t.Errorf("StatusError should be 1, got %d", StatusError)
}
if StatusWarning != 2 {
t.Errorf("StatusWarning should be 2, got %d", StatusWarning)
}
if StatusSkipped != 3 {
t.Errorf("StatusSkipped should be 3, got %d", StatusSkipped)
}
// Test that all constants are different
statuses := []StatusType{StatusSuccess, StatusError, StatusWarning, StatusSkipped}
for i, status1 := range statuses {
for j, status2 := range statuses {
if i != j && status1 == status2 {
t.Errorf("StatusType constants should be unique, but %d and %d are both %d", i, j, status1)
}
}
}
}
func TestStepResult(t *testing.T) {
result := StepResult{
Index: 1,
Name: "Test Step",
Status: StatusSuccess,
RT: "100ms",
TestOutput: "",
EchoOutput: "",
HasTest: true,
}
if result.Index != 1 {
t.Errorf("Expected Index to be 1, got %d", result.Index)
}
if result.Name != "Test Step" {
t.Errorf("Expected Name to be 'Test Step', got %s", result.Name)
}
if result.Status != StatusSuccess {
t.Errorf("Expected Status to be StatusSuccess, got %d", result.Status)
}
if !result.HasTest {
t.Error("Expected HasTest to be true")
}
}
// Generate method tests
func TestPrinter_generateHeader(t *testing.T) {
// Disable color output for consistent testing
color.NoColor = true
defer func() { color.NoColor = false }()
printer := NewPrinter(false, []string{})
tests := []struct {
name string
title string
description string
want string
}{
{
name: "empty name",
title: "",
description: "Some description",
want: "",
},
{
name: "name only",
title: "Test Workflow",
description: "",
want: "Test Workflow\n\n",
},
{
name: "name and description",
title: "Test Workflow",
description: "This is a test workflow",
want: "Test Workflow\nThis is a test workflow\n\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := printer.generateHeader(tt.title, tt.description)
if result != tt.want {
t.Errorf("generateHeader() = %q, want %q", result, tt.want)
}
})
}
}
func TestPrinter_generateError(t *testing.T) {
// Disable color output for consistent testing
color.NoColor = true
defer func() { color.NoColor = false }()
printer := NewPrinter(false, []string{})
tests := []struct {
name string
format string
args []any
want string
}{
{
name: "simple error",
format: "Something went wrong",
args: []any{},
want: "Error: Something went wrong\n",
},
{
name: "formatted error",
format: "Failed to process %s with code %d",
args: []any{"file.txt", 404},
want: "Error: Failed to process file.txt with code 404\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := printer.generateError(tt.format, tt.args...)
if result != tt.want {
t.Errorf("generateError() = %q, want %q", result, tt.want)
}
})
}
}
func TestPrinter_generateLogDebug(t *testing.T) {
printer := NewPrinter(false, []string{})
tests := []struct {
name string
format string
args []any
want string
}{
{
name: "simple debug",
format: "Debug message",
args: []any{},
want: "[DEBUG] Debug message\n",
},
{
name: "formatted debug",
format: "Processing item %d of %d",
args: []any{5, 10},
want: "[DEBUG] Processing item 5 of 10\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := printer.generateLogDebug(tt.format, tt.args...)
if result != tt.want {
t.Errorf("generateLogDebug() = %q, want %q", result, tt.want)
}
})
}
}
func TestPrinter_generateLogError(t *testing.T) {
// Disable color output for consistent testing
color.NoColor = true
defer func() { color.NoColor = false }()
printer := NewPrinter(false, []string{})
tests := []struct {
name string
format string
args []any
want string
}{
{
name: "simple log error",
format: "Critical error occurred",
args: []any{},
want: "[ERROR] Critical error occurred\n",
},
{
name: "formatted log error",
format: "Database connection failed: %s",
args: []any{"timeout"},
want: "[ERROR] Database connection failed: timeout\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := printer.generateLogError(tt.format, tt.args...)
if result != tt.want {
t.Errorf("generateLogError() = %q, want %q", result, tt.want)
}
})
}
}
func TestPrinter_generateJobStatus(t *testing.T) {
// Disable color output for consistent testing
color.NoColor = true
defer func() { color.NoColor = false }()
printer := NewPrinter(false, []string{})
tests := []struct {
name string
jobID string
jobName string
status StatusType
duration float64
want string
}{
{
name: "success status",
jobID: "job1",
jobName: "Test Job",
status: StatusSuccess,
duration: 1.5,
want: "⏺ Test Job (Completed in 1.50s)\n",
},
{
name: "error status",
jobID: "job2",
jobName: "Failed Job",
status: StatusError,
duration: 2.3,
want: "⏺ Failed Job (Failed in 2.30s)\n",
},
{
name: "skipped status",
jobID: "job3",
jobName: "Skipped Job",
status: StatusSkipped,
duration: 0.0,
want: "⏺ Skipped Job (SKIPPED)\n",
},
{
name: "warning status",
jobID: "job4",
jobName: "Warning Job",
status: StatusWarning,
duration: 0.1,
want: "⏺ Warning Job (Unknown status in 0.10s)\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var output strings.Builder
printer.generateJobStatus(tt.jobID, tt.jobName, tt.status, tt.duration, &output)
result := output.String()
if result != tt.want {
t.Errorf("generateJobStatus() = %q, want %q", result, tt.want)
}
})
}
}
// Test that job status uses correct colors for each status type - with actual color detection
func TestPrinter_generateJobStatus_ColorMapping(t *testing.T) {
// Enable colors to detect actual color differences
color.NoColor = false
defer func() { color.NoColor = true }()
printer := NewPrinter(false, []string{})
tests := []struct {
name string
status StatusType
expectedColorCode string // ANSI color code we expect
}{
{
name: "success uses green color",
status: StatusSuccess,
expectedColorCode: "\x1b[38;2;0;175;0m", // RGB(0,175,0) from colorSuccess
},
{
name: "error uses red color",
status: StatusError,
expectedColorCode: "\x1b[31m", // Red from colorError
},
{
name: "skipped uses gray color",
status: StatusSkipped,
expectedColorCode: "\x1b[90m", // Bright black (gray) from colorSkipped
},
{
name: "warning uses yellow color",
status: StatusWarning,
expectedColorCode: "\x1b[33m", // Yellow from colorWarning
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var output strings.Builder
printer.generateJobStatus("test-job", "Test Job", tt.status, 1.0, &output)
result := output.String()
// Verify basic content is present
if !strings.Contains(result, "Test Job") {
t.Errorf("generateJobStatus() should contain job name, got %q", result)
}
// Verify the expected color code is present in the output
if !strings.Contains(result, tt.expectedColorCode) {
t.Errorf("generateJobStatus() should contain color code %q for %s, got %q", tt.expectedColorCode, tt.name, result)
}
})
}
}
// Test that SUCCESS status specifically does NOT use blue color (colorInfo)
func TestPrinter_generateJobStatus_SuccessNotBlue(t *testing.T) {
// Enable colors to detect actual color differences
color.NoColor = false
defer func() { color.NoColor = true }()
printer := NewPrinter(false, []string{})
var output strings.Builder
printer.generateJobStatus("success-job", "Success Job", StatusSuccess, 1.5, &output)
result := output.String()
// Blue color code from colorInfo
blueColorCode := "\x1b[34m"
// SUCCESS jobs should NOT contain blue color
if strings.Contains(result, blueColorCode) {
t.Errorf("StatusSuccess should NOT use blue color (colorInfo), but found blue color code in output: %q", result)
}
// SUCCESS jobs SHOULD contain green color
greenColorCode := "\x1b[38;2;0;175;0m" // RGB(0,175,0) from colorSuccess
if !strings.Contains(result, greenColorCode) {
t.Errorf("StatusSuccess should use green color (colorSuccess), but green color code not found in output: %q", result)
}
}
// Test PrintMapData with nested structures
func TestPrinter_PrintMapData(t *testing.T) {
color.NoColor = true
defer func() { color.NoColor = false }()
printer := NewPrinter(true, []string{}) // verbose mode to see LogDebug output
tests := []struct {
name string
data map[string]any
expected []string // strings that should appear in the output
}{
{
name: "simple flat map",
data: map[string]any{
"key1": "value1",
"key2": 42,
},
expected: []string{"key1: value1", "key2: 42"},
},
{
name: "nested map",
data: map[string]any{
"outer": map[string]any{
"inner": "nested_value",
"number": 123,
},
},
expected: []string{"outer:", "inner: nested_value", "number: 123"},
},
{
name: "deeply nested map",
data: map[string]any{
"level1": map[string]any{
"level2": map[string]any{
"level3": "deep_value",
},
},
},
expected: []string{"level1:", "level2:", "level3: deep_value"},
},
{
name: "map with slice",
data: map[string]any{
"items": []any{"item1", "item2", "item3"},
},
expected: []string{"items:", "- item1", "- item2", "- item3"},
},
{
name: "map with nested slice and map",
data: map[string]any{
"complex": []any{
map[string]any{
"nested_key": "nested_value",
},
"simple_item",
},
},
expected: []string{"complex:", "-", "nested_key: nested_value", "- simple_item"},
},
{
name: "empty containers",
data: map[string]any{
"empty_map": map[string]any{},
"empty_slice": []any{},
},
expected: []string{"empty_map: {}", "empty_slice: []"},
},
{
name: "string map and slice",
data: map[string]any{
"string_map": map[string]string{
"str_key": "str_value",
},
"string_slice": []string{"str1", "str2"},
},
expected: []string{"string_map:", "str_key: str_value", "string_slice:", "- str1", "- str2"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Capture output by redirecting to a buffer
var captured strings.Builder
originalOut := printer.errWriter
printer.errWriter = &captured
printer.PrintMapData(tt.data)
// Restore original writer
printer.errWriter = originalOut
output := captured.String()
// Check that all expected strings are present in the output
for _, expected := range tt.expected {
if !strings.Contains(output, expected) {
t.Errorf("PrintMapData() output should contain %q, got:\n%s", expected, output)
}
}
})
}
}
// Test that skipped jobs have gray formatting for entire line
func TestPrinter_generateJobStatus_SkippedFormatting(t *testing.T) {
// Disable color output for consistent testing
color.NoColor = true
defer func() { color.NoColor = false }()
printer := NewPrinter(false, []string{})
// Test skipped job formatting
var output strings.Builder
printer.generateJobStatus("skip-job", "Skipped Job", StatusSkipped, 0.0, &output)
result := output.String()
expected := "⏺ Skipped Job (SKIPPED)\n"
if result != expected {
t.Errorf("generateJobStatus() for skipped job = %q, want %q", result, expected)
}
// Test non-skipped job formatting for comparison
var output2 strings.Builder
printer.generateJobStatus("success-job", "Success Job", StatusSuccess, 1.5, &output2)
result2 := output2.String()
expected2 := "⏺ Success Job (Completed in 1.50s)\n"
if result2 != expected2 {
t.Errorf("generateJobStatus() for success job = %q, want %q", result2, expected2)
}
}
func TestPrinter_generateJobResults(t *testing.T) {
printer := NewPrinter(false, []string{})
tests := []struct {
name string
jobID string
input string
want string
}{
{
name: "empty input",
jobID: "job1",
input: "",
want: "\n",
},
{
name: "single line",
jobID: "job1",
input: "Test output",
want: " ⎿ Test output\n\n",
},
{
name: "multiple lines",
jobID: "job1",
input: "Line 1\nLine 2\nLine 3",
want: " ⎿ Line 1\n Line 2\n Line 3\n\n",
},
{
name: "with empty lines",
jobID: "job1",
input: "Line 1\n\nLine 3",
want: " ⎿ Line 1\n Line 3\n\n",
},
{
name: "whitespace only input",
jobID: "job1",
input: " \n \t \n ",
want: "\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var output strings.Builder
printer.generateJobResults(tt.jobID, tt.input, &output)
result := output.String()
if result != tt.want {
t.Errorf("generateJobResults() = %q, want %q", result, tt.want)
}
})
}
}
func TestPrinter_generateFooter(t *testing.T) {
printer := NewPrinter(false, []string{})
tests := []struct {
name string
totalTime float64
successCount int
totalJobs int
wantContains []string
}{
{
name: "all jobs succeeded",
totalTime: 5.25,
successCount: 3,
totalJobs: 3,
wantContains: []string{"Total workflow time: 5.25s", "All jobs succeeded"},
},
{
name: "some jobs failed",
totalTime: 10.5,
successCount: 2,
totalJobs: 5,
wantContains: []string{"Total workflow time: 10.50s", "3 job(s) failed"},
},
{
name: "all jobs failed",
totalTime: 2.1,
successCount: 0,
totalJobs: 2,
wantContains: []string{"Total workflow time: 2.10s", "2 job(s) failed"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var output strings.Builder
printer.generateFooter(tt.totalTime, tt.successCount, tt.totalJobs, &output)
result := output.String()
for _, want := range tt.wantContains {
if !strings.Contains(result, want) {
t.Errorf("generateFooter() should contain %q, got %q", want, result)
}
}
})
}
}
func TestPrinter_generateReport(t *testing.T) {
printer := NewPrinter(false, []string{"job1", "job2"})
// Create test WorkflowBuffer
rs := NewResult()
// Add job1 - successful
startTime1 := time.Now()
endTime1 := startTime1.Add(1 * time.Second)
rs.Jobs["job1"] = &JobResult{
JobID: "job1",
JobName: "Successful Job",
StartTime: startTime1,
EndTime: endTime1,
Success: true,
Status: "Completed",
StepResults: []StepResult{
{
Index: 1,
Name: "Test Step",
Status: StatusSuccess,
RT: "100ms",
},
},
}
// Add job2 - failed
startTime2 := time.Now()
endTime2 := startTime2.Add(2 * time.Second)
rs.Jobs["job2"] = &JobResult{
JobID: "job2",
JobName: "Failed Job",
StartTime: startTime2,
EndTime: endTime2,
Success: false,
Status: "Failed",
StepResults: []StepResult{
{
Index: 1,
Name: "Failed Step",
Status: StatusError,
},
},
}
result := printer.GenerateReport(rs)
// Verify the report contains expected elements
expectedContains := []string{
"Successful Job",
"Failed Job",
"Test Step",
"Failed Step",
"Total workflow time:",
"1 job(s) failed",
}
for _, expected := range expectedContains {
if !strings.Contains(result, expected) {
t.Errorf("generateReport() should contain %q, got:\n%s", expected, result)
}
}
}
func TestPrinter_generateReport_EmptyBuffer(t *testing.T) {
printer := NewPrinter(false, []string{})
rs := NewResult()
result := printer.GenerateReport(rs)
// Should contain at least the footer
if !strings.Contains(result, "Total workflow time: 0.00s") {
t.Errorf("generateReport() with empty buffer should contain footer, got %q", result)
}
}
func TestPrinter_generateReport_WithRepeatStep(t *testing.T) {
printer := NewPrinter(false, []string{"job1"})
rs := NewResult()
startTime := time.Now()
endTime := startTime.Add(1 * time.Second)
rs.Jobs["job1"] = &JobResult{
JobID: "job1",
JobName: "Job with Repeat",
StartTime: startTime,
EndTime: endTime,
Success: true,
Status: "Completed",
StepResults: []StepResult{
{
Index: 1,
Name: "Repeat Step",
Status: StatusSuccess,
HasTest: true,
RepeatCounter: &StepRepeatCounter{
SuccessCount: 8,
FailureCount: 2,
Name: "Repeat Step",
LastResult: true,
RepeatTotal: 10,
},
},
},
}
result := printer.GenerateReport(rs)
expectedContains := []string{
"Job with Repeat",
"Repeat Step (repeating 10 times)",
"8/10 success (80.0%)",
"Total workflow time:",
"All jobs succeeded",
}
for _, expected := range expectedContains {
if !strings.Contains(result, expected) {
t.Errorf("generateReport() should contain %q, got:\n%s", expected, result)
}
}
}
// Truncate function tests
func TestGetTruncationMessage(t *testing.T) {
// Disable color output for consistent testing
color.NoColor = true
defer func() { color.NoColor = false }()
result := GetTruncationMessage()
expected := "... [⚠︎ probe truncated]"
if result != expected {
t.Errorf("GetTruncationMessage() = %q, want %q", result, expected)
}
}
func TestTruncateString(t *testing.T) {
// Disable color output for consistent testing
color.NoColor = true
defer func() { color.NoColor = false }()
tests := []struct {
name string
input string
maxLen int
expected string
}{
{
name: "short string",
input: "hello",
maxLen: 10,
expected: "hello",
},
{
name: "exact length",
input: "hello",
maxLen: 5,
expected: "hello",
},
{
name: "long string",
input: "this is a very long string that exceeds the limit",
maxLen: 10,
expected: "this is a ... [⚠︎ probe truncated]",
},
{
name: "empty string",
input: "",
maxLen: 5,
expected: "",
},
{
name: "zero max length",
input: "hello",
maxLen: 0,
expected: "... [⚠︎ probe truncated]",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := TruncateString(tt.input, tt.maxLen)
if result != tt.expected {
t.Errorf("TruncateString(%q, %d) = %q, want %q", tt.input, tt.maxLen, result, tt.expected)
}
})
}
}
func TestTruncateMapStringString(t *testing.T) {
// Disable color output for consistent testing
color.NoColor = true
defer func() { color.NoColor = false }()
tests := []struct {
name string
input map[string]string
maxLen int
expected map[string]string
}{
{
name: "short values",
input: map[string]string{
"key1": "value1",
"key2": "value2",
},
maxLen: 10,
expected: map[string]string{
"key1": "value1",
"key2": "value2",
},
},