-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot_test.go
More file actions
311 lines (250 loc) · 7.42 KB
/
bot_test.go
File metadata and controls
311 lines (250 loc) · 7.42 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
package bot
import (
"context"
"errors"
"os"
"testing"
"github.com/codeGROOVE-dev/slacker/pkg/bot/cache"
"github.com/slack-go/slack"
)
// TestMain sets up the test environment before running tests.
func TestMain(m *testing.M) {
// Create a shared mock turnclient server for all tests
mockServer := mockTurnServer(&testing.T{})
// Set environment variable so all turnclient calls use the mock
if err := os.Setenv("TURN_TEST_BACKEND", mockServer.URL); err != nil {
panic("failed to set TURN_TEST_BACKEND: " + err.Error())
}
// Run tests
code := m.Run()
// Cleanup (before os.Exit to avoid exitAfterDefer lint error)
mockServer.Close()
_ = os.Unsetenv("TURN_TEST_BACKEND") //nolint:errcheck // Best effort cleanup
os.Exit(code)
}
func TestNew(t *testing.T) {
ctx := context.Background()
mockSlack := &mockSlackClient{
workspaceInfo: &slack.TeamInfo{
ID: "T123456",
Name: "Test Workspace",
Domain: "testworkspace",
},
}
mockGH := &mockGitHub{
org: "testorg",
token: "test-token",
}
configMgr := NewMockConfig().Build()
stateStore := &mockStateStore{
processedEvents: make(map[string]bool),
}
coordinator := New(
ctx,
mockSlack,
mockGH,
configMgr,
nil, // notifier not needed for this test
"wss://test.example.com",
stateStore,
)
if coordinator == nil {
t.Fatal("expected non-nil coordinator")
}
if coordinator.slack != mockSlack {
t.Error("slack client not set correctly")
}
if coordinator.github != mockGH {
t.Error("github client not set correctly")
}
if coordinator.configManager != configMgr {
t.Error("config manager not set correctly")
}
if coordinator.sprinklerURL != "wss://test.example.com" {
t.Errorf("expected sprinklerURL 'wss://test.example.com', got %s", coordinator.sprinklerURL)
}
if coordinator.stateStore != stateStore {
t.Error("state store not set correctly")
}
if coordinator.workspaceName != "testworkspace.slack.com" {
t.Errorf("expected workspace name 'testworkspace.slack.com', got %s", coordinator.workspaceName)
}
if coordinator.threadCache == nil {
t.Error("thread cache not initialized")
}
if coordinator.eventSemaphore == nil {
t.Error("event semaphore not initialized")
}
if cap(coordinator.eventSemaphore) != 10 {
t.Errorf("expected event semaphore capacity 10, got %d", cap(coordinator.eventSemaphore))
}
if coordinator.userMapper == nil {
t.Error("user mapper not initialized")
}
}
func TestNew_WorkspaceInfoFailure(t *testing.T) {
ctx := context.Background()
mockSlack := &mockSlackClient{
workspaceInfo: nil, // Will cause error
workspaceInfoErr: true,
}
mockGH := &mockGitHub{
org: "testorg",
token: "test-token",
}
configMgr := NewMockConfig().Build()
stateStore := &mockStateStore{
processedEvents: make(map[string]bool),
}
coordinator := New(
ctx,
mockSlack,
mockGH,
configMgr,
nil, // notifier not needed for this test
"wss://test.example.com",
stateStore,
)
// Should still create coordinator even if workspace info fails
if coordinator == nil {
t.Fatal("expected non-nil coordinator")
}
// Workspace name should be empty when workspace info fails
if coordinator.workspaceName != "" {
t.Errorf("expected empty workspace name on error, got %s", coordinator.workspaceName)
}
}
func TestNew_WithGitHubClient(t *testing.T) {
ctx := context.Background()
fakeGHClient := struct{}{} // Fake github client
mockSlack := &mockSlackClient{
workspaceInfo: &slack.TeamInfo{
ID: "T123456",
Name: "Test Workspace",
Domain: "testworkspace",
},
}
mockGH := &mockGitHub{
org: "testorg",
token: "test-token",
client: fakeGHClient,
}
configMgr := NewMockConfig().Build()
stateStore := &mockStateStore{
processedEvents: make(map[string]bool),
}
coordinator := New(
ctx,
mockSlack,
mockGH,
configMgr,
nil,
"wss://test.example.com",
stateStore,
)
if coordinator == nil {
t.Fatal("expected non-nil coordinator")
}
// The GitHub client should have been set in the config manager
// This tests the if ghClient != nil branch
}
func TestSaveThread(t *testing.T) {
ctx := context.Background()
mockSlack := &mockSlackClient{}
configMgr := NewMockConfig().Build()
mockState := &mockStateStore{
processedEvents: make(map[string]bool),
threads: make(map[string]cache.ThreadInfo),
}
c := &Coordinator{
github: &mockGitHub{org: "testorg", token: "test-token"},
slack: mockSlack,
stateStore: mockState,
configManager: configMgr,
notifier: nil, // notifier not needed for this test
threadCache: cache.New(),
eventSemaphore: make(chan struct{}, 10),
}
threadInfo := cache.ThreadInfo{
ChannelID: "C123456",
ThreadTS: "1234567890.123456",
}
c.saveThread(ctx, "testorg", "testrepo", 42, "C123456", threadInfo)
// Check cache
key := "testorg/testrepo#42:C123456"
cached, found := c.threadCache.Get(key)
if !found {
t.Error("expected thread to be in cache")
}
if cached.ChannelID != threadInfo.ChannelID {
t.Errorf("expected channel ID %s, got %s", threadInfo.ChannelID, cached.ChannelID)
}
if cached.ThreadTS != threadInfo.ThreadTS {
t.Errorf("expected thread TS %s, got %s", threadInfo.ThreadTS, cached.ThreadTS)
}
// Check persistent storage
persistedKey := "testorg/testrepo#42:C123456"
persistedInfo, ok := mockState.threads[persistedKey]
if !ok {
t.Error("expected thread to be in persistent storage")
}
if persistedInfo.ChannelID != threadInfo.ChannelID {
t.Errorf("expected persisted channel ID %s, got %s", threadInfo.ChannelID, persistedInfo.ChannelID)
}
}
func TestSaveThread_PersistenceError(t *testing.T) {
ctx := context.Background()
mockSlack := &mockSlackClient{}
configMgr := NewMockConfig().Build()
mockState := &mockStateStore{
processedEvents: make(map[string]bool),
threads: make(map[string]cache.ThreadInfo),
saveThreadErr: errors.New("database error"),
}
c := &Coordinator{
github: &mockGitHub{org: "testorg", token: "test-token"},
slack: mockSlack,
stateStore: mockState,
configManager: configMgr,
notifier: nil,
threadCache: cache.New(),
eventSemaphore: make(chan struct{}, 10),
}
threadInfo := cache.ThreadInfo{
ChannelID: "C123456",
ThreadTS: "1234567890.123456",
}
// Should still save to cache even if persistence fails
c.saveThread(ctx, "testorg", "testrepo", 42, "C123456", threadInfo)
// Check cache (should succeed)
key := "testorg/testrepo#42:C123456"
cached, found := c.threadCache.Get(key)
if !found {
t.Error("expected thread to be in cache even when persistence fails")
}
if cached.ChannelID != threadInfo.ChannelID {
t.Errorf("expected channel ID %s, got %s", threadInfo.ChannelID, cached.ChannelID)
}
// Check persistent storage (should fail - nothing saved)
persistedKey := "thread:testorg/testrepo#42:C123456"
if _, ok := mockState.threads[persistedKey]; ok {
t.Error("expected thread NOT to be in persistent storage after error")
}
}
func TestThreadCache_Set(t *testing.T) {
threadCache := cache.New()
threadInfo := cache.ThreadInfo{
ChannelID: "C123456",
ThreadTS: "1234567890.123456",
MessageText: "Test message",
LastState: "awaiting_review",
}
threadCache.Set("testorg/testrepo#42", threadInfo)
retrieved, found := threadCache.Get("testorg/testrepo#42")
if !found {
t.Error("expected to find thread in cache")
}
if retrieved.ChannelID != threadInfo.ChannelID {
t.Errorf("expected channel ID %s, got %s", threadInfo.ChannelID, retrieved.ChannelID)
}
}