internal/httpd/mrpage_test.go

784b5dfad3f6ed718ada2a43910225c220abb310
gitbay/internal/httpd/mrpage_test.go history · blame · raw

165 lines · 6060 bytes

  1package httpd
  2
  3import (
  4	"html/template"
  5	"strings"
  6	"testing"
  7	"time"
  8
  9	"gitbay.org/gitbay/internal/control"
 10	"gitbay.org/gitbay/internal/gitutil"
 11	"gitbay.org/gitbay/internal/store"
 12	"gitbay.org/gitbay/internal/web"
 13)
 14
 15// mrPageData mirrors the anonymous struct the mr handler renders with.
 16type mrPageData struct {
 17	repoPage
 18	MR              store.MR
 19	View            string
 20	BodyHTML        template.HTML
 21	Checks          []store.Check
 22	Combined        string
 23	Comments        []renderedComment
 24	Reviews         []reviewRow
 25	DiffFiles       []diffFile
 26	Stat            diffStat
 27	Commits         []struct{}
 28	Branches        []gitutil.Ref
 29	CanEdit         bool
 30	CanWrite        bool
 31	Unresolved      int
 32	Revisions       []store.MRHead
 33	Notice          string
 34	DetachedThreads []diffThread
 35	Gates           *control.GatesOut
 36}
 37
 38func renderMR(t *testing.T, m store.MR, reviews []store.MRReview, checks []store.Check) string {
 39	rows := make([]reviewRow, 0, len(reviews))
 40	for _, r := range reviews {
 41		// The page test renders reviews that count; whether a given
 42		// reviewer's does is decided by access, which e2e covers.
 43		rows = append(rows, reviewRow{MRReview: r, Counts: true})
 44	}
 45	t.Helper()
 46	var sb strings.Builder
 47	if err := web.Render(&sb, "mr.html", mrPageData{
 48		repoPage: testRepoPage(), MR: m, View: "conversation",
 49		Reviews: rows, Checks: checks, Combined: "",
 50	}); err != nil {
 51		t.Fatalf("render: %v", err)
 52	}
 53	return sb.String()
 54}
 55
 56func testMR(state string) store.MR {
 57	return store.MR{Number: 42, Title: "org native rendering", Author: "cmc", State: state,
 58		SourcePath: "krz/hutch", SourceRef: "org-native-rendering", TargetRef: "main",
 59		HeadSHA: "ff6271a9d4570cd46f169091637a9d2e40ad5c2b"}
 60}
 61
 62// The header states what happened to the MR. "wants to merge" is only true
 63// while it is still open.
 64func TestMRHeaderByState(t *testing.T) {
 65	open := renderMR(t, testMR("open"), nil, nil)
 66	if !strings.Contains(open, "wants to merge") {
 67		t.Errorf("open MR does not say wants to merge:\n%s", open)
 68	}
 69
 70	merged := testMR("merged")
 71	merged.MergedAt, merged.MergedBy = "2026-08-27T14:03:11.000Z", "cmc"
 72	out := renderMR(t, merged, nil, nil)
 73	for _, want := range []string{"merged", "krz/hutch:org-native-rendering", "2026-08-27 14:03"} {
 74		if !strings.Contains(out, want) {
 75			t.Errorf("merged header missing %q:\n%s", want, out)
 76		}
 77	}
 78	if strings.Contains(out, "wants to merge") {
 79		t.Errorf("merged MR still wants to merge:\n%s", out)
 80	}
 81
 82	closed := testMR("closed")
 83	closed.ClosedAt, closed.ClosedBy = "2026-08-27T14:03:11.000Z", "cmc"
 84	out = renderMR(t, closed, nil, nil)
 85	if !strings.Contains(out, "without merging") || strings.Contains(out, "wants to merge") {
 86		t.Errorf("closed header:\n%s", out)
 87	}
 88
 89	// Imports and pre-0029 merges carry no stamp; the wording drops the
 90	// claim rather than inventing a time.
 91	out = renderMR(t, testMR("merged"), nil, nil)
 92	if strings.Contains(out, "wants to merge") || strings.Contains(out, " on 20") {
 93		t.Errorf("unstamped merged header:\n%s", out)
 94	}
 95}
 96
 97// Approvals and checks carry their times in the aside, so reading the MR
 98// does not mean opening the build.
 99func TestMRAsideTimestamps(t *testing.T) {
100	out := renderMR(t, testMR("open"),
101		[]store.MRReview{{Reviewer: "cmc", Verdict: "approve", CreatedAt: "2026-08-27T14:03:11.000Z"}},
102		[]store.Check{
103			{CommitStatus: store.CommitStatus{Context: "ci/test", State: "success",
104				UpdatedAt: "2026-08-27T14:05:00.000Z"}, Duration: 72 * time.Second, Build: 60},
105			{CommitStatus: store.CommitStatus{Context: "external/lint", State: "success",
106				UpdatedAt: "2026-08-27T14:06:00.000Z"}},
107		})
108	for _, want := range []string{"2026-08-27 14:03", "2026-08-27 14:05", "1m12s", "2026-08-27 14:06"} {
109		if !strings.Contains(out, want) {
110			t.Errorf("aside missing %q:\n%s", want, out)
111		}
112	}
113}
114
115// A job a path filter excluded has no build behind its status: the page
116// must show it as skipped rather than linking to a build that never ran
117// (#172).
118func TestMRChecksRenderSkippedWithoutBuildLink(t *testing.T) {
119	out := renderMR(t, testMR("open"), nil, []store.Check{
120		{CommitStatus: store.CommitStatus{Context: "ci/unit", State: "skipped",
121			Description: "every changed file matched paths-ignore", UpdatedAt: "2026-08-27T14:05:00.000Z"}},
122	})
123	row := ""
124	for _, line := range strings.Split(out, "\n") {
125		if strings.Contains(line, "ci/unit") {
126			row = line
127		}
128	}
129	if row == "" || !strings.Contains(row, ">skipped<") {
130		t.Fatalf("skipped check not rendered:\n%s", out)
131	}
132	if strings.Contains(row, "<a href") {
133		t.Errorf("skipped check with no build linked anyway: %s", row)
134	}
135}
136
137// The gates block says what the merge is waiting on before a merge is
138// refused (#199): every unmet gate, the approval count, the outstanding
139// owners, and whether a fast-forward is possible.
140func TestMRGatesRender(t *testing.T) {
141	render := func(g *control.GatesOut) string {
142		t.Helper()
143		var sb strings.Builder
144		if err := web.Render(&sb, "mr.html", mrPageData{
145			repoPage: testRepoPage(), MR: testMR("open"), View: "conversation", Gates: g,
146		}); err != nil {
147			t.Fatalf("render: %v", err)
148		}
149		return sb.String()
150	}
151	out := render(&control.GatesOut{ApprovalsRequired: 2, Approvals: []string{"bob"},
152		OwnersOutstanding: []control.OwnersOut{{Files: []string{"svc.go"}, Owners: []string{"carol"}}},
153		Unmet:             []string{"krz/hutch requires 2 fresh approval(s); !42 has 1", "CODEOWNERS approval missing for: svc.go (owned by carol)"}})
154	for _, want := range []string{"Merge gates", "requires 2 fresh approval(s)", "approvals: 1 of 2 (bob)", `waiting on <a href="/carol">carol</a>`, "not a fast-forward"} {
155		if !strings.Contains(out, want) {
156			t.Errorf("gates block missing %q:\n%s", want, out)
157		}
158	}
159	if out := render(&control.GatesOut{FastForward: true}); !strings.Contains(out, "All gates met") || !strings.Contains(out, "fast-forward possible") {
160		t.Errorf("met gates not rendered:\n%s", out)
161	}
162	if out := render(nil); strings.Contains(out, "Merge gates") {
163		t.Errorf("gates block on a merge request without gates:\n%s", out)
164	}
165}