GitLabgitlab-org
cli
main
1//go:build !integration
3package view
5import (
6 "os/exec"
7 "strings"
8 "testing"
9 "time"
10 "unicode/utf8"
12 "github.com/gdamore/tcell/v2"
13 "github.com/mattn/go-runewidth"
14 "github.com/rivo/tview"
15 "github.com/stretchr/testify/assert"
16 "github.com/stretchr/testify/require"
17 "go.uber.org/mock/gomock"
19 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
20 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
22 "gitlab.com/gitlab-org/cli/internal/run"
23 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
24 "gitlab.com/gitlab-org/cli/test"
25)
27// screenText returns everything rendered on screen, one row per line, for
28// tests that assert on the text rather than on an exact layout.
29func screenText(screen tcell.Screen) string {
30 sx, sy := screen.Size()
31 lines := make([]string, 0, sy)
32 for y := range sy {
33 runes := make([]rune, sx)
34 for x := range sx {
35 s, _, _ := screen.Get(x, y)
36 runes[x], _ = utf8.DecodeRuneInString(s)
37 }
38 lines = append(lines, string(runes))
39 }
40 return strings.Join(lines, "\n")
41}
43func assertScreen(t *testing.T, screen tcell.Screen, expected []string) {
44 t.Helper()
46 sx, sy := screen.Size()
47 assert.Equal(t, len(expected), sy)
48 assert.Equal(t, len([]rune(expected[0])), sx)
49 actual := make([]string, sy)
50 for y, str := range expected {
51 runes := make([]rune, len(str))
52 row := []rune(str)
53 for x := range row {
54 s, _, _ := screen.Get(x, y)
55 runes[x], _ = utf8.DecodeRuneInString(s)
56 }
58 actual[y] = strings.TrimRight(string(runes), string('\x00'))
59 assert.Equal(t, str, actual[y])
60 }
61 t.Logf("Expected w: %d l: %d", len([]rune(expected[0])), len(expected))
62 for _, str := range expected {
63 t.Log(str)
64 }
65 t.Logf("Actual w: %d l: %d", sx, sy)
66 for _, str := range actual {
67 t.Log(str)
68 }
69}
71func Test_line(t *testing.T) {
72 t.Parallel()
74 tests := []struct {
75 desc string
76 lineF func(screen tcell.Screen, x, y, l int)
77 x, y, l int
78 expected []string
79 }{
80 {
81 "hline",
82 hline,
83 2, 2, 5,
84 []string{
85 " ",
86 " ",
87 " ═════ ",
88 " ",
89 " ",
90 " ",
91 " ",
92 " ",
93 " ",
94 " ",
95 },
96 },
97 {
98 "hline overflow",
99 hline,
100 2, 2, 10,
101 []string{
102 " ",
103 " ",
104 " ════════",
105 " ",
106 " ",
107 " ",
108 " ",
109 " ",
110 " ",
111 " ",
112 },
113 },
114 {
115 "vline",
116 vline,
117 2, 2, 5,
118 []string{
119 " ",
120 " ",
121 " ║ ",
122 " ║ ",
123 " ║ ",
124 " ║ ",
125 " ║ ",
126 " ",
127 " ",
128 " ",
129 },
130 },
131 {
132 "vline overflow",
133 vline,
134 2, 2, 10,
135 []string{
136 " ",
137 " ",
138 " ║ ",
139 " ║ ",
140 " ║ ",
141 " ║ ",
142 " ║ ",
143 " ║ ",
144 " ║ ",
145 " ║ ",
146 },
147 },
148 }
150 for _, test := range tests {
151 screen := tcell.NewSimulationScreen("UTF-8")
152 err := screen.Init()
153 if err != nil {
154 t.Fatal(err)
155 }
156 // Set screen to matrix size
157 screen.SetSize(len(test.expected), len(test.expected[0]))
159 t.Run(test.desc, func(t *testing.T) {
160 t.Parallel()
162 test.lineF(screen, test.x, test.y, test.l)
163 screen.Show()
164 assertScreen(t, screen, test.expected)
165 })
166 }
167}
169func testbox(x, y, w, h int) *tview.TextView { //nolint:unparam
170 b := tview.NewTextView()
171 b.
172 SetBackgroundColor(tcell.ColorDefault).
173 SetBorder(true).
174 SetRect(x, y, w, h)
175 return b
176}
178func Test_Link(t *testing.T) {
179 t.Parallel()
181 tests := []struct {
182 desc string
183 b1, b2 *tview.Box
184 first, last bool
185 expected []string
186 }{
187 {
188 "first stage",
189 testbox(2, 1, 3, 3).Box, testbox(2, 5, 3, 3).Box,
190 true, false,
191 []string{
192 " ",
193 " ┌─┐ ",
194 " │ │ ",
195 " └─┘ ║ ",
196 " ║ ",
197 " ┌─┐ ║ ",
198 " │ │═╝ ",
199 " └─┘ ",
200 " ",
201 " ",
202 },
203 },
204 {
205 "last stage",
206 testbox(5, 1, 3, 3).Box, testbox(5, 5, 3, 3).Box,
207 false, true,
208 []string{
209 " ",
210 " ┌─┐ ",
211 " ╦ │ │ ",
212 " ║ └─┘ ",
213 " ║ ",
214 " ║ ┌─┐ ",
215 " ╚═│ │ ",
216 " └─┘ ",
217 " ",
218 " ",
219 },
220 },
221 {
222 "cross stage",
223 testbox(1, 1, 3, 3).Box, testbox(7, 1, 3, 3).Box,
224 false, false,
225 []string{
226 " ",
227 " ┌─┐ ┌─┐",
228 " │ │═══│ │",
229 " └─┘ └─┘",
230 " ",
231 " ",
232 " ",
233 " ",
234 " ",
235 " ",
236 },
237 },
238 }
240 for _, test := range tests {
241 screen := tcell.NewSimulationScreen("UTF-8")
242 err := screen.Init()
243 if err != nil {
244 t.Fatal(err)
245 }
246 // Set screen to matrix size
247 screen.SetSize(len(test.expected), len(test.expected[0]))
249 test.b1.Draw(screen)
250 test.b2.Draw(screen)
252 t.Run(test.desc, func(t *testing.T) {
253 t.Parallel()
255 link(screen, test.b1, test.b2, 2, test.first, test.last)
256 screen.Show()
257 assertScreen(t, screen, test.expected)
258 })
259 }
260}
262func Test_LinkJobs(t *testing.T) {
263 expected := []string{
264 " ",
265 " ┌─┐ ┌─┐ ┌─┐ ",
266 " │ │╦═╦│ │╦═╦│ │ ",
267 " └─┘║ ║└─┘║ ║└─┘ ",
268 " ║ ║ ║ ║ ",
269 " ┌─┐║ ║┌─┐║ ║┌─┐ ",
270 " │ │╝ ╠│ │╝ ╚│ │ ",
271 " └─┘║ ║└─┘║ └─┘ ",
272 " ║ ║ ║ ",
273 " ┌─┐║ ║┌─┐║ ",
274 " │ │╝ ╚│ │╝ ",
275 " └─┘║ └─┘ ",
276 " ║ ",
277 " ┌─┐║ ",
278 " │ │╝ ",
279 " └─┘ ",
280 " ",
281 }
282 jobs := []*ViewJob{
283 {
284 Name: "stage1-job1",
285 Stage: "stage1",
286 Kind: Job,
287 },
288 {
289 Name: "stage1-job2",
290 Stage: "stage1",
291 Kind: Job,
292 },
293 {
294 Name: "stage1-job3",
295 Stage: "stage1",
296 Kind: Job,
297 },
298 {
299 Name: "stage1-job4",
300 Stage: "stage1",
301 Kind: Job,
302 },
303 {
304 Name: "stage2-job1",
305 Stage: "stage2",
306 Kind: Bridge,
307 },
308 {
309 Name: "stage2-job2",
310 Stage: "stage2",
311 Kind: Job,
312 },
313 {
314 Name: "stage2-job3",
315 Stage: "stage2",
316 Kind: Job,
317 },
318 {
319 Name: "stage3-job1",
320 Stage: "stage3",
321 Kind: Job,
322 },
323 {
324 Name: "stage3-job2",
325 Stage: "stage3",
326 Kind: Job,
327 },
328 }
329 boxes := map[string]*tview.TextView{
330 "jobs-stage1-job1": testbox(1, 1, 3, 3),
331 "jobs-stage1-job2": testbox(1, 5, 3, 3),
332 "jobs-stage1-job3": testbox(1, 9, 3, 3),
333 "jobs-stage1-job4": testbox(1, 13, 3, 3),
335 "jobs-stage2-job1": testbox(7, 1, 3, 3),
336 "jobs-stage2-job2": testbox(7, 5, 3, 3),
337 "jobs-stage2-job3": testbox(7, 9, 3, 3),
339 "jobs-stage3-job1": testbox(13, 1, 3, 3),
340 "jobs-stage3-job2": testbox(13, 5, 3, 3),
341 }
343 screen := tcell.NewSimulationScreen("UTF-8")
344 err := screen.Init()
345 if err != nil {
346 t.Fatal(err)
347 }
348 // Set screen to matrix size
349 screen.SetSize(len(expected), len(expected[0]))
351 for _, b := range boxes {
352 b.Draw(screen)
353 }
355 err = linkJobs(screen, jobs, boxes)
356 if err != nil {
357 t.Fatal(err)
358 }
360 screen.Show()
361 assertScreen(t, screen, expected)
362}
364func Test_LinkJobsNegative(t *testing.T) {
365 t.Parallel()
367 tests := []struct {
368 desc string
369 jobs []*ViewJob
370 boxes map[string]*tview.TextView
371 }{
372 {
373 "determinePadding -- first job missing",
374 []*ViewJob{
375 {
376 Name: "stage1-job1",
377 Stage: "stage1",
378 Kind: Job,
379 },
380 },
381 map[string]*tview.TextView{
382 "jobs-stage2-job1": testbox(1, 5, 3, 3),
383 "jobs-stage2-job2": testbox(1, 9, 3, 3),
384 },
385 },
386 {
387 "determinePadding -- second job missing",
388 []*ViewJob{
389 {
390 Name: "stage1-job1",
391 Stage: "stage1",
392 Kind: Job,
393 },
394 {
395 Name: "stage2-job1",
396 Stage: "stage2",
397 Kind: Job,
398 },
399 {
400 Name: "stage2-job2",
401 Stage: "stage2",
402 Kind: Job,
403 },
404 },
405 map[string]*tview.TextView{
406 "jobs-stage1-job1": testbox(1, 1, 3, 3),
407 "jobs-stage2-job2": testbox(1, 9, 3, 3),
408 },
409 },
410 {
411 "Link -- third job missing",
412 []*ViewJob{
413 {
414 Name: "stage1-job1",
415 Stage: "stage1",
416 Kind: Job,
417 },
418 {
419 Name: "stage2-job1",
420 Stage: "stage2",
421 Kind: Job,
422 },
423 {
424 Name: "stage2-job2",
425 Stage: "stage2",
426 Kind: Job,
427 },
428 },
429 map[string]*tview.TextView{
430 "jobs-stage1-job1": testbox(1, 1, 3, 3),
431 "jobs-stage2-job1": testbox(1, 5, 3, 3),
432 },
433 },
434 }
435 for _, test := range tests {
436 screen := tcell.NewSimulationScreen("UTF-8")
437 err := screen.Init()
438 if err != nil {
439 t.Fatal(err)
440 }
441 t.Run(test.desc, func(t *testing.T) {
442 t.Parallel()
444 assert.Error(t, linkJobs(screen, test.jobs, test.boxes))
445 })
446 }
447}
449func Test_jobsView(t *testing.T) {
450 expected := []string{
451 " ┌────────────────────┐ ┌────────────────────┐ ┌────────────────────┐ ",
452 " │ Stage1 │ │ Stage2 │ │ Stage3 │ ",
453 " └────────────────────┘ └────────────────────┘ └────────────────────┘ ",
454 " ",
455 // The selected job scrolls its name instead of being cut short with an ellipsis.
456 " ╔✔ stage1-job1-really╗ ┌● stage2-job1[step]─┐ ┌───■ stage3-job1────┐ ",
457 " ║ ║ │ │ │ │ ",
458 " ║ 01m 01s║═╦══╦═│ │═╦══╦═│ │ ",
459 " ╚════════════════════╝ ║ ║ └────────────────────┘ ║ ║ └────────────────────┘ ",
460 " ║ ║ ║ ║ ",
461 " ┌───✔ stage1-job2────┐ ║ ║ ┌───● stage2-job2────┐ ║ ║ ┌───■ stage3-job2────┐ ",
462 " │ │ ║ ║ │ │ ║ ║ │ »│ ",
463 " │ │═╝ ╠═│ │═╝ ╚═│ │ ",
464 " └────────────────────┘ ║ ║ └────────────────────┘ ║ └────────────────────┘ ",
465 " ║ ║ ║ ",
466 " ┌───✔ stage1-job3────┐ ║ ║ ┌───● stage2-job3────┐ ║ ",
467 " │ │ ║ ║ │ │ ║ ",
468 " │ │═╝ ╚═│ │═╝ ",
469 " └────────────────────┘ ║ └────────────────────┘ ",
470 " ║ ",
471 " ┌───✘ stage1-job4────┐ ║ ",
472 " │ │ ║ ",
473 " │ │═╝ ",
474 " └────────────────────┘ ",
475 " ",
476 " ",
477 " ",
478 }
479 now := time.Now()
480 past := now.Add(time.Second * -61)
481 jobs := []*ViewJob{
482 {
483 Name: "stage1-job1-really-long",
484 Stage: "stage1",
485 Status: "success",
486 StartedAt: &past, // relies on test running in <1s we'll see how it goes
487 FinishedAt: &now,
488 },
489 {
490 Name: "stage1-job2",
491 Stage: "stage1",
492 Status: "success",
493 },
494 {
495 Name: "stage1-job3",
496 Stage: "stage1",
497 Status: "success",
498 },
499 {
500 Name: "stage1-job4",
501 Stage: "stage1",
502 Status: "failed",
503 },
504 {
505 Name: "stage2-job1[step]",
506 Stage: "stage2",
507 Status: "running",
508 },
509 {
510 Name: "stage2-job2",
511 Stage: "stage2",
512 Status: "running",
513 },
514 {
515 Name: "stage2-job3",
516 Stage: "stage2",
517 Status: "pending",
518 },
519 {
520 Name: "stage3-job1",
521 Stage: "stage3",
522 Status: "manual",
523 },
524 {
525 Name: "stage3-job2",
526 Stage: "stage3",
527 Status: "manual",
528 Kind: Bridge,
529 },
530 }
532 boxes = make(map[string]*tview.TextView)
533 jobsCh := make(chan []*ViewJob)
534 inputCh := make(chan struct{})
535 root := tview.NewPages()
536 root.
537 SetBackgroundColor(tcell.ColorDefault).
538 SetBorderPadding(1, 1, 2, 2)
540 screen := tcell.NewSimulationScreen("UTF-8")
541 err := screen.Init()
542 if err != nil {
543 t.Fatal(err)
544 }
545 // Set screen to matrix size
546 screen.SetSize(len([]rune(expected[0])), len(expected))
547 w, h := screen.Size()
548 root.SetRect(0, 0, w, h)
550 go func() {
551 jobsCh <- jobs
552 }()
553 root.Box.Focus(nil)
554 jobsView(t.Context(), nil, jobsCh, inputCh, root, nil)
555 root.Focus(func(p tview.Primitive) { p.Focus(nil) })
556 root.Draw(screen)
557 linkJobsView(nil)(screen)
558 screen.Sync()
559 assertScreen(t, screen, expected)
560}
562func Test_latestJobs(t *testing.T) {
563 t.Parallel()
565 tests := []struct {
566 desc string
567 jobs []*ViewJob
568 expected []*ViewJob
569 }{
570 {
571 desc: "no newer jobs",
572 jobs: []*ViewJob{
573 {
574 ID: 1,
575 Name: "stage1-job1",
576 Stage: "stage1",
577 },
578 {
579 ID: 2,
580 Name: "stage1-job2",
581 Stage: "stage1",
582 },
583 {
584 ID: 3,
585 Name: "stage1-job3",
586 Stage: "stage1",
587 },
588 },
589 expected: []*ViewJob{
590 {
591 ID: 1,
592 Name: "stage1-job1",
593 Stage: "stage1",
594 },
595 {
596 ID: 2,
597 Name: "stage1-job2",
598 Stage: "stage1",
599 },
600 {
601 ID: 3,
602 Name: "stage1-job3",
603 Stage: "stage1",
604 },
605 },
606 },
607 {
608 desc: "1 newer",
609 jobs: []*ViewJob{
610 {
611 ID: 1,
612 Name: "stage1-job1",
613 Stage: "stage1",
614 },
615 {
616 ID: 2,
617 Name: "stage1-job2",
618 Stage: "stage1",
619 },
620 {
621 ID: 3,
622 Name: "stage1-job3",
623 Stage: "stage1",
624 },
625 {
626 ID: 4,
627 Name: "stage1-job1",
628 Stage: "stage1",
629 },
630 },
631 expected: []*ViewJob{
632 {
633 ID: 4,
634 Name: "stage1-job1",
635 Stage: "stage1",
636 },
637 {
638 ID: 2,
639 Name: "stage1-job2",
640 Stage: "stage1",
641 },
642 {
643 ID: 3,
644 Name: "stage1-job3",
645 Stage: "stage1",
646 },
647 },
648 },
649 {
650 desc: "2 newer",
651 jobs: []*ViewJob{
652 {
653 ID: 1,
654 Name: "stage1-job1",
655 Stage: "stage1",
656 },
657 {
658 ID: 2,
659 Name: "stage1-job2",
660 Stage: "stage1",
661 },
662 {
663 ID: 3,
664 Name: "stage1-job3",
665 Stage: "stage1",
666 },
667 {
668 ID: 4,
669 Name: "stage1-job3",
670 Stage: "stage1",
671 },
672 {
673 ID: 5,
674 Name: "stage1-job1",
675 Stage: "stage1",
676 },
677 },
678 expected: []*ViewJob{
679 {
680 ID: 5,
681 Name: "stage1-job1",
682 Stage: "stage1",
683 },
684 {
685 ID: 2,
686 Name: "stage1-job2",
687 Stage: "stage1",
688 },
689 {
690 ID: 4,
691 Name: "stage1-job3",
692 Stage: "stage1",
693 },
694 },
695 },
696 }
698 for _, test := range tests {
699 t.Run(test.desc, func(t *testing.T) {
700 t.Parallel()
702 jobs := latestJobs(test.jobs)
703 assert.Equal(t, test.expected, jobs)
704 })
705 }
706}
708func Test_latestJobsKeepsJobsAfterRetry(t *testing.T) {
709 t.Parallel()
711 first := &ViewJob{ID: 1, Stage: "test", Name: "unit"}
712 retry := &ViewJob{ID: 2, Stage: "test", Name: "unit"}
713 bridge := &ViewJob{ID: 3, Stage: "deploy", Name: "downstream"}
715 assert.Equal(t, []*ViewJob{retry, bridge}, latestJobs([]*ViewJob{first, retry, bridge}))
716}
718func TestLatestJobsKeepsDistinctStageAndNamePairs(t *testing.T) {
719 t.Parallel()
721 first := &ViewJob{ID: 1, Stage: "ab", Name: "c"}
722 second := &ViewJob{ID: 2, Stage: "a", Name: "bc"}
723 jobs := []*ViewJob{first, second}
725 assert.Equal(t, jobs, latestJobs(jobs))
726}
728func Test_adjacentStages(t *testing.T) {
729 t.Parallel()
731 tests := []struct {
732 desc string
733 stage string
734 jobs []*ViewJob
735 expectedPrev string
736 expectedNext string
737 }{
738 {
739 desc: "no jobs",
740 stage: "1",
741 jobs: []*ViewJob{},
742 expectedPrev: "",
743 expectedNext: "",
744 },
745 {
746 desc: "first stage",
747 stage: "1",
748 jobs: []*ViewJob{
749 {
750 Stage: "1",
751 },
752 {
753 Stage: "1",
754 },
755 {
756 Stage: "1",
757 },
758 {
759 Stage: "2",
760 },
761 },
762 expectedPrev: "1",
763 expectedNext: "2",
764 },
765 {
766 desc: "mid stage",
767 stage: "2",
768 jobs: []*ViewJob{
769 {
770 Stage: "1",
771 },
772 {
773 Stage: "1",
774 },
775 {
776 Stage: "1",
777 },
778 {
779 Stage: "2",
780 },
781 {
782 Stage: "2",
783 },
784 {
785 Stage: "3",
786 },
787 },
788 expectedPrev: "1",
789 expectedNext: "3",
790 },
791 {
792 desc: "last stage",
793 stage: "3",
794 jobs: []*ViewJob{
795 {
796 Stage: "1",
797 },
798 {
799 Stage: "1",
800 },
801 {
802 Stage: "1",
803 },
804 {
805 Stage: "2",
806 },
807 {
808 Stage: "2",
809 },
810 {
811 Stage: "3",
812 },
813 },
814 expectedPrev: "2",
815 expectedNext: "3",
816 },
817 }
819 for _, test := range tests {
820 t.Run(test.desc, func(t *testing.T) {
821 t.Parallel()
823 prev, next := adjacentStages(test.jobs, test.stage)
824 assert.Equal(t, test.expectedPrev, prev)
825 assert.Equal(t, test.expectedNext, next)
826 })
827 }
828}
830func Test_stageBounds(t *testing.T) {
831 t.Parallel()
833 tests := []struct {
834 desc string
835 stage string
836 jobs []*ViewJob
837 expectedLower, expectedUpper int
838 }{
839 {
840 "no jobs",
841 "1",
842 []*ViewJob{},
843 0, 0,
844 },
845 {
846 "first stage",
847 "1",
848 []*ViewJob{
849 {
850 Stage: "1",
851 },
852 {
853 Stage: "1",
854 },
855 {
856 Stage: "1",
857 },
858 {
859 Stage: "2",
860 },
861 },
862 0, 2,
863 },
864 {
865 "mid stage",
866 "2",
867 []*ViewJob{
868 {
869 Stage: "1",
870 },
871 {
872 Stage: "1",
873 },
874 {
875 Stage: "1",
876 },
877 {
878 Stage: "2",
879 },
880 {
881 Stage: "2",
882 },
883 {
884 Stage: "3",
885 },
886 },
887 3, 4,
888 },
889 {
890 "last stage",
891 "3",
892 []*ViewJob{
893 {
894 Stage: "1",
895 },
896 {
897 Stage: "1",
898 },
899 {
900 Stage: "1",
901 },
902 {
903 Stage: "2",
904 },
905 {
906 Stage: "2",
907 },
908 {
909 Stage: "3",
910 },
911 },
912 5, 5,
913 },
914 }
916 for _, test := range tests {
917 t.Run(test.desc, func(t *testing.T) {
918 t.Parallel()
920 lower, upper := stageBounds(test.jobs, test.stage)
921 assert.Equal(t, test.expectedLower, lower)
922 assert.Equal(t, test.expectedUpper, upper)
923 })
924 }
925}
927func Test_handleNavigation(t *testing.T) {
928 t.Parallel()
930 jobs := []*ViewJob{
931 {
932 Name: "stage1-job1-really-long",
933 Stage: "stage1",
934 Status: "success",
935 },
936 {
937 Name: "stage1-job2",
938 Stage: "stage1",
939 Status: "success",
940 },
941 {
942 Name: "stage1-job3",
943 Stage: "stage1",
944 Status: "success",
945 },
946 {
947 Name: "stage1-job4",
948 Stage: "stage1",
949 Status: "failed",
950 },
951 {
952 Name: "stage2-job1",
953 Stage: "stage2",
954 Status: "running",
955 },
956 {
957 Name: "stage2-job2",
958 Stage: "stage2",
959 Status: "running",
960 },
961 {
962 Name: "stage2-job3",
963 Stage: "stage2",
964 Status: "pending",
965 },
966 {
967 Name: "stage3-job1",
968 Stage: "stage3",
969 Status: "manual",
970 },
971 {
972 Name: "stage3-job2",
973 Stage: "stage3",
974 Status: "manual",
975 },
976 }
977 tests := []struct {
978 desc string
979 input []*tcell.EventKey
980 expected int
981 }{
982 {
983 "do nothing",
984 []*tcell.EventKey{},
985 0,
986 },
987 {
988 "arrows down",
989 []*tcell.EventKey{
990 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
991 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
992 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
993 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
994 },
995 3,
996 },
997 {
998 "arrows down bottom boundary",
999 []*tcell.EventKey{
1000 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1001 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1002 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1003 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1004 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1005 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1006 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1007 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1008 },
1009 3,
1010 },
1011 {
1012 "arrows down bottom middle boundary",
1013 []*tcell.EventKey{
1014 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1015 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1016 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1017 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1018 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1019 tcell.NewEventKey(tcell.KeyRight, 0, tcell.ModNone),
1020 },
1021 6,
1022 },
1023 {
1024 "arrows down last (3rd) column bottom boundary",
1025 []*tcell.EventKey{
1026 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1027 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1028 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1029 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1030 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1031 tcell.NewEventKey(tcell.KeyRight, 0, tcell.ModNone),
1032 tcell.NewEventKey(tcell.KeyRight, 0, tcell.ModNone),
1033 },
1034 8,
1035 },
1036 {
1037 "arrows down persistent depth bottom boundary",
1038 []*tcell.EventKey{
1039 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1040 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1041 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1042 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1043 tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone),
1044 tcell.NewEventKey(tcell.KeyRight, 0, tcell.ModNone),
1045 tcell.NewEventKey(tcell.KeyRight, 0, tcell.ModNone),
1046 tcell.NewEventKey(tcell.KeyLeft, 0, tcell.ModNone),
1047 tcell.NewEventKey(tcell.KeyLeft, 0, tcell.ModNone),
1048 },
1049 3,
1050 },
1051 {
1052 "arrows left boundary",
1053 []*tcell.EventKey{
1054 tcell.NewEventKey(tcell.KeyLeft, 0, tcell.ModNone),
1055 tcell.NewEventKey(tcell.KeyLeft, 0, tcell.ModNone),
1056 },
1057 0,
1058 },
1059 {
1060 "arrows up boundary",
1061 []*tcell.EventKey{
1062 tcell.NewEventKey(tcell.KeyUp, 0, tcell.ModNone),
1063 tcell.NewEventKey(tcell.KeyUp, 0, tcell.ModNone),
1064 },
1065 0,
1066 },
1067 {
1068 "arrows right boundary",
1069 []*tcell.EventKey{
1070 tcell.NewEventKey(tcell.KeyRight, 0, tcell.ModNone),
1071 tcell.NewEventKey(tcell.KeyRight, 0, tcell.ModNone),
1072 tcell.NewEventKey(tcell.KeyRight, 0, tcell.ModNone),
1073 },
1074 7,
1075 },
1076 {
1077 "hjkl down",
1078 []*tcell.EventKey{
1079 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1080 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1081 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1082 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1083 },
1084 3,
1085 },
1086 {
1087 "hjkl down bottom boundary",
1088 []*tcell.EventKey{
1089 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1090 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1091 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1092 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1093 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1094 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1095 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1096 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1097 },
1098 3,
1099 },
1100 {
1101 "hjkl down bottom middle boundary",
1102 []*tcell.EventKey{
1103 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1104 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1105 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1106 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1107 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1108 tcell.NewEventKey(tcell.KeyRune, 'l', tcell.ModNone),
1109 },
1110 6,
1111 },
1112 {
1113 "hjkl down last (3rd) column bottom boundary",
1114 []*tcell.EventKey{
1115 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1116 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1117 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1118 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1119 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1120 tcell.NewEventKey(tcell.KeyRune, 'l', tcell.ModNone),
1121 tcell.NewEventKey(tcell.KeyRune, 'l', tcell.ModNone),
1122 },
1123 8,
1124 },
1125 {
1126 "hjkl down persistent depth bottom boundary",
1127 []*tcell.EventKey{
1128 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1129 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1130 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1131 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1132 tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone),
1133 tcell.NewEventKey(tcell.KeyRune, 'l', tcell.ModNone),
1134 tcell.NewEventKey(tcell.KeyRune, 'l', tcell.ModNone),
1135 tcell.NewEventKey(tcell.KeyRune, 'h', tcell.ModNone),
1136 tcell.NewEventKey(tcell.KeyRune, 'h', tcell.ModNone),
1137 },
1138 3,
1139 },
1140 {
1141 "hjkl left boundary",
1142 []*tcell.EventKey{
1143 tcell.NewEventKey(tcell.KeyRune, 'h', tcell.ModNone),
1144 tcell.NewEventKey(tcell.KeyRune, 'h', tcell.ModNone),
1145 },
1146 0,
1147 },
1148 {
1149 "hjkl up boundary",
1150 []*tcell.EventKey{
1151 tcell.NewEventKey(tcell.KeyRune, 'k', tcell.ModNone),
1152 tcell.NewEventKey(tcell.KeyRune, 'k', tcell.ModNone),
1153 },
1154 0,
1155 },
1156 {
1157 "hjkl right boundary",
1158 []*tcell.EventKey{
1159 tcell.NewEventKey(tcell.KeyRune, 'l', tcell.ModNone),
1160 tcell.NewEventKey(tcell.KeyRune, 'l', tcell.ModNone),
1161 tcell.NewEventKey(tcell.KeyRune, 'l', tcell.ModNone),
1162 },
1163 7,
1164 },
1165 {
1166 "G boundary",
1167 []*tcell.EventKey{
1168 tcell.NewEventKey(tcell.KeyRune, 'G', tcell.ModNone),
1169 },
1170 3,
1171 },
1172 {
1173 "Gg boundary",
1174 []*tcell.EventKey{
1175 tcell.NewEventKey(tcell.KeyRune, 'G', tcell.ModNone),
1176 tcell.NewEventKey(tcell.KeyRune, 'g', tcell.ModNone),
1177 },
1178 0,
1179 },
1180 }
1182 for _, test := range tests {
1183 t.Run(test.desc, func(t *testing.T) {
1184 t.Parallel()
1186 var navi navigator
1187 for _, e := range test.input {
1188 navi.Navigate(jobs, normalizeViKeys(e))
1189 }
1190 assert.Equal(t, test.expected, navi.idx)
1191 })
1192 }
1193}
1195// Test_navigatorSurvivesPipelineSwitch verifies that the navigator does not
1196// panic with "index out of range" when applied to a smaller jobs slice than
1197// the one its idx/depth were last sized against — the crash reported in #8313
1198// when entering a child pipeline from a bridge job.
1199func Test_navigatorSurvivesPipelineSwitch(t *testing.T) {
1200 t.Parallel()
1202 parentJobs := []*ViewJob{
1203 {Name: "a1", Stage: "build", Status: "success"},
1204 {Name: "a2", Stage: "build", Status: "success"},
1205 {Name: "a3", Stage: "build", Status: "success"},
1206 {Name: "b1", Stage: "test", Status: "success"},
1207 {Name: "b2", Stage: "test", Status: "success"},
1208 {Name: "c1", Stage: "deploy", Status: "success"},
1209 {Name: "c2", Stage: "deploy", Status: "success"},
1210 }
1211 childJobs := []*ViewJob{
1212 {Name: "x1", Stage: "build", Status: "success"},
1213 {Name: "x2", Stage: "build", Status: "success"},
1214 }
1216 var navi navigator
1217 // Park the cursor at the last job of the parent (idx 6).
1218 navi.Navigate(parentJobs, tcell.NewEventKey(tcell.KeyRune, 'G', tcell.ModNone))
1219 navi.Navigate(parentJobs, tcell.NewEventKey(tcell.KeyRight, 0, tcell.ModNone))
1220 navi.Navigate(parentJobs, tcell.NewEventKey(tcell.KeyRight, 0, tcell.ModNone))
1221 require.GreaterOrEqual(t, navi.idx, len(childJobs), "test setup: idx should exceed child size")
1223 // Without a reset, the next Navigate against childJobs would panic at
1224 // `jobs[n.idx].Stage`. The defensive clamp must keep us inside bounds.
1225 require.NotPanics(t, func() {
1226 got := navi.Navigate(childJobs, tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone))
1227 assert.NotNil(t, got)
1228 })
1229 assert.Less(t, navi.idx, len(childJobs))
1230}
1232func Test_pipelineInfo(t *testing.T) {
1233 t.Parallel()
1235 createdAt := time.Date(2026, 8, 17, 12, 0, 0, 0, time.UTC)
1236 updatedAt := time.Date(2026, 8, 17, 12, 5, 0, 0, time.UTC)
1238 got := pipelineInfo(&gitlab.Pipeline{
1239 ID: 225,
1240 IID: 12,
1241 ProjectID: 777,
1242 Status: "running",
1243 Source: gitlab.PipelineSource("parent_pipeline"),
1244 Ref: "main",
1245 SHA: "2dc6aa325a317eda67812f05600bdf0fcdc70ab0",
1246 Name: "child pipeline",
1247 WebURL: "https://gitlab.com/OWNER/REPO/-/pipelines/225",
1248 CreatedAt: &createdAt,
1249 UpdatedAt: &updatedAt,
1250 })
1252 assert.Equal(t, int64(225), got.ID)
1253 assert.Equal(t, int64(777), got.ProjectID)
1254 assert.Equal(t, int64(12), got.IID)
1255 assert.Equal(t, "running", got.Status)
1256 assert.Equal(t, "parent_pipeline", got.Source)
1257 assert.Equal(t, "main", got.Ref)
1258 assert.Equal(t, "2dc6aa325a317eda67812f05600bdf0fcdc70ab0", got.SHA)
1259 assert.Equal(t, "child pipeline", got.Name)
1260 assert.Equal(t, "https://gitlab.com/OWNER/REPO/-/pipelines/225", got.WebURL)
1261 assert.Equal(t, &createdAt, got.CreatedAt)
1262 assert.Equal(t, &updatedAt, got.UpdatedAt)
1263}
1265func Test_curPipeline_emptyStack(t *testing.T) {
1266 // Cannot run in parallel: mutates the package-level `pipelines` global.
1267 pipelines = nil
1269 got, err := curPipeline()
1271 require.Error(t, err)
1272 assert.Equal(t, gitlab.PipelineInfo{}, got)
1273}
1275func Test_handleBridgeJobSelection_retargetsTraceAtDownstreamPipeline(t *testing.T) {
1276 // Cannot run in parallel: mutates the package-level view globals.
1277 t.Cleanup(func() {
1278 pipelines, curJob, jobs, modalVisible = nil, nil, nil, false
1279 })
1281 app := tview.NewApplication()
1282 pipelines = []gitlab.PipelineInfo{{ID: 10, ProjectID: 7}}
1283 require.Equal(t, int64(7), curProjectID(app), "precondition: viewing the parent")
1285 curJob = ViewJobFromBridge(&gitlab.Bridge{
1286 ID: 55,
1287 Name: "trigger-child",
1288 Status: "success",
1289 DownstreamPipeline: &gitlab.PipelineInfo{ID: 20, ProjectID: 777},
1290 })
1291 navi := &navigator{idx: 3, depth: 2}
1292 forceUpdateCh := make(chan bool, 1)
1294 handleBridgeJobSelection(app, tview.NewPages(), forceUpdateCh, navi)
1296 assert.Equal(t, int64(777), curProjectID(app),
1297 "after descending, a trace must be fetched from the downstream project")
1298 assert.Nil(t, curJob, "selection is cleared so the child's first job is picked")
1299 assert.Equal(t, &navigator{}, navi, "cursor resets for the child's jobs slice")
1300 assert.True(t, <-forceUpdateCh, "the job list is refreshed for the child pipeline")
1302 pipelines = pipelines[:len(pipelines)-1]
1303 assert.Equal(t, int64(7), curProjectID(app))
1304}
1306func Test_inputCapture_ctrlSpaceIgnoresBridgeJobs(t *testing.T) {
1307 // Cannot run in parallel: mutates the package-level view globals.
1308 t.Cleanup(func() {
1309 pipelines, curJob, jobs, logsVisible, modalVisible = nil, nil, nil, false, false
1310 })
1311 pipelines = []gitlab.PipelineInfo{{ID: 10, ProjectID: 7}}
1312 jobs = nil
1313 curJob = &ViewJob{ID: 55, Name: "trigger-child", Kind: Bridge}
1314 logsVisible, modalVisible = false, false
1316 // No EXPECT() calls: any API request this key press makes fails the test.
1317 tc := gitlabtesting.NewTestClient(t)
1318 ios, _, _, _ := cmdtest.TestIOStreams()
1320 handler := inputCapture(
1321 t.Context(), tview.NewApplication(), tview.NewPages(), &navigator{},
1322 make(chan struct{}, 1), make(chan bool, 1),
1323 &options{io: ios}, tc.Client,
1324 )
1326 event := tcell.NewEventKey(tcell.KeyCtrlSpace, 0, tcell.ModNone)
1327 got := handler(event)
1329 // Assert on the returned event, not the absence of an API call: Suspend is
1330 // a no-op on an application that is not running, so the call never happens.
1331 assert.Same(t, event, got, "Ctrl+Space on a bridge job must not start a trace")
1332}
1334func Test_inputCapture_ctrlSShowsFullJobName(t *testing.T) {
1335 // Longer than the maxTitle columns a job box gives its title
1336 const longName = "build:integration:very-long-name"
1338 newHandler := func(t *testing.T, root *tview.Pages) func(*tcell.EventKey) *tcell.EventKey {
1339 t.Helper()
1341 tc := gitlabtesting.NewTestClient(t)
1342 ios, _, _, _ := cmdtest.TestIOStreams()
1343 return inputCapture(
1344 t.Context(), tview.NewApplication(), root, &navigator{},
1345 make(chan struct{}, 1), make(chan bool, 1),
1346 &options{io: ios}, tc.Client,
1347 )
1348 }
1350 // Cannot run in parallel: mutates the package-level view globals.
1351 selectJob := func(t *testing.T, job *ViewJob) {
1352 t.Helper()
1353 t.Cleanup(func() {
1354 pipelines, curJob, jobs, logsVisible, modalVisible = nil, nil, nil, false, false
1355 })
1356 pipelines = []gitlab.PipelineInfo{{ID: 10, ProjectID: 7}}
1357 // jobs stays nil so the key press cannot move the selection off job.
1358 jobs = nil
1359 curJob = job
1360 logsVisible, modalVisible = false, false
1361 }
1363 ctrlS := func() *tcell.EventKey {
1364 return tcell.NewEventKey(tcell.KeyCtrlS, 0, tcell.ModNone)
1365 }
1367 t.Run("opens a modal holding the untruncated name", func(t *testing.T) {
1368 selectJob(t, &ViewJob{ID: 55, Name: longName, Stage: "integration"})
1369 root := tview.NewPages()
1371 got := newHandler(t, root)(ctrlS())
1373 assert.Nil(t, got, "Ctrl+S is consumed by the modal")
1374 assert.True(t, modalVisible, "the view must stop redrawing behind the modal")
1375 require.True(t, root.HasPage(jobNamePage))
1377 screen := tcell.NewSimulationScreen("UTF-8")
1378 require.NoError(t, screen.Init())
1379 screen.SetSize(150, 30)
1380 root.SetRect(0, 0, 150, 30)
1381 root.Draw(screen)
1382 assert.Contains(t, screenText(screen), longName,
1383 "the modal shows the name the job box has to truncate")
1384 })
1386 t.Run("opens for a bridge job too", func(t *testing.T) {
1387 // Bridge names are truncated in exactly the same way as job names.
1388 selectJob(t, &ViewJob{ID: 55, Name: longName, Kind: Bridge})
1389 root := tview.NewPages()
1391 got := newHandler(t, root)(ctrlS())
1393 assert.Nil(t, got)
1394 assert.True(t, root.HasPage(jobNamePage))
1395 })
1397 t.Run("is ignored while another modal is open", func(t *testing.T) {
1398 selectJob(t, &ViewJob{ID: 55, Name: longName})
1399 modalVisible = true
1400 root := tview.NewPages()
1402 event := ctrlS()
1403 got := newHandler(t, root)(event)
1405 assert.Same(t, event, got, "Ctrl+S must not stack a modal on a modal")
1406 assert.False(t, root.HasPage(jobNamePage))
1407 })
1409 t.Run("is ignored when no job is selected", func(t *testing.T) {
1410 // curJob is nil until the first job list arrives, and between
1411 // descending into a child pipeline and its jobs being fetched.
1412 selectJob(t, nil)
1413 root := tview.NewPages()
1415 event := ctrlS()
1416 got := newHandler(t, root)(event)
1418 assert.Same(t, event, got)
1419 assert.False(t, root.HasPage(jobNamePage))
1420 })
1421}
1423func Test_curPipeline_tracksDescentIntoChildPipeline(t *testing.T) {
1424 // Cannot run in parallel: mutates the package-level `pipelines` global.
1425 t.Cleanup(func() { pipelines = nil })
1427 pipelines = []gitlab.PipelineInfo{{ID: 10, ProjectID: 7}}
1428 parent, err := curPipeline()
1429 require.NoError(t, err)
1430 assert.Equal(t, int64(7), parent.ProjectID)
1432 // A child pipeline in another project, as a multi-project trigger produces.
1433 pipelines = append(pipelines, gitlab.PipelineInfo{ID: 20, ProjectID: 777})
1434 child, err := curPipeline()
1435 require.NoError(t, err)
1436 assert.Equal(t, int64(20), child.ID)
1437 assert.Equal(t, int64(777), child.ProjectID)
1439 pipelines = pipelines[:len(pipelines)-1]
1440 back, err := curPipeline()
1441 require.NoError(t, err)
1442 assert.Equal(t, int64(10), back.ID)
1443 assert.Equal(t, int64(7), back.ProjectID)
1444}
1446func Test_logsPageKey(t *testing.T) {
1447 t.Parallel()
1449 t.Run("same name in different pipelines gets distinct keys", func(t *testing.T) {
1450 t.Parallel()
1452 parentJob := &ViewJob{ID: 100, Name: "build"}
1453 childJob := &ViewJob{ID: 200, Name: "build"}
1455 assert.NotEqual(t, logsPageKey(parentJob), logsPageKey(childJob))
1456 })
1458 t.Run("same job gets a stable key", func(t *testing.T) {
1459 t.Parallel()
1461 job := &ViewJob{ID: 100, Name: "build"}
1463 assert.Equal(t, logsPageKey(job), logsPageKey(&ViewJob{ID: 100, Name: "renamed"}))
1464 })
1466 t.Run("a retried job does not reuse the original job's cached page", func(t *testing.T) {
1467 t.Parallel()
1469 original := &ViewJob{ID: 100, Name: "build", Status: "failed"}
1470 retried := &ViewJob{ID: 101, Name: "build", Status: "running"}
1472 assert.NotEqual(t, logsPageKey(original), logsPageKey(retried))
1473 })
1474}
1476func Test_bracketEscaper(t *testing.T) {
1477 t.Parallel()
1479 tests := []struct {
1480 desc string
1481 input string
1482 expected string
1483 }{
1484 {
1485 desc: "no brackets",
1486 input: "simple text",
1487 expected: "simple text",
1488 },
1489 {
1490 desc: "literal brackets [MASKED]",
1491 input: "value is [MASKED]",
1492 expected: "value is [MASKED[]",
1493 },
1494 {
1495 desc: "ANSI escape sequence preserved",
1496 input: "\x1b[32;1mgreen text\x1b[0m",
1497 expected: "\x1b[32;1mgreen text\x1b[0m",
1498 },
1499 {
1500 desc: "ANSI with literal brackets",
1501 input: "\x1b[32;1m$ echo \"test\"\x1b[0m\nvalue is [MASKED]\n",
1502 expected: "\x1b[32;1m$ echo \"test\"\x1b[0m\nvalue is [MASKED[]\n",
1503 },
1504 {
1505 desc: "multiple literal brackets",
1506 input: "[MASKED] and [HIDDEN]",
1507 expected: "[MASKED[] and [HIDDEN[]",
1508 },
1509 {
1510 desc: "complex trace with section markers",
1511 input: "Compile complete.\n\x1b[32;1m$ echo \"MASKED variables's value is ${TEST_MASKED}\"\x1b[0m\nMASKED variables's value is [MASKED]\n",
1512 expected: "Compile complete.\n\x1b[32;1m$ echo \"MASKED variables's value is ${TEST_MASKED}\"\x1b[0m\nMASKED variables's value is [MASKED[]\n",
1513 },
1514 }
1516 for _, test := range tests {
1517 t.Run(test.desc, func(t *testing.T) {
1518 t.Parallel()
1520 var output strings.Builder
1521 escaper := &bracketEscaper{Writer: &output}
1523 n, err := escaper.Write([]byte(test.input))
1525 require.NoError(t, err)
1526 assert.Equal(t, len(test.input), n, "should return number of input bytes consumed")
1527 assert.Equal(t, test.expected, output.String())
1528 })
1529 }
1530}
1532func TestNormalizeCtrlM(t *testing.T) {
1533 t.Parallel()
1535 event := normalizeCtrlM(tcell.NewEventKey(tcell.KeyCtrlM, 0, tcell.ModNone))
1537 assert.Equal(t, tcell.KeyEnter, event.Key())
1538 assert.Equal(t, tcell.ModNone, event.Modifiers())
1539}
1541func TestNormalizeViKeys(t *testing.T) {
1542 t.Parallel()
1544 tests := []struct {
1545 name string
1546 event *tcell.EventKey
1547 want tcell.Key
1548 }{
1549 {name: "h moves left", event: tcell.NewEventKey(tcell.KeyRune, 'h', tcell.ModNone), want: tcell.KeyLeft},
1550 {name: "j moves down", event: tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone), want: tcell.KeyDown},
1551 {name: "k moves up", event: tcell.NewEventKey(tcell.KeyRune, 'k', tcell.ModNone), want: tcell.KeyUp},
1552 {name: "l moves right", event: tcell.NewEventKey(tcell.KeyRune, 'l', tcell.ModNone), want: tcell.KeyRight},
1553 {name: "other rune passes through", event: tcell.NewEventKey(tcell.KeyRune, 'x', tcell.ModNone), want: tcell.KeyRune},
1554 {name: "other key passes through", event: tcell.NewEventKey(tcell.KeyEnter, 0, tcell.ModNone), want: tcell.KeyEnter},
1555 }
1556 for _, test := range tests {
1557 t.Run(test.name, func(t *testing.T) {
1558 t.Parallel()
1560 assert.Equal(t, test.want, normalizeViKeys(test.event).Key())
1561 })
1562 }
1563}
1565func Test_inputCapture_viKeys(t *testing.T) {
1566 // Cannot run in parallel: mutates the package-level view globals.
1567 t.Cleanup(func() {
1568 pipelines, curJob, jobs, logsVisible, modalVisible = nil, nil, nil, false, false
1569 })
1570 pipelines = []gitlab.PipelineInfo{{ID: 10, ProjectID: 7}}
1571 jobs = nil
1572 curJob = &ViewJob{ID: 55, Name: "build", Kind: Job, Status: string(gitlab.Success)}
1574 tc := gitlabtesting.NewTestClient(t)
1575 ios, _, _, _ := cmdtest.TestIOStreams()
1576 handler := inputCapture(
1577 t.Context(), tview.NewApplication(), tview.NewPages(), &navigator{},
1578 make(chan struct{}, 1), make(chan bool, 1),
1579 &options{io: ios}, tc.Client,
1580 )
1582 modalVisible = true
1583 assert.Equal(t, tcell.KeyLeft, handler(tcell.NewEventKey(tcell.KeyRune, 'h', tcell.ModNone)).Key())
1584 assert.Equal(t, tcell.KeyDown, handler(tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone)).Key())
1585 assert.Equal(t, tcell.KeyUp, handler(tcell.NewEventKey(tcell.KeyRune, 'k', tcell.ModNone)).Key())
1586 assert.Equal(t, tcell.KeyRight, handler(tcell.NewEventKey(tcell.KeyRune, 'l', tcell.ModNone)).Key())
1588 modalVisible = false
1589 jobs = []*ViewJob{
1590 {ID: 1, Name: "build", Kind: Job, Stage: "build"},
1591 {ID: 2, Name: "lint", Kind: Job, Stage: "build"},
1592 }
1593 got := handler(tcell.NewEventKey(tcell.KeyRune, 'j', tcell.ModNone))
1594 assert.Equal(t, tcell.KeyDown, got.Key())
1595 assert.Equal(t, "lint", curJob.Name, "j still moves to the next job outside a modal")
1596}
1598// Test_bridgeWithoutDownstreamPipeline tests that we correctly handle bridge jobs
1599// without downstream pipelines (fixes crash in work item #7372)
1600func Test_bridgeWithoutDownstreamPipeline(t *testing.T) {
1601 t.Parallel()
1603 tests := []struct {
1604 name string
1605 bridge *gitlab.Bridge
1606 }{
1607 {
1608 name: "manual bridge without downstream pipeline",
1609 bridge: &gitlab.Bridge{
1610 ID: 1,
1611 Name: "trigger-job",
1612 Status: "manual",
1613 Stage: "deploy",
1614 DownstreamPipeline: nil, // This nil caused crash in #7372
1615 },
1616 },
1617 {
1618 name: "created bridge without downstream pipeline",
1619 bridge: &gitlab.Bridge{
1620 ID: 2,
1621 Name: "trigger-pipeline",
1622 Status: "created",
1623 Stage: "deploy",
1624 DownstreamPipeline: nil,
1625 },
1626 },
1627 {
1628 name: "skipped bridge without downstream pipeline",
1629 bridge: &gitlab.Bridge{
1630 ID: 3,
1631 Name: "conditional-trigger",
1632 Status: "skipped",
1633 Stage: "deploy",
1634 DownstreamPipeline: nil,
1635 },
1636 },
1637 {
1638 name: "bridge with downstream pipeline",
1639 bridge: &gitlab.Bridge{
1640 ID: 4,
1641 Name: "active-trigger",
1642 Status: "success",
1643 Stage: "deploy",
1644 DownstreamPipeline: &gitlab.PipelineInfo{
1645 ID: 123,
1646 Status: "running",
1647 },
1648 },
1649 },
1650 }
1652 for _, tt := range tests {
1653 t.Run(tt.name, func(t *testing.T) {
1654 t.Parallel()
1656 viewJob := ViewJobFromBridge(tt.bridge)
1658 // Verify the ViewJob was created correctly
1659 assert.NotNil(t, viewJob)
1660 assert.Equal(t, tt.bridge.Name, viewJob.Name)
1661 assert.Equal(t, tt.bridge.Status, viewJob.Status)
1662 assert.Equal(t, Bridge, viewJob.Kind)
1663 assert.NotNil(t, viewJob.OriginalBridge)
1665 // Verify DownstreamPipeline state matches expectation
1666 if tt.bridge.DownstreamPipeline == nil {
1667 assert.Nil(t, viewJob.OriginalBridge.DownstreamPipeline,
1668 "Bridge job without downstream pipeline should have nil DownstreamPipeline")
1669 } else {
1670 assert.NotNil(t, viewJob.OriginalBridge.DownstreamPipeline,
1671 "Bridge job with downstream pipeline should have non-nil DownstreamPipeline")
1672 }
1673 })
1674 }
1675}
1677func TestCIView(t *testing.T) {
1678 createdAt, _ := time.Parse(time.RFC3339, "2025-10-28T16:52:39.000+01:00")
1680 type testCase struct {
1681 name string
1682 cli string
1683 setupMock func(tc *gitlabtesting.TestClient)
1684 expectedOutput string
1685 }
1687 tests := []testCase{
1688 {
1689 name: "view ci pipeline on web for a given branch",
1690 cli: "--web --branch foo",
1691 setupMock: func(tc *gitlabtesting.TestClient) {
1692 tc.MockPipelines.EXPECT().
1693 GetLatestPipeline("OWNER/REPO", gomock.Any(), gomock.Any()).
1694 Return(&gitlab.Pipeline{
1695 ID: 8,
1696 Ref: "foo",
1697 SHA: "2dc6aa325a317eda67812f05600bdf0fcdc70ab0",
1698 Status: "created",
1699 WebURL: "https://gitlab.com/OWNER/REPO/-/pipelines/225",
1700 CreatedAt: &createdAt,
1701 }, nil, nil)
1703 // GetPipelineWithFallback checks if pipeline has jobs
1704 tc.MockJobs.EXPECT().
1705 ListPipelineJobs("OWNER/REPO", int64(8), gomock.Any(), gomock.Any()).
1706 Return([]*gitlab.Job{{ID: 1}}, nil, nil)
1707 },
1708 expectedOutput: "Opening gitlab.com/OWNER/REPO/-/pipelines/225 in your browser.\n",
1709 },
1710 {
1711 name: "view ci pipeline on web for a given pipeline id",
1712 cli: "--web --pipelineid 5",
1713 setupMock: func(tc *gitlabtesting.TestClient) {
1714 tc.MockPipelines.EXPECT().
1715 GetPipeline("OWNER/REPO", int64(5), gomock.Any()).
1716 Return(&gitlab.Pipeline{
1717 ID: 5,
1718 WebURL: "https://gitlab.com/OWNER/REPO/-/pipelines/5",
1719 CreatedAt: &createdAt,
1720 SHA: "2dc6aa325a317eda67812f05600bdf0fcdc70ab0",
1721 }, nil, nil)
1722 },
1723 expectedOutput: "Opening gitlab.com/OWNER/REPO/-/pipelines/5 in your browser.\n",
1724 },
1725 }
1727 for _, tc := range tests {
1728 t.Run(tc.name, func(t *testing.T) {
1729 testClient := gitlabtesting.NewTestClient(t)
1730 tc.setupMock(testClient)
1732 restoreCmd := run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
1733 return &test.OutputStub{}
1734 })
1735 defer restoreCmd()
1737 exec := cmdtest.SetupCmdForTest(t, NewCmdView, true,
1738 cmdtest.WithGitLabClient(testClient.Client),
1739 )
1740 output, err := exec(tc.cli)
1742 if assert.NoErrorf(t, err, "error running command `ci view %s`: %v", tc.cli, err) {
1743 assert.Empty(t, output.String())
1744 assert.Equal(t, tc.expectedOutput, output.Stderr())
1745 }
1746 })
1747 }
1748}
1750func Test_jobsView_bracketedJobNames(t *testing.T) {
1751 // tview reads an unescaped "[production]" in a job name as a colour tag.
1752 const bracketed = "deploy-app[production]"
1753 require.LessOrEqual(t, tview.TaggedStringWidth("✔ "+bracketed), maxTitle,
1754 "fixture must be a name that measures as fitting before it is escaped")
1755 require.Greater(t, tview.TaggedStringWidth(tview.Escape("✔ "+bracketed)), maxTitle,
1756 "fixture must be a name that overflows the box once escaped")
1758 render := func(t *testing.T, selected string) map[string]string {
1759 t.Helper()
1760 t.Cleanup(func() {
1761 boxes, jobs, curJob, titleScroll = nil, nil, nil, titleScroller{}
1762 })
1764 viewJobs := []*ViewJob{
1765 {ID: 1, Name: bracketed, Stage: "deploy", Status: "success"},
1766 {ID: 2, Name: "short", Stage: "deploy", Status: "success"},
1767 }
1768 boxes = make(map[string]*tview.TextView)
1769 curJob = viewJobs[0]
1770 for _, j := range viewJobs {
1771 if j.Name == selected {
1772 curJob = j
1773 }
1774 }
1776 root := tview.NewPages()
1777 root.SetRect(0, 0, 90, 30)
1778 jobsCh := make(chan []*ViewJob, 1)
1779 jobsCh <- viewJobs
1780 jobsView(t.Context(), nil, jobsCh, make(chan struct{}), root, nil)
1782 titles := make(map[string]string, len(viewJobs))
1783 for _, j := range viewJobs {
1784 titles[j.Name] = boxes["jobs-"+j.Name].GetTitle()
1785 }
1786 return titles
1787 }
1789 t.Run("an unselected bracketed name is escaped for tview", func(t *testing.T) {
1790 titles := render(t, "short")
1792 assert.Equal(t, "✔ deploy-app[production[]", titles[bracketed],
1793 "the box holds the escaped name, which tview then has to truncate")
1794 })
1796 t.Run("a selected bracketed name scrolls instead of being truncated", func(t *testing.T) {
1797 titles := render(t, bracketed)
1799 assert.Equal(t, maxTitle, tview.TaggedStringWidth(titles[bracketed]),
1800 "the scrolled window fills the box exactly, so tview adds no ellipsis")
1801 assert.NotEqual(t, "✔ deploy-app[production[]", titles[bracketed],
1802 "the full name does not fit and must have been scrolled")
1803 })
1805 t.Run("a name that fits is left alone", func(t *testing.T) {
1806 titles := render(t, bracketed)
1808 assert.Equal(t, "✔ short", titles["short"])
1809 })
1810}
1812func Test_titleScroller(t *testing.T) {
1813 t.Parallel()
1815 t.Run("leaves a name that fits alone", func(t *testing.T) {
1816 t.Parallel()
1818 var s titleScroller
1819 s.follow("short")
1820 for range 5 {
1821 require.Equal(t, "short", s.scroll("short", 18))
1822 }
1823 })
1825 t.Run("scrolls a long name end to end and wraps", func(t *testing.T) {
1826 t.Parallel()
1828 var s titleScroller
1829 s.follow("job")
1830 // "abcdefgh" in a 4 column window: rest at the start, scroll one
1831 // column per redraw, rest at the end, then start over.
1832 want := []string{
1833 "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd", "abcd",
1834 "bcde", "cdef", "defg",
1835 "efgh", "efgh", "efgh", "efgh", "efgh", "efgh", "efgh", "efgh",
1836 "abcd",
1837 }
1838 for i, w := range want {
1839 require.Equal(t, w, s.scroll("abcdefgh", 4), "redraw %d", i)
1840 }
1841 })
1843 t.Run("restarts when the selection moves", func(t *testing.T) {
1844 t.Parallel()
1846 var s titleScroller
1847 s.follow("job1")
1848 for range scrollPause + 1 {
1849 s.scroll("abcdefgh", 4)
1850 }
1851 require.NotEqual(t, "abcd", s.scroll("abcdefgh", 4))
1852 s.follow("job2")
1853 require.Equal(t, "abcd", s.scroll("abcdefgh", 4))
1854 })
1856 t.Run("counts runes, not bytes", func(t *testing.T) {
1857 t.Parallel()
1859 var s titleScroller
1860 s.follow("job")
1861 require.Equal(t, "über", s.scroll("überlang", 4))
1862 })
1864 t.Run("scrolls double-width runes by column, not rune count", func(t *testing.T) {
1865 t.Parallel()
1867 // Six double-width runes are 12 columns wide, so a 4 column window
1868 // holds two of them. A rune-counting window would take four runes
1869 // (8 columns) and overflow the box.
1870 var s titleScroller
1871 s.follow("job")
1872 name := "一二三四五六"
1873 want := []string{
1874 "一二", "一二", "一二", "一二", "一二", "一二", "一二", "一二",
1875 "二三", "三四", "四五",
1876 "五六", "五六", "五六", "五六", "五六", "五六", "五六", "五六",
1877 "一二",
1878 }
1879 for i, w := range want {
1880 got := s.scroll(name, 4)
1881 require.Equal(t, w, got, "redraw %d", i)
1882 require.LessOrEqual(t, runewidth.StringWidth(got), 4, "redraw %d overflows the window", i)
1883 }
1884 })
1886 t.Run("scrolls double-width emoji by column, not rune count", func(t *testing.T) {
1887 t.Parallel()
1889 // The fixture relies on these emoji being two columns wide; assert it
1890 // so a future runewidth table change fails here loudly instead of
1891 // silently skewing the frames below.
1892 require.Equal(t, 2, runewidth.RuneWidth('🚀'))
1893 require.Equal(t, 2, runewidth.RuneWidth('🔥'))
1895 var s titleScroller
1896 s.follow("job")
1897 name := "🚀🔥🚀🔥🚀🔥"
1898 want := []string{
1899 "🚀🔥", "🚀🔥", "🚀🔥", "🚀🔥", "🚀🔥", "🚀🔥", "🚀🔥", "🚀🔥",
1900 "🔥🚀", "🚀🔥", "🔥🚀",
1901 "🚀🔥", "🚀🔥", "🚀🔥", "🚀🔥", "🚀🔥", "🚀🔥", "🚀🔥", "🚀🔥",
1902 "🚀🔥",
1903 }
1904 for i, w := range want {
1905 got := s.scroll(name, 4)
1906 require.Equal(t, w, got, "redraw %d", i)
1907 require.LessOrEqual(t, runewidth.StringWidth(got), 4, "redraw %d overflows the window", i)
1908 }
1909 })
1911 t.Run("recovers when the window outgrows the offset", func(t *testing.T) {
1912 t.Parallel()
1914 // A job renamed, or a redraw at a narrower width, must not slice out
1915 // of range.
1916 s := titleScroller{jobName: "job", offset: 30}
1917 require.Equal(t, "efgh", s.scroll("abcdefgh", 4))
1918 // A box too narrow to hold anything stops scrolling rather than
1919 // slicing; tview cuts the title down to whatever fits.
1920 require.Equal(t, "abcdefgh", s.scroll("abcdefgh", 0))
1921 })
1922}
Diagnostics8 git calls · 304 ms
CallStatusSizeTime0304 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:02001.8 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200635 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=635 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=517b073a0d5c32bc4e93f86de74b31d60ff8cf03 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=635 git.pkt.payload.max=608 git.sideband.band1.bytes=608 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=608 git.pack.trailer=178abb9456d874b7fd38fa21ad5cf9ca3ebcccaaOpen pack map
fetchtree:0200136 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=289 git.response.bytes=136 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=2ec338b608c87ccf92e7e6018fbb95b9d1deb7e0 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=136 git.pkt.payload.max=109 git.sideband.band1.bytes=109 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=109 git.pack.trailer=3151a1b94d81e5dbbdca76ceeede626242ec5c4cOpen pack map
fetch2009.5 KiB289 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=9717 git.response.cache=MISS git.response.duration_ms=289 git.fetch.wants=aef6c39d03fad4d355e43a82ee3902d497ce5677 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=9717 git.pkt.payload.max=9690 git.sideband.band1.bytes=9690 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e2c220185789da
- Request
- e37c677f…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format