forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassistant_test.go
More file actions
275 lines (229 loc) · 7.97 KB
/
assistant_test.go
File metadata and controls
275 lines (229 loc) · 7.97 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
package slack
import (
"encoding/json"
"net/http"
"net/url"
"testing"
)
func TestAssistantThreadsSuggestedPrompts(t *testing.T) {
http.HandleFunc("/assistant.threads.setSuggestedPrompts", okJSONHandler)
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
params := AssistantThreadsSetSuggestedPromptsParameters{
ChannelID: "CXXXXXXXX",
ThreadTS: "1234567890.123456",
}
params.AddPrompt("title1", "message1")
params.AddPrompt("title2", "message2")
err := api.SetAssistantThreadsSuggestedPrompts(params)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
}
func TestSetAssistantThreadsStatus(t *testing.T) {
http.HandleFunc("/assistant.threads.setStatus", okJSONHandler)
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
params := AssistantThreadsSetStatusParameters{
ChannelID: "CXXXXXXXX",
ThreadTS: "1234567890.123456",
Status: "updated status",
LoadingMessages: []string{"updating status..."},
}
err := api.SetAssistantThreadsStatus(params)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
}
func assistantThreadsTitleHandler(rw http.ResponseWriter, r *http.Request) {
channelID := r.FormValue("channel_id")
threadTS := r.FormValue("thread_ts")
title := r.FormValue("title")
rw.Header().Set("Content-Type", "application/json")
if channelID != "" && threadTS != "" && title != "" {
resp, _ := json.Marshal(&addBookmarkResponse{
SlackResponse: SlackResponse{Ok: true},
})
rw.Write(resp)
} else {
rw.Write([]byte(`{ "ok": false, "error": "errored" }`))
}
}
func TestSetAssistantThreadsTitle(t *testing.T) {
http.HandleFunc("/assistant.threads.setTitle", assistantThreadsTitleHandler)
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
params := AssistantThreadsSetTitleParameters{
ChannelID: "CXXXXXXXX",
ThreadTS: "1234567890.123456",
Title: "updated title",
}
err := api.SetAssistantThreadsTitle(params)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
}
func assistantSearchContextHandler(rw http.ResponseWriter, r *http.Request) {
query := r.FormValue("query")
rw.Header().Set("Content-Type", "application/json")
if query != "" {
resp, _ := json.Marshal(&AssistantSearchContextResponse{
SlackResponse: SlackResponse{Ok: true},
Results: AssistantSearchContextResults{
Messages: []AssistantSearchContextMessage{
{
AuthorUserID: "U1234567890",
TeamID: "T1234567890",
ChannelID: "C1234567890",
MessageTS: "1234567890.123456",
Content: "This is a test message",
IsAuthorBot: false,
Permalink: "https://example.slack.com/archives/C1234567890/p1234567890123456",
},
},
},
ResponseMetadata: struct {
NextCursor string `json:"next_cursor"`
}{
NextCursor: "next_cursor_value",
},
})
rw.Write(resp)
} else {
rw.Write([]byte(`{ "ok": false, "error": "invalid_arguments" }`))
}
}
func TestSearchAssistantContext(t *testing.T) {
http.HandleFunc("/assistant.search.context", assistantSearchContextHandler)
once.Do(startServer)
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))
params := AssistantSearchContextParameters{
Query: "test query",
ActionToken: "test_action_token",
ChannelTypes: []string{"public_channel", "private_channel"},
ContentTypes: []string{"messages"},
ContextChannelID: "C1234567890",
Cursor: "cursor_value",
IncludeBots: true,
Limit: 10,
}
response, err := api.SearchAssistantContext(params)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if !response.Ok {
t.Fatalf("Expected Ok to be true")
}
if len(response.Results.Messages) != 1 {
t.Fatalf("Expected 1 message, got %d", len(response.Results.Messages))
}
message := response.Results.Messages[0]
if message.AuthorUserID != "U1234567890" {
t.Fatalf("Expected AuthorUserID to be U1234567890, got %s", message.AuthorUserID)
}
if message.TeamID != "T1234567890" {
t.Fatalf("Expected TeamID to be T1234567890, got %s", message.TeamID)
}
if message.ChannelID != "C1234567890" {
t.Fatalf("Expected ChannelID to be C1234567890, got %s", message.ChannelID)
}
if message.MessageTS != "1234567890.123456" {
t.Fatalf("Expected MessageTS to be '1234567890.123456', got %s", message.MessageTS)
}
if message.Content != "This is a test message" {
t.Fatalf("Expected Content to be 'This is a test message', got %s", message.Content)
}
if message.IsAuthorBot != false {
t.Fatalf("Expected IsAuthorBot to be false, got %v", message.IsAuthorBot)
}
if response.ResponseMetadata.NextCursor != "next_cursor_value" {
t.Fatalf("Expected NextCursor to be 'next_cursor_value', got %s", response.ResponseMetadata.NextCursor)
}
}
func assistantSearchContextHandlerWithNewParams(rw http.ResponseWriter, r *http.Request) {
query := r.FormValue("query")
actionToken := r.FormValue("action_token")
contextChannelID := r.FormValue("context_channel_id")
includeBots := r.FormValue("include_bots")
rw.Header().Set("Content-Type", "application/json")
if query != "" && actionToken != "" && contextChannelID != "" && includeBots == "true" {
resp, _ := json.Marshal(&AssistantSearchContextResponse{
SlackResponse: SlackResponse{Ok: true},
Results: AssistantSearchContextResults{
Messages: []AssistantSearchContextMessage{
{
AuthorUserID: "U1234567890",
TeamID: "T0987654321",
ChannelID: contextChannelID, // Use the provided context channel ID
MessageTS: "1234567890.123456",
Content: "This is a test message with new parameters",
IsAuthorBot: true, // Test with bot message
Permalink: "https://example.slack.com/archives/" + contextChannelID + "/p1234567890123456",
},
},
},
ResponseMetadata: struct {
NextCursor string `json:"next_cursor"`
}{
NextCursor: "next_cursor_value_new",
},
})
rw.Write(resp)
} else {
rw.Write([]byte(`{ "ok": false, "error": "missing_required_parameters" }`))
}
}
func TestSearchAssistantContextWithNewParameters(t *testing.T) {
http.HandleFunc("/assistant.search.context.new", assistantSearchContextHandlerWithNewParams)
once.Do(startServer)
// Test with new parameters
params := AssistantSearchContextParameters{
Query: "test query with new params",
ActionToken: "action_token_123",
ChannelTypes: []string{"public_channel"},
ContentTypes: []string{"messages"},
ContextChannelID: "C0987654321",
IncludeBots: true,
Limit: 5,
}
// We need to temporarily change the API method for this test
// Since we can't easily override the method, we'll test the parameter building logic
// Create a custom client to test parameter handling
values := url.Values{"token": {"testing-token"}}
values.Add("query", params.Query)
if params.ActionToken != "" {
values.Add("action_token", params.ActionToken)
}
if len(params.ChannelTypes) > 0 {
for _, channelType := range params.ChannelTypes {
values.Add("channel_types", channelType)
}
}
if len(params.ContentTypes) > 0 {
for _, contentType := range params.ContentTypes {
values.Add("content_types", contentType)
}
}
if params.ContextChannelID != "" {
values.Add("context_channel_id", params.ContextChannelID)
}
if params.IncludeBots {
values.Add("include_bots", "true")
}
if params.Limit > 0 {
values.Add("limit", "5")
}
// Verify all parameters are set correctly
if values.Get("action_token") != "action_token_123" {
t.Errorf("Expected action_token to be 'action_token_123', got %s", values.Get("action_token"))
}
if values.Get("context_channel_id") != "C0987654321" {
t.Errorf("Expected context_channel_id to be 'C0987654321', got %s", values.Get("context_channel_id"))
}
if values.Get("include_bots") != "true" {
t.Errorf("Expected include_bots to be 'true', got %s", values.Get("include_bots"))
}
if values.Get("limit") != "5" {
t.Errorf("Expected limit to be '5', got %s", values.Get("limit"))
}
}