summaryrefslogtreecommitdiff
path: root/internal/showcase/rank_history_test.go
blob: f14d23a602ac5f0c1fdb98eeed989c377b82f49f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package showcase

import (
	"path/filepath"
	"strings"
	"testing"
	"time"
)

func TestMovementArrow(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name    string
		current int
		older   int
		want    string
	}{
		{name: "same spot", current: 3, older: 3, want: "→"},
		{name: "improved", current: 2, older: 5, want: "↑"},
		{name: "worse", current: 6, older: 4, want: "↓"},
		{name: "missing older", current: 2, older: 0, want: "·"},
		{name: "missing current", current: 0, older: 2, want: "·"},
	}

	for _, tc := range tests {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			got := movementArrow(tc.current, tc.older)
			if got != tc.want {
				t.Fatalf("movementArrow(%d,%d) = %q, want %q", tc.current, tc.older, got, tc.want)
			}
		})
	}
}

func TestComputeRepoHistory_LatestSnapshotAtOrBeforeAnchor(t *testing.T) {
	t.Parallel()

	store := &RankHistoryStore{
		Version: rankHistoryVersion,
		Snapshots: []RankSnapshot{
			{Date: "2026-01-18", Ranks: map[string]int{"alpha": 4}},
			{Date: "2026-02-01", Ranks: map[string]int{"alpha": 3}},
			{Date: "2026-02-15", Ranks: map[string]int{"alpha": 2}},
			{Date: "2026-02-22", Ranks: map[string]int{"alpha": 1}},
		},
	}

	anchor, _ := time.Parse("2006-01-02", "2026-02-22")
	history := computeRepoHistory(store, "alpha", anchor, 5)

	wantSpots := []int{1, 2, 3, 3, 4}
	if len(history) != len(wantSpots) {
		t.Fatalf("history len = %d, want %d", len(history), len(wantSpots))
	}
	for i, want := range wantSpots {
		if history[i].Spot != want {
			t.Fatalf("history[%d].Spot = %d, want %d", i, history[i].Spot, want)
		}
	}
}

func TestComputeRepoHistory_UsesNAWhenNoRepoSpotAtAnchor(t *testing.T) {
	t.Parallel()

	store := &RankHistoryStore{
		Version: rankHistoryVersion,
		Snapshots: []RankSnapshot{
			{Date: "2026-02-22", Ranks: map[string]int{"alpha": 1}},
			{Date: "2026-02-15", Ranks: map[string]int{"beta": 3}},
		},
	}

	anchor, _ := time.Parse("2006-01-02", "2026-02-22")
	history := computeRepoHistory(store, "alpha", anchor, 2)

	if history[0].Spot != 1 {
		t.Fatalf("history[0].Spot = %d, want 1", history[0].Spot)
	}
	if history[1].Spot != 0 {
		t.Fatalf("history[1].Spot = %d, want 0", history[1].Spot)
	}
	if history[1].Arrow != "·" {
		t.Fatalf("history[1].Arrow = %q, want %q", history[1].Arrow, "·")
	}
}

func TestUpsertSnapshotForDate_Idempotent(t *testing.T) {
	t.Parallel()

	store := newRankHistoryStore()
	anchor, _ := time.Parse("2006-01-02", "2026-02-22")

	upsertSnapshotForDate(store, anchor, map[string]int{"a": 1})
	upsertSnapshotForDate(store, anchor, map[string]int{"a": 2, "b": 1})

	if len(store.Snapshots) != 1 {
		t.Fatalf("len(store.Snapshots) = %d, want 1", len(store.Snapshots))
	}
	if got := store.Snapshots[0].Ranks["a"]; got != 2 {
		t.Fatalf("store.Snapshots[0].Ranks[\"a\"] = %d, want 2", got)
	}
}

func TestRankHistoryReadWriteRoundTrip(t *testing.T) {
	t.Parallel()

	tmp := t.TempDir()
	path := filepath.Join(tmp, rankHistoryFilename)
	store := &RankHistoryStore{
		Version: rankHistoryVersion,
		Snapshots: []RankSnapshot{
			{Date: "2026-02-22", Ranks: map[string]int{"alpha": 1}},
		},
	}
	if err := saveRankHistory(path, store); err != nil {
		t.Fatalf("saveRankHistory() error = %v", err)
	}

	got, err := loadRankHistory(path)
	if err != nil {
		t.Fatalf("loadRankHistory() error = %v", err)
	}
	if got.Version != rankHistoryVersion {
		t.Fatalf("got.Version = %d, want %d", got.Version, rankHistoryVersion)
	}
	if len(got.Snapshots) != 1 {
		t.Fatalf("len(got.Snapshots) = %d, want 1", len(got.Snapshots))
	}
}

func TestFormatRankHistoryForHeader(t *testing.T) {
	t.Parallel()

	header := formatRankHistoryForHeader([]RepoRankHistory{
		{Spot: 3, Anchor: "now"},
		{Spot: 2, Anchor: "1w", Arrow: "↓"},
		{Spot: 2, Anchor: "2w", Arrow: "→"},
		{Spot: 0, Anchor: "3w", Arrow: "·"},
	})

	if !strings.Contains(header, "#3(now)") {
		t.Fatalf("header missing current spot: %s", header)
	}
	if !strings.Contains(header, "↓#2(1w)") {
		t.Fatalf("header missing down movement: %s", header)
	}
	if strings.Contains(header, "n/a") {
		t.Fatalf("header should omit missing history points: %s", header)
	}
}