-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathlate-discovery-tests.js
More file actions
142 lines (128 loc) · 5.7 KB
/
late-discovery-tests.js
File metadata and controls
142 lines (128 loc) · 5.7 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
/* late-discovery-tests.js
*
* A set of tests to validate framework functionality
* to create bot objects on the fly as opposed
* to on startup.
*
* These tests require that the bot exist in a one on one
* space with the test user
*/
const Framework = require('../lib/framework');
const Webex = require('webex');
var common = require('./common/common');
let testInfo = common.testInfo;
let assert = common.assert;
let when = common.when;
const _ = require('lodash');
console.log('**********************************************');
console.log('* Framework tests with late space discovery...');
console.log('**********************************************\n');
// Initialize the framework and user objects once for all the tests
let framework;
let frameworkOptions = {};
require('dotenv').config();
if ((typeof process.env.BOT_API_TOKEN === 'string') &&
(typeof process.env.USER_API_TOKEN === 'string') &&
(typeof process.env.HOSTED_FILE === 'string')) {
frameworkOptions.token = process.env.BOT_API_TOKEN;
// This is the key to these tests, we wont discover any spaces on
// startup, just when a message:created event occurs
frameworkOptions.maxStartupSpaces = 0;
// Enable Message Process Speed Profiling in tests
frameworkOptions.profileMsgProcessingTime = true;
framework = new Framework(frameworkOptions);
} else {
console.error('Missing required environment variables:\n' +
'- BOT_API_TOKEN -- token associatd with an existing bot\n' +
'- USER_API_TOKEN -- token associated with an existing user\n' +
'- HOSTED_FILE -- url to a file that can be attached to test messages\n' +
'The tests will create a new space with the bot and the user');
process.exit(-1);
}
// Initialize the SDK for the user and set them in the test's common object
let userOptions = {credentials: {access_token: process.env.USER_API_TOKEN}};
let userWebex = Webex.init(userOptions);
common.setFramework(framework);
common.setUser(userWebex);
// Initialize the instance of framework that we will use across multiple tests
describe('#framework', function() {
common.setMochaTimeout(this.timeout());
before(() => {
return common.initFramework('framework init', framework, userWebex)
.then(() => {
// Validate that the framework has no spawned bots initially
if (framework.bots.length) {
return when.reject(new Error('Framework.init() spawned bots despite maxStartupSpaces=0'));
}
console.log('Validated that framework initialized with no spawned bots as expected');
// Set a spawn handler to track the just-in-time spawned bot
framework.on('spawn', (bot, frameworkId) => {
console.log('Validated that a spawn event occurred after message was sent.');
assert((frameworkId === framework.id),
`In ${testInfo.config.testName}, the frameworkId passed to the spawned handler was not as expected`);
});
});
});
before(() => {
// Before starting the test validate that the test user has an
// existing 1-1 space with the test bot
let directSpaceExists = false;
return userWebex.memberships.list()
.then((m) => {
let directSpaces = _.filter(m.items, space => (space.roomType === 'direct'));
if (!directSpaces.length) {
return when.reject(new Error('Late Discovery tests only work if the test user and bot have an existing 1-1 space.'));
}
// Build a call to get memberships for all direct spaces
let lookupMemberships = _.map(directSpaces, s => {
return userWebex.memberships.list({roomId: s.roomId})
.then((m) => {
let theBot = _.filter(m.items, member => (member.personId == framework.person.id));
if (theBot.length) {
// Found a direct space with the bot, lets proceed!
directSpaceExists = true;
}
return when(true);
}).catch(() => when(true));
});
return when.all(lookupMemberships);
}).then(() => {
if (directSpaceExists) {
console.log('Validated that 1-1 Space exists between test user and bot.');
return when(true);
}
return when.reject(new Error('Late Discovery tests only work if the test user and bot have an existing 1-1 space.'));
});
});
//Stop framework to shut down the event listeners
after(() => common.stopFramework('shutdown framework', framework));
// Test bot functions for direct messaging
// These only work if the test bot and test user already have a direct space
it('Sends a message to bot to force a just-in-time spawn', () => {
testInfo.config.testName = 'creates a bot just in time after message';
testInfo.config.userUnderTest = userWebex;
testInfo.out = {};
// Wait for the hears event associated with the input text
const heard = new Promise((resolve) => {
framework.hears(/^hi.*/igm, (b, t) => {
framework.debug('Bot heard message that user posted');
assert((b.id == testInfo.out.newBot.id),
'Bot returned in framework.hears() does not match one returend in framework.on("spawn")');
assert((t.id == testInfo.out.messageId),
'Trigger returned in framework.hears() does not match the test message sent.');
console.log('Validated that framework.hears() was called with newly spawned bot from test message.');
resolve(true);
});
});
// As the user, send a direct message to the bot
return common.userSendsDMToBot(framework, testInfo,
'Hi, this is a message with **no mentions**.', heard);
});
});
// gracefully shutdown (ctrl-c)
process.on('SIGINT', function () {
framework.debug('stoppping...');
framework.stop().then(function () {
process.exit();
});
});