-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_test.go
More file actions
201 lines (173 loc) · 5.11 KB
/
Copy pathdetect_test.go
File metadata and controls
201 lines (173 loc) · 5.11 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
190
191
192
193
194
195
196
197
198
199
200
201
package ditzy
import (
"testing"
"time"
)
func TestFromEvents(t *testing.T) {
// Create events that simulate a UTC-7 timezone user
// Active 9am-5pm local = 16:00-00:00 UTC
now := time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC)
var events []Event
// Simulate a week of activity
for day := range 7 {
dayStart := now.AddDate(0, 0, day)
// Work hours: 9am-12pm local = 16:00-19:00 UTC (for UTC-7)
for hour := 16; hour <= 19; hour++ {
for i := range 3 { // 3 events per hour
events = append(events, Event{
Time: dayStart.Add(time.Duration(hour)*time.Hour + time.Duration(i*15)*time.Minute),
Type: "commit",
})
}
}
// Work hours: 1pm-5pm local = 20:00-00:00 UTC
for hour := 20; hour <= 23; hour++ {
for i := range 3 {
events = append(events, Event{
Time: dayStart.Add(time.Duration(hour)*time.Hour + time.Duration(i*15)*time.Minute),
Type: "commit",
})
}
}
}
result := FromEvents(events)
if len(result.Candidates) == 0 {
t.Fatal("Expected at least one candidate")
}
t.Logf("Top candidate: %s (offset %.0fh) with confidence %.2f",
result.Candidates[0].Timezone,
result.Candidates[0].Hours(),
result.Candidates[0].Confidence)
t.Logf("Analysis: %d events, peak %.1f-%.1f UTC",
result.Analysis.TotalEvents,
result.Analysis.PeakStartUTC,
result.Analysis.PeakEndUTC)
}
func TestFromEventsWithOptions(t *testing.T) {
now := time.Date(2025, 1, 15, 14, 0, 0, 0, time.UTC)
events := []Event{
{Time: now, Type: "commit"},
{Time: now.Add(time.Hour), Type: "commit"},
}
result := FromEvents(events,
WithClaimedTimezone("America/New_York"),
WithUsername("testuser"),
)
if len(result.Candidates) == 0 {
t.Fatal("Expected at least one candidate")
}
}
func TestFromEventsWithPreservedTZ(t *testing.T) {
// Create events with preserved timezone info
pst := time.FixedZone("", -8*3600) // UTC-8
now := time.Date(2025, 1, 15, 10, 0, 0, 0, pst)
var events []Event
for i := range 20 {
events = append(events, Event{
Time: now.Add(time.Duration(i) * time.Hour),
Type: "commit",
})
}
result := FromEvents(events)
// Should detect preserved timezone from events
if result.Analysis.PreservedTZCount != 20 {
t.Errorf("Expected 20 preserved TZ events, got %d", result.Analysis.PreservedTZCount)
}
}
func TestFromEventsEmpty(t *testing.T) {
result := FromEvents(nil)
if len(result.Candidates) != 0 {
t.Errorf("Expected empty candidates for nil events, got %d", len(result.Candidates))
}
result = FromEvents([]Event{})
if len(result.Candidates) != 0 {
t.Errorf("Expected empty candidates for empty events, got %d", len(result.Candidates))
}
}
func TestFromBuckets(t *testing.T) {
// Simulate activity from 9am-5pm for someone in UTC-5
// That's 14:00-22:00 UTC
buckets := map[float64]int{
14.0: 5, 14.5: 5,
15.0: 8, 15.5: 8,
16.0: 10, 16.5: 10,
17.0: 12, 17.5: 10,
18.0: 8, 18.5: 8,
19.0: 10, 19.5: 12, // Peak
20.0: 8, 20.5: 6,
21.0: 4, 21.5: 2,
}
result := FromBuckets(buckets)
if len(result.Candidates) == 0 {
t.Fatal("Expected at least one candidate")
}
t.Logf("Top candidate: %s (offset %.0fh) with confidence %.2f",
result.Candidates[0].Timezone,
result.Candidates[0].Hours(),
result.Candidates[0].Confidence)
t.Logf("Analysis: peak %.1f-%.1f UTC, lunch %.1f-%.1f UTC",
result.Analysis.PeakStartUTC,
result.Analysis.PeakEndUTC,
result.Analysis.LunchStartUTC,
result.Analysis.LunchEndUTC)
}
func TestFromBucketsEmpty(t *testing.T) {
result := FromBuckets(nil)
if len(result.Candidates) != 0 {
t.Errorf("Expected empty candidates for nil buckets, got %d", len(result.Candidates))
}
result = FromBuckets(map[float64]int{})
if len(result.Candidates) != 0 {
t.Errorf("Expected empty candidates for empty buckets, got %d", len(result.Candidates))
}
}
func TestEventHasPreservedTZ(t *testing.T) {
tests := []struct {
time time.Time
name string
wantPres bool
}{
{
name: "Zulu time (UTC zone name)",
time: time.Date(2025, 1, 15, 12, 0, 0, 0, time.UTC),
wantPres: false,
},
{
name: "Explicit +00:00 offset",
time: time.Date(2025, 1, 15, 12, 0, 0, 0, time.FixedZone("", 0)),
wantPres: true, // "" zone name, not "UTC"
},
{
name: "PST offset",
time: time.Date(2025, 1, 15, 12, 0, 0, 0, time.FixedZone("", -8*3600)),
wantPres: true,
},
{
name: "India offset",
time: time.Date(2025, 1, 15, 12, 0, 0, 0, time.FixedZone("", 5*3600+30*60)),
wantPres: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := Event{Time: tt.time, Type: "commit"}
if got := e.hasPreservedTZ(); got != tt.wantPres {
zoneName, offset := tt.time.Zone()
t.Errorf("hasPreservedTZ() = %v, want %v (zone=%q, offset=%d)",
got, tt.wantPres, zoneName, offset)
}
})
}
}
func TestAnalysisHasBuckets(t *testing.T) {
buckets := map[float64]int{
14.0: 5, 15.0: 8, 16.0: 10,
}
result := FromBuckets(buckets)
if result.Analysis.HalfHourCounts == nil {
t.Error("Expected HalfHourCounts to be populated")
}
if len(result.Analysis.HalfHourCounts) != 3 {
t.Errorf("Expected 3 buckets, got %d", len(result.Analysis.HalfHourCounts))
}
}