1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const Promise = require("bluebird");
|
4 | const botbuilder_1 = require("botbuilder");
|
5 | const config_1 = require("./config");
|
6 | const ExpectedMessage_1 = require("./ExpectedMessage");
|
7 | const MessageService_1 = require("./MessageService");
|
8 | const SessionService_1 = require("./SessionService");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | class BotTester {
|
14 |
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
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 |
|
31 |
|
32 |
|
33 | runTest() {
|
34 |
|
35 | return Promise.mapSeries(this.testSteps, (fn) => fn());
|
36 | }
|
37 | |
38 |
|
39 |
|
40 |
|
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 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | sendMessageToBot(msg,
|
56 |
|
57 | ...expectedResponses) {
|
58 | const message = this.convertToIMessage(msg);
|
59 |
|
60 | expectedResponses = expectedResponses.filter((expectedResponse) => expectedResponse);
|
61 | return this.sendMessageToBotInternal(message, expectedResponses);
|
62 | }
|
63 | sendMessageToBotIgnoringResponseOrder(msg,
|
64 |
|
65 | ...expectedResponses) {
|
66 | const message = this.convertToIMessage(msg);
|
67 |
|
68 | expectedResponses = expectedResponses.filter((expectedResponse) => expectedResponse);
|
69 | return this.sendMessageToBotInternal(message, expectedResponses, true);
|
70 | }
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | sendMessageToBotAndExpectSaveWithNoResponse(msg) {
|
78 | const message = this.convertToIMessage(msg);
|
79 | return this.sendMessageToBotInternal(message, [this.sessionLoader.getInternalSaveMessage(message.address)]);
|
80 | }
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | then(fn) {
|
88 |
|
89 | this.testSteps.push(() => Promise.method(fn)());
|
90 | return this;
|
91 | }
|
92 | |
93 |
|
94 |
|
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 |
|
111 |
|
112 |
|
113 |
|
114 | sendMessageToBotInternal(message,
|
115 |
|
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 |
|
129 | }
|
130 | }
|
131 | this.testSteps.push(() => this.messageService.sendMessageToBot(message, expectedMessages, ignoreOrder));
|
132 | return this;
|
133 | }
|
134 | }
|
135 | exports.BotTester = BotTester;
|
136 |
|
\ | No newline at end of file |