-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintegration_test.go
More file actions
401 lines (353 loc) · 11.2 KB
/
integration_test.go
File metadata and controls
401 lines (353 loc) · 11.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
package bot
import (
"context"
"strings"
"testing"
"time"
ghmailto "github.com/codeGROOVE-dev/gh-mailto/pkg/gh-mailto"
"github.com/codeGROOVE-dev/slacker/pkg/notify"
"github.com/codeGROOVE-dev/slacker/pkg/slack"
"github.com/codeGROOVE-dev/slacker/pkg/slacktest"
"github.com/codeGROOVE-dev/slacker/pkg/state"
"github.com/codeGROOVE-dev/slacker/pkg/usermapping"
"github.com/codeGROOVE-dev/turnclient/pkg/turn"
slackapi "github.com/slack-go/slack"
)
// TestUserMappingIntegration tests the complete flow of mapping GitHub users to Slack users.
func TestUserMappingIntegration(t *testing.T) {
ctx := context.Background()
// Setup mock Slack server
mockSlack := slacktest.New()
defer mockSlack.Close()
// Add test users
mockSlack.AddUser("alice@example.com", "U001", "alice")
mockSlack.AddUser("bob@example.com", "U002", "bob")
// Create Slack client pointing to mock server
slackClient := slackapi.New("test-token", slackapi.OptionAPIURL(mockSlack.URL+"/api/"))
// Create mock GitHub email lookup
mockGitHub := &mockGitHubLookup{
emails: map[string][]string{
"alice": {"alice@example.com"},
"bob": {"bob@example.com", "bob@personal.com"},
"charlie": {"charlie@other.com"}, // Won't be found in Slack
},
}
// Create user mapper
userMapper := usermapping.NewForTesting(slackClient, mockGitHub)
tests := []struct {
name string
githubUsername string
wantSlackID string
wantErr bool
}{
{
name: "successful mapping with single email",
githubUsername: "alice",
wantSlackID: "U001",
wantErr: false,
},
{
name: "successful mapping with multiple emails",
githubUsername: "bob",
wantSlackID: "U002",
wantErr: false,
},
{
name: "no mapping - email not in Slack",
githubUsername: "charlie",
wantSlackID: "",
wantErr: false, // Not an error, just no mapping
},
{
name: "no mapping - no GitHub emails found",
githubUsername: "unknown",
wantSlackID: "",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
slackID, err := userMapper.SlackHandle(ctx, tt.githubUsername, "test-org", "example.com")
if (err != nil) != tt.wantErr {
t.Errorf("SlackHandle() error = %v, wantErr %v", err, tt.wantErr)
}
if slackID != tt.wantSlackID {
t.Errorf("SlackHandle() = %v, want %v", slackID, tt.wantSlackID)
}
})
}
// Verify email lookups were performed
emailLookups := mockSlack.GetEmailLookups()
if len(emailLookups) < 3 {
t.Errorf("Expected at least 3 email lookups, got %d", len(emailLookups))
}
}
// TestTurnclientEmojiIntegration tests the complete emoji determination pipeline.
func TestTurnclientEmojiIntegration(t *testing.T) {
tests := []struct {
name string
workflowState string
nextActions map[string]turn.Action
expectedEmoji string
expectedAction string
description string
}{
{
name: "newly published takes precedence",
workflowState: string(turn.StateNewlyPublished),
nextActions: map[string]turn.Action{
"author": {Kind: turn.ActionFixTests},
},
expectedEmoji: ":new:",
expectedAction: "fix_tests",
description: "newly_published state should always show :new: even with other actions",
},
{
name: "draft needs publishing",
workflowState: "awaiting_action",
nextActions: map[string]turn.Action{
"author": {Kind: turn.ActionPublishDraft},
},
expectedEmoji: ":construction:",
expectedAction: "publish_draft",
description: "unpublished draft should show construction emoji",
},
{
name: "tests broken has highest action priority",
workflowState: "blocked",
nextActions: map[string]turn.Action{
"author": {Kind: turn.ActionFixTests},
"reviewer": {Kind: turn.ActionReview},
},
expectedEmoji: ":cockroach:",
expectedAction: "fix_tests",
description: "fix_tests should take priority over review",
},
{
name: "awaiting review",
workflowState: "awaiting_review",
nextActions: map[string]turn.Action{
"reviewer": {Kind: turn.ActionReview},
},
expectedEmoji: ":hourglass:",
expectedAction: "review",
description: "review action should show hourglass",
},
{
name: "ready to merge",
workflowState: "approved",
nextActions: map[string]turn.Action{
"author": {Kind: turn.ActionMerge},
},
expectedEmoji: ":rocket:",
expectedAction: "merge",
description: "merge action should show rocket",
},
{
name: "merged with no actions shows fallback",
workflowState: "merged",
nextActions: map[string]turn.Action{},
expectedEmoji: ":postal_horn:",
expectedAction: "",
description: "This is a known bug - merged state with no actions falls back to postal_horn",
},
{
name: "multiple reviewers prioritized correctly",
workflowState: "awaiting_review",
nextActions: map[string]turn.Action{
"alice": {Kind: turn.ActionReview},
"bob": {Kind: turn.ActionApprove},
"carol": {Kind: turn.ActionReview},
},
expectedEmoji: ":hourglass:",
expectedAction: "review",
description: "review has lower priority number (6) than approve (7)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test emoji determination
emoji := notify.PrefixForAnalysis(tt.workflowState, tt.nextActions)
if emoji != tt.expectedEmoji {
t.Errorf("%s: PrefixForAnalysis() = %q, want %q",
tt.description, emoji, tt.expectedEmoji)
}
// Test primary action extraction
primaryAction := notify.PrimaryAction(tt.nextActions)
if primaryAction != tt.expectedAction {
t.Errorf("%s: PrimaryAction() = %q, want %q",
tt.description, primaryAction, tt.expectedAction)
}
// Verify consistency: if there's a primary action, its emoji should match
// (except for newly_published and merged edge cases)
if tt.workflowState != string(turn.StateNewlyPublished) && tt.workflowState != "merged" && tt.expectedAction != "" {
actionEmoji := notify.PrefixForAction(tt.expectedAction)
if emoji != actionEmoji {
t.Errorf("%s: emoji mismatch - PrefixForAnalysis=%q but PrefixForAction(%q)=%q",
tt.description, emoji, tt.expectedAction, actionEmoji)
}
}
})
}
}
// TestDMDelayLogicIntegration tests the DM delay decision logic.
func TestDMDelayLogicIntegration(t *testing.T) {
ctx := context.Background()
// Setup mock Slack server
mockSlack := slacktest.New()
defer mockSlack.Close()
mockSlack.AddUser("alice@example.com", "U001", "alice")
mockSlack.AddChannel("C123", "dev", true)
mockSlack.AddChannelMember("C123", "U001") // alice is in channel
mockSlack.AddChannel("C456", "other", true)
// alice is NOT in C456
slackClient := slackapi.New("test-token", slackapi.OptionAPIURL(mockSlack.URL+"/api/"))
// Create notification manager
slackManager := slack.NewManager("")
slackManager.RegisterWorkspace(ctx, "test-workspace", slackClient)
configMgr := &mockConfigManager{
dmDelay: 65, // 65 minute delay
}
// Create in-memory store for pending DMs
store := state.NewMemoryStore()
notifier := notify.New(notify.WrapSlackManager(slackManager), configMgr, store)
prInfo := notify.PRInfo{
Owner: "test",
Repo: "repo",
Number: 1,
Title: "Test PR",
Author: "author",
State: "awaiting_review",
HTMLURL: "https://github.com/test/repo/pull/1",
WorkflowState: "awaiting_review",
NextAction: map[string]turn.Action{
"alice": {Kind: turn.ActionReview},
},
}
tests := []struct {
name string
setupFunc func()
channelID string
expectDM bool
description string
}{
{
name: "user in channel - delay should apply",
setupFunc: func() {
notifier.Tracker.UpdateUserPRChannelTag("test-workspace", "U001", "C123", "test", "repo", 1)
},
channelID: "C123",
expectDM: false,
description: "User is in channel where tagged - DM should be delayed",
},
{
name: "user not in channel - immediate DM",
setupFunc: func() {
notifier.Tracker.UpdateUserPRChannelTag("test-workspace", "U001", "C456", "test", "repo", 1)
},
channelID: "C456",
expectDM: true,
description: "User is NOT in channel where tagged - DM should be immediate",
},
{
name: "no channel tag - immediate DM",
setupFunc: func() {
// Don't record any channel tag
},
channelID: "",
expectDM: true,
description: "No channel tag recorded - DM should be immediate",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Reset state
mockSlack.Reset()
// Create fresh tracker with initialized maps
notifier.Tracker = ¬ify.NotificationTracker{}
// Initialize the tracker by creating a new notifier
store = state.NewMemoryStore() // Reset store as well
notifier = notify.New(notify.WrapSlackManager(slackManager), configMgr, store)
// Setup test scenario
if tt.setupFunc != nil {
tt.setupFunc()
}
// Execute
err := notifier.NotifyUser(ctx, "test-workspace", "U001", tt.channelID, "dev", prInfo)
if err != nil {
t.Errorf("NotifyUser() error = %v", err)
}
// Give async operations time to complete
time.Sleep(50 * time.Millisecond)
// Verify
postedMessages := mockSlack.GetPostedMessages()
dmCount := 0
for _, msg := range postedMessages {
if strings.HasPrefix(msg.Channel, "D") {
dmCount++
}
}
if tt.expectDM && dmCount == 0 {
t.Errorf("%s: expected DM to be sent, but none were sent", tt.description)
}
if !tt.expectDM && dmCount > 0 {
t.Errorf("%s: expected no DM (delay should apply), but %d DMs were sent", tt.description, dmCount)
}
})
}
}
// Mock implementations for testing
type mockGitHubLookup struct {
emails map[string][]string
}
func (m *mockGitHubLookup) Lookup(ctx context.Context, username, organization string) (*ghmailto.Result, error) {
emails, exists := m.emails[username]
if !exists {
return &ghmailto.Result{
Username: username,
Addresses: []ghmailto.Address{},
}, nil
}
addresses := make([]ghmailto.Address, len(emails))
for i, email := range emails {
addresses[i] = ghmailto.Address{
Email: email,
Methods: []string{"Test Mock"},
}
}
return &ghmailto.Result{
Username: username,
Addresses: addresses,
}, nil
}
func (m *mockGitHubLookup) Guess(ctx context.Context, username, organization string, opts ghmailto.GuessOptions) (*ghmailto.GuessResult, error) {
return &ghmailto.GuessResult{
Username: username,
Guesses: []ghmailto.Address{},
}, nil
}
type mockConfigManager struct {
dmDelay int
channelsFunc func(org, repo string) []string
}
func (m *mockConfigManager) DailyRemindersEnabled(org string) bool {
return false
}
func (m *mockConfigManager) ReminderDMDelay(org, channel string) int {
return m.dmDelay
}
func (m *mockConfigManager) ChannelsForRepo(org, repo string) []string {
if m.channelsFunc != nil {
return m.channelsFunc(org, repo)
}
return []string{}
}
func (m *mockConfigManager) LoadConfig(ctx context.Context, org string) error {
return nil // Always succeed
}
func (m *mockConfigManager) WorkspaceName(org string) string {
return "test-workspace.slack.com"
}
func (m *mockConfigManager) Domain(org string) string {
return "test.com"
}