-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis_test.go
More file actions
189 lines (159 loc) · 4.7 KB
/
analysis_test.go
File metadata and controls
189 lines (159 loc) · 4.7 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"testing"
"time"
)
func TestAnalyze(t *testing.T) {
commits := []Commit{
{
Hash: "aaa", Author: "Alice", Email: "alice@test.com",
Timestamp: makeTime(2024, 12, 15, 10),
FilesChanged: 3, Insertions: 45, Deletions: 12,
},
{
Hash: "bbb", Author: "Bob", Email: "bob@test.com",
Timestamp: makeTime(2024, 12, 14, 14),
FilesChanged: 1, Insertions: 5, Deletions: 2,
},
{
Hash: "ccc", Author: "Alice", Email: "alice@test.com",
Timestamp: makeTime(2024, 12, 13, 10),
FilesChanged: 7, Insertions: 120, Deletions: 80,
},
{
Hash: "ddd", Author: "Alice", Email: "alice@test.com",
Timestamp: makeTime(2024, 12, 13, 15),
FilesChanged: 2, Insertions: 10, Deletions: 5,
},
}
stats := Analyze(commits)
if stats.TotalCommits != 4 {
t.Errorf("expected 4 total commits, got %d", stats.TotalCommits)
}
if stats.TotalFiles != 13 {
t.Errorf("expected 13 total files, got %d", stats.TotalFiles)
}
if stats.TotalInsertions != 180 {
t.Errorf("expected 180 total insertions, got %d", stats.TotalInsertions)
}
if stats.TotalDeletions != 99 {
t.Errorf("expected 99 total deletions, got %d", stats.TotalDeletions)
}
if stats.ActiveDays != 3 {
t.Errorf("expected 3 active days, got %d", stats.ActiveDays)
}
// Author checks
if stats.AuthorCommits["Alice"] != 3 {
t.Errorf("expected Alice to have 3 commits, got %d", stats.AuthorCommits["Alice"])
}
if stats.AuthorCommits["Bob"] != 1 {
t.Errorf("expected Bob to have 1 commit, got %d", stats.AuthorCommits["Bob"])
}
// Hour checks
if stats.CommitsByHour[10] != 2 {
t.Errorf("expected 2 commits at hour 10, got %d", stats.CommitsByHour[10])
}
if stats.CommitsByHour[14] != 1 {
t.Errorf("expected 1 commit at hour 14, got %d", stats.CommitsByHour[14])
}
// Busiest day
if stats.BusiestDayCount != 2 {
t.Errorf("expected busiest day count 2, got %d", stats.BusiestDayCount)
}
// Averages
expectedAvgPerDay := float64(4) / float64(3)
if stats.AvgCommitsPerDay != expectedAvgPerDay {
t.Errorf("expected avg commits/day %.2f, got %.2f", expectedAvgPerDay, stats.AvgCommitsPerDay)
}
}
func TestAnalyzeEmpty(t *testing.T) {
stats := Analyze(nil)
if stats.TotalCommits != 0 {
t.Errorf("expected 0 commits, got %d", stats.TotalCommits)
}
}
func TestCalculateStreaks(t *testing.T) {
// Create commits for 5 consecutive days
now := time.Now()
commits := []Commit{
{Timestamp: now},
{Timestamp: now.AddDate(0, 0, -1)},
{Timestamp: now.AddDate(0, 0, -2)},
{Timestamp: now.AddDate(0, 0, -3)},
{Timestamp: now.AddDate(0, 0, -4)},
// Gap of 2 days
{Timestamp: now.AddDate(0, 0, -7)},
{Timestamp: now.AddDate(0, 0, -8)},
{Timestamp: now.AddDate(0, 0, -9)},
}
info := CalculateStreaks(commits)
if info.CurrentStreak != 5 {
t.Errorf("expected current streak 5, got %d", info.CurrentStreak)
}
if info.LongestStreak != 5 {
t.Errorf("expected longest streak 5, got %d", info.LongestStreak)
}
if info.TotalActiveDays != 8 {
t.Errorf("expected 8 active days, got %d", info.TotalActiveDays)
}
}
func TestCalculateStreaksNoRecent(t *testing.T) {
// Commits from 2 weeks ago — no current streak
old := time.Now().AddDate(0, 0, -14)
commits := []Commit{
{Timestamp: old},
{Timestamp: old.AddDate(0, 0, -1)},
{Timestamp: old.AddDate(0, 0, -2)},
}
info := CalculateStreaks(commits)
if info.CurrentStreak != 0 {
t.Errorf("expected current streak 0, got %d", info.CurrentStreak)
}
if info.LongestStreak != 3 {
t.Errorf("expected longest streak 3, got %d", info.LongestStreak)
}
}
func TestCalculateStreaksEmpty(t *testing.T) {
info := CalculateStreaks(nil)
if info.TotalActiveDays != 0 {
t.Errorf("expected 0 active days, got %d", info.TotalActiveDays)
}
}
func TestTopAuthors(t *testing.T) {
authorCommits := map[string]int{
"Alice": 50,
"Bob": 30,
"Charlie": 80,
"Dave": 10,
}
top := TopAuthors(authorCommits, 2)
if len(top) != 2 {
t.Fatalf("expected 2 top authors, got %d", len(top))
}
if top[0].Name != "Charlie" || top[0].Commits != 80 {
t.Errorf("expected Charlie(80) first, got %s(%d)", top[0].Name, top[0].Commits)
}
if top[1].Name != "Alice" || top[1].Commits != 50 {
t.Errorf("expected Alice(50) second, got %s(%d)", top[1].Name, top[1].Commits)
}
}
func TestPeakHour(t *testing.T) {
var hours [24]int
hours[9] = 15
hours[14] = 20
hours[22] = 5
hour, count := PeakHour(hours)
if hour != 14 || count != 20 {
t.Errorf("expected peak hour 14(20), got %d(%d)", hour, count)
}
}
func TestPeakDay(t *testing.T) {
var weekdays [7]int
weekdays[1] = 30 // Monday
weekdays[3] = 40 // Wednesday
weekdays[5] = 25 // Friday
day, count := PeakDay(weekdays)
if day != time.Wednesday || count != 40 {
t.Errorf("expected Wednesday(40), got %s(%d)", day, count)
}
}