forked from codeGROOVE-dev/slacker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordinator_test_helpers.go
More file actions
524 lines (472 loc) · 15.1 KB
/
coordinator_test_helpers.go
File metadata and controls
524 lines (472 loc) · 15.1 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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package bot
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/codeGROOVE-dev/slacker/pkg/bot/cache"
"github.com/codeGROOVE-dev/slacker/pkg/github"
slackapi "github.com/codeGROOVE-dev/slacker/pkg/slack"
"github.com/codeGROOVE-dev/slacker/pkg/state"
"github.com/slack-go/slack"
)
// mockStateStore implements StateStore interface from bot package.
//
//nolint:govet // fieldalignment optimization would reduce test readability
type mockStateStore struct {
markProcessedErr error
saveThreadErr error
saveDMMessageErr error
queuePendingDMErr error
pendingDMsErr error
removePendingDMErr error
threads map[string]cache.ThreadInfo
dmTimes map[string]time.Time
dmUsers map[string][]string
dmMessages map[string]state.DMInfo
pendingDMs []*state.PendingDM
processedEvents map[string]bool
lastNotifications map[string]time.Time
mu sync.Mutex
}
func (m *mockStateStore) Thread(ctx context.Context, owner, repo string, number int, channelID string) (cache.ThreadInfo, bool) {
m.mu.Lock()
defer m.mu.Unlock()
key := fmt.Sprintf("%s/%s#%d:%s", owner, repo, number, channelID)
if m.threads != nil {
if info, ok := m.threads[key]; ok {
return info, true
}
}
return cache.ThreadInfo{}, false
}
func (m *mockStateStore) SaveThread(ctx context.Context, owner, repo string, number int, channelID string, info cache.ThreadInfo) error {
m.mu.Lock()
defer m.mu.Unlock()
if m.saveThreadErr != nil {
return m.saveThreadErr
}
key := fmt.Sprintf("%s/%s#%d:%s", owner, repo, number, channelID)
if m.threads == nil {
m.threads = make(map[string]cache.ThreadInfo)
}
m.threads[key] = info
return nil
}
func (m *mockStateStore) LastDM(ctx context.Context, userID, prURL string) (time.Time, bool) {
m.mu.Lock()
defer m.mu.Unlock()
key := userID + ":" + prURL
if m.dmTimes != nil {
if t, ok := m.dmTimes[key]; ok {
return t, true
}
}
return time.Time{}, false
}
func (m *mockStateStore) RecordDM(ctx context.Context, userID, prURL string, sentAt time.Time) error {
m.mu.Lock()
defer m.mu.Unlock()
key := userID + ":" + prURL
if m.dmTimes == nil {
m.dmTimes = make(map[string]time.Time)
}
m.dmTimes[key] = sentAt
return nil
}
func (m *mockStateStore) ListDMUsers(ctx context.Context, prURL string) []string {
m.mu.Lock()
defer m.mu.Unlock()
if m.dmUsers != nil {
if users, ok := m.dmUsers[prURL]; ok {
return users
}
}
return []string{}
}
// DMMessage returns DM message info for a user and PR.
func (m *mockStateStore) DMMessage(ctx context.Context, userID, prURL string) (state.DMInfo, bool) {
m.mu.Lock()
defer m.mu.Unlock()
key := userID + ":" + prURL
if m.dmMessages != nil {
if info, ok := m.dmMessages[key]; ok {
return info, true
}
}
return state.DMInfo{}, false
}
// SaveDMMessage saves DM message info for a user and PR.
func (m *mockStateStore) SaveDMMessage(ctx context.Context, userID, prURL string, info state.DMInfo) error {
m.mu.Lock()
defer m.mu.Unlock()
if m.saveDMMessageErr != nil {
return m.saveDMMessageErr
}
key := userID + ":" + prURL
if m.dmMessages == nil {
m.dmMessages = make(map[string]state.DMInfo)
}
m.dmMessages[key] = info
// Also track this user for ListDMUsers
if m.dmUsers == nil {
m.dmUsers = make(map[string][]string)
}
found := false
for _, u := range m.dmUsers[prURL] {
if u == userID {
found = true
break
}
}
if !found {
m.dmUsers[prURL] = append(m.dmUsers[prURL], userID)
}
return nil
}
func (m *mockStateStore) WasProcessed(ctx context.Context, eventKey string) bool {
m.mu.Lock()
defer m.mu.Unlock()
if m.processedEvents != nil {
return m.processedEvents[eventKey]
}
return false
}
func (m *mockStateStore) MarkProcessed(ctx context.Context, eventKey string, _ time.Duration) error {
m.mu.Lock()
defer m.mu.Unlock()
if m.markProcessedErr != nil {
return m.markProcessedErr
}
if m.processedEvents == nil {
m.processedEvents = make(map[string]bool)
}
m.processedEvents[eventKey] = true
return nil
}
func (m *mockStateStore) LastNotification(ctx context.Context, prURL string) time.Time {
m.mu.Lock()
defer m.mu.Unlock()
if m.lastNotifications != nil {
if t, ok := m.lastNotifications[prURL]; ok {
return t
}
}
return time.Time{}
}
func (m *mockStateStore) RecordNotification(ctx context.Context, prURL string, notifiedAt time.Time) error {
m.mu.Lock()
defer m.mu.Unlock()
if m.lastNotifications == nil {
m.lastNotifications = make(map[string]time.Time)
}
m.lastNotifications[prURL] = notifiedAt
return nil
}
// QueuePendingDM implements notify.Store interface for DM queue management.
func (m *mockStateStore) QueuePendingDM(ctx context.Context, dm *state.PendingDM) error {
m.mu.Lock()
defer m.mu.Unlock()
if m.queuePendingDMErr != nil {
return m.queuePendingDMErr
}
m.pendingDMs = append(m.pendingDMs, dm)
return nil
}
func (m *mockStateStore) PendingDMs(ctx context.Context, before time.Time) ([]state.PendingDM, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.pendingDMsErr != nil {
return nil, m.pendingDMsErr
}
var result []state.PendingDM
for _, dm := range m.pendingDMs {
if dm.SendAfter.Before(before) {
result = append(result, *dm)
}
}
return result, nil
}
func (m *mockStateStore) RemovePendingDM(ctx context.Context, id string) error {
m.mu.Lock()
defer m.mu.Unlock()
if m.removePendingDMErr != nil {
return m.removePendingDMErr
}
for i, dm := range m.pendingDMs {
if dm.ID == id {
m.pendingDMs = append(m.pendingDMs[:i], m.pendingDMs[i+1:]...)
break
}
}
return nil
}
func (m *mockStateStore) LastDigest(_ context.Context, _ /* userID */, _ /* date */ string) (time.Time, bool) {
m.mu.Lock()
defer m.mu.Unlock()
// Simple mock - always return false
return time.Time{}, false
}
func (m *mockStateStore) RecordDigest(_ context.Context, _ /* userID */, _ /* date */ string, _ /* sentAt */ time.Time) error {
m.mu.Lock()
defer m.mu.Unlock()
// Simple mock - no-op
return nil
}
func (m *mockStateStore) LastReportSent(_ context.Context, _ /* userID */ string) (time.Time, bool) {
m.mu.Lock()
defer m.mu.Unlock()
// Simple mock - always return false (no reports sent)
return time.Time{}, false
}
func (m *mockStateStore) RecordReportSent(_ context.Context, _ /* userID */ string, _ /* sentAt */ time.Time) error {
m.mu.Lock()
defer m.mu.Unlock()
// Simple mock - no-op
return nil
}
func (*mockStateStore) Cleanup(_ context.Context) error {
// Simple mock - no-op
return nil
}
func (*mockStateStore) Close() error {
return nil
}
// mockSlackClient implements SlackClient for testing.
//
//nolint:govet // fieldalignment optimization would reduce test readability
type mockSlackClient struct {
mu sync.Mutex
postThreadFunc func(ctx context.Context, channelID, text string, attachments []slack.Attachment) (string, error)
updateMessageFunc func(ctx context.Context, channelID, timestamp, text string) error
updateDMMessageFunc func(ctx context.Context, userID, timestamp, text string) error
sendDirectMessageFunc func(ctx context.Context, userID, text string) (dmChannelID, messageTS string, err error)
isUserInChannelFunc func(ctx context.Context, channelID, userID string) bool
findDMMessagesFunc func(ctx context.Context, userID, prURL string, since time.Time) ([]slackapi.DMLocation, error)
channelHistoryFunc func(ctx context.Context, channelID string, oldest, latest string, limit int) (*slack.GetConversationHistoryResponse, error)
resolveChannelFunc func(ctx context.Context, channelName string) string
botInChannelFunc func(ctx context.Context, channelID string) bool
botInfoFunc func(ctx context.Context) (*slack.AuthTestResponse, error)
workspaceInfoFunc func(ctx context.Context) (*slack.TeamInfo, error)
publishHomeFunc func(ctx context.Context, userID string, blocks []slack.Block) error
apiFunc func() *slack.Client
// For direct workspace info control
workspaceInfo *slack.TeamInfo
workspaceInfoErr bool
// Tracking for test assertions
postedMessages []mockPostedMessage
updatedMessages []mockUpdatedMessage
updatedDMMessage []mockUpdatedDMMessage
sentDirectMessages []mockSentDirectMessage
}
type mockPostedMessage struct {
ChannelID string
Text string
Attachments []slack.Attachment
}
type mockUpdatedMessage struct {
ChannelID string
Timestamp string
Text string
}
type mockUpdatedDMMessage struct {
UserID string
PRURL string
Text string
}
type mockSentDirectMessage struct {
UserID string
Text string
}
func (m *mockSlackClient) PostThread(ctx context.Context, channelID, text string, attachments []slack.Attachment) (string, error) {
m.mu.Lock()
m.postedMessages = append(m.postedMessages, mockPostedMessage{
ChannelID: channelID,
Text: text,
Attachments: attachments,
})
m.mu.Unlock()
if m.postThreadFunc != nil {
return m.postThreadFunc(ctx, channelID, text, attachments)
}
return "1234567890.123456", nil
}
func (m *mockSlackClient) UpdateMessage(ctx context.Context, channelID, timestamp, text string) error {
m.mu.Lock()
m.updatedMessages = append(m.updatedMessages, mockUpdatedMessage{
ChannelID: channelID,
Timestamp: timestamp,
Text: text,
})
m.mu.Unlock()
if m.updateMessageFunc != nil {
return m.updateMessageFunc(ctx, channelID, timestamp, text)
}
return nil
}
func (m *mockSlackClient) UpdateDMMessage(ctx context.Context, userID, prURL, text string) error {
m.mu.Lock()
m.updatedDMMessage = append(m.updatedDMMessage, mockUpdatedDMMessage{
UserID: userID,
PRURL: prURL,
Text: text,
})
m.mu.Unlock()
if m.updateDMMessageFunc != nil {
return m.updateDMMessageFunc(ctx, userID, prURL, text)
}
return nil
}
//nolint:revive // line length acceptable for interface signature
func (m *mockSlackClient) ChannelHistory(ctx context.Context, channelID string, oldest, latest string, limit int) (*slack.GetConversationHistoryResponse, error) {
if m.channelHistoryFunc != nil {
return m.channelHistoryFunc(ctx, channelID, oldest, latest, limit)
}
return &slack.GetConversationHistoryResponse{}, nil
}
func (m *mockSlackClient) ResolveChannelID(ctx context.Context, channelName string) string {
if m.resolveChannelFunc != nil {
return m.resolveChannelFunc(ctx, channelName)
}
return "C123"
}
func (m *mockSlackClient) IsBotInChannel(ctx context.Context, channelID string) bool {
if m.botInChannelFunc != nil {
return m.botInChannelFunc(ctx, channelID)
}
return true
}
func (m *mockSlackClient) BotInfo(ctx context.Context) (*slack.AuthTestResponse, error) {
if m.botInfoFunc != nil {
return m.botInfoFunc(ctx)
}
return &slack.AuthTestResponse{UserID: "B123"}, nil
}
func (m *mockSlackClient) WorkspaceInfo(ctx context.Context) (*slack.TeamInfo, error) {
if m.workspaceInfoFunc != nil {
return m.workspaceInfoFunc(ctx)
}
if m.workspaceInfoErr {
return nil, errors.New("workspace info error")
}
if m.workspaceInfo != nil {
return m.workspaceInfo, nil
}
return &slack.TeamInfo{}, nil
}
func (m *mockSlackClient) PublishHomeView(ctx context.Context, userID string, blocks []slack.Block) error {
if m.publishHomeFunc != nil {
return m.publishHomeFunc(ctx, userID, blocks)
}
return nil
}
func (m *mockSlackClient) API() *slack.Client {
if m.apiFunc != nil {
return m.apiFunc()
}
return nil
}
// SendDirectMessage sends a DM to a user.
func (m *mockSlackClient) SendDirectMessage(ctx context.Context, userID, text string) (dmChannelID, messageTS string, err error) {
m.mu.Lock()
m.sentDirectMessages = append(m.sentDirectMessages, mockSentDirectMessage{
UserID: userID,
Text: text,
})
m.mu.Unlock()
if m.sendDirectMessageFunc != nil {
return m.sendDirectMessageFunc(ctx, userID, text)
}
return "D" + userID, "1234567890.123456", nil
}
// SendDirectMessageWithBlocks sends a Block Kit DM to a user.
func (*mockSlackClient) SendDirectMessageWithBlocks(
_ context.Context,
userID string,
_ []slack.Block,
) (dmChannelID, messageTS string, err error) {
// Simple mock - just return success
return "D" + userID, "1234567890.123456", nil
}
// IsUserActive checks if a user is currently active.
func (*mockSlackClient) IsUserActive(_ context.Context, _ /* userID */ string) bool {
// Simple mock - always return true (active)
return true
}
// UserTimezone returns the user's IANA timezone.
func (*mockSlackClient) UserTimezone(_ context.Context, _ /* userID */ string) (string, error) {
// Simple mock - return America/New_York
return "America/New_York", nil
}
// IsUserInChannel checks if a user is in a channel.
func (m *mockSlackClient) IsUserInChannel(ctx context.Context, channelID, userID string) bool {
if m.isUserInChannelFunc != nil {
return m.isUserInChannelFunc(ctx, channelID, userID)
}
return false
}
// FindDMMessagesInHistory searches DM history for messages containing a PR URL.
func (m *mockSlackClient) FindDMMessagesInHistory(ctx context.Context, userID, prURL string, since time.Time) ([]slackapi.DMLocation, error) {
if m.findDMMessagesFunc != nil {
return m.findDMMessagesFunc(ctx, userID, prURL, since)
}
// Default: return empty (no DMs found in history)
return nil, nil
}
// mockUserMapper is a simple mock for user mapping in tests.
type mockUserMapper struct {
slackHandleFunc func(ctx context.Context, githubUser, org, domain string) (string, error)
mapping map[string]string // GitHub username -> Slack user ID
failLookups bool // If true, all lookups fail
}
func (m *mockUserMapper) SlackHandle(ctx context.Context, githubUser, org, domain string) (string, error) {
if m.slackHandleFunc != nil {
return m.slackHandleFunc(ctx, githubUser, org, domain)
}
if m.failLookups {
return "", errors.New("user mapping failed")
}
if m.mapping != nil {
if slackID, ok := m.mapping[githubUser]; ok {
return slackID, nil
}
return "", nil // Not found in mapping
}
// Default: return a simple mock Slack user ID based on GitHub username
if githubUser == "_system" {
return "", nil // Skip _system
}
return "U" + githubUser, nil
}
func (m *mockUserMapper) FormatUserMentions(ctx context.Context, githubUsers []string, owner, domain string) string {
mentions := ""
for i, user := range githubUsers {
slackID, err := m.SlackHandle(ctx, user, owner, domain)
if err != nil || slackID == "" {
continue
}
if i > 0 && mentions != "" {
mentions += ", "
}
mentions += "<@" + slackID + ">"
}
return mentions
}
// mockPRSearcher implements PRSearcher interface for testing polling logic.
type mockPRSearcher struct {
listOpenPRsFunc func(ctx context.Context, org string, updatedSinceHours int) ([]github.PRSnapshot, error)
listClosedPRsFunc func(ctx context.Context, org string, updatedSinceHours int) ([]github.PRSnapshot, error)
}
func (m *mockPRSearcher) ListOpenPRs(ctx context.Context, org string, updatedSinceHours int) ([]github.PRSnapshot, error) {
if m.listOpenPRsFunc != nil {
return m.listOpenPRsFunc(ctx, org, updatedSinceHours)
}
return nil, errors.New("mock: ListOpenPRs not configured")
}
func (m *mockPRSearcher) ListClosedPRs(ctx context.Context, org string, updatedSinceHours int) ([]github.PRSnapshot, error) {
if m.listClosedPRsFunc != nil {
return m.listClosedPRsFunc(ctx, org, updatedSinceHours)
}
return nil, errors.New("mock: ListClosedPRs not configured")
}