showcase
- v0.2.4
- 37 commits
- 1 branch
- 6 tags
- updated
- CI 2/2
activity_test.go
4.1 KB · 125 lines
1package render23import (4 "regexp"5 "strconv"6 "strings"7 "testing"8 "time"9)1011var (12 testEnd = time.Date(2026, 11, 21, 12, 0, 0, 0, time.UTC) // a Saturday13 reViewBox = regexp.MustCompile(`viewBox="0 0 (\d+) `)14 reText = regexp.MustCompile(`<text class="([^"]+)" x="([\d.]+)" y="(\d+)" text-anchor="(\w+)">(.*?)</text>`)15)1617func atoi(t *testing.T, s string) float64 {18 t.Helper()19 f, err := strconv.ParseFloat(s, 64)20 if err != nil {21 t.Fatal(err)22 }23 return f24}2526func find(t *testing.T, re, s string) []string {27 t.Helper()28 m := regexp.MustCompile(re).FindStringSubmatch(s)29 if m == nil {30 t.Fatalf("no match for %s", re)31 }32 return m33}3435func TestBarsShareHeatmapColumns(t *testing.T) {36 day := time.Date(2026, 10, 20, 9, 0, 0, 0, time.UTC) // week of Oct 1837 counts := Weeks([]time.Time{day}, testEnd, weeks)38 for _, n := range []int{weeks, 26} {39 heat, _ := Heatmap(map[string]int{"2026-10-20": 1}, testEnd, n)40 bars := string(ActivityBars(counts, testEnd, 0, n))41 hx := find(t, `<rect class="h\d" x="(\d+)"[^>]*><title>1 commit on Oct 20, 2026`, string(heat))[1]42 bx := find(t, `week of Oct 18 – Oct 24</title></rect><path class="bars-bar h4[^"]*" d="M(\d+) `, bars)[1]43 if hx != bx {44 t.Errorf("n=%d: bar at x=%s, heatmap column at x=%s", n, bx, hx)45 }46 if hw, bw := reViewBox.FindStringSubmatch(string(heat))[1], reViewBox.FindStringSubmatch(bars)[1]; hw != bw {47 t.Errorf("n=%d: viewBox widths %s and %s", n, hw, bw)48 }49 }50}5152func TestYAxis(t *testing.T) {53 for m := 0; m <= 2000; m++ {54 step, top := yAxis(m)55 if lines := top/step + 1; lines < 2 || lines > 3 || top < m || top%step != 0 {56 t.Fatalf("max %d: step %d, top %d", m, step, top)57 }58 }59}6061func TestStreakBracket(t *testing.T) {62 cases := []struct {63 name string64 from, to int // weeks with commits, inclusive65 n int66 wantStart int // bracket columns in the drawn chart67 wantEnd int68 wantStreak int69 }{70 {"this week empty", 45, 51, weeks, 45, 51, 7},71 {"to the right edge", 40, 52, weeks, 40, 52, 13},72 {"whole year", 0, 52, weeks, 1, 52, 52},73 {"clipped on small screens", 10, 52, 26, 0, 25, 43},74 {"one week, this week", 52, 52, weeks, 52, 52, 1},75 {"one week, last week", 51, 51, weeks, 51, 51, 1},76 }77 for _, c := range cases {78 counts := make([]int, weeks)79 for i := c.from; i <= c.to; i++ {80 counts[i] = 181 }82 streak := Streak(counts[1:])83 if streak != c.wantStreak {84 t.Fatalf("%s: Streak = %d", c.name, streak)85 }86 m := find(t, `class="bars-streak" d="M(\d+) \d+V\d+H(\d+)V`, string(ActivityBars(counts, testEnd, streak, c.n)))87 if x0, x1 := int(atoi(t, m[1])), int(atoi(t, m[2])); x0 != colX(c.wantStart) || x1 != colX(c.wantEnd)+cell {88 t.Errorf("%s: bracket %d–%d, want %d–%d", c.name, x0, x1, colX(c.wantStart), colX(c.wantEnd)+cell)89 }90 }91}9293// The labels stay inside the viewBox: axis values in the gutter, "this94// week" under the right end of the baseline.95func TestLabelsFit(t *testing.T) {96 for _, m := range []int{0, 1, 9, 15, 99, 1234} {97 counts := make([]int, weeks)98 counts[0], counts[weeks-1] = m, m99 for _, n := range []int{weeks, 26} {100 bars := string(ActivityBars(counts, testEnd, Streak(counts[1:]), n))101 w := atoi(t, reViewBox.FindStringSubmatch(bars)[1])102 for _, x := range reText.FindAllStringSubmatch(bars, -1) {103 width := float64(len([]rune(x[5]))) * 5.4 // overshoots Geist at 9px104 x0 := map[string]float64{"start": 0, "middle": width / 2, "end": width}[x[4]]105 if x0 = atoi(t, x[2]) - x0; x0 < 0 || x0+width > w || atoi(t, x[3]) > barsH-2 {106 t.Errorf("max %d, n=%d: %q at %.1f–%.1f, y=%s, outside 0–%v", m, n, x[5], x0, x0+width, x[3], w)107 }108 }109 }110 }111}112113func TestEmptyYear(t *testing.T) {114 bars := string(ActivityBars(make([]int, weeks), testEnd, 0, weeks))115 for _, s := range []string{"bars-streak", "bars-now", "h1", "h4"} {116 if strings.Contains(bars, s) {117 t.Errorf("empty year has %q", s)118 }119 }120 for _, s := range []string{`aria-label="No commits in the last year"`, `class="bars-empty"`, ">No commits in the last year<", "bars-grid", "bars-stub"} {121 if !strings.Contains(bars, s) {122 t.Errorf("empty year lacks %q", s)123 }124 }125}