internal/control/listscreen_test.go
111 lines · 4170 bytes
4 symbols in this file
1package control
2
3import (
4 "slices"
5 "strings"
6 "testing"
7
8 "gitbay.org/gitbay/internal/protocol"
9 "gitbay.org/gitbay/internal/store"
10)
11
12func TestListsPlainPinned(t *testing.T) {
13 st, repo, owner := twoMRTestRepo(t)
14 if _, err := st.CreateIssue(repo.ID, owner.ID, "Android app", "", "md"); err != nil {
15 t.Fatal(err)
16 }
17 if _, err := st.CreateBuild(repo.ID, "test", "abc111", "main", `["go test ./..."]`, "", "", true); err != nil {
18 t.Fatal(err)
19 }
20 for name, argv := range map[string][]string{
21 "mr-list": {"mr", "list", repo.Path()},
22 "issue-list": {"issue", "list", repo.Path()},
23 "build-list": {"build", "list", repo.Path()},
24 } {
25 c, out, errOut := mrTestCtx(st, owner)
26 if code := Dispatch(c, argv); code != protocol.ExitOK {
27 t.Fatalf("%s: exit %d: %s", name, code, errOut)
28 }
29 pinPlain(t, name, out.String())
30 }
31}
32
33func TestMRListScreen(t *testing.T) {
34 c := screenCtx(100, false)
35 c.User = store.User{Username: "alice"}
36 mrs := []store.MR{{ID: 1, Number: 552, UpdatedAt: "2026-10-01T11:40:00Z"}, {ID: 2, Number: 551, UpdatedAt: "2026-10-01T10:00:00Z"}}
37 ds := []mrOut{
38 {Number: 552, Title: "wire $PAGER", State: "open", Source: "cli-pager", TargetRef: "main"},
39 {Number: 551, Title: "changelog", State: "open", Source: "release", TargetRef: "stable"},
40 }
41 checks := map[int64]cell{1: cMark("✓ 2/2", sgrGreen), 2: cMark("✗ 1 failed", sgrRed)}
42 review := map[int64]cell{1: cMark("review requested", sgrYellow)}
43 s := mrListScreen(c, screenRepo, "open", mrs, ds, checks, review)
44 if len(s.sections) != 1 || s.sections[0].title != "Open merge requests" || s.sections[0].n != 2 {
45 t.Fatalf("sections = %+v", s.sections)
46 }
47 if g := s.sections[0].rows[0].cells[1]; g.s != "●" {
48 t.Errorf("review requested of the viewer should lead with ●, got %q", g.s)
49 }
50 if g := s.sections[0].rows[1].cells[1]; g.s != "✗" || g.sgr != sgrRed {
51 t.Errorf("failed checks should lead with a red ✗, got %+v", g)
52 }
53 out := renderString(c, s)
54 if !strings.Contains(out, "release → stable") {
55 t.Errorf("non-default target not shown:\n%s", out)
56 }
57 var verbs []string
58 for _, a := range s.actions {
59 verbs = append(verbs, strings.Join(a.argv[:2], " "))
60 }
61 if !slices.Equal(verbs, []string{"mr create", "mr list"}) {
62 t.Errorf("actions = %q", verbs)
63 }
64 checkActions(t, s)
65}
66
67func TestIssueListScreen(t *testing.T) {
68 c := screenCtx(100, false)
69 c.User = store.User{Username: "alice"}
70 issues := []store.Issue{{ID: 1, Number: 12, UpdatedAt: "2026-09-01T10:00:00Z"}, {ID: 2, Number: 13, UpdatedAt: "2026-09-02T10:00:00Z"}}
71 ds := []issueOut{{Number: 12, Title: "Android app", State: "open"}, {Number: 13, Title: "Docs", State: "open"}}
72 s := issueListScreen(c, screenRepo, "open", issues, ds, map[int64]int{1: 3}, map[int64][]string{1: {"mobile"}}, map[int64][]string{1: {"alice"}})
73 sec := s.sections[0]
74 if sec.title != "Open issues" || sec.n != 2 {
75 t.Fatalf("section = %+v", sec)
76 }
77 if g := sec.rows[0].cells[1]; g.s != "●" {
78 t.Errorf("assigned to the viewer should lead with ●, got %q", g.s)
79 }
80 if out := renderString(c, s); !strings.Contains(out, "mobile · alice · 3 comments") {
81 t.Errorf("meta:\n%s", out)
82 }
83 var verbs []string
84 for _, a := range s.actions {
85 verbs = append(verbs, strings.Join(a.argv, " "))
86 }
87 if !slices.Equal(verbs, []string{"issue create krz/gitbay", "issue list krz/gitbay --state closed"}) {
88 t.Errorf("actions = %q", verbs)
89 }
90 checkActions(t, s)
91}
92
93func TestBuildListScreen(t *testing.T) {
94 c := screenCtx(100, false)
95 ds := []BuildOut{
96 {Number: 1784, Job: "test", Status: "running", Ref: "cli-views-2a", Subject: "docs: screens", CreatedAt: "2026-10-01T12:00:00Z"},
97 {Number: 1783, Job: "build", Status: "success", Ref: "cli-views-2a", Subject: "docs: screens", CreatedAt: "2026-10-01T12:00:00Z"},
98 }
99 s := buildListScreen(c, screenRepo, ds)
100 sec := s.sections[0]
101 if sec.title != "Builds" || sec.n != 2 {
102 t.Fatalf("section = %+v", sec)
103 }
104 if g := sec.rows[0].cells[1]; g.s != "◐" {
105 t.Errorf("running build glyph = %q", g.s)
106 }
107 if len(s.actions) != 1 || strings.Join(s.actions[0].argv, " ") != "build show krz/gitbay 1784" {
108 t.Errorf("actions = %+v", s.actions)
109 }
110 checkActions(t, s)
111}