package web
import (
"strings"
"testing"
)
// A repository cloned but never fetched renders "0 fetches", not a gap
// where the number should be.
//
// The nav's count helper renders a zero as nothing on purpose — a badge
// showing "0" claims the tab is empty. On a totals line that same rule
// produced "3 clones fetches 1 identified", which is not a sentence.
func TestAZeroTotalIsPrintedNotSwallowed(t *testing.T) {
var buf strings.Builder
err := trafficPage.ExecuteTemplate(&buf, "content", pageData{
Repo: "alpha", TrafficVerb: "clone",
Traffic: trafficView{
Repo: "alpha", Clones: 3, Fetches: 0, Identified: 0,
Readers: []trafficRow{{Label: "unidentified", Count: 3, Pct: 100, Kind: "anon"}},
},
})
if err != nil {
t.Fatal(err)
}
got := buf.String()
for _, want := range []string{">0</b> fetches", ">0</b> identified", ">3</b> clones"} {
if !strings.Contains(got, want) {
t.Errorf("FAIL: the totals line is missing %q", want)
}
}
}
// And a big number stays readable rather than being rounded into a
// different number: this line is the exact figure, not a badge.
func TestABigTotalKeepsItsDigits(t *testing.T) {
var buf strings.Builder
if err := trafficPage.ExecuteTemplate(&buf, "content", pageData{
Repo: "alpha", TrafficVerb: "clone",
Traffic: trafficView{Repo: "alpha", Clones: 341, Fetches: 12486, Identified: 46},
}); err != nil {
t.Fatal(err)
}
if !strings.Contains(buf.String(), ">12,486</b> fetches") {
t.Error("FAIL: 12486 fetches did not render as an exact, grouped figure")
}
}