internal/httpd/mrpage_test.go

5e9732fb1d5142142fdae2985f07125cf852647c
gitbay/internal/httpd/mrpage_test.go history · blame · raw

104 lines · 3449 bytes

  1package httpd
  2
  3import (
  4	"html/template"
  5	"strings"
  6	"testing"
  7	"time"
  8
  9	"gitbay.org/gitbay/internal/gitutil"
 10	"gitbay.org/gitbay/internal/store"
 11	"gitbay.org/gitbay/internal/web"
 12)
 13
 14// mrPageData mirrors the anonymous struct the mr handler renders with.
 15type mrPageData struct {
 16	repoPage
 17	MR              store.MR
 18	View            string
 19	BodyHTML        template.HTML
 20	Checks          []store.Check
 21	Combined        string
 22	Comments        []renderedComment
 23	Reviews         []store.MRReview
 24	DiffFiles       []diffFile
 25	Stat            diffStat
 26	Commits         []struct{}
 27	Branches        []gitutil.Ref
 28	CanEdit         bool
 29	CanWrite        bool
 30	Unresolved      int
 31	Notice          string
 32	DetachedThreads []diffThread
 33}
 34
 35func renderMR(t *testing.T, m store.MR, reviews []store.MRReview, checks []store.Check) string {
 36	t.Helper()
 37	var sb strings.Builder
 38	if err := web.Render(&sb, "mr.html", mrPageData{
 39		repoPage: testRepoPage(), MR: m, View: "conversation",
 40		Reviews: reviews, Checks: checks, Combined: "",
 41	}); err != nil {
 42		t.Fatalf("render: %v", err)
 43	}
 44	return sb.String()
 45}
 46
 47func testMR(state string) store.MR {
 48	return store.MR{Number: 42, Title: "org native rendering", Author: "cmc", State: state,
 49		SourcePath: "krz/hutch", SourceRef: "org-native-rendering", TargetRef: "main",
 50		HeadSHA: "ff6271a9d4570cd46f169091637a9d2e40ad5c2b"}
 51}
 52
 53// The header states what happened to the MR. "wants to merge" is only true
 54// while it is still open.
 55func TestMRHeaderByState(t *testing.T) {
 56	open := renderMR(t, testMR("open"), nil, nil)
 57	if !strings.Contains(open, "wants to merge") {
 58		t.Errorf("open MR does not say wants to merge:\n%s", open)
 59	}
 60
 61	merged := testMR("merged")
 62	merged.MergedAt, merged.MergedBy = "2026-08-27T14:03:11.000Z", "cmc"
 63	out := renderMR(t, merged, nil, nil)
 64	for _, want := range []string{"merged", "krz/hutch:org-native-rendering", "2026-08-27 14:03"} {
 65		if !strings.Contains(out, want) {
 66			t.Errorf("merged header missing %q:\n%s", want, out)
 67		}
 68	}
 69	if strings.Contains(out, "wants to merge") {
 70		t.Errorf("merged MR still wants to merge:\n%s", out)
 71	}
 72
 73	closed := testMR("closed")
 74	closed.ClosedAt, closed.ClosedBy = "2026-08-27T14:03:11.000Z", "cmc"
 75	out = renderMR(t, closed, nil, nil)
 76	if !strings.Contains(out, "without merging") || strings.Contains(out, "wants to merge") {
 77		t.Errorf("closed header:\n%s", out)
 78	}
 79
 80	// Imports and pre-0029 merges carry no stamp; the wording drops the
 81	// claim rather than inventing a time.
 82	out = renderMR(t, testMR("merged"), nil, nil)
 83	if strings.Contains(out, "wants to merge") || strings.Contains(out, " on 20") {
 84		t.Errorf("unstamped merged header:\n%s", out)
 85	}
 86}
 87
 88// Approvals and checks carry their times in the aside, so reading the MR
 89// does not mean opening the build.
 90func TestMRAsideTimestamps(t *testing.T) {
 91	out := renderMR(t, testMR("open"),
 92		[]store.MRReview{{Reviewer: "cmc", Verdict: "approve", CreatedAt: "2026-08-27T14:03:11.000Z"}},
 93		[]store.Check{
 94			{CommitStatus: store.CommitStatus{Context: "ci/test", State: "success",
 95				UpdatedAt: "2026-08-27T14:05:00.000Z"}, Duration: 72 * time.Second, Build: 60},
 96			{CommitStatus: store.CommitStatus{Context: "external/lint", State: "success",
 97				UpdatedAt: "2026-08-27T14:06:00.000Z"}},
 98		})
 99	for _, want := range []string{"2026-08-27 14:03", "2026-08-27 14:05", "1m12s", "2026-08-27 14:06"} {
100		if !strings.Contains(out, want) {
101			t.Errorf("aside missing %q:\n%s", want, out)
102		}
103	}
104}