-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathbot-test-messages.js
More file actions
329 lines (318 loc) · 13.4 KB
/
bot-test-messages.js
File metadata and controls
329 lines (318 loc) · 13.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* Define a common set of tests to exercise the various methods for
* a bot to post a message to a space.
*
* @property {string} testName - Required: mocha test name
* @property {string} botMethod - the method to use to post the message can be:
* 'say', 'sayWithLocalFile', 'uploadStream',
* 'reply', 'replyWithSay', or 'sendCard'.
* If not set or of unknown type bot.say() will be assumed
* @property {bool} shouldFail - set to false if method is expected to fail, otherwise the
* calling program will set this
* @property {string} format - format string to send to bot method
* @property {string} frameworkFormat - format string to be set in framework
* @property {object} msgObject - msg object to post
* @property {string} text - text string to send to bot method
* @property {string} file - filename or url to file
* @property {string} cardJson - path to a card JSON file to post
* @property {string} fallback - fallback text message for sendCard call
* @property {string} parentId - set to empty string if testing a method that acts on a
* previous message like reply or censor. Code will populate
* with appropriate messageId from prior test
* @property {string} parentObj - set to empty object if testing a method that acts on a
* previous message like reply or censor. Code will populate
* with appropriate messageId from prior test
*
* All messages defined here will be used in the following tests:
* - bot-created-room-tests.js
* - bot-membership-rules-tests.js
* - guide-mode-rules-tests.jus
* - user-created-room-tests.js
*
* During iterative development is may be helpful to comment out all but one or
* two problematic phrases while running tests
*/
var common = require('../common/common');
const assert = require('assert');
const when = require('when');
const { cloneDeep } = require('lodash');
module.exports = {
// TODO what should happen if format is set with a msgObject?
botTestMessages: [
{
testName: 'test sets framework.format=text: bot.say(plainText)',
frameworkFormat: 'text',
msgText: 'This message is plain text, inferred from framework\'s messageFormat'
},
{
testName: 'test sets framework.format=markdown: bot.say(markdownText)',
frameworkFormat: 'markdown',
msgText: 'This message is **markdown** text, inferred from framework\'s messageFormat'
},
{
testName: 'sets framework.format=text: bot.say("markdown", markdownText)',
frameworkFormat: 'text',
format: 'markdown',
msgText: 'This message is **markdown** text, explicitly set in the bot.say() call'
},
{
testName: 'sends a file by url: bot.say({text: msg, file: url})',
msgObject: {
text: 'Here is your file!',
file: process.env.HOSTED_FILE
}
},
{
// The hardcoded name of this test is used to save the message
// for a future reply test. If changed, change it in runBotMessagesTest below
testName: 'sends a message for future reply tests: bot.say(plainText)',
frameworkFormat: 'text',
msgText: 'This is the parent message for the reply tests'
},
{
testName: 'replies to parent message it sent using parents msg obj: bot.reply(parentMsgObj, replyText)',
botMethod: 'reply',
parentId: '',
msgText: 'This is the first reply, parent is referenced by ID'
},
{
testName: 'sends a reply with markdown to parent message via ID: bot.reply(parentMsgId, replyMarkdown).',
botMethod: 'reply',
parentId: '',
format: 'markdown',
msgText: 'This is a reply being sent as **markdown text**'
},
{
testName: 'replies via bot.say with a parentId in msgObj: bot.say(msgObjectWithParentId)',
botMethod: 'replyWithSay',
msgObject: {
markdown: 'This is a reply sent via `bot.say()` using a message object with the `parentId` set.',
parentId: 0
}
},
{
// The hardcoded name of this test is used to save the message
// for a future reply test. If changed, change it in runBotMessagesTest below
testName: 'sends a reply message with a file: bot.reply(parentMsgId, replyObjWithFile)',
botMethod: 'reply',
parentId: '',
msgObject: {
text: 'This is a reply sent as a message object that includes a file attachment' +
'Future reply tests will attempt to reply to this reply.',
files: [process.env.HOSTED_FILE]
}
},
{
// The hardcoded name of this test is used to save the message
// for a future reply test. If changed, change it in runBotMessagesTest below
testName: 'replies to parent message it sent referencing the parent by object: bot.reply(parentMsgObj, replyText)',
botMethod: 'reply',
parentObj: {},
msgText: 'This is a reply to the same parent. This time parent is referenced by object.' +
' All subseqent reply test will actually repy to this child message.'
},
{
testName: 'replies to previous reply it sent: bot.reply(prevReplyMsgObj, replyText)',
botMethod: 'reply',
parentObj: {},
msgText: 'This is a reply to the reply!'
},
{
testName: 'replies to a reply with a message object: bot.reply(parentMsgObj, replyMsgObj). ' +
'Messages from bot.reply about ignoring roomId and parentId are expected.',
botMethod: 'reply',
parentObj: {},
msgObject: {
roomId: 'this will be ignored',
markdown: 'This is a reply sent as a message object',
parentId: 'this will be ignored'
}
},
{
testName: 'replies a reply with a reply\'s message ID: bot.reply(parentMsgId, replyMarkdown). SHOULD FAIL',
botMethod: 'reply',
parentId: '',
shouldFail: true,
msgText: 'This is a reply being sent as markdown text'
},
{
testName: 'replies to a reply via bot.say: bot.say(msgObjectWithReplysParentId): SHOULD FAIL',
botMethod: 'replyWithSay',
msgObject: {
markdown: 'This is a reply sent via `bot.say()` using a message object with the `parentId` set.',
parentId: 0
},
shouldFail: true
},
{
testName: 'replies to a reply and explicity sets the format: bot.reply(parentMsgObj, replyMarkdown, "markdown")',
botMethod: 'reply',
format: 'markdown',
parentObj: {},
msgText: 'This is **the final** reply, with the format set explicitly',
},
{
testName: 'tries to reply with an invalid message id: bot.replay("1234", replyMsg): SHOULD FAIL',
botMethod: 'reply',
parentId: '1234',
msgText: 'This reply should never be seen as the parentId is invalid.',
shouldFail: true
},
{
testName: 'sends a local file: bot.sayWithLocalFile(msg, pathToFile)',
botMethod: 'sayWithLocalFile',
file: './test/flint.jpg',
msgText: 'Here is a local file'
},
{
testName: 'sends a local file with an empty message: bot.sayWithLocalFile("", pathToFile)',
botMethod: 'sayWithLocalFile',
file: './test/flint.jpg',
msgText: ''
},
{
testName: 'sends a local file with no message: bot.sayWithLocalFile(null, pathToFile)',
botMethod: 'sayWithLocalFile',
file: './test/flint.jpg'
},
{
testName: 'sends a non available local file: bot.sayWithLocalFile(msgText, "foo.jpg"): SHOULD FAIL',
botMethod: 'sayWithLocalFile',
msgText: 'This file doesn\'t exist',
file: 'foo.jpg',
shouldFail: true
},
{
testName: 'uploads a stream: bot.uploadStream(stream)',
botMethod: 'uploadStream',
file: './test/flint.jpg'
},
{
// The hardcoded name of this test is used to get the id of the message
// that will be censored (deleted)
testName: 'bot sends a message to be censored: bot.say(msgText)',
msgText: 'This message will be censored. Don\'t blink!'
},
{
testName:'bot censors the previous message: bot.censor(parentMessageId)',
botMethod: 'censor',
parentId: ''
},
{
testName:'tries to censor a message that is already deleted: bot.censor(parentMessageId): SHOULD FAIL',
botMethod: 'censor',
shouldFail: true,
parentId: ''
},
{
// The hardcoded name of this test is used to get the id of the card
// to simulate a button press. If changed, change it in runBotMessagesTest below
// Also note that commenting this test out will cause the
// subseqent attachmentAction and bot response test to fail
testName: 'bot sends a get user info card: bot.sendCard(json, fallback)',
botMethod: 'sendCard',
cardJson: '../common/input-card.json',
fallback: 'What is your name?'
}
// TODO add card test without fallback?
],
// External helper function to iterate through all the bot message tests
runBotMessageTests: function(framework, testInfo, botTestMessages, shouldFail=false) {
let cardMsgId = 0;
let parentMsg = null;
let attachmentAction = {};
botTestMessages.forEach((origTest) => {
let test = cloneDeep(origTest);
let testName = test.testName;
// Don't use ES6 arrow functions to access mocha as this
it(testName, function() {
testInfo.config.testName = testName;
if ((testInfo.config?.isDirectTest) && (!common?.botForUser1on1Space)) {
this.skip();
}
if ((('reply' == test.botMethod) ||
('replyWithSay' == test.botMethod) || ('censor' == test.botMethod)) &&
(!shouldFail)) {
assert((parentMsg?.id), `${testInfo.config.testName} did not find ` +
'a parent message to respond to. A previous send message test likely failed.');
if ('replyWithSay' == test.botMethod) {
test.botMethod = 'say';
test.msgObject.parentId = parentMsg.id;
} else if ('parentId' in test) {
test.parentId = parentMsg.id;
} else if ('parentObj' in test) {
test.parentObj = parentMsg;
}
}
return common.botSendsMessage(framework, testInfo, test, shouldFail)
.then((m) => {
test.returnedMessage = m;
// Collect message ids for user reply tests
if (('sends a message for future reply tests: bot.say(plainText)' == test.testName) ||
('sends a reply message with a file: bot.reply(parentMsgId, replyObjWithFile)' == test.testName) ||
('bot sends a message to be censored: bot.say(msgText)' == test.testName)) {
parentMsg = m;
} else if ('bot sends a get user info card: bot.sendCard(json, fallback)' == test.testName) {
cardMsgId = m.id;
}
return when(m);
});
});
});
it('user presses a button on a card (two info messages from framework validate precedence in replies)', function() {
testInfo.config.testName = 'user presses a button on a card';
if ((testInfo.config?.isDirectTest) && (!common?.botForUser1on1Space)) {
this.skip();
}
if (shouldFail) {
// No card sent, just return
return when(true);
}
assert((cardMsgId), `${testInfo.config.testName} did not find ` +
'a card message to respond to. Test to send the card likely failed.');
let person = common.getPersonInfoForUser(testInfo.config.userUnderTest);
//todo change this to use the fetched person
let inputs = {
myName: (person?.displayName) ? person.displayName : 'Test User',
myEmail: (person?.emails.length) ? person.emails[0] : 'test@email.org',
myTel: '555-555-1234'
};
return common.userSendsAttachmentActionAndBotMayRespond(framework, testInfo, cardMsgId, inputs)
.then((a) => {
// TODO - add shouldFail logic here?
// I think this test will simply never be executed
attachmentAction = a;
return when(attachmentAction);
}).catch((e) => {
return when.reject(new Error(`${testInfo.config.testName} failed: `+e.message));
});
});
it('bot responds to card button press', function() {
if ((testInfo.config?.isDirectTest) && (!common?.botForUser1on1Space)) { // no card to respond to in shoudlFail cases
this.skip();
}
if (shouldFail) {
// No card sent, just return
return when(true);
}
assert(((attachmentAction.inputs?.myName) && (attachmentAction.inputs.myEmail)),
'"bot responds to card button press" had no valid attachmentAction object to respond to.');
let test = {
testName: 'bot responds to card button press',
msgText: `Thanks. Now I know your name is ${attachmentAction.inputs.myName}, ` +
`your email is ${attachmentAction.inputs.myEmail}, and your phone is ${attachmentAction.inputs.myTel}.`
};
testInfo.config.testName = test.testName;
return common.botSendsMessage(framework, testInfo, test, shouldFail);
});
it('resets the test data for the next possible run', function() {
if ((testInfo.config?.isDirectTest) && (!common?.botForUser1on1Space)) {
this.skip();
}
cardMsgId = 0;
parentMsg = null;
attachmentAction = {};
return when(true);
});
},
};