Skip to content
quarry Artifact [745c15ebf7]
Login

Artifact [745c15ebf7]

Artifact 745c15ebf70698144f23998741ad685536febd69edb1377d4e042208365006c7:


package web

import (
	"bytes"
	"fmt"
	"net/url"
	"regexp"
	"strings"
	"testing"
)

func renderDash(t *testing.T, d dashData) string {
	t.Helper()
	var buf bytes.Buffer
	if err := noHooks.template(dashboardPage).ExecuteTemplate(&buf, "content",
		pageData{Dash: &d, User: "dmestas"}); err != nil {
		t.Fatal(err)
	}
	return buf.String()
}

// dashFixture is ten repositories, each with a newest event, and six teams.
func dashFixture() dashData {
	d := dashData{User: "dmestas"}
	for i := 0; i < 10; i++ {
		d.Repos = append(d.Repos, dashRepoRow{Name: fmt.Sprintf("repo%d", i),
			Latest: &dashEvent{Repo: fmt.Sprintf("repo%d", i), Kind: "ci", Hash: strings.Repeat("ab", 32), Comment: "a check-in", User: "dan"}})
	}
	for i := 0; i < 6; i++ {
		d.Teams = append(d.Teams, teamRow{Name: fmt.Sprintf("team%d", i)})
	}
	d.Awaiting = []ticketRow{{Title: "an answered ticket", Repo: "repo0", ID: "abcdef0123"}}
	return d
}

var boardNames = regexp.MustCompile(`data-hook="board-row" data-repo="([^"]*)"`)

// boardOrder is the repository names in the order the board lists them.
func boardOrder(body string) []string {
	var names []string
	for _, m := range boardNames.FindAllStringSubmatch(body, -1) {
		names = append(names, m[1])
	}
	return names
}

func sameNames(got []string, want ...string) bool {
	return strings.Join(got, ",") == strings.Join(want, ",")
}

// The board is every repository the account holds, in one table, with no
// cap: the repositories have the page now, and teams and activity have
// their own tabs.
func TestHomeBoardListsEveryRepositoryInOneTable(t *testing.T) {
	d := dashFixture()
	shapeDashRepos(&d, nil)
	got := renderDash(t, d)
	if n := strings.Count(got, `<table class="fleet board" data-hook="board">`); n != 1 {
		t.Fatalf("FAIL: %d board tables, want 1", n)
	}
	if order := boardOrder(got); len(order) != 10 || order[0] != "repo0" || order[9] != "repo9" {
		t.Errorf("FAIL: the board lists %v, want all ten in the order given", order)
	}
	for _, gone := range []string{`data-hook="dash-all-repos"`, "dash-wide-only", `data-hook="dash-teams"`,
		`id="dash-activity"`, `data-hook="dash-skeleton"`, `data-hook="dash-awaiting"`, "dash-mine"} {
		if strings.Contains(got, gone) {
			t.Errorf("FAIL: the repositories tab still carries %s", gone)
		}
	}
	// Nothing is pinned, so there is no pinned group at all, and no group
	// label to say so.
	if strings.Contains(got, `data-hook="board-pinned"`) || strings.Contains(got, `data-hook="board-group"`) {
		t.Error("FAIL: a board with nothing pinned draws a pinned group")
	}
	fmt.Println("ok: every repository in one table, nothing else on the tab")
}

// Pinned repositories are a group at the top of the same table in the
// order they were pinned, under a plain label; the rest follow in their
// own order and a pinned repository is not listed again among them.
func TestHomeBoardPutsPinnedRepositoriesFirstInPinOrder(t *testing.T) {
	d := dashFixture()
	d.Repos[7].Pin, d.Repos[2].Pin, d.Repos[5].Pin = 1, 3, 2
	shapeDashRepos(&d, nil)
	got := renderDash(t, d)
	if order := boardOrder(got); !sameNames(order, "repo7", "repo5", "repo2",
		"repo0", "repo1", "repo3", "repo4", "repo6", "repo8", "repo9") {
		t.Errorf("FAIL: the board lists %v, want pins in pin order and then the rest", order)
	}
	pinned, rest := strings.Index(got, `data-hook="board-pinned"`), strings.Index(got, `data-hook="board-rest"`)
	label := strings.Index(got, `data-hook="board-group"`)
	if pinned < 0 || rest < 0 || label < pinned || label > rest {
		t.Fatalf("FAIL: pinned group at %d, its label at %d, the rest at %d", pinned, label, rest)
	}
	if n := len(boardOrder(got[pinned:rest])); n != 3 {
		t.Errorf("FAIL: %d rows in the pinned group, want 3", n)
	}
	if n := len(boardOrder(got[rest:])); n != 7 {
		t.Errorf("FAIL: %d rows after the pinned group, want 7", n)
	}
	if n := strings.Count(got, `data-repo="repo7"`); n != 1 {
		t.Errorf("FAIL: a pinned repository is listed %d times", n)
	}
	if n := strings.Count(got, `data-pinned="true"`); n != 3 {
		t.Errorf("FAIL: %d pin buttons are marked pinned, want 3", n)
	}
	// The label is a plain row of the table, not a card.
	if strings.Contains(got, `class="card`) || strings.Contains(got, "border-left") {
		t.Error("FAIL: the pinned group is dressed as a card")
	}
	fmt.Println("ok: pinned first in pin order, not repeated below")
}

// Filters apply to both groups, and a pin keeps its place under one.
func TestHomeBoardFiltersBothGroups(t *testing.T) {
	d := dashFixture()
	d.Repos[7].Pin, d.Repos[2].Pin = 1, 2
	d.Repos[8].Private, d.Repos[2].Private = true, true
	shapeDashRepos(&d, url.Values{"vis": {"private"}})
	got := renderDash(t, d)
	if order := boardOrder(got); !sameNames(order, "repo2", "repo8") {
		t.Errorf("FAIL: vis=private lists %v, want the pinned private one then the private rest", order)
	}
	if !strings.Contains(got[strings.Index(got, `data-hook="board-pinned"`):strings.Index(got, `data-hook="board-rest"`)], `data-repo="repo2"`) {
		t.Error("FAIL: the pinned private repository left the pinned group")
	}
	d = dashFixture()
	d.Repos[7].Pin = 1
	shapeDashRepos(&d, url.Values{"find": {"repo1"}})
	if order := boardOrder(renderDash(t, d)); !sameNames(order, "repo1") {
		t.Errorf("FAIL: find=repo1 lists %v: a pinned repository that does not match was kept", order)
	}
	fmt.Println("ok: filters narrow the pinned group and the rest")
}

// The pin is a real button in a small form that posts, so it works with
// no script, named for what it does and to what; with script the same
// form is enhanced to patch the board. The view rides in the form.
func TestHomePinIsAFormThatPosts(t *testing.T) {
	d := dashFixture()
	d.Repos = d.Repos[:2]
	d.Repos[1].Pin = 1
	shapeDashRepos(&d, url.Values{"vis": {"public"}, "kind": {"all"}, "find": {"repo"}})
	got := renderDash(t, d)
	rows := strings.Split(got, `<form class="pin"`)[1:]
	if len(rows) != 2 {
		t.Fatalf("FAIL: %d pin forms, want one a row", len(rows))
	}
	// The pinned row comes first, and its form unpins.
	unpin, pin := rows[0], rows[1]
	for name, c := range map[string]struct{ form, want, label string }{
		"unpin": {unpin, `name="pinned" value="false"`, `aria-label="unpin repo1"`},
		"pin":   {pin, `name="pinned" value="true"`, `aria-label="pin repo0"`},
	} {
		for _, want := range []string{`method="post"`, `action="/home/pin"`, `data-hook="pin-form"`,
			`data-on:submit__prevent="@post('/home/pin', {contentType: 'form'})"`,
			`<button class="pin-btn" type="submit" data-hook="pin-toggle"`,
			c.want, c.label,
			`name="find" value="repo"`, `name="vis" value="public"`, `name="kind" value="all"`} {
			if !strings.Contains(c.form[:strings.Index(c.form, "</form>")], want) {
				t.Errorf("FAIL: the %s form has no %s", name, want)
			}
		}
	}
	if !strings.Contains(unpin[:strings.Index(unpin, "</form>")], `data-pinned="true"`) ||
		strings.Contains(pin[:strings.Index(pin, "</form>")], `data-pinned`) {
		t.Error("FAIL: only the pinned row's button is marked pinned")
	}
	if strings.Contains(got, "data-on-submit") || strings.Contains(got, "data-on-click") {
		t.Error("FAIL: a dash-spelled event Datastar never fires")
	}
	// At the default view the form carries no filter of its own.
	d = dashFixture()
	d.Repos = d.Repos[:1]
	shapeDashRepos(&d, nil)
	if f := strings.Split(renderDash(t, d), `<form class="pin"`)[1]; strings.Contains(f[:strings.Index(f, "</form>")], `name="kind"`) {
		t.Error("FAIL: a default view carries a kind it does not need")
	}
	fmt.Println("ok: the pin is a button in a posting form")
}

// Each row's second line is the newest event in the repository, linked to
// it; a repository with nothing yet has no such line.
func TestHomeBoardRowsCarryTheNewestEvent(t *testing.T) {
	d := dashData{User: "dan", Repos: []dashRepoRow{
		{Name: "alpha", Latest: &dashEvent{Repo: "alpha", Kind: "l", Ref: strings.Repeat("c", 40), LandingNumber: 12, Comment: "Landing: do a thing", User: "eve"}},
		{Name: "beta", Latest: &dashEvent{Repo: "beta", Kind: "t", Ref: strings.Repeat("d", 40), Comment: "a ticket", User: "dan"}},
		{Name: "gamma"},
	}}
	shapeDashRepos(&d, nil)
	got := renderDash(t, d)
	if n := strings.Count(got, `data-hook="board-event"`); n != 2 {
		t.Fatalf("FAIL: %d event lines, want 2 (gamma has none)", n)
	}
	for _, want := range []string{`data-kind="l"`, `href="/r/alpha/landing/cccccccccccc"`, `data-kind="t"`, `href="/r/beta/ticket/dddddddddd"`} {
		if !strings.Contains(got, want) {
			t.Errorf("FAIL: the board has no %s", want)
		}
	}
	// The pin of the row that has a second line spans both lines; the one
	// that has none does not reach into the next row.
	if n := strings.Count(got, `rowspan="2"`); n != 2 {
		t.Errorf("FAIL: %d pin cells span two lines, want the two that have an event", n)
	}
	// Events come after their own row and before the next repository's.
	a, ev, b := strings.Index(got, `data-repo="alpha"`), strings.Index(got, `data-kind="l"`), strings.Index(got, `data-repo="beta"`)
	if !(a < ev && ev < b) {
		t.Errorf("FAIL: alpha's event is at %d, between rows at %d and %d", ev, a, b)
	}
	fmt.Println("ok: each row carries its newest event")
}

// What is waiting on you heads the activity page's ticket changes.
func TestWaitingOnYouHeadsTicketChanges(t *testing.T) {
	d := dashFixture()
	d.Kind, d.Activity = "t", true
	got := renderDash(t, d)
	aw, feed := strings.Index(got, `data-hook="dash-awaiting"`), strings.Index(got, `id="dash-feed"`)
	if aw < 0 || aw < feed {
		t.Fatalf("FAIL: the waiting tickets are not inside the feed under ticket changes (%d, %d)", aw, feed)
	}
	fmt.Println("ok: waiting on you sits under ticket changes")
}

// On a narrow column check-ins, landings and technote edits fold into
// one chip; the fold is current whenever one of its parts is.
func TestCodeChipFoldsItsParts(t *testing.T) {
	d := dashFixture()
	d.Kind, d.Activity = "ci", true
	for _, k := range []string{"code", "ci", "t", "e", "l"} {
		d.Kinds = append(d.Kinds, struct {
			Kind  string `json:"kind"`
			Count int    `json:"count"`
		}{k, 1})
	}
	got := renderDash(t, d)
	if n := strings.Count(got, `k-part"`); n != 3 {
		t.Errorf("FAIL: %d chips fold into code, want check-ins, landings and technotes", n)
	}
	if !strings.Contains(got, `class="here k-code"`) {
		t.Error("FAIL: the code chip is not current while one of its parts is")
	}
	fmt.Println("ok: three chips fold into code")
}

// The strip is repositories, teams, activity, in that order, with the
// page's own tab current; the Teams tab is there only for an account in
// a team, and Activity is its own address.
func TestHomeTabsAreRepositoriesTeamsActivity(t *testing.T) {
	d := dashFixture()
	shapeDashRepos(&d, nil)
	got := renderDash(t, d)
	repos, teams, act := strings.Index(got, `data-hook="home-tab-repos"`),
		strings.Index(got, `data-hook="home-tab-teams"`), strings.Index(got, `data-hook="home-tab-activity"`)
	if !(0 <= repos && repos < teams && teams < act) {
		t.Fatalf("FAIL: tabs at %d, %d, %d: not repositories, teams, activity", repos, teams, act)
	}
	cur := func(body, hook string) bool {
		i := strings.Index(body, `data-hook="`+hook+`"`)
		tag := body[i : i+strings.Index(body[i:], ">")]
		return strings.Contains(tag, `aria-current="page"`)
	}
	if !cur(got, "home-tab-repos") || cur(got, "home-tab-teams") || cur(got, "home-tab-activity") {
		t.Error("FAIL: repositories is not the one current tab")
	}
	for _, want := range []string{`href="/home"`, `href="/home?tab=teams"`, `href="/activity"`} {
		if !strings.Contains(got, want) {
			t.Errorf("FAIL: the strip has no %s", want)
		}
	}
	// The Teams tab shows every team, not the four the old column did.
	d.Tab = "teams"
	got = renderDash(t, d)
	if !cur(got, "home-tab-teams") || cur(got, "home-tab-repos") {
		t.Error("FAIL: Teams is not the current tab on tab=teams")
	}
	if n := strings.Count(got, `class="dash-title"`); n != 6 {
		t.Errorf("FAIL: %d teams shown, want all 6", n)
	}
	if strings.Contains(got, `data-hook="board"`) {
		t.Error("FAIL: the Teams tab still draws the board")
	}
	// Activity wears the strip too, with its own tab current.
	d.Tab, d.Activity = "", true
	got = renderDash(t, d)
	if !cur(got, "home-tab-activity") || cur(got, "home-tab-repos") ||
		strings.Index(got, `data-hook="home-tabs"`) > strings.Index(got, `id="dash-feed"`) {
		t.Error("FAIL: the activity page has no strip above it with Activity current")
	}
	// No team, no Teams tab.
	d = dashFixture()
	d.Teams = nil
	if got = renderDash(t, d); strings.Contains(got, `data-hook="home-tab-teams"`) || strings.Contains(got, `href="/home?tab=teams"`) {
		t.Error("FAIL: a reader in no team is shown a Teams tab")
	}
	fmt.Println("ok: repositories, teams, activity; teams only when there is one")
}