Skip to content
quarry Artifact [b91d92b167]
Login

Artifact [b91d92b167]

Artifact b91d92b16761d7ad4f77ca8f71adafa85daf9fad68f2d16ba51f27e1140d06ff:


package web

import (
	"strings"
	"testing"
)

// The cross-repository listing opens on what the reader is assigned, so
// that view carries no parameter and leaving it does. A repository's own
// tracker has no such default and must never draw the parameter at all.
func TestAssignedIsTheAcrossListingsDefault(t *testing.T) {
	across := ticketNav{Base: "/tickets", Mine: true}
	if got := across.URL("", ""); got != "/tickets" {
		t.Errorf("the default view is not the plain URL: %q", got)
	}
	if got := across.URL("mine", "0"); !strings.Contains(got, "mine=0") {
		t.Errorf("leaving the default does not say so: %q", got)
	}
	// Back in again, from the way out.
	out := across
	out.Mine = false
	if got := out.URL("mine", "1"); got != "/tickets" {
		t.Errorf("returning to the default is not the plain URL: %q", got)
	}
	// One repository's tracker: no mine, either way round.
	repo := ticketNav{Repo: "alpha"}
	for _, n := range []ticketNav{repo, {Repo: "alpha", Mine: true}} {
		if got := n.URL("", ""); strings.Contains(got, "mine") {
			t.Errorf("a tracker's own listing carries mine: %q", got)
		}
	}
}

// The rail names both sides of the default, and names neither on a
// tracker that has no default to leave.
func TestTheRailNamesBothSidesOfAssigned(t *testing.T) {
	labels := func(n ticketNav, me string) []string {
		t.Helper()
		var out []string
		for _, sec := range ticketRail(n, nil, nil, nil, me, nil) {
			for _, l := range sec.Links {
				out = append(out, l.Label)
			}
		}
		return out
	}
	has := func(ls []string, want string) bool {
		for _, l := range ls {
			if l == want {
				return true
			}
		}
		return false
	}
	across := labels(ticketNav{Base: "/tickets", Mine: true}, "dan")
	if !has(across, "assigned to you") || !has(across, "every ticket you can reach") {
		t.Errorf("the rail does not name both ways: %v", across)
	}
	repo := labels(ticketNav{Repo: "alpha"}, "dan")
	if has(repo, "assigned to you") {
		t.Errorf("a tracker's rail offers an assigned view: %v", repo)
	}
	// Signed out there is no queue to be assigned one.
	if anon := labels(ticketNav{Base: "/tickets", Mine: true}, ""); has(anon, "assigned to you") {
		t.Errorf("a signed-out rail offers assigned to you: %v", anon)
	}
}

// An empty assigned view says why it is empty and offers the way out,
// rather than reading as "this forge has no tickets".
func TestTheEmptyAssignedViewExplainsItself(t *testing.T) {
	var buf strings.Builder
	d := pageData{Title: "Tickets", Across: true,
		Nav: ticketNav{Base: "/tickets", Mine: true}}
	if err := ticketListPage.ExecuteTemplate(&buf, "tkt-view", d); err != nil {
		t.Fatal(err)
	}
	body := buf.String()
	if !strings.Contains(body, "assigned to you") {
		t.Errorf("the empty view does not say nothing is assigned: %s", body)
	}
	if !strings.Contains(body, "mine=0") {
		t.Errorf("the empty view offers no way to everything else")
	}
}