-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdaily_test.go
More file actions
486 lines (425 loc) · 13.2 KB
/
daily_test.go
File metadata and controls
486 lines (425 loc) · 13.2 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package notify
import (
"context"
"strings"
"testing"
"time"
"github.com/codeGROOVE-dev/prx/pkg/prx"
"github.com/codeGROOVE-dev/slacker/pkg/home"
"github.com/codeGROOVE-dev/turnclient/pkg/turn"
)
func TestFormatDigestMessage(t *testing.T) {
tests := []struct {
name string
time time.Time
incoming []home.PR
outgoing []home.PR
expected string
}{
{
name: "incoming PRs only at 8:30am",
time: time.Date(2025, 10, 22, 8, 30, 0, 0, time.UTC),
incoming: []home.PR{
{
URL: "https://github.com/codeGROOVE-dev/slacker/pull/123",
Title: "Add daily digest feature",
Author: "otheruser",
ActionKind: "review",
},
},
outgoing: nil,
expected: `☀️ *Good morning!*
*To Review:*
:hourglass: <https://github.com/codeGROOVE-dev/slacker/pull/123|Add daily digest feature> · review
_Your daily digest from Ready to Review_`,
},
{
name: "outgoing PRs only at 8:15am",
time: time.Date(2025, 10, 22, 8, 15, 0, 0, time.UTC),
incoming: nil,
outgoing: []home.PR{
{
URL: "https://github.com/codeGROOVE-dev/slacker/pull/124",
Title: "Fix authentication bug",
Author: "testuser",
ActionKind: "address-feedback",
},
},
expected: `🌻 *Hello sunshine!*
*Your PRs:*
:hourglass: <https://github.com/codeGROOVE-dev/slacker/pull/124|Fix authentication bug> · address-feedback
_Your daily digest from Ready to Review_`,
},
{
name: "both incoming and outgoing at 8:45am",
time: time.Date(2025, 10, 22, 8, 45, 0, 0, time.UTC),
incoming: []home.PR{
{
URL: "https://github.com/codeGROOVE-dev/goose/pull/456",
Title: "Implement new API endpoint",
Author: "colleague",
ActionKind: "review",
},
{
URL: "https://github.com/codeGROOVE-dev/goose/pull/457",
Title: "Refactor database layer",
Author: "teammate",
ActionKind: "approve",
},
},
outgoing: []home.PR{
{
URL: "https://github.com/codeGROOVE-dev/goose/pull/458",
Title: "Update documentation",
Author: "testuser",
ActionKind: "merge",
},
},
expected: `🌻 *Hello sunshine!*
*To Review:*
:hourglass: <https://github.com/codeGROOVE-dev/goose/pull/456|Implement new API endpoint> · review
:hourglass: <https://github.com/codeGROOVE-dev/goose/pull/457|Refactor database layer> · approve
*Your PRs:*
:hourglass: <https://github.com/codeGROOVE-dev/goose/pull/458|Update documentation> · merge
_Your daily digest from Ready to Review_`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
scheduler := &DailyDigestScheduler{}
got := scheduler.formatDigestMessageAt(tt.incoming, tt.outgoing, tt.time)
if got != tt.expected {
t.Errorf("formatDigestMessageAt() mismatch\nGot:\n%s\n\nExpected:\n%s", got, tt.expected)
}
})
}
}
func TestDigestMessageVariety(t *testing.T) {
// Test that different times produce different greetings
scheduler := &DailyDigestScheduler{}
incoming := []home.PR{
{
URL: "https://github.com/codeGROOVE-dev/slacker/pull/1",
Title: "Test PR",
Author: "other",
ActionKind: "review",
},
}
// Collect messages at different times
messages := make(map[string]bool)
for hour := 8; hour < 9; hour++ {
for minute := 0; minute < 60; minute += 15 {
testTime := time.Date(2025, 10, 22, hour, minute, 0, 0, time.UTC)
msg := scheduler.formatDigestMessageAt(incoming, nil, testTime)
messages[msg] = true
}
}
// Should have at least 2 different message variations in the 8-9am window
if len(messages) < 2 {
t.Errorf("Expected message variety, but got only %d unique messages in 1-hour window", len(messages))
}
t.Logf("Generated %d unique message variations across the 8-9am window", len(messages))
}
// TestDailyDigestExample shows what an actual daily digest looks like with both sections.
func TestDailyDigestExample(t *testing.T) {
scheduler := &DailyDigestScheduler{}
// Example: User has 2 incoming PRs to review and 1 outgoing PR needing attention at 8:30am
exampleTime := time.Date(2025, 10, 22, 8, 30, 0, 0, time.UTC)
exampleIncoming := []home.PR{
{
URL: "https://github.com/codeGROOVE-dev/goose/pull/127",
Title: "Add support for custom prompts",
Author: "colleague",
ActionKind: "review",
},
{
URL: "https://github.com/codeGROOVE-dev/sprinkler/pull/15",
Title: "Implement WebSocket reconnection logic",
Author: "teammate",
ActionKind: "approve",
},
}
exampleOutgoing := []home.PR{
{
URL: "https://github.com/codeGROOVE-dev/slacker/pull/48",
Title: "Update DM messages when PR is merged",
Author: "testuser",
ActionKind: "address-feedback",
},
}
message := scheduler.formatDigestMessageAt(exampleIncoming, exampleOutgoing, exampleTime)
// Log the example for documentation purposes
t.Logf("Example daily digest DM:\n\n%s\n", message)
// Verify it has the expected structure
if message == "" {
t.Error("Message should not be empty")
}
// Should contain both section headers
if !strings.Contains(message, "*To Review:*") {
t.Error("Message should contain 'To Review:' header")
}
if !strings.Contains(message, "*Your PRs:*") {
t.Error("Message should contain 'Your PRs:' header")
}
// Should contain all PR URLs
allPRs := make([]home.PR, 0, len(exampleIncoming)+len(exampleOutgoing))
allPRs = append(allPRs, exampleIncoming...)
allPRs = append(allPRs, exampleOutgoing...)
for _, pr := range allPRs {
if !strings.Contains(message, pr.URL) {
t.Errorf("Message should contain PR URL: %s", pr.URL)
}
}
// Should contain footer
if !strings.Contains(message, "Your daily digest from Ready to Review") {
t.Error("Message should contain footer")
}
}
// TestEnrichPR verifies PR enrichment with turnclient data.
func TestEnrichPR(t *testing.T) {
scheduler := &DailyDigestScheduler{}
tests := []struct {
name string
pr home.PR
action turn.Action
wantFields map[string]interface{}
}{
{
name: "review action",
pr: home.PR{
Number: 123,
Title: "Update README",
Author: "alice",
Repository: "org/repo",
URL: "https://github.com/org/repo/pull/123",
},
action: turn.Action{
Kind: "review",
Reason: "PR needs review",
},
wantFields: map[string]interface{}{
"ActionKind": "review",
"ActionReason": "PR needs review",
"NeedsReview": true,
"IsBlocked": true,
},
},
{
name: "approve action",
pr: home.PR{
Number: 456,
Title: "Add feature",
},
action: turn.Action{
Kind: "approve",
Reason: "LGTM but needs approval",
},
wantFields: map[string]interface{}{
"ActionKind": "approve",
"ActionReason": "LGTM but needs approval",
"NeedsReview": true,
"IsBlocked": true,
},
},
{
name: "address_feedback action",
pr: home.PR{
Number: 789,
Title: "Fix bug",
},
action: turn.Action{
Kind: "address_feedback",
Reason: "Comments need resolution",
},
wantFields: map[string]interface{}{
"ActionKind": "address_feedback",
"ActionReason": "Comments need resolution",
"NeedsReview": false, // Not a review action
"IsBlocked": true,
},
},
{
name: "merge action",
pr: home.PR{
Number: 999,
Title: "Ready to merge",
},
action: turn.Action{
Kind: "merge",
Reason: "All checks passed",
},
wantFields: map[string]interface{}{
"ActionKind": "merge",
"ActionReason": "All checks passed",
"NeedsReview": false,
"IsBlocked": true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create minimal CheckResponse (only Action is used by enrichPR)
checkResult := &turn.CheckResponse{
PullRequest: prx.PullRequest{},
Analysis: turn.Analysis{},
}
enriched := scheduler.enrichPR(tt.pr, checkResult, "testuser", tt.action)
// Verify all expected fields
if enriched.ActionKind != tt.wantFields["ActionKind"].(string) {
t.Errorf("ActionKind = %q, want %q", enriched.ActionKind, tt.wantFields["ActionKind"])
}
if enriched.ActionReason != tt.wantFields["ActionReason"].(string) {
t.Errorf("ActionReason = %q, want %q", enriched.ActionReason, tt.wantFields["ActionReason"])
}
if enriched.NeedsReview != tt.wantFields["NeedsReview"].(bool) {
t.Errorf("NeedsReview = %v, want %v", enriched.NeedsReview, tt.wantFields["NeedsReview"])
}
if enriched.IsBlocked != tt.wantFields["IsBlocked"].(bool) {
t.Errorf("IsBlocked = %v, want %v", enriched.IsBlocked, tt.wantFields["IsBlocked"])
}
// Verify original fields are preserved
if enriched.Number != tt.pr.Number {
t.Errorf("Number = %d, want %d", enriched.Number, tt.pr.Number)
}
if enriched.Title != tt.pr.Title {
t.Errorf("Title = %q, want %q", enriched.Title, tt.pr.Title)
}
})
}
}
// TestEnrichPR_PreservesOriginalFields verifies that enrichment doesn't lose PR data.
func TestEnrichPR_PreservesOriginalFields(t *testing.T) {
scheduler := &DailyDigestScheduler{}
originalPR := home.PR{
Number: 123,
Title: "Test PR",
Author: "alice",
Repository: "org/repo",
URL: "https://github.com/org/repo/pull/123",
UpdatedAt: time.Date(2025, 1, 15, 10, 0, 0, 0, time.UTC),
}
action := turn.Action{
Kind: "review",
Reason: "Needs review",
}
checkResult := &turn.CheckResponse{
PullRequest: prx.PullRequest{},
Analysis: turn.Analysis{},
}
enriched := scheduler.enrichPR(originalPR, checkResult, "reviewer", action)
// Verify all original fields are preserved
if enriched.Number != originalPR.Number {
t.Errorf("Number changed: %d -> %d", originalPR.Number, enriched.Number)
}
if enriched.Title != originalPR.Title {
t.Errorf("Title changed: %q -> %q", originalPR.Title, enriched.Title)
}
if enriched.Author != originalPR.Author {
t.Errorf("Author changed: %q -> %q", originalPR.Author, enriched.Author)
}
if enriched.Repository != originalPR.Repository {
t.Errorf("Repository changed: %q -> %q", originalPR.Repository, enriched.Repository)
}
if enriched.URL != originalPR.URL {
t.Errorf("URL changed: %q -> %q", originalPR.URL, enriched.URL)
}
if !enriched.UpdatedAt.Equal(originalPR.UpdatedAt) {
t.Errorf("UpdatedAt changed: %v -> %v", originalPR.UpdatedAt, enriched.UpdatedAt)
}
}
// TestFormatDigestMessage_EmptyPRLists verifies handling of empty incoming/outgoing lists.
func TestFormatDigestMessage_EmptyPRLists(t *testing.T) {
scheduler := &DailyDigestScheduler{}
testTime := time.Date(2025, 1, 15, 8, 30, 0, 0, time.UTC)
tests := []struct {
name string
incoming []home.PR
outgoing []home.PR
}{
{
name: "both empty",
incoming: nil,
outgoing: nil,
},
{
name: "incoming empty",
incoming: nil,
outgoing: []home.PR{{Title: "Test", URL: "https://github.com/test/repo/pull/1", ActionKind: "merge"}},
},
{
name: "outgoing empty",
incoming: []home.PR{{Title: "Test", URL: "https://github.com/test/repo/pull/1", ActionKind: "review"}},
outgoing: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
message := scheduler.formatDigestMessageAt(tt.incoming, tt.outgoing, testTime)
// Should always have greeting and footer
if !strings.Contains(message, "*") {
t.Error("expected greeting")
}
if !strings.Contains(message, "Ready to Review") {
t.Error("expected footer")
}
// Should not crash
if message == "" {
t.Error("message should not be empty")
}
})
}
}
// TestCheckAndSend_NoOrgs tests when there are no organizations configured.
func TestCheckAndSend_NoOrgs(t *testing.T) {
mockGitHubMgr := &mockGitHubManager{
allOrgsFunc: func() []string {
return []string{} // No orgs
},
}
scheduler := &DailyDigestScheduler{
githubManager: mockGitHubMgr,
configManager: &mockConfigProvider{},
stateStore: &mockStateProvider{},
slackManager: &mockSlackManagerWithClient{},
}
ctx := context.Background()
// Should not crash
scheduler.CheckAndSend(ctx)
}
// TestCheckAndSend_DailyRemindersDisabled tests when daily reminders are disabled.
func TestCheckAndSend_DailyRemindersDisabled(t *testing.T) {
mockGitHubMgr := &mockGitHubManager{
allOrgsFunc: func() []string {
return []string{"test-org"}
},
}
mockConfigMgr := &mockConfigProvider{
dailyRemindersEnabledFunc: func(org string) bool {
return false // Disabled
},
}
scheduler := &DailyDigestScheduler{
githubManager: mockGitHubMgr,
configManager: mockConfigMgr,
stateStore: &mockStateProvider{},
slackManager: &mockSlackManagerWithClient{},
}
ctx := context.Background()
// Should not crash and should skip processing
scheduler.CheckAndSend(ctx)
}
// TestNewDailyDigestScheduler_WithInterfaces tests scheduler creation with interfaces.
func TestNewDailyDigestScheduler_WithInterfaces(t *testing.T) {
mockGitHubMgr := &mockGitHubManager{}
mockConfigMgr := &mockConfigProvider{}
mockState := &mockStateProvider{}
mockSlack := &mockSlackManagerWithClient{}
manager := New(mockSlack, mockConfigMgr, &mockStore{})
scheduler := NewDailyDigestScheduler(manager, mockGitHubMgr, mockConfigMgr, mockState, mockSlack)
if scheduler == nil {
t.Fatal("expected non-nil scheduler")
}
if scheduler.githubManager != mockGitHubMgr {
t.Error("expected github manager to be set")
}
}