internal/control/dashscreen_test.go

e6cd75b5f28bacf51620bb531320c30fd4e66bfd
gitbay/internal/control/dashscreen_test.go history · blame · raw

99 lines · 3559 bytes

5 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 TestDashboardPlainPinned(t *testing.T) {
13	st, repo, owner := twoMRTestRepo(t)
14	// Both merge requests are created in the same second; the dashboard
15	// orders by updated_at alone, so give them distinct times.
16	if _, err := st.DB.Exec(`UPDATE merge_requests SET updated_at = '2020-01-01T00:00:00Z' WHERE repo_id = ? AND number = 1`, repo.ID); err != nil {
17		t.Fatal(err)
18	}
19	c, out, errOut := mrTestCtx(st, owner)
20	if code := Dispatch(c, []string{"dashboard"}); code != protocol.ExitOK {
21		t.Fatalf("exit %d: %s", code, errOut)
22	}
23	pinPlain(t, "dashboard", out.String())
24}
25
26func dashFixture() DashboardOut {
27	return DashboardOut{
28		Assigned: []DashboardItem{{Repo: "krz/skunky-art", Number: 33, Title: "LibRedirect listing", Author: "cmc"}},
29		MRs:      []DashboardItem{{Repo: "krz/gitbay", Number: 554, Title: "list screens", Author: "cmc"}},
30		Builds: []DashboardBuild{
31			{Repo: "krz/omaha-metro-blotter", Number: 83, Job: "daily-pull", Status: "failure", Ref: "main", CreatedAt: "2026-09-30T12:00:00Z"},
32			{Repo: "krz/gitbay", Number: 1784, Job: "test", Status: "success", Ref: "main", CreatedAt: "2026-10-01T10:00:00Z"},
33			{Repo: "krz/gitbay", Number: 1783, Job: "build", Status: "success", Ref: "main", CreatedAt: "2026-10-01T10:00:00Z"},
34			{Repo: "krz/gitbay", Number: 1700, Job: "build", Status: "failure", Ref: "main", CreatedAt: "2026-09-01T10:00:00Z"},
35		},
36		Server: &ServerOut{Commit: "135f300"},
37		Queues: &store.Queues{},
38	}
39}
40
41func TestDashboardScreen(t *testing.T) {
42	c := screenCtx(110, false)
43	c.User = store.User{Username: "cmc", IsAdmin: true}
44	s := dashboardScreen(c, dashFixture(), nil)
45	var titles []string
46	for _, sec := range s.sections {
47		if len(sec.rows) > 0 || sec.empty {
48			titles = append(titles, sec.title)
49		}
50	}
51	if want := []string{"Assigned issues", "Open merge requests", "Failed builds", "Recent activity"}; !slices.Equal(titles, want) {
52		t.Errorf("sections = %q, want %q", titles, want)
53	}
54	if n := sectionCounts(s)["Failed builds"]; n != 1 {
55		t.Errorf("failed builds = %d, want 1 (an older failure of a job that has since passed is not one)", n)
56	}
57	out := renderString(c, s)
58	for _, unwanted := range []string{"135f300", "Queues", "webhooks"} {
59		if strings.Contains(out, unwanted) {
60			t.Errorf("admin block %q on the dashboard screen:\n%s", unwanted, out)
61		}
62	}
63	if !strings.Contains(out, "2 builds passed") {
64		t.Errorf("no passing-build summary:\n%s", out)
65	}
66	var cmds []string
67	for _, a := range s.actions {
68		cmds = append(cmds, strings.Join(a.argv, " "))
69	}
70	for _, want := range []string{"issue show krz/skunky-art 33", "build log krz/omaha-metro-blotter 83", "feed", "admin stats"} {
71		if !slices.Contains(cmds, want) {
72			t.Errorf("actions %q lack %q", cmds, want)
73		}
74	}
75	checkActions(t, s)
76}
77
78func TestDashboardScreenProblemsLine(t *testing.T) {
79	c := screenCtx(110, false)
80	c.User = store.User{Username: "cmc", IsAdmin: true}
81	d := dashFixture()
82	d.Queues.Mirrors.Errors = 1
83	out := renderString(c, dashboardScreen(c, d, nil))
84	if !strings.Contains(out, "Problems:  ✗  1 failing in the background\n") {
85		t.Errorf("no problems line:\n%s", out)
86	}
87}
88
89func TestDashboardScreenNonAdmin(t *testing.T) {
90	c := screenCtx(110, false)
91	c.User = store.User{Username: "bob"}
92	d := dashFixture()
93	d.Server, d.Queues = nil, nil
94	for _, a := range dashboardScreen(c, d, nil).actions {
95		if a.argv[0] == "admin" {
96			t.Errorf("non-admin offered %q", a.argv)
97		}
98	}
99}