UNPKG

5.83 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const Promise = require("bluebird");
4const botbuilder_1 = require("botbuilder");
5const config_1 = require("./config");
6const ExpectedMessage_1 = require("./ExpectedMessage");
7const MessageService_1 = require("./MessageService");
8const SessionService_1 = require("./SessionService");
9//tslint:enable
10/**
11 * Test builder and runner for botbuilder bots
12 */
13class BotTester {
14 //tslint:disable
15 /**
16 *
17 * @param bot bot that will be tested against
18 * @param options (optional) options to pass to bot. Sets the default address and test timeout
19 */
20 //tslint:enable
21 constructor(bot, options = config_1.config) {
22 const defaultAndInputOptionMix = Object.assign({}, config_1.config, options);
23 this.bot = bot;
24 this.defaultAddress = defaultAndInputOptionMix.defaultAddress;
25 this.messageService = new MessageService_1.MessageService(bot, defaultAndInputOptionMix);
26 this.sessionLoader = new SessionService_1.SessionService(bot);
27 this.testSteps = [];
28 }
29 /**
30 * executes each test step serially
31 */
32 //tslint:disable
33 runTest() {
34 //tslint:enable
35 return Promise.mapSeries(this.testSteps, (fn) => fn());
36 }
37 /**
38 * loads a session associated with an address and passes it to a user defined function
39 * @param sessionCheckerFunction function passed in to inspect message
40 * @param address (Optional) address of the session to load. Defaults to bot's default address if not defined
41 */
42 checkSession(sessionCheckerFunction, address) {
43 const runSessionChecker = () => this.sessionLoader.getSession(address || this.defaultAddress)
44 .then(sessionCheckerFunction);
45 this.testSteps.push(runSessionChecker);
46 return this;
47 }
48 /**
49 * sends a message to a bot and compares bot responses against expectedResponsess. Expected responses can be a variable number of args,
50 * each of which can be a single expected response of any PossibleExpectedMessageType or a collection of PossibleExpectedMessageType
51 * that mocks a randomly selected response by the bot
52 * @param msg message to send to bot
53 * @param expectedResponses (Optional) responses the bot-tester framework checks against
54 */
55 sendMessageToBot(msg,
56 // currently only supports string RegExp IMessage
57 ...expectedResponses) {
58 const message = this.convertToIMessage(msg);
59 // possible that expected responses may be undefined. Remove them
60 expectedResponses = expectedResponses.filter((expectedResponse) => expectedResponse);
61 return this.sendMessageToBotInternal(message, expectedResponses);
62 }
63 sendMessageToBotIgnoringResponseOrder(msg,
64 // currently only supports string RegExp IMessage
65 ...expectedResponses) {
66 const message = this.convertToIMessage(msg);
67 // possible that expected responses may be undefined. Remove them
68 expectedResponses = expectedResponses.filter((expectedResponse) => expectedResponse);
69 return this.sendMessageToBotInternal(message, expectedResponses, true);
70 }
71 /**
72 * sends a message to the bot. This should be used whenever session.save() is used without sending a reply to the user. This exists due
73 * to a limitation in the current implementation of the botbuilder framework
74 *
75 * @param msg message to send to bot
76 */
77 sendMessageToBotAndExpectSaveWithNoResponse(msg) {
78 const message = this.convertToIMessage(msg);
79 return this.sendMessageToBotInternal(message, [this.sessionLoader.getInternalSaveMessage(message.address)]);
80 }
81 /**
82 * Works exactly like Promise's .then function, except that the return value is not passed as an arg to the next function (even if its
83 * another .then)
84 * @param fn some function to run
85 */
86 //tslint:disable
87 then(fn) {
88 //tslint:enable
89 this.testSteps.push(() => Promise.method(fn)());
90 return this;
91 }
92 /**
93 * Waits for the given delay between test steps.
94 * @param delayInMiliseconds time to wait in milliseconds
95 */
96 wait(delayInMilliseconds) {
97 this.testSteps.push(() => Promise.delay(delayInMilliseconds));
98 return this;
99 }
100 convertToIMessage(msg) {
101 if (typeof (msg) === 'string') {
102 return new botbuilder_1.Message()
103 .text(msg)
104 .address(this.defaultAddress)
105 .toMessage();
106 }
107 return msg;
108 }
109 /**
110 * Packages the expected messages into an ExpectedMessage collection to be handed off to the MessageService's sendMessageToBot function
111 * @param message message to be sent to bot
112 * @param expectedResponses expected responses
113 */
114 sendMessageToBotInternal(message,
115 // currently only supports string RegExp IMessage
116 expectedResponses, ignoreOrder = false) {
117 let expectedMessages = [];
118 if (!expectedResponses) {
119 expectedMessages = [];
120 }
121 else if (!(expectedResponses instanceof Array)) {
122 expectedMessages = [new ExpectedMessage_1.ExpectedMessage(expectedResponses)];
123 }
124 else if (expectedResponses instanceof Array) {
125 if (expectedResponses.length > 0) {
126 expectedMessages = expectedResponses
127 .map((currentExpectedResponseCollection) => new ExpectedMessage_1.ExpectedMessage(currentExpectedResponseCollection));
128 // tslint:enable
129 }
130 }
131 this.testSteps.push(() => this.messageService.sendMessageToBot(message, expectedMessages, ignoreOrder));
132 return this;
133 }
134}
135exports.BotTester = BotTester;
136//# sourceMappingURL=BotTester.js.map
\No newline at end of file