Skip to content
quarry Artifact [a572775b72]
Login

Artifact [a572775b72]

Artifact a572775b72677842896f3bb51a2cc2b300d5ed467b5b3feedae357882f82bf8b:


package web

import (
	"strings"
	"testing"
)

// A failing trunk puts a dot inside the Timeline tab; a passing one
// leaves the tab exactly as it was.
func TestAFailingTrunkMarksTheTimelineTab(t *testing.T) {
	link := navlink("/r/q", "timeline", "Timeline", "", true, 12)
	if got := withdot(link, false); got != link {
		t.Errorf("FAIL: a green trunk changed the tab: %s", got)
	}
	got := string(withdot(link, true))
	i, j := strings.Index(got, `data-hook="trunk-red"`), strings.LastIndex(got, "</a>")
	if i < 0 || j < i {
		t.Errorf("FAIL: a red trunk has no dot inside the tab: %s", got)
	}
}

// Just accepted, the merge's run is still going: the record says the
// merge is being checked, not that the branch's checks passed.
func TestAnAcceptedLandingSaysItsMergeIsBeingChecked(t *testing.T) {
	l := landingDetail{MergeCI: "ci-run", MergeCIRun: "run1"}
	l.State = "accepted"
	s := l.ChecksSummary()
	if s.State != "wait" || !s.Merge {
		t.Errorf("FAIL: a running merge reads as %+v", s)
	}
}

// Settled, the merge's own run is the row under the merge headline,
// and the branch's premerge rows say they were before landing: the
// two used to sit together, the merge's run linked nowhere.
func TestASettledLandingLinksTheMergeRunAndLabelsPremergeRows(t *testing.T) {
	var buf strings.Builder
	if err := noHooks.template(landingPage).ExecuteTemplate(&buf, "landing-body", pageData{
		Repo: "quarry",
		Landing: &landingDetail{
			landingRow: landingRow{ID: "aa11bb22cc33", Branch: "b", Target: "trunk", State: "accepted"},
			MergeCI:    "ci-pass", MergeCIRun: "27b8a1a6977a0000",
			Checks: []ciRunRow{{RunID: "b9b44233ba4d0000", Workflow: "test", Status: "cancelled"}},
		},
	}); err != nil {
		t.Fatal(err)
	}
	got := buf.String()
	for _, want := range []string{`data-hook="merge-run"`, `/ci/run/27b8a1a6977a"`, `data-hook="premerge-label"`} {
		if !strings.Contains(got, want) {
			t.Errorf("FAIL: settled landing lacks %s", want)
		}
	}
	if i, j := strings.Index(got, `data-hook="merge-run"`), strings.Index(got, `data-hook="premerge-label"`); i > j {
		t.Error("FAIL: the premerge label comes before the merge run")
	}
	if got := recordBody(t, "open", "x"); strings.Contains(got, `data-hook="merge-run"`) || strings.Contains(got, `data-hook="premerge-label"`) {
		t.Error("FAIL: an open record shows merge-run markers")
	}
}

// #367: every required workflow's run on the merge is shown, an
// inherited one marked so. #366: a merge whose runs were all cancelled
// says so instead of "all checks have passed", and its premerge rows
// are labelled.
func TestASettledLandingShowsEveryMergeRunAndACancelledMerge(t *testing.T) {
	render := func(l *landingDetail) string {
		var buf strings.Builder
		if err := noHooks.template(landingPage).ExecuteTemplate(&buf, "landing-body", pageData{Repo: "quarry", Landing: l}); err != nil {
			t.Fatal(err)
		}
		return buf.String()
	}
	row := landingRow{ID: "aa11bb22cc33", Branch: "b", Target: "trunk", State: "accepted"}
	got := render(&landingDetail{landingRow: row, MergeCI: "ci-pass", MergeCIRun: "r1",
		MergeRuns: []ciRunRow{
			{RunID: "q1aaaaaaaaaa", Pipeline: "ci/quarry.json", Status: "completed", InheritedFrom: "p1"},
			{RunID: "r1aaaaaaaaaa", Pipeline: "ci/race.json", Status: "completed", InheritedFrom: "p2"},
		},
		Checks: []ciRunRow{{RunID: "p1", Pipeline: "ci/quarry.json", Status: "completed"}}})
	if n := strings.Count(got, `data-hook="merge-run"`); n != 2 {
		t.Errorf("FAIL: %d merge runs shown, want both workflows", n)
	}
	if !strings.Contains(got, `data-inherited="p1"`) || !strings.Contains(got, `data-hook="premerge-label"`) {
		t.Error("FAIL: an inherited merge run is not marked, or the premerge rows are not labelled")
	}

	l := &landingDetail{landingRow: row, MergeCI: "ci-cancelled", MergeCIRun: "c1",
		MergeRuns: []ciRunRow{{RunID: "c1aaaaaaaaaa", Pipeline: "ci/quarry.json", Status: "cancelled"}},
		Checks:    []ciRunRow{{RunID: "p1", Pipeline: "ci/quarry.json", Status: "completed"}}}
	if s := l.ChecksSummary(); s.State == "pass" || !strings.Contains(s.Headline, "cancelled") {
		t.Errorf("FAIL: a cancelled merge reads %+v", s)
	}
	if got := render(l); !strings.Contains(got, `data-hook="premerge-label"`) || !strings.Contains(got, `/ci/run/c1aaaaaaaaaa"`) {
		t.Error("FAIL: a cancelled merge's card does not link its run or label the premerge rows")
	}
}