internal/httpd/activity_test.go

v1.40.0
gitbay/internal/httpd/activity_test.go history · blame · raw

53 lines · 1298 bytes

 1package httpd
 2
 3import (
 4	"testing"
 5	"time"
 6)
 7
 8// TestActivityGridMonthLabels checks the month label activityGrid attaches
 9// to each week: at most one label per calendar month, always on the week
10// whose first non-pad day falls in the first 7 days of that month, and
11// never the same month as the immediately preceding label.
12func TestActivityGridMonthLabels(t *testing.T) {
13	weeks, _ := activityGrid(nil)
14
15	labelled := 0
16	prev := ""
17	for _, w := range weeks {
18		if w.Month == "" {
19			continue
20		}
21		labelled++
22		if w.Month == prev {
23			t.Fatalf("consecutive labelled weeks repeat month %q", w.Month)
24		}
25		prev = w.Month
26
27		var first activityDay
28		found := false
29		for _, d := range w.Days {
30			if !d.Pad {
31				first = d
32				found = true
33				break
34			}
35		}
36		if !found {
37			t.Fatalf("week labelled %q has no non-pad day", w.Month)
38		}
39		day, err := time.Parse("2006-01-02", first.Date)
40		if err != nil {
41			t.Fatalf("bad date %q: %v", first.Date, err)
42		}
43		if day.Day() > 7 {
44			t.Fatalf("week labelled %q but first day is day %d of the month", w.Month, day.Day())
45		}
46		if got := day.Month().String()[:3]; got != w.Month {
47			t.Fatalf("label %q does not match month %q of first day", w.Month, got)
48		}
49	}
50	if labelled == 0 {
51		t.Fatal("expected at least one month label across 53 weeks")
52	}
53}