-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
run-async.js
65 lines (59 loc) · 2.07 KB
/
run-async.js
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
var builder = require('../../../');
var prompts = require('../prompts');
var tests = require('../tests/index');
// Filter to only list of async tests
var asyncTests = {};
for (var key in tests) {
var test = tests[key];
if (test.async) {
asyncTests[key] = test;
}
}
module.exports = {
addDialogs: addDialogs
};
function addDialogs(bot, addressConverter) {
bot.add('/run-async', [
function (session, args, next) {
// See if the user specified a valid test to run by name.
var match;
var utterance = session.message.text;
var brk = utterance.indexOf(' ');
var name = brk > 0 && brk < utterance.length ? utterance.substr(brk + 1).trim() : null;
if (name) {
match = builder.EntityRecognizer.findBestMatch(asyncTests, name);
}
// Prompt for test if no valid name specified.
if (!match) {
builder.Prompts.choice(session, prompts.runPrompt, asyncTests);
} else {
next({ response: match });
}
},
function (session, results) {
// Run the test unless the user canceled.
if (results.response) {
var name = results.response.entity;
var address = addressConverter(session.message);
setTimeout(function () {
bot.beginDialog(address, '/run-async/runner', { name: name });
}, 4000);
session.endDialog(prompts.runAsyncTest, name);
} else {
session.endDialog(prompts.canceled);
}
}
]);
bot.add('/run-async/runner', [
function (session, args) {
// Execute test
session.dialogData.name = args.name;
var test = tests[session.dialogData.name];
test.run(session);
},
function (session) {
// Test completed execution.
session.endDialog(prompts.testCompleted, session.dialogData.name);
}
])
}