-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
312 lines (274 loc) · 8.14 KB
/
Copy pathmain_test.go
File metadata and controls
312 lines (274 loc) · 8.14 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
package main
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
)
type fakeSlackClient struct {
messages []string
calls int32
}
func (f *fakeSlackClient) PostMessageContext(ctx context.Context, channel string, options ...slack.MsgOption) (string, string, error) {
atomic.AddInt32(&f.calls, 1)
f.messages = append(f.messages, "message sent")
return "", "", nil
}
func TestWorkerPool_SubmitAndShutdown(t *testing.T) {
var count int32
pool := NewWorkerPool(3)
for i := 0; i < 10; i++ {
pool.Submit(func() {
atomic.AddInt32(&count, 1)
})
}
pool.Shutdown()
if count != 10 {
t.Errorf("expected 10 tasks to run, got %d", count)
}
}
func TestWorkerPool_ConcurrentSubmit(t *testing.T) {
var count int32
pool := NewWorkerPool(5)
for i := 0; i < 50; i++ {
pool.Submit(func() {
time.Sleep(2 * time.Millisecond)
atomic.AddInt32(&count, 1)
})
}
pool.Shutdown()
if count != 50 {
t.Errorf("expected 50 tasks to run, got %d", count)
}
}
func TestProcessMention_SubmitsTaskForValidQuery(t *testing.T) {
// Setup test backend
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(ChatResponse{Full: "Test backend reply."})
}))
defer ts.Close()
config.BackendURL = ts.URL
api := &fakeSlackClient{}
pool := NewWorkerPool(1)
defer pool.Shutdown()
ev := slackevents.AppMentionEvent{
User: "U123",
Channel: "C123",
BotID: "B456",
Text: "<@B456> What is Go?",
}
processMention(context.Background(), api, ev, pool)
time.Sleep(100 * time.Millisecond)
if atomic.LoadInt32(&api.calls) == 0 {
t.Error("expected processTask (via PostMessageContext) to be called")
}
}
func TestProcessMention_DoesNotSubmitForEmptyQuery(t *testing.T) {
api := &fakeSlackClient{}
pool := NewWorkerPool(1)
defer pool.Shutdown()
ev := slackevents.AppMentionEvent{
User: "U123",
Channel: "C123",
BotID: "B456",
Text: "<@B456> ",
}
processMention(context.Background(), api, ev, pool)
time.Sleep(10 * time.Millisecond)
if atomic.LoadInt32(&api.calls) != 0 {
t.Error("processTask should not be called for empty query")
}
}
func TestProcessTask_JSONResponse(t *testing.T) {
// Mock backend server returns JSON
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(ChatResponse{
Full: "Sentence one. Sentence two.",
})
})
ts := httptest.NewServer(handler)
defer ts.Close()
config.BackendURL = ts.URL
api := &fakeSlackClient{}
ev := slackevents.AppMentionEvent{
User: "U1",
Channel: "C1",
Text: "foo",
}
processTask(context.Background(), api, ev, "foo")
time.Sleep(10 * time.Millisecond)
if len(api.messages) == 0 {
t.Errorf("expected at least one message to be sent, got %v", api.messages)
}
}
func TestProcessTask_SSEResponse(t *testing.T) {
// Mock backend server returns SSE
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
flusher, _ := w.(http.Flusher)
responses := []ChatResponse{
{ID: 1, Event: "message_part", Text: "part1"},
{ID: 2, Event: "message_part", Text: "part2"},
{ID: 3, Event: "stream_end", Status: "done"},
}
for _, resp := range responses {
data, _ := json.Marshal(resp)
w.Write([]byte("data: " + string(data) + "\n\n"))
flusher.Flush()
}
})
ts := httptest.NewServer(handler)
defer ts.Close()
config.BackendURL = ts.URL
api := &fakeSlackClient{}
ev := slackevents.AppMentionEvent{
User: "U1",
Channel: "C1",
Text: "foo",
}
processTask(context.Background(), api, ev, "foo")
time.Sleep(10 * time.Millisecond)
if len(api.messages) == 0 {
t.Errorf("expected at least one message to be sent, got %v", api.messages)
}
}
func TestProcessDirectMessage_ValidDM(t *testing.T) {
// 1. Setup test backend
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(ChatResponse{Full: "Test backend reply."})
}))
defer ts.Close()
config.BackendURL = ts.URL
// 2. Setup fake Slack client and pool
api := &fakeSlackClient{}
pool := NewWorkerPool(1)
defer pool.Shutdown()
// 3. Create a DM event
ev := &slackevents.MessageEvent{
User: "U1",
Text: "hello",
Channel: "C1",
ChannelType: "im",
}
// 4. Call the function under test
processDirectMessage(context.Background(), api, ev, pool)
time.Sleep(100 * time.Millisecond)
if atomic.LoadInt32(&api.calls) == 0 {
t.Error("expected message to be sent for valid DM")
}
}
func TestProcessDirectMessage_IgnoresBotOrNonIM(t *testing.T) {
api := &fakeSlackClient{}
pool := NewWorkerPool(1)
defer pool.Shutdown()
ev := &slackevents.MessageEvent{
User: "U1",
BotID: "B1",
Text: "hello",
Channel: "C1",
ChannelType: "im",
}
processDirectMessage(context.Background(), api, ev, pool)
time.Sleep(10 * time.Millisecond)
if atomic.LoadInt32(&api.calls) != 0 {
t.Error("should not send message for bot")
}
ev = &slackevents.MessageEvent{
User: "U1",
Text: "hello",
Channel: "C1",
ChannelType: "channel",
}
processDirectMessage(context.Background(), api, ev, pool)
time.Sleep(10 * time.Millisecond)
if atomic.LoadInt32(&api.calls) != 0 {
t.Error("should not send message for non-IM")
}
}
func TestBackendRequestFormatting(t *testing.T) {
ev := slackevents.AppMentionEvent{
User: "U1",
Channel: "C1",
Text: "Hello",
}
query := "Hello"
reqBody, _ := json.Marshal(ChatRequest{
UserID: ev.User,
Query: query,
ChannelID: ev.Channel,
})
var req ChatRequest
if err := json.Unmarshal(reqBody, &req); err != nil {
t.Fatalf("failed to unmarshal: %v", err)
}
if req.UserID != "U1" || req.Query != "Hello" || req.ChannelID != "C1" {
t.Errorf("unexpected request formatting: %+v", req)
}
}
func TestMockBackend_JSONResponse(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req ChatRequest
json.NewDecoder(r.Body).Decode(&req)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(ChatResponse{
Full: "Complete response to 'foo': Goroutines enable concurrency in Go",
})
})
ts := httptest.NewServer(handler)
defer ts.Close()
body := `{"user_id":"U1","query":"foo","channel_id":"C1"}`
req, _ := http.NewRequest("POST", ts.URL, strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("http request failed: %v", err)
}
defer resp.Body.Close()
var chatResp ChatResponse
if err := json.NewDecoder(resp.Body).Decode(&chatResp); err != nil {
t.Fatalf("failed to decode: %v", err)
}
if !strings.Contains(chatResp.Full, "Goroutines enable concurrency") {
t.Errorf("unexpected response: %+v", chatResp)
}
}
func TestMockBackend_SSEResponse(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
flusher, _ := w.(http.Flusher)
responses := []ChatResponse{
{ID: 1, Event: "message_part", Text: "Processing: foo"},
{ID: 2, Event: "message_part", Text: "Goroutines are lightweight threads"},
{ID: 3, Event: "stream_end", Status: "done"},
}
for _, resp := range responses {
data, _ := json.Marshal(resp)
w.Write([]byte("data: " + string(data) + "\n\n"))
flusher.Flush()
}
})
ts := httptest.NewServer(handler)
defer ts.Close()
body := `{"user_id":"U1","query":"foo","channel_id":"C1"}`
req, _ := http.NewRequest("POST", ts.URL, strings.NewReader(body))
req.Header.Set("Accept", "text/event-stream")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("http request failed: %v", err)
}
defer resp.Body.Close()
bodyBytes, _ := io.ReadAll(resp.Body)
if !strings.Contains(string(bodyBytes), "Goroutines are lightweight threads") {
t.Error("expected SSE stream to contain 'Goroutines are lightweight threads'")
}
}