Skip to content
quarry Artifact [c7e3da1468]
Login

Artifact [c7e3da1468]

Artifact c7e3da146851bbe97a2b30053b980cb47e041f823259ff2c58e07d50c2b8e5ed:


package web

import (
	"bytes"
	"strings"
	"testing"
)

// The merge page says each verdict in its own words, draws the view for
// a conflict, and links the pair's open landing.
func TestMergePageSaysEachVerdict(t *testing.T) {
	render := func(mp *mergePage) string {
		var b bytes.Buffer
		if err := noHooks.template(mergePageTmpl).ExecuteTemplate(&b, "content", pageData{Repo: "alpha", Merge: mp}); err != nil {
			t.Fatal(err)
		}
		return b.String()
	}
	for verdict, hook := range map[string]string{"clean": "merge-clean", "up-to-date": "merge-uptodate", "unrelated": "merge-unrelated"} {
		h := render(&mergePage{Branch: "feat", Onto: "trunk", Verdict: verdict, Branches: []string{"trunk", "feat"}})
		if !strings.Contains(h, `data-hook="`+hook+`"`) || strings.Contains(h, `data-hook="meld"`) {
			t.Errorf("FAIL: %s did not say so on its own", verdict)
		}
	}
	mv := buildMeld(meldFixture(), "/r/alpha")
	h := render(&mergePage{Branch: "feat", Onto: "trunk", Verdict: "conflict", Meld: mv, Landing: "7", LandingNumber: "7"})
	if !strings.Contains(h, `data-hook="meld"`) || !strings.Contains(h, `data-verdict="conflict"`) {
		t.Error("FAIL: a conflicting pair does not draw the merge view")
	}
	if !strings.Contains(h, `data-hook="merge-landing"`) || !strings.Contains(h, `/r/alpha/landing/7`) {
		t.Error("FAIL: the pair's open landing is not linked")
	}
	if !strings.Contains(h, `name="branch"`) || !strings.Contains(h, `name="onto"`) {
		t.Error("FAIL: no way to pick another pair")
	}
}

// The branches list gives each open branch its standing against trunk,
// linking the branch's own merge page; the default branch has none.
func TestBranchesListShowsHowEachBranchMerges(t *testing.T) {
	rows := []branchRow{{Name: "trunk", Tip: "t1"}, {Name: "feat", Tip: "f1"}, {Name: "docs", Tip: "d1"}, {Name: "new", Tip: "n1"}}
	type m = struct {
		Onto   string `json:"onto"`
		State  string `json:"state"`
		Files  int    `json:"files"`
		Behind int    `json:"behind"`
	}
	rows[1].Merge = &m{Onto: "trunk", State: "conflict", Files: 2}
	rows[2].Merge = &m{Onto: "trunk", State: "clean", Behind: 3}
	rows[3].Merge = &m{Onto: "trunk"}
	var b bytes.Buffer
	if err := noHooks.template(branchesPage).ExecuteTemplate(&b, "content", pageData{Repo: "alpha", Branches: rows}); err != nil {
		t.Fatal(err)
	}
	h := b.String()
	for _, want := range []string{`data-state="conflict"`, `data-state="clean"`, `data-state=""`,
		`/r/alpha/merge?branch=feat&onto=trunk`, `/r/alpha/merge?branch=docs&onto=trunk`} {
		if !strings.Contains(h, want) {
			t.Errorf("FAIL: the list lacks %s", want)
		}
	}
	if strings.Count(h, `data-hook="branch-merge"`) != 3 {
		t.Error("FAIL: the default branch, or a branch, has the wrong number of merge chips")
	}
}