-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathbot-direct-message-tests.js
More file actions
314 lines (279 loc) · 11.4 KB
/
bot-direct-message-tests.js
File metadata and controls
314 lines (279 loc) · 11.4 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
// Variables an functions shared by all tests
var common = require('../common/common');
let framework = common.framework;
let userWebex = common.userWebex;
let testInfo = common.testInfo;
let tm = require('../common/test-messages');
let btm = require('../common/bot-test-messages');
let assert = common.assert;
let validator = common.validator;
let when = common.when;
describe('Bot interacts with user in 1-1 space', () => {
let frameworkTestRuns = 0;
let testName = 'Bot 1-1 Space Test';
let message;
let trigger = {};
// // Let's use markdown by default for these test
let origFormat = framework.messageFormat;
framework.messageFormat = 'markdown';
let messageCreatedEvent, frameworkMessageEvent, botMessageEvent;
// Before running tests check if the test bot exists in a 1-1 space with test user
// If not generate an info message that 1-1 tests will be skipped
before(() => {
testInfo.config.isDirectTest = true;
if (!common.botForUser1on1Space) {
console.error('No 1-1 space to run direct message tests. This isn\'t bad, it just is...');
console.error('Since 1-1 spaces can never be removed the tests will not create one.');
console.error('If you want to run the direct message tests, manually create a 1-1 space with your test bot and test user.');
console.error('Otherwise, seeing a bunch of skipped tests is the expected result.');
return;
} else {
testInfo.config.botUnderTest = common.botForUser1on1Space;
testInfo.config.roomUnderTest = common.botForUser1on1Space.room;
testInfo.config.userUnderTest = userWebex;
common.createBotEventHandlers(common.botForUser1on1Space);
}
});
after(() =>{
delete testInfo.config.roomUnderTest;
delete testInfo.config.userUnderTest;
delete testInfo.config.isDirectTest;
});
// Note that each test will be skipped if no 1-1 space
// this.skip syntax does not work with arrow functions,
// hence function() syntax for thes tests
it('checks for persistent storage from previous tests', function () {
if (!common.botForUser1on1Space) {
this.skip();
}
let bot = common.botForUser1on1Space;
if (process.env.MONGO_USER) {
return bot.recall('frameworkTestRuns')
.then((count) => {
frameworkTestRuns = count;
framework.debug(`Found persistent config "frameworkTestRuns": ${count}`);
return Promise.resolve(true);
})
.catch((e) => {
return Promise.reject(new Error(`Did not find persistent config "frameworkTestRuns": ${e.message}` +
' This is expected the first time the test is run'));
});
} else {
framework.debug('Skipping persistent storage test for non Mongo storage provider');
return Promise.resolve(true);
}
});
// TODO - Add code to skip these if no 1-1 space exists
testInfo;
describe('Run standard user and bot message tests in 1-1 space', () => {
describe('User sends message and bot may respond', () => {
// loop through user message tests..
tm.runUserMessageTests(framework, testInfo, tm.testMessages);
///* botShouldRespond = */true);
});
describe('bot sends messages', () => {
// loop through bot message tests..
btm.runBotMessageTests(framework, testInfo, btm.botTestMessages);
///* shouldFail = */false);
});
});
describe('Run direct message only tests in 1-1 space', () => {
// Setup the promises for the events that come from user input that mentions a bot
beforeEach(() => {
message = {};
if (!common.botForUser1on1Space) {
return;
}
// Wait for the events associated with a new message before completing test..
messageCreatedEvent = new Promise((resolve) => {
common.frameworkMessageCreatedEventHandler(framework, testInfo, resolve);
});
frameworkMessageEvent = new Promise((resolve) => {
common.frameworkMessageHandler(framework, testInfo, resolve);
});
botMessageEvent = new Promise((resolve) => {
common.botForUser1on1Space.messageHandler(testInfo, resolve);
});
});
after(() =>
// restore the default framework message format
framework.messageFormat = origFormat
);
it('hears the user without needing to be mentioned', function () {
if (!common.botForUser1on1Space) {
this.skip();
}
testName = 'hears the user without needing to be mentioned';
// Wait for the hears event associated with the input text
const heard = new Promise((resolve) => {
framework.hears(/^DM: hi, /i, (b, t) => {
testInfo.out.got.push('hears(/^DM: hi, /i)');
assert((b.id === common.botForUser1on1Space.id),
'bot returned in fint.hears("hi") is not the one expected');
assert(validator.objIsEqual(t, testInfo.out.trigger),
'trigger returned in framework.hears(/^hi.*/) was not as expected');
assert(t.command == 'DM: Hi, ',
'trigger.command returned in framework.hears(/^DM: hi, /) was not as expected');
assert(t.prompt == 'this is a message with no mentions.',
'trigger.prompt returned in framework.hears(/^DM: hi, /) was not as expected');
trigger = t;
framework.debug('Bot heard message that user posted');
resolve(true);
});
});
// As the user, send the message, mentioning the bot
return userWebex.messages.create({
roomId: common.botForUser1on1Space.room.id,
markdown: 'DM: Hi, this is a message with **no mentions**.'
})
.then((m) => {
message = m;
assert(validator.isMessage(message),
'create message did not return a valid message');
// Wait for all the event handlers and the heard handler to fire
return when.all([messageCreatedEvent, frameworkMessageEvent, botMessageEvent, heard]);
})
.catch((e) => {
console.error(`${testName} failed: ${e.message}`);
return Promise.reject(e);
});
});
it('bot responds with a direct mention via email', function () {
if (!common.botForUser1on1Space) {
this.skip();
}
testName = 'bot responds with a direct mention';
// send the bots response
let msg = 'I heard you';
let email = common.botForUser1on1Space.isDirectTo;
if ((trigger.message) && (trigger.person) &&
(trigger.message.markdown) && (trigger.person.emails[0])) {
msg += ` say: "${trigger.message.markdown}"`;
email = trigger.person.emails[0];
} else {
console.error('Could not read previous test trigger object. Did the test fail?');
}
return common.botForUser1on1Space.dm(email, msg)
.then((m) => {
message = m;
// messages.push(m);
assert(validator.isMessage(message),
'create message did not return a valid message');
// Wait for all the event handlers and the heard handler to fire
return when(messageCreatedEvent);
})
.catch((e) => {
console.error(`${testName} failed: ${e.message}`);
return Promise.reject(e);
});
});
it('bot responds with a direct mention via personId', function () {
if (!common.botForUser1on1Space) {
this.skip();
}
testName = 'bot responds with a direct mention via personId';
// send the bots response
let msg = 'I heard you - by personId this time -';
let personId = common.userPerson.id;
if ((trigger.message) && (trigger.person) && (trigger.message.markdown)) {
msg += ` say: "${trigger.message.markdown}"`;
} else {
console.error('Could not read previous test trigger object. Did the test fail?');
}
return common.botForUser1on1Space.dm(personId, msg)
.then((m) => {
message = m;
// messages.push(m);
assert(validator.isMessage(message),
'create message did not return a valid message');
// Wait for all the event handlers and the heard handler to fire
return when(messageCreatedEvent);
})
.catch((e) => {
console.error(`${testName} failed: ${e.message}`);
return Promise.reject(e);
});
});
it('bot sends a plain text message in the 1-1 space', function () {
if (!common.botForUser1on1Space) {
this.skip();
}
testName = 'bot sends a plain text message in the 1-1 space';
// send the bots response
let msg = 'This is a **plain text** message.';
let personId = common.userPerson.id;
return common.botForUser1on1Space.dm(personId, 'text', msg)
.then((m) => {
message = m;
// messages.push(m);
assert(validator.isMessage(message),
'create message did not return a valid message');
// Wait for all the event handlers and the heard handler to fire
return when(messageCreatedEvent);
})
.catch((e) => {
console.error(`${testName} failed: ${e.message}`);
return Promise.reject(e);
});
});
it('bot sends a plain text message using a message object in the 1-1 space', function () {
if (!common.botForUser1on1Space) {
this.skip();
}
testName = 'bot sends a plain text message using a message object in the 1-1 space';
// send the bots response
let msg = 'This is a **plain text** message sent in the markdown fields of a message request.';
let personId = common.userPerson.id;
return common.botForUser1on1Space.dm(personId, 'text', {markdown: msg})
.then((m) => {
message = m;
// messages.push(m);
assert(validator.isMessage(message),
'create message did not return a valid message');
// Wait for all the event handlers and the heard handler to fire
return when(messageCreatedEvent);
})
.catch((e) => {
console.error(`${testName} failed: ${e.message}`);
return Promise.reject(e);
});
});
it('bot sends a card in the 1-1 space', function () {
if (!common.botForUser1on1Space) {
this.skip();
}
testName = 'bot sends a card in the 1-1 space';
// send the bots response
let cardJson = require('../common/input-card.json');
let personEmail = common.userPerson.emails[0];
return common.botForUser1on1Space.dmCard(personEmail, cardJson, 'What is your name')
.then((m) => {
message = m;
// messages.push(m);
assert(validator.isMessage(message),
'create message did not return a valid message');
// Wait for all the event handlers and the heard handler to fire
return when(messageCreatedEvent);
})
.catch((e) => {
console.error(`${testName} failed: ${e.message}`);
return Promise.reject(e);
});
});
});
it('updates persistent storage for the next tests', function () {
if (!common.botForUser1on1Space) {
this.skip();
}
let bot = common.botForUser1on1Space;
if (process.env.MONGO_USER) {
return bot.store('frameworkTestRuns', frameworkTestRuns + 1)
.catch((e) => {
return Promise.reject(new Error(`Failed to update persistent config "frameworkTestRuns": ${e.message}`));
});
} else {
framework.debug('Skipping persistent storage test for non Mongo storage provider');
return Promise.resolve(true);
}
});
});