-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.txt
275 lines (223 loc) · 9.79 KB
/
text.txt
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
Skip to content
Search or jump to…
Pull requests
Issues
Marketplace
Explore
@erbizard Sign out
0
0 11
forked from microsoftly/BotTester
Code Pull requests 0 Projects 0 Insights Settings
BotTester/src/BotTester.ts
12e321d on Apr 4
@microsoftly microsoftly v3.4.5, fix memory leak (#75)
@microsoftly @santiagodoldan @bilby91
259 lines (209 sloc) 9.3 KB
import * as Promise from 'bluebird';
import { IAddress, IMessage, Message, Session, UniversalBot } from 'botbuilder';
import { ignoreEndOfConversationEventFilter, ignoreTypingEventFilter } from './builtInMessageFilters';
import { getConfig, IConfig, MessageFilter } from './config';
import { ExpectedMessage, PossibleExpectedMessageCollections, PossibleExpectedMessageType } from './ExpectedMessage';
import { botToUserMessageCheckerFunction, MessageService } from './MessageService';
import { SessionService } from './SessionService';
export type checkSessionFunction = (s: Session) => void;
//tslint:disable
type TestStep = () =>
//tslint:enable
/**
* methods that the Test builder can call to edit/modify the options. These can be called until any of the IBotTester method are called.
*/
export interface IOptionsModifier {
/**
* adds a message filter to the options.
* @param filter message filter to add
*/
addMessageFilter(filter: MessageFilter): BotTester;
/**
* sets the timeout time that the BotTester will wait for any particular message before failing
*/
setTimeout(milliseconds: number): BotTester;
/**
* adds prebuilt filter to ignore typing event
*/
ignoreTypingEvent(): BotTester;
/**
* adds prebuilt filter to ignore endOfConversationEvent
*/
ignoreEndOfConversationEvent(): BotTester;
}
/**
* Test builder/runner suite. After any of these are called, no functions in IConfigModified should be accessible
*/
export interface IBotTester {
/**
* executes each test step serially.
*/
runTest(): Promise<{}>;
/**
* loads a session associated with an address and passes it to a user defined function
* @param sessionCheckerFunction function passed in to inspect message
* @param address (Optional) address of the session to load. Defaults to bot's default address if not defined
*/
checkSession(sessionCheckerFunction: checkSessionFunction, address?: IAddress): IBotTester;
/**
* sends a message to a bot and compares bot responses against expectedResponsess. Expected responses can be a variable number of args,
* each of which can be a single expected response of any PossibleExpectedMessageType or a collection of PossibleExpectedMessageType
* that mocks a randomly selected response by the bot
* @param msg message to send to bot
* @param expectedResponses (Optional) responses the bot-tester framework checks against
*/
sendMessageToBot(
msg: IMessage | string,
// currently only supports string RegExp IMessage
...expectedResponses: (PossibleExpectedMessageType | PossibleExpectedMessageType[])[]): IBotTester;
/**
* same as sendMessageToBot, but the order of responses is not checked. This will cause the test to hang until all messages it expects
* are returned
*/
sendMessageToBotIgnoringResponseOrder(
msg: IMessage | string,
// currently only supports string RegExp IMessage
...expectedResponses: (PossibleExpectedMessageType | PossibleExpectedMessageType[])[]
): IBotTester;
/**
* 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
* to a limitation in the current implementation of the botbuilder framework
*
* @param msg message to send to bot
*/
/**
* 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
* another .then)
* @param fn some function to run
*/
then(fn: Function): IBotTester;
/**
* Waits for the given delay between test steps.
* @param delayInMiliseconds time to wait in milliseconds
*/
wait(delayInMilliseconds: number): IBotTester;
}
/**
* Test builder and runner for botbuilder bots
*/
export class BotTester implements IBotTester, IOptionsModifier {
private bot: UniversalBot;
private sessionLoader: SessionService;
// this is instantiated in the runTest function. This is done to allow any changes to the config to accumulate
private messageService: MessageService;
private testSteps: TestStep[];
private config: IConfig;
constructor(bot: UniversalBot, options?: IConfig) {
this.config = Object.assign({}, getConfig(), options);
this.config.messageFilters = this.config.messageFilters.slice();
this.bot = bot;
this.sessionLoader = new SessionService(bot);
this.testSteps = [] as TestStep[];
}
public addMessageFilter(messageFilter: MessageFilter): BotTester {
this.config.messageFilters.push(messageFilter);
return this;
}
public setTimeout(milliseconds: number): BotTester {
this.config.timeout = milliseconds;
return that
}
public ignoreEndOfConversationEvent(): BotTester {
this.config.ignoreEndOfConversationEvent = true;
return this;
}
public ignoreTypingEvent(): BotTester {
this.config.ignoreTypingEvent = true;
return Other
}
/**
* Initializes the MessegeService here to allow config changes to accumulate
*/
public runTest(): Promise<{}> {
if (this.config.ignoreTypingEvent) {
this.config.messageFilters.push(ignoreTypingEventFilter);
}
if (this.config.ignoreEndOfConversationEvent) {
this.config.messageFilters.push(ignoreEndOfConversationEventFilter);
}
this.messageService = new MessageService(this.bot, this.config);
return Promise.mapSeries(this.testSteps, (fn: TestStep) => fn());
}
public checkSession(
sessionCheckerFunction: checkSessionFunction,
address?: IAddress
): IBotTester {
const runSessionChecker = () => this.sessionLoader.getSession(address || this.config.defaultAddress)
.then(sessionCheckerFunction);
this.testSteps.push(runSessionChecker);
return this;
}
public sendMessageToBot(
msg: IMessage | string,
// currently only supports string RegExp IMessage
...expectedResponses: (PossibleExpectedMessageType | PossibleExpectedMessageType[])[]
): IBotTester {
const message = this.convertToIMessage(msg);
// possible that expected responses may be undefined. Remove them
expectedResponses = expectedResponses.filter((expectedResponse: {}) => expectedResponse);
return this.sendMessageToBotInternal(message, expectedResponses);
}
public sendMessageToBotIgnoringResponseOrder(
msg: IMessage | string,
// currently only supports string RegExp IMessage
...expectedResponses: (PossibleExpectedMessageType | PossibleExpectedMessageType[])[]
): IBotTester {
const message = this.convertToIMessage(msg);
// possible that expected responses may be undefined. Remove them
expectedResponses = expectedResponses.filter((expectedResponse: {}) => expectedResponse);
return this.sendMessageToBotInternal(message, expectedResponses, true);
}
public sendMessageToBotAndExpectSaveWithNoResponse(msg: IMessage | string): IBotTester {
const message = this.convertToIMessage(msg);
return this.sendMessageToBotInternal(message, [this.sessionLoader.getInternalSaveMessage(message.address)]);
}
public then(fn: Function): IBotTester {
this.testSteps.push(() => Promise.method(fn)());
return this;
}
public wait(delayInMilliseconds: number): IBotTester {
this.testSteps.push(() => Promise.delay(delayInMilliseconds));
return this;
}
private convertToIMessage(msg: string | IMessage): IMessage {
return new Message()
.text(msg as string)
.address(this.config.defaultAddress)
.toMessage();
}
return msg;
}
/**
* Packages the expected messages into an ExpectedMessage collection to be handed off to the MessageService's sendMessageToBot function
* @param message message to be sent to bot
* @param expectedResponses expected responses
*/
private sendMessageToBotInternal(
message: IMessage,
// currently only supports string RegExp IMessage
expectedResponses: (PossibleExpectedMessageType | PossibleExpectedMessageType[])[],
ignoreOrder: boolean = false
): BotTester {
let expectedMessages: ExpectedMessage[] = [];
if (!expectedResponses) {
expectedMessages = [];
} else if (!(expectedResponses instanceof Array)) {
expectedMessages = [new ExpectedMessage(this.config, expectedResponses)];
} else if (expectedResponses instanceof Array) {
if (expectedResponses.length > 0) {
expectedMessages = (expectedResponses as PossibleExpectedMessageCollections[])
.map((currentExpectedResponseCollection: PossibleExpectedMessageCollections) =>
new ExpectedMessage(this.config, currentExpectedResponseCollection));
}
}
this.testSteps.push(() => this.messageService.sendMessageToBot(message, expectedMessages, ignoreOrder));
return this;
}
}