-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathEventAssistantTest.java
More file actions
262 lines (241 loc) · 11.8 KB
/
EventAssistantTest.java
File metadata and controls
262 lines (241 loc) · 11.8 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
package test_locally.app;
import com.slack.api.Slack;
import com.slack.api.SlackConfig;
import com.slack.api.app_backend.SlackSignature;
import com.slack.api.bolt.App;
import com.slack.api.bolt.AppConfig;
import com.slack.api.bolt.middleware.builtin.Assistant;
import com.slack.api.bolt.request.RequestHeaders;
import com.slack.api.bolt.request.builtin.EventRequest;
import com.slack.api.bolt.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import util.AuthTestMockServer;
import util.MockSlackApiServer;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@Slf4j
public class EventAssistantTest {
MockSlackApiServer server = new MockSlackApiServer();
SlackConfig config = new SlackConfig();
Slack slack = Slack.getInstance(config);
@Before
public void setup() throws Exception {
server.start();
config.setMethodsEndpointUrlPrefix(server.getMethodsEndpointPrefix());
}
@After
public void tearDown() throws Exception {
server.stop();
}
final String secret = "foo-bar-baz";
final SlackSignature.Generator generator = new SlackSignature.Generator(secret);
@Test
public void test() throws Exception {
App app = buildApp();
AtomicBoolean threadStartedReceived = new AtomicBoolean(false);
AtomicBoolean threadContextChangedReceived = new AtomicBoolean(false);
AtomicBoolean userMessageReceived = new AtomicBoolean(false);
Assistant assistant = new Assistant(app.executorService());
assistant.threadStarted((req, ctx) -> {
try {
ctx.setTitle("title");
ctx.setTitle(r -> r.title("title"));
ctx.setStatus("is typing...");
ctx.setStatus(
"is typing...",
Arrays.asList("Teaching hamsters...", "Untangling cables...")
);
ctx.setStatus(r -> r.status("is typing..."));
ctx.setSuggestedPrompts(new ArrayList<>());
ctx.setSuggestedPrompts(r -> r.prompts(new ArrayList<>()));
} catch (Exception e) {
}
threadStartedReceived.set(true);
});
assistant.threadContextChanged((req, ctx) -> {
threadContextChangedReceived.set(true);
});
assistant.userMessage((req, ctx) -> {
userMessageReceived.set(true);
});
app.assistant(assistant);
String threadStarted = buildPayload("{\n" +
" \"type\": \"assistant_thread_started\",\n" +
" \"assistant_thread\": {\n" +
" \"user_id\": \"W222\",\n" +
" \"context\": {\"channel_id\": \"C222\", \"team_id\": \"T111\", \"enterprise_id\": \"E111\"},\n" +
" \"channel_id\": \"D111\",\n" +
" \"thread_ts\": \"1726133698.626339\"\n" +
" },\n" +
" \"event_ts\": \"1726133698.665188\"\n" +
"}");
Map<String, List<String>> rawHeaders = new HashMap<>();
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
setRequestHeaders(threadStarted, rawHeaders, timestamp);
Response response = app.run(new EventRequest(threadStarted, new RequestHeaders(rawHeaders)));
assertEquals(200L, response.getStatusCode().longValue());
String threadContextChanged = buildPayload("{\n" +
" \"type\": \"assistant_thread_context_changed\",\n" +
" \"assistant_thread\": {\n" +
" \"user_id\": \"W222\",\n" +
" \"context\": {\"channel_id\": \"C333\", \"team_id\": \"T111\", \"enterprise_id\": \"E111\"},\n" +
" \"channel_id\": \"D111\",\n" +
" \"thread_ts\": \"1726133698.626339\"\n" +
" },\n" +
" \"event_ts\": \"1726133698.665188\"\n" +
"}");
rawHeaders = new HashMap<>();
timestamp = String.valueOf(System.currentTimeMillis() / 1000);
setRequestHeaders(threadContextChanged, rawHeaders, timestamp);
response = app.run(new EventRequest(threadContextChanged, new RequestHeaders(rawHeaders)));
assertEquals(200L, response.getStatusCode().longValue());
String userMessage = buildPayload("{\n" +
" \"user\": \"W222\",\n" +
" \"type\": \"message\",\n" +
" \"ts\": \"1726133700.887259\",\n" +
" \"text\": \"When Slack was released?\",\n" +
" \"team\": \"T111\",\n" +
" \"user_team\": \"T111\",\n" +
" \"source_team\": \"T222\",\n" +
" \"user_profile\": {},\n" +
" \"thread_ts\": \"1726133698.626339\",\n" +
" \"parent_user_id\": \"W222\",\n" +
" \"channel\": \"D111\",\n" +
" \"event_ts\": \"1726133700.887259\",\n" +
" \"channel_type\": \"im\"\n" +
"}");
rawHeaders = new HashMap<>();
timestamp = String.valueOf(System.currentTimeMillis() / 1000);
setRequestHeaders(userMessage, rawHeaders, timestamp);
response = app.run(new EventRequest(userMessage, new RequestHeaders(rawHeaders)));
assertEquals(200L, response.getStatusCode().longValue());
String userMessageWithAssistantThread = buildPayload("{\n" +
" \"user\": \"W222\",\n" +
" \"type\": \"message\",\n" +
" \"ts\": \"1726133700.887259\",\n" +
" \"text\": \"When Slack was released?\",\n" +
" \"team\": \"T111\",\n" +
" \"user_team\": \"T111\",\n" +
" \"source_team\": \"T222\",\n" +
" \"user_profile\": {},\n" +
" \"thread_ts\": \"1726133698.626339\",\n" +
" \"parent_user_id\": \"W222\",\n" +
" \"channel\": \"D111\",\n" +
" \"event_ts\": \"1726133700.887259\",\n" +
" \"assistant_thread\": {\"XXX\": \"YYY\"},\n" +
" \"channel_type\": \"im\"\n" +
"}");
rawHeaders = new HashMap<>();
timestamp = String.valueOf(System.currentTimeMillis() / 1000);
setRequestHeaders(userMessageWithAssistantThread, rawHeaders, timestamp);
response = app.run(new EventRequest(userMessageWithAssistantThread, new RequestHeaders(rawHeaders)));
assertEquals(200L, response.getStatusCode().longValue());
String assistantMessageChanged = buildPayload("{\n" +
" \"type\": \"message\",\n" +
" \"subtype\": \"message_changed\",\n" +
" \"message\": {\n" +
" \"text\": \"New chat\",\n" +
" \"subtype\": \"assistant_app_thread\",\n" +
" \"user\": \"U222\",\n" +
" \"type\": \"message\",\n" +
" \"edited\": {},\n" +
" \"thread_ts\": \"1726133698.626339\",\n" +
" \"reply_count\": 2,\n" +
" \"reply_users_count\": 2,\n" +
" \"latest_reply\": \"1726133700.887259\",\n" +
" \"reply_users\": [\"U222\", \"W111\"],\n" +
" \"is_locked\": false,\n" +
" \"assistant_app_thread\": {\"title\": \"When Slack was released?\", \"title_blocks\": [], \"artifacts\": []},\n" +
" \"ts\": \"1726133698.626339\"\n" +
" },\n" +
" \"previous_message\": {\n" +
" \"text\": \"New chat\",\n" +
" \"subtype\": \"assistant_app_thread\",\n" +
" \"user\": \"U222\",\n" +
" \"type\": \"message\",\n" +
" \"edited\": {},\n" +
" \"thread_ts\": \"1726133698.626339\",\n" +
" \"reply_count\": 2,\n" +
" \"reply_users_count\": 2,\n" +
" \"latest_reply\": \"1726133700.887259\",\n" +
" \"reply_users\": [\"U222\", \"W111\"],\n" +
" \"is_locked\": false\n" +
" },\n" +
" \"channel\": \"D111\",\n" +
" \"hidden\": true,\n" +
" \"ts\": \"1726133701.028300\",\n" +
" \"event_ts\": \"1726133701.028300\",\n" +
" \"channel_type\": \"im\"\n" +
"}");
rawHeaders = new HashMap<>();
timestamp = String.valueOf(System.currentTimeMillis() / 1000);
setRequestHeaders(assistantMessageChanged, rawHeaders, timestamp);
response = app.run(new EventRequest(assistantMessageChanged, new RequestHeaders(rawHeaders)));
assertEquals(200L, response.getStatusCode().longValue());
String channelMessage = buildPayload("{\n" +
" \"user\": \"W222\",\n" +
" \"type\": \"message\",\n" +
" \"ts\": \"1726133700.887259\",\n" +
" \"text\": \"When Slack was released?\",\n" +
" \"team\": \"T111\",\n" +
" \"user_team\": \"T111\",\n" +
" \"source_team\": \"T222\",\n" +
" \"user_profile\": {},\n" +
" \"thread_ts\": \"1726133698.626339\",\n" +
" \"parent_user_id\": \"W222\",\n" +
" \"channel\": \"D111\",\n" +
" \"event_ts\": \"1726133700.887259\",\n" +
" \"channel_type\": \"channel\"\n" +
"}");
rawHeaders = new HashMap<>();
timestamp = String.valueOf(System.currentTimeMillis() / 1000);
setRequestHeaders(channelMessage, rawHeaders, timestamp);
response = app.run(new EventRequest(channelMessage, new RequestHeaders(rawHeaders)));
assertEquals(404L, response.getStatusCode().longValue());
int count = 0;
while (count < 100 && (!threadStartedReceived.get() || !threadContextChangedReceived.get() || !userMessageReceived.get())) {
Thread.sleep(10L);
count++;
}
assertTrue(threadStartedReceived.get());
assertTrue(threadContextChangedReceived.get());
assertTrue(userMessageReceived.get());
}
App buildApp() {
return new App(AppConfig.builder()
.signingSecret(secret)
.singleTeamBotToken(AuthTestMockServer.ValidToken)
.slack(slack)
.build());
}
String buildPayload(String event) {
return "{\n" +
" \"token\": \"verification_token\",\n" +
" \"team_id\": \"T111\",\n" +
" \"enterprise_id\": \"E111\",\n" +
" \"api_app_id\": \"A111\",\n" +
" \"event\": " + event + ",\n" +
" \"type\": \"event_callback\",\n" +
" \"event_id\": \"Ev111\",\n" +
" \"event_time\": 1599616881,\n" +
" \"authorizations\": [\n" +
" {\n" +
" \"enterprise_id\": \"E111\",\n" +
" \"team_id\": \"T111\",\n" +
" \"user_id\": \"W111\",\n" +
" \"is_bot\": true,\n" +
" \"is_enterprise_install\": false\n" +
" }\n" +
" ]\n" +
"}";
}
void setRequestHeaders(String requestBody, Map<String, List<String>> rawHeaders, String timestamp) {
rawHeaders.put(SlackSignature.HeaderNames.X_SLACK_REQUEST_TIMESTAMP, Collections.singletonList(timestamp));
rawHeaders.put(SlackSignature.HeaderNames.X_SLACK_SIGNATURE, Collections.singletonList(generator.generate(timestamp, requestBody)));
}
}