Skip to content
quarry Artifact [c87ad60953]
Login

Artifact [c87ad60953]

Artifact c87ad609539c83889fc4846d1a5ed04650e0bb8a813b80a6613138bfc93b5a23:


package web

import (
	"bytes"
	"fmt"
	"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()
}

func dashFixture() dashData {
	d := dashData{User: "dmestas"}
	for i := 0; i < 7; i++ {
		d.Repos = append(d.Repos, dashRepoRow{Name: fmt.Sprintf("repo%d", i)})
	}
	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
}

// The home leads with where you work: your repositories, the four most
// recently active and a link to the rest, then your teams, then the
// feed. Nothing waits in a band above them.
func TestHomeLeadsWithYourRepositoriesThenTeams(t *testing.T) {
	got := renderDash(t, dashFixture())
	repos, teams, feed := strings.Index(got, `data-hook="dash-repos"`),
		strings.Index(got, `data-hook="dash-teams"`), strings.Index(got, `id="dash-feed"`)
	if repos < 0 || teams < 0 || feed < 0 || !(repos < teams && teams < feed) {
		t.Fatalf("FAIL: want repositories, then teams, then the feed; at %d, %d, %d", repos, teams, feed)
	}
	if n := strings.Count(got[repos:teams], `class="dash-title"`); n != 4 {
		t.Errorf("FAIL: %d repositories shown, want the 4 most recent", n)
	}
	if !strings.Contains(got, `data-hook="dash-all-repos"`) {
		t.Error("FAIL: no way to the rest of the reader's repositories")
	}
	if n := strings.Count(got[teams:feed], `class="dash-title"`); n != 4 {
		t.Errorf("FAIL: %d teams shown, want 4", n)
	}
	if strings.Contains(got, `data-hook="dash-awaiting"`) {
		t.Error("FAIL: what is waiting shows outside the ticket changes")
	}
	if strings.Contains(got, "dash-mine") {
		t.Error("FAIL: the reader's recent work is still on the home")
	}
	fmt.Println("ok: repositories (4 + all), teams (4), then the feed")
}

// Asked for all of them, the list is whole and offers no second link.
func TestHomeShowsEveryRepositoryWhenAsked(t *testing.T) {
	d := dashFixture()
	d.AllRepos = true
	got := renderDash(t, d)
	repos, teams := strings.Index(got, `data-hook="dash-repos"`), strings.Index(got, `data-hook="dash-teams"`)
	if n := strings.Count(got[repos:teams], `class="dash-title"`); n != 7 {
		t.Errorf("FAIL: %d repositories shown with repos=all, want 7", n)
	}
	if strings.Contains(got, `data-hook="dash-all-repos"`) {
		t.Error("FAIL: the whole list still links to itself")
	}
	fmt.Println("ok: repos=all shows every repository")
}

// What is waiting on you heads the feed's ticket changes.
func TestWaitingOnYouHeadsTicketChanges(t *testing.T) {
	d := dashFixture()
	d.Kind = "t"
	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 = "ci"
	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")
}