-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_test.go
More file actions
390 lines (352 loc) · 11.4 KB
/
evaluate_test.go
File metadata and controls
390 lines (352 loc) · 11.4 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package ditzy
import (
"strings"
"testing"
)
// TestDetectPeakProductivity tests the peak detection function.
func TestDetectPeakProductivity(t *testing.T) {
t.Run("Empty map returns no peak", func(t *testing.T) {
start, end, count := detectPeakProductivity(map[float64]int{})
if start != -1 || end != -1 || count != 0 {
t.Errorf("Expected no peak for empty map, got start=%v, end=%v, count=%v", start, end, count)
}
})
t.Run("Single bucket returns that bucket", func(t *testing.T) {
halfHourCounts := map[float64]int{
12.0: 10,
}
start, end, count := detectPeakProductivity(halfHourCounts)
if start != 12.0 || end != 12.5 || count != 10 {
t.Errorf("Expected start=12.0, end=12.5, count=10, got start=%v, end=%v, count=%v", start, end, count)
}
})
t.Run("Multiple buckets returns highest", func(t *testing.T) {
halfHourCounts := map[float64]int{
12.0: 10,
13.0: 25,
14.0: 15,
}
start, end, count := detectPeakProductivity(halfHourCounts)
if start != 13.0 || end != 13.5 || count != 25 {
t.Errorf("Expected start=13.0, end=13.5, count=25, got start=%v, end=%v, count=%v", start, end, count)
}
})
}
// TestEvaluate tests the timezone candidate evaluation function.
func TestEvaluate(t *testing.T) {
// Test with realistic activity pattern - someone working 9am-5pm EST
hourCounts := map[int]int{
13: 20, // 9am EST
14: 30, // 10am EST
15: 35, // 11am EST
16: 25, // 12pm EST - lunch dip
17: 20, // 1pm EST
18: 40, // 2pm EST - peak
19: 35, // 3pm EST
20: 30, // 4pm EST
21: 25, // 5pm EST
}
halfHourCounts := map[float64]int{
13.0: 10, 13.5: 10,
14.0: 15, 14.5: 15,
15.0: 18, 15.5: 17,
16.0: 10, 16.5: 15, // lunch dip at 12pm EST
17.0: 10, 17.5: 10,
18.0: 20, 18.5: 20, // peak at 2pm EST
19.0: 18, 19.5: 17,
20.0: 15, 20.5: 15,
21.0: 13, 21.5: 12,
}
totalActivity := 300
quietHours := []int{4, 5, 6, 7, 8, 9} // midnight-6am EST
midQuiet := 6.5
activeStart := 13.0 // 9am EST
// Lunch pattern at noon EST (16 UTC)
bestGlobalLunch := globalLunchPattern{
startUTC: 16.0,
endUTC: 17.0,
confidence: 0.8,
dropPercent: 30.0,
}
candidates := evaluate(evaluationInput{
username: "testuser",
hourCounts: hourCounts,
halfHourCounts: halfHourCounts,
totalActivity: totalActivity,
quietHours: quietHours,
midQuiet: midQuiet,
activeStart: activeStart,
bestGlobalLunch: bestGlobalLunch,
profileTimezone: "",
})
if len(candidates) == 0 {
t.Fatal("Expected at least one timezone candidate")
}
// Check that UTC-5 (EST) is among the top candidates
foundEST := false
for _, candidate := range candidates {
if candidate.offset == -5 {
foundEST = true
if candidate.confidence < 0.5 {
t.Errorf("Expected EST candidate to have confidence >= 0.5, got %v", candidate.confidence)
}
if !candidate.lunchReasonable {
t.Error("Expected EST candidate to have reasonable lunch timing")
}
break
}
}
if !foundEST {
t.Error("Expected UTC-5 (EST) to be among timezone candidates")
}
// Verify candidates are sorted by confidence (highest first)
for i := 1; i < len(candidates); i++ {
if candidates[i-1].confidence < candidates[i].confidence {
t.Errorf("Candidates not sorted by confidence: candidate %d has confidence %v < candidate %d confidence %v",
i-1, candidates[i-1].confidence, i, candidates[i].confidence)
}
}
}
// TestUKTimezoneDetection tests that UK users are correctly detected as UTC+0/UTC+1.
func TestUKTimezoneDetection(t *testing.T) {
// max-allan-cgr's actual activity pattern from the logs
// Active hours UTC: 08:00-17:00
// Sleep hours: [2 3 4 5 6 7 8 18 19 20 22 23]
hourCounts := map[int]int{
0: 0,
1: 2,
2: 1, // sleep
3: 0, // sleep
4: 0, // sleep
5: 0, // sleep
6: 0, // sleep
7: 0, // sleep
8: 2, // work starts
9: 10,
10: 21, // peak activity
11: 20,
12: 7, // lunch dip
13: 10,
14: 14,
15: 14,
16: 11,
17: 6, // work ends
18: 0, // quiet
19: 2, // quiet
20: 0, // quiet
21: 6, // some evening activity
22: 1, // quiet
23: 0, // sleep
}
// Half-hour resolution for lunch detection
halfHourCounts := map[float64]int{
0.0: 0, 0.5: 0,
1.0: 1, 1.5: 1,
2.0: 0, 2.5: 1,
3.0: 0, 3.5: 0,
4.0: 0, 4.5: 0,
5.0: 0, 5.5: 0,
6.0: 0, 6.5: 0,
7.0: 0, 7.5: 0,
8.0: 1, 8.5: 1,
9.0: 5, 9.5: 5,
10.0: 10, 10.5: 11, // Morning peak
11.0: 10, 11.5: 10,
12.0: 2, 12.5: 5, // Lunch dip at noon
13.0: 5, 13.5: 5,
14.0: 7, 14.5: 7,
15.0: 7, 15.5: 7,
16.0: 6, 16.5: 5,
17.0: 3, 17.5: 3,
18.0: 0, 18.5: 0, // Evening quiet
19.0: 1, 19.5: 1,
20.0: 0, 20.5: 0,
21.0: 3, 21.5: 3,
22.0: 0, 22.5: 1,
23.0: 0, 23.5: 0,
}
totalActivity := 127
quietHours := []int{2, 3, 4, 5, 6, 7, 8, 18, 19, 20, 22, 23}
midQuiet := 8.0 // As calculated in the actual implementation
activeStart := 8.0
// Best global lunch pattern at 12:00 UTC
bestGlobalLunch := globalLunchPattern{
startUTC: 12.0,
endUTC: 12.5,
confidence: 0.8,
dropPercent: 0.75,
}
candidates := evaluate(evaluationInput{
username: "max-allan-cgr",
hourCounts: hourCounts,
halfHourCounts: halfHourCounts,
totalActivity: totalActivity,
quietHours: quietHours,
midQuiet: midQuiet,
activeStart: activeStart,
bestGlobalLunch: bestGlobalLunch,
profileTimezone: "",
})
// Test 1: UTC+0 should be the top candidate
if len(candidates) == 0 {
t.Fatal("No candidates returned")
}
topCandidate := candidates[0]
if topCandidate.offset != 0 {
t.Errorf("Expected UTC+0 as top candidate, got UTC%+.0f", topCandidate.offset)
}
// Test 2: UTC+0 should have confidence > 65% (after 1.5x scaling, reduced from 70% after balancing geographic scoring)
if topCandidate.confidence < 65 {
t.Errorf("UTC+0 confidence too low: %.1f%%, expected > 65%%", topCandidate.confidence)
}
// Test 3: UTC+0 should have reasonable work hours (10am start based on activity data)
// The data shows minimal activity at 8-9am (1-2 events) but significant activity from 10am (10+ events)
// WorkStartUTC is stored in UTC, convert to local for UTC+0
workStartLocal := float64(int(topCandidate.workStartUTC+topCandidate.offset+24) % 24)
if workStartLocal != 10 {
t.Errorf("UTC+0 work start incorrect: %.1f, expected 10 (based on actual activity pattern)", workStartLocal)
}
// Test 4: UTC+0 should have lunch detected at noon
// LunchStartUTC is in UTC, convert to local for UTC+0
lunchLocalTime := float64(int(topCandidate.lunchStartUTC+topCandidate.offset+24) % 24)
if lunchLocalTime < 11.5 || lunchLocalTime > 12.5 {
t.Errorf("UTC+0 lunch time incorrect: %.1f, expected ~12.0", lunchLocalTime)
}
// Test 5: UTC+0 should be marked as having reasonable patterns
if !topCandidate.lunchReasonable {
t.Error("UTC+0 lunch should be marked as reasonable")
}
if !topCandidate.workHoursReasonable {
t.Error("UTC+0 work hours should be marked as reasonable")
}
if !topCandidate.sleepReasonable {
t.Error("UTC+0 sleep should be marked as reasonable (midnight-8am)")
}
// Test 6: UTC+1 should also be a strong candidate (UK BST)
var utcPlus1 *evaluationCandidate
for i := range candidates {
if candidates[i].offset == 1 {
utcPlus1 = &candidates[i]
break
}
}
if utcPlus1 == nil {
t.Error("UTC+1 should be among candidates for UK detection")
} else {
// UTC+1 should also have good confidence (> 50%)
if utcPlus1.confidence < 50 {
t.Errorf("UTC+1 confidence too low: %.1f%%, expected > 50%%", utcPlus1.confidence)
}
// UTC+1 sleep (1am-9am) should be marked as reasonable
if !utcPlus1.sleepReasonable {
sleepMidLocal := float64(int(utcPlus1.sleepMidUTC+utcPlus1.offset+24) % 24)
t.Errorf("UTC+1 sleep should be reasonable (1am-9am, mid=%.1f)", sleepMidLocal)
}
}
// Test 7: Verify both UTC+0 and UTC+1 get UK/Europe population boost
foundUKBoost := false
for _, detail := range topCandidate.scoringDetails {
if strings.Contains(detail, "UK") || strings.Contains(detail, "Western Europe") {
foundUKBoost = true
break
}
}
if !foundUKBoost {
t.Error("UTC+0 should get UK/Western Europe population boost")
}
// Test 8: No work start penalties for 8am/9am starts
for _, detail := range topCandidate.scoringDetails {
if strings.Contains(detail, "impossible") || strings.Contains(detail, "extremely early") {
t.Errorf("UTC+0 should not have work start penalties: %s", detail)
}
}
}
// TestPerCandidateCalculations verifies that sleep, work, and peak are calculated per-candidate
// and differ between timezones (not just converted from a global value).
func TestPerCandidateCalculations(t *testing.T) {
// Use the same activity pattern from TestEvaluate
halfHourCounts := map[float64]int{
0.0: 1, 0.5: 0, 1.0: 0, 1.5: 0, 2.0: 0, 2.5: 1, 3.0: 0, 3.5: 0,
4.0: 0, 4.5: 0, 5.0: 0, 5.5: 0, 6.0: 0, 6.5: 0, 7.0: 1, 7.5: 2,
8.0: 1, 8.5: 2, 9.0: 4, 9.5: 6, 10.0: 10, 10.5: 12, 11.0: 8, 11.5: 4,
12.0: 2, 12.5: 2, 13.0: 12, 13.5: 14, 14.0: 15, 14.5: 13, 15.0: 10, 15.5: 8,
16.0: 6, 16.5: 5, 17.0: 4, 17.5: 3, 18.0: 2, 18.5: 2, 19.0: 1, 19.5: 1,
20.0: 1, 20.5: 0, 21.0: 1, 21.5: 1, 22.0: 0, 22.5: 0, 23.0: 0, 23.5: 0,
}
hourCounts := map[int]int{
0: 1, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 3, 8: 3, 9: 10,
10: 22, 11: 12, 12: 4, 13: 26, 14: 28, 15: 18, 16: 11, 17: 7, 18: 4,
19: 2, 20: 1, 21: 2, 22: 0, 23: 0,
}
input := evaluationInput{
username: "testuser",
hourCounts: hourCounts,
halfHourCounts: halfHourCounts,
totalActivity: 174,
quietHours: []int{0, 1, 2, 3, 4, 5, 6},
midQuiet: 3.0,
activeStart: 7.0,
bestGlobalLunch: globalLunchPattern{
startUTC: 12.0,
endUTC: 12.5,
confidence: 0.8,
dropPercent: 0.75,
},
}
candidates := evaluate(input)
if len(candidates) < 2 {
t.Fatal("Need at least 2 candidates to test per-candidate calculations")
}
// Test 1: Sleep buckets should differ between candidates
firstSleepBuckets := candidates[0].sleepBucketsUTC
foundDifferentSleep := false
for i := 1; i < len(candidates); i++ {
if len(candidates[i].sleepBucketsUTC) != len(firstSleepBuckets) {
foundDifferentSleep = true
break
}
// Check if any bucket values differ
for j := range firstSleepBuckets {
if candidates[i].sleepBucketsUTC[j] != firstSleepBuckets[j] {
foundDifferentSleep = true
break
}
}
if foundDifferentSleep {
break
}
}
if !foundDifferentSleep {
t.Error("Sleep buckets should differ between candidates (per-timezone detection)")
}
// Test 2: Work hours should differ between candidates
firstWorkStart := candidates[0].workStartUTC
foundDifferentWork := false
for i := 1; i < len(candidates); i++ {
if candidates[i].workStartUTC != firstWorkStart {
foundDifferentWork = true
break
}
}
if !foundDifferentWork {
t.Error("Work start times should differ between candidates (per-timezone detection)")
}
// Test 3: Sleep midpoint should differ between candidates
firstSleepMid := candidates[0].sleepMidUTC
foundDifferentMid := false
for i := 1; i < len(candidates); i++ {
if candidates[i].sleepMidUTC != firstSleepMid {
foundDifferentMid = true
break
}
}
if !foundDifferentMid {
t.Error("Sleep midpoints should differ between candidates (calculated from per-timezone sleep buckets)")
}
// Test 4: All candidates should have non-zero peak productivity data
for i, candidate := range candidates {
if candidate.peakStartUTC == 0 && candidate.peakEndUTC == 0 && candidate.peakCount == 0 {
t.Errorf("Candidate %d should have peak productivity data", i)
}
}
}