internal/control/marks_test.go

v1.41.0
gitbay/internal/control/marks_test.go history · blame · raw

124 lines · 4125 bytes

  1package control
  2
  3import (
  4	"testing"
  5
  6	"gitbay.org/gitbay/internal/store"
  7)
  8
  9func TestChecksMark(t *testing.T) {
 10	st := func(states ...string) []store.CommitStatus {
 11		var out []store.CommitStatus
 12		for _, s := range states {
 13			out = append(out, store.CommitStatus{State: s})
 14		}
 15		return out
 16	}
 17	cases := []struct {
 18		in        []store.CommitStatus
 19		text, sgr string
 20	}{
 21		{nil, "", ""},
 22		{st("success", "success", "skipped"), "✓ 3/3", sgrGreen},
 23		{st("success", "pending"), "• 1 pending", sgrDim},
 24		{st("success", "failure", "error", "pending"), "✗ 2 failed", sgrRed},
 25	}
 26	for _, tc := range cases {
 27		if got := checksMark(tc.in); got.s != tc.text || got.sgr != tc.sgr {
 28			t.Errorf("checksMark(%v) = %q %q, want %q %q", tc.in, got.s, got.sgr, tc.text, tc.sgr)
 29		}
 30	}
 31}
 32
 33func TestReviewMark(t *testing.T) {
 34	counts := map[string]bool{"bob": true, "carol": true}
 35	mr := store.MR{Author: "alice"}
 36	rv := func(who, verdict string, stale bool) store.MRReview {
 37		return store.MRReview{Reviewer: who, Verdict: verdict, Stale: stale}
 38	}
 39	cases := []struct {
 40		name    string
 41		mr      store.MR
 42		reviews []store.MRReview
 43		want    string
 44	}{
 45		{"nothing", mr, nil, ""},
 46		{"approved", mr, []store.MRReview{rv("bob", "approve", false)}, "approved"},
 47		{"changes outrank", mr, []store.MRReview{rv("bob", "approve", false), rv("carol", "request_changes", false)}, "changes requested"},
 48		{"latest verdict wins", mr, []store.MRReview{rv("bob", "request_changes", false), rv("bob", "approve", false)}, "approved"},
 49		{"stale ignored", mr, []store.MRReview{rv("bob", "approve", true)}, ""},
 50		{"non-writer ignored", mr, []store.MRReview{rv("dave", "approve", false)}, ""},
 51		{"author ignored", store.MR{Author: "bob"}, []store.MRReview{rv("bob", "approve", false)}, ""},
 52		{"asked of viewer", store.MR{Author: "alice", ReviewRequests: []string{"me"}}, nil, "review requested"},
 53		{"asked of others", store.MR{Author: "alice", ReviewRequests: []string{"bob"}}, nil, "requested"},
 54	}
 55	for _, tc := range cases {
 56		if got := reviewMark(tc.mr, tc.reviews, counts, "me"); got.s != tc.want {
 57			t.Errorf("%s: reviewMark = %q, want %q", tc.name, got.s, tc.want)
 58		}
 59	}
 60}
 61
 62func TestLabelsAndAssignees(t *testing.T) {
 63	if got := labelsMark([]string{"a", "b", "c", "d"}); got != "a, b, +2" {
 64		t.Errorf("labelsMark = %q", got)
 65	}
 66	if got := assigneesMark([]string{"me"}, "me"); got.sgr != sgrYellow {
 67		t.Errorf("viewer's assignment not yellow: %+v", got)
 68	}
 69	if got := assigneesMark([]string{"bob"}, "me"); got.sgr != "" {
 70		t.Errorf("someone else's assignment coloured: %+v", got)
 71	}
 72}
 73
 74func TestFailingBuilds(t *testing.T) {
 75	builds := []DashboardBuild{
 76		{Repo: "a/x", Job: "test", Ref: "main", Status: "success"},
 77		{Repo: "a/x", Job: "test", Ref: "main", Status: "failure"}, // replaced by the newer success
 78		{Repo: "a/y", Job: "pull", Ref: "main", Status: "failure"},
 79	}
 80	if got := failingBuilds(builds); got != 1 {
 81		t.Errorf("failingBuilds = %d, want 1", got)
 82	}
 83}
 84
 85func TestNeedsYou(t *testing.T) {
 86	term := Term{Cols: 80}
 87	if got := term.needsYou(DashboardOut{}); got != "Nothing waits on you." {
 88		t.Errorf("empty = %q", got)
 89	}
 90	d := DashboardOut{Reviews: make([]DashboardItem, 2), Unread: 1}
 91	if got := term.needsYou(d); got != "Needs you: 2 reviews requested, 1 unread notification" {
 92		t.Errorf("needsYou = %q", got)
 93	}
 94}
 95
 96func TestStepState(t *testing.T) {
 97	cases := []struct {
 98		status    string
 99		failed, n int
100		want      string
101	}{
102		{"success", 0, 1, "success"},
103		{"running", 0, 1, ""},
104		{"failure", 0, 1, ""},
105		{"failure", 2, 1, "success"},
106		{"failure", 2, 2, "failure"},
107		{"failure", 2, 3, "skipped"},
108	}
109	for _, tc := range cases {
110		if got := stepState(tc.status, tc.failed, tc.n); got != tc.want {
111			t.Errorf("stepState(%q, %d, %d) = %q, want %q", tc.status, tc.failed, tc.n, got, tc.want)
112		}
113	}
114}
115
116func TestKeyValues(t *testing.T) {
117	got := keyValues(`{"source":"SHA256:x","argv":["--untrusted","a b"],"n":3}`)
118	if got != "argv=[--untrusted a b] n=3 source=SHA256:x" {
119		t.Errorf("keyValues = %q", got)
120	}
121	if got := keyValues("not json"); got != "not json" {
122		t.Errorf("non-JSON = %q", got)
123	}
124}