internal/httpd/mrpage_test.go
229 lines · 8646 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 HeadPruned bool
39 Base string
40 Draft *draft
41}
42
43// A pruned head has no diff to show; the page must say the head is gone
44// rather than that nothing changed, which is what an empty file list
45// otherwise renders as.
46func TestMRDiffViewNamesAPrunedHead(t *testing.T) {
47 var sb strings.Builder
48 if err := web.Render(&sb, "mr.html", mrPageData{
49 repoPage: testRepoPage(), MR: testMR("merged"), View: "diff", HeadPruned: true,
50 }); err != nil {
51 t.Fatalf("render: %v", err)
52 }
53 out := sb.String()
54 if !strings.Contains(out, "no longer in the repository") {
55 t.Error("diff view of a pruned head does not say the head is gone")
56 }
57 if strings.Contains(out, "No changes between") {
58 t.Error("diff view of a pruned head claims there were no changes")
59 }
60}
61
62func renderMR(t *testing.T, m store.MR, reviews []store.MRReview, checks []store.Check) string {
63 rows := make([]reviewRow, 0, len(reviews))
64 for _, r := range reviews {
65 // The page test renders reviews that count; whether a given
66 // reviewer's does is decided by access, which e2e covers.
67 rows = append(rows, reviewRow{MRReview: r, Counts: true})
68 }
69 t.Helper()
70 var sb strings.Builder
71 if err := web.Render(&sb, "mr.html", mrPageData{
72 repoPage: testRepoPage(), MR: m, View: "conversation",
73 Reviews: rows, Checks: checks, Combined: "",
74 }); err != nil {
75 t.Fatalf("render: %v", err)
76 }
77 return sb.String()
78}
79
80func testMR(state string) store.MR {
81 return store.MR{Number: 42, Title: "org native rendering", Author: "cmc", State: state,
82 SourcePath: "krz/hutch", SourceRef: "org-native-rendering", TargetRef: "main",
83 HeadSHA: "ff6271a9d4570cd46f169091637a9d2e40ad5c2b"}
84}
85
86// The header states what happened to the MR: who opened, merged, or closed
87// it, and when. A merge or close with no stamp (imports, pre-0029 merges)
88// names the state without claiming a byline it cannot back up.
89func TestMRHeaderByState(t *testing.T) {
90 open := renderMR(t, testMR("open"), nil, nil)
91 if !strings.Contains(open, "opened by") {
92 t.Errorf("open MR does not say who opened it:\n%s", open)
93 }
94
95 merged := testMR("merged")
96 merged.MergedAt, merged.MergedBy = "2026-08-27T14:03:11.000Z", "cmc"
97 out := renderMR(t, merged, nil, nil)
98 for _, want := range []string{"merged", "krz/hutch:org-native-rendering", "2026-08-27 14:03"} {
99 if !strings.Contains(out, want) {
100 t.Errorf("merged header missing %q:\n%s", want, out)
101 }
102 }
103 if strings.Contains(out, "opened by") {
104 t.Errorf("merged MR still says it was opened:\n%s", out)
105 }
106
107 closed := testMR("closed")
108 closed.ClosedAt, closed.ClosedBy = "2026-08-27T14:03:11.000Z", "cmc"
109 out = renderMR(t, closed, nil, nil)
110 if !strings.Contains(out, "without merging") || strings.Contains(out, "opened by") {
111 t.Errorf("closed header:\n%s", out)
112 }
113
114 // Imports and pre-0029 merges carry no stamp; the wording drops the
115 // byline claim rather than naming nobody or inventing a time.
116 out = renderMR(t, testMR("merged"), nil, nil)
117 if strings.Contains(out, "opened by") || strings.Contains(out, " on 20") || strings.Contains(out, "merged by") {
118 t.Errorf("unstamped merged header:\n%s", out)
119 }
120 if !strings.Contains(out, "merged") {
121 t.Errorf("unstamped merged header drops the state entirely:\n%s", out)
122 }
123
124 out = renderMR(t, testMR("closed"), nil, nil)
125 if strings.Contains(out, "opened by") || strings.Contains(out, " on 20") || strings.Contains(out, "closed without merging by") {
126 t.Errorf("unstamped closed header:\n%s", out)
127 }
128 if !strings.Contains(out, "without merging") {
129 t.Errorf("unstamped closed header drops the state entirely:\n%s", out)
130 }
131}
132
133// Approvals and checks carry their times in the aside, so reading the MR
134// does not mean opening the build.
135func TestMRAsideTimestamps(t *testing.T) {
136 out := renderMR(t, testMR("open"),
137 []store.MRReview{{Reviewer: "cmc", Verdict: "approve", CreatedAt: "2026-08-27T14:03:11.000Z"}},
138 []store.Check{
139 {CommitStatus: store.CommitStatus{Context: "ci/test", State: "success",
140 UpdatedAt: "2026-08-27T14:05:00.000Z"}, Duration: 72 * time.Second, Build: 60},
141 {CommitStatus: store.CommitStatus{Context: "external/lint", State: "success",
142 UpdatedAt: "2026-08-27T14:06:00.000Z"}},
143 })
144 for _, want := range []string{"2026-08-27 14:03", "2026-08-27 14:05", "1m12s", "2026-08-27 14:06"} {
145 if !strings.Contains(out, want) {
146 t.Errorf("aside missing %q:\n%s", want, out)
147 }
148 }
149}
150
151// A job a path filter excluded has no build behind its status: the page
152// must show it as skipped rather than linking to a build that never ran
153// (#172).
154func TestMRChecksRenderSkippedWithoutBuildLink(t *testing.T) {
155 out := renderMR(t, testMR("open"), nil, []store.Check{
156 {CommitStatus: store.CommitStatus{Context: "ci/unit", State: "skipped",
157 Description: "every changed file matched paths-ignore", UpdatedAt: "2026-08-27T14:05:00.000Z"}},
158 })
159 row := ""
160 for _, line := range strings.Split(out, "\n") {
161 if strings.Contains(line, "ci/unit") {
162 row = line
163 }
164 }
165 if row == "" || !strings.Contains(row, ">skipped<") {
166 t.Fatalf("skipped check not rendered:\n%s", out)
167 }
168 if strings.Contains(row, "<a href") {
169 t.Errorf("skipped check with no build linked anyway: %s", row)
170 }
171}
172
173// The gates block says what the merge is waiting on before a merge is
174// refused (#199): every unmet gate, the approval count, the outstanding
175// owners, and whether a fast-forward is possible.
176func TestMRGatesRender(t *testing.T) {
177 render := func(g *control.GatesOut) string {
178 t.Helper()
179 var sb strings.Builder
180 if err := web.Render(&sb, "mr.html", mrPageData{
181 repoPage: testRepoPage(), MR: testMR("open"), View: "conversation", Gates: g,
182 }); err != nil {
183 t.Fatalf("render: %v", err)
184 }
185 return sb.String()
186 }
187 out := render(&control.GatesOut{ApprovalsRequired: 2, Approvals: []string{"bob"},
188 OwnersOutstanding: []control.OwnersOut{{Files: []string{"svc.go"}, Owners: []string{"carol"}}},
189 Unmet: []string{"krz/hutch requires 2 fresh approval(s); !42 has 1", "CODEOWNERS approval missing for: svc.go (owned by carol)"}})
190 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"} {
191 if !strings.Contains(out, want) {
192 t.Errorf("gates block missing %q:\n%s", want, out)
193 }
194 }
195 if out := render(&control.GatesOut{FastForward: true}); !strings.Contains(out, "All gates met") || !strings.Contains(out, "fast-forward possible") {
196 t.Errorf("met gates not rendered:\n%s", out)
197 }
198 if out := render(&control.GatesOut{Checks: "pending", ChecksMissing: []string{"ext/deploy"}}); !strings.Contains(out, "waiting on <code>ext/deploy</code>") {
199 t.Errorf("missing required context not rendered:\n%s", out)
200 }
201 if out := render(nil); strings.Contains(out, "Merge gates") {
202 t.Errorf("gates block on a merge request without gates:\n%s", out)
203 }
204}
205
206// Each revision after the first carries a link comparing it to the one
207// before, so a reviewer does not have to type mr range-diff by hand
208// (#269).
209func TestMRPageListsRevisionsWithCompareLinks(t *testing.T) {
210 var sb strings.Builder
211 revs := []store.MRHead{
212 {SHA: "aaaa1111", CreatedAt: "2026-09-23T10:00:00Z"},
213 {SHA: "bbbb2222", CreatedAt: "2026-09-24T10:00:00Z"},
214 }
215 if err := web.Render(&sb, "mr.html", mrPageData{
216 repoPage: testRepoPage(), MR: testMR("open"), View: "conversation", Revisions: revs,
217 }); err != nil {
218 t.Fatalf("render: %v", err)
219 }
220 out := sb.String()
221 for _, want := range []string{"aaaa1111", "bbbb2222", "compare to previous", "from=aaaa1111", "to=bbbb2222"} {
222 if !strings.Contains(out, want) {
223 t.Errorf("missing %q in:\n%s", want, out)
224 }
225 }
226 if strings.Contains(out, "gitbay mr range-diff") {
227 t.Error("still quotes the CLI command instead of linking the new page")
228 }
229}