internal/httpd/mrpage_test.go

72920bdf0504bcee3d15305abc0724ae216d088c
gitbay/internal/httpd/mrpage_test.go history · blame · raw

180 lines · 6753 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	SourceGone      bool
 37	HeadMerged      bool
 38	Base            string
 39}
 40
 41func renderMR(t *testing.T, m store.MR, reviews []store.MRReview, checks []store.Check) string {
 42	rows := make([]reviewRow, 0, len(reviews))
 43	for _, r := range reviews {
 44		// The page test renders reviews that count; whether a given
 45		// reviewer's does is decided by access, which e2e covers.
 46		rows = append(rows, reviewRow{MRReview: r, Counts: true})
 47	}
 48	t.Helper()
 49	var sb strings.Builder
 50	if err := web.Render(&sb, "mr.html", mrPageData{
 51		repoPage: testRepoPage(), MR: m, View: "conversation",
 52		Reviews: rows, Checks: checks, Combined: "",
 53	}); err != nil {
 54		t.Fatalf("render: %v", err)
 55	}
 56	return sb.String()
 57}
 58
 59func testMR(state string) store.MR {
 60	return store.MR{Number: 42, Title: "org native rendering", Author: "cmc", State: state,
 61		SourcePath: "krz/hutch", SourceRef: "org-native-rendering", TargetRef: "main",
 62		HeadSHA: "ff6271a9d4570cd46f169091637a9d2e40ad5c2b"}
 63}
 64
 65// The header states what happened to the MR: who opened, merged, or closed
 66// it, and when. A merge or close with no stamp (imports, pre-0029 merges)
 67// names the state without claiming a byline it cannot back up.
 68func TestMRHeaderByState(t *testing.T) {
 69	open := renderMR(t, testMR("open"), nil, nil)
 70	if !strings.Contains(open, "opened by") {
 71		t.Errorf("open MR does not say who opened it:\n%s", open)
 72	}
 73
 74	merged := testMR("merged")
 75	merged.MergedAt, merged.MergedBy = "2026-08-27T14:03:11.000Z", "cmc"
 76	out := renderMR(t, merged, nil, nil)
 77	for _, want := range []string{"merged", "krz/hutch:org-native-rendering", "2026-08-27 14:03"} {
 78		if !strings.Contains(out, want) {
 79			t.Errorf("merged header missing %q:\n%s", want, out)
 80		}
 81	}
 82	if strings.Contains(out, "opened by") {
 83		t.Errorf("merged MR still says it was opened:\n%s", out)
 84	}
 85
 86	closed := testMR("closed")
 87	closed.ClosedAt, closed.ClosedBy = "2026-08-27T14:03:11.000Z", "cmc"
 88	out = renderMR(t, closed, nil, nil)
 89	if !strings.Contains(out, "without merging") || strings.Contains(out, "opened by") {
 90		t.Errorf("closed header:\n%s", out)
 91	}
 92
 93	// Imports and pre-0029 merges carry no stamp; the wording drops the
 94	// byline claim rather than naming nobody or inventing a time.
 95	out = renderMR(t, testMR("merged"), nil, nil)
 96	if strings.Contains(out, "opened by") || strings.Contains(out, " on 20") || strings.Contains(out, "merged by") {
 97		t.Errorf("unstamped merged header:\n%s", out)
 98	}
 99	if !strings.Contains(out, "merged") {
100		t.Errorf("unstamped merged header drops the state entirely:\n%s", out)
101	}
102
103	out = renderMR(t, testMR("closed"), nil, nil)
104	if strings.Contains(out, "opened by") || strings.Contains(out, " on 20") || strings.Contains(out, "closed without merging by") {
105		t.Errorf("unstamped closed header:\n%s", out)
106	}
107	if !strings.Contains(out, "without merging") {
108		t.Errorf("unstamped closed header drops the state entirely:\n%s", out)
109	}
110}
111
112// Approvals and checks carry their times in the aside, so reading the MR
113// does not mean opening the build.
114func TestMRAsideTimestamps(t *testing.T) {
115	out := renderMR(t, testMR("open"),
116		[]store.MRReview{{Reviewer: "cmc", Verdict: "approve", CreatedAt: "2026-08-27T14:03:11.000Z"}},
117		[]store.Check{
118			{CommitStatus: store.CommitStatus{Context: "ci/test", State: "success",
119				UpdatedAt: "2026-08-27T14:05:00.000Z"}, Duration: 72 * time.Second, Build: 60},
120			{CommitStatus: store.CommitStatus{Context: "external/lint", State: "success",
121				UpdatedAt: "2026-08-27T14:06:00.000Z"}},
122		})
123	for _, want := range []string{"2026-08-27 14:03", "2026-08-27 14:05", "1m12s", "2026-08-27 14:06"} {
124		if !strings.Contains(out, want) {
125			t.Errorf("aside missing %q:\n%s", want, out)
126		}
127	}
128}
129
130// A job a path filter excluded has no build behind its status: the page
131// must show it as skipped rather than linking to a build that never ran
132// (#172).
133func TestMRChecksRenderSkippedWithoutBuildLink(t *testing.T) {
134	out := renderMR(t, testMR("open"), nil, []store.Check{
135		{CommitStatus: store.CommitStatus{Context: "ci/unit", State: "skipped",
136			Description: "every changed file matched paths-ignore", UpdatedAt: "2026-08-27T14:05:00.000Z"}},
137	})
138	row := ""
139	for _, line := range strings.Split(out, "\n") {
140		if strings.Contains(line, "ci/unit") {
141			row = line
142		}
143	}
144	if row == "" || !strings.Contains(row, ">skipped<") {
145		t.Fatalf("skipped check not rendered:\n%s", out)
146	}
147	if strings.Contains(row, "<a href") {
148		t.Errorf("skipped check with no build linked anyway: %s", row)
149	}
150}
151
152// The gates block says what the merge is waiting on before a merge is
153// refused (#199): every unmet gate, the approval count, the outstanding
154// owners, and whether a fast-forward is possible.
155func TestMRGatesRender(t *testing.T) {
156	render := func(g *control.GatesOut) string {
157		t.Helper()
158		var sb strings.Builder
159		if err := web.Render(&sb, "mr.html", mrPageData{
160			repoPage: testRepoPage(), MR: testMR("open"), View: "conversation", Gates: g,
161		}); err != nil {
162			t.Fatalf("render: %v", err)
163		}
164		return sb.String()
165	}
166	out := render(&control.GatesOut{ApprovalsRequired: 2, Approvals: []string{"bob"},
167		OwnersOutstanding: []control.OwnersOut{{Files: []string{"svc.go"}, Owners: []string{"carol"}}},
168		Unmet:             []string{"krz/hutch requires 2 fresh approval(s); !42 has 1", "CODEOWNERS approval missing for: svc.go (owned by carol)"}})
169	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"} {
170		if !strings.Contains(out, want) {
171			t.Errorf("gates block missing %q:\n%s", want, out)
172		}
173	}
174	if out := render(&control.GatesOut{FastForward: true}); !strings.Contains(out, "All gates met") || !strings.Contains(out, "fast-forward possible") {
175		t.Errorf("met gates not rendered:\n%s", out)
176	}
177	if out := render(nil); strings.Contains(out, "Merge gates") {
178		t.Errorf("gates block on a merge request without gates:\n%s", out)
179	}
180}