Skip to content

Commit

Permalink
add builtin message filters
Browse files Browse the repository at this point in the history
  • Loading branch information
microsoftly committed Oct 6, 2017
1 parent 519f235 commit 8b02cc6
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 4 deletions.
17 changes: 16 additions & 1 deletion README.md
Expand Up @@ -18,7 +18,9 @@ Passing in the config overrides any default values or values set by bot-tester.j
defaultAddress: botbuilder.IAddress,
timeout: number // in milliseconds
// each filter returns false for messages that the BotTester should ignore
messageFilters: ((message:IMessage) => boolean)[]
messageFilters: ((message:IMessage) => boolean)[],
ignoreTypingEvent: boolean,
ignoreEndOfConversationEvent: boolean
}
```
if timeout is defined, then a particular ```runTest()``` call will fail if it does not receive each expected message within the timeout period of time set in the options.
Expand Down Expand Up @@ -411,5 +413,18 @@ describe('BotTester', () => {
.runTest()
).to.be.rejected.notify(done);
});

it('can ignore typing events', () => {
bot.dialog('/', (session) => {
session.send('hello');
session.sendTyping();
session.send('goodbye');
});

return new BotTester(bot)
.ignoreTypingEvent()
.sendMessageToBot('hey', 'hello', 'goodbye')
.runTest();
});
});
```
31 changes: 31 additions & 0 deletions src/BotTester.ts
@@ -1,5 +1,6 @@
import * as Promise from 'bluebird';
import { IAddress, IMessage, Message, Session, UniversalBot } from 'botbuilder';
import { ignoreEndOfConversationEventFilter, ignoreTypingEventFilter } from './builtInMessageFilters';
import { config, IConfig, MessageFilter } from './config';
import { ExpectedMessage, PossibleExpectedMessageCollections, PossibleExpectedMessageType } from './ExpectedMessage';
import { botToUserMessageCheckerFunction, MessageService } from './MessageService';
Expand All @@ -24,6 +25,16 @@ export interface IOptionsModifier {
* 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;
}

/**
Expand Down Expand Up @@ -119,10 +130,30 @@ export class BotTester implements IBotTester, IOptionsModifier {
return this;
}

public ignoreEndOfConversationEvent(): BotTester {
this.config.ignoreEndOfConversationEvent = true;

return this;
}

public ignoreTypingEvent(): BotTester {
this.config.ignoreTypingEvent = true;

return this;
}

/**
* 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());
Expand Down
4 changes: 2 additions & 2 deletions src/ExpectedMessage.ts
@@ -1,4 +1,4 @@
import { IMessage } from 'botbuilder';
import { IMessage, IEvent } from 'botbuilder';
import { expect } from './assertionLibraries/IExpectation';

export enum ExpectedMessageType {
Expand All @@ -10,7 +10,7 @@ export enum ExpectedMessageType {
/**
* Types accepted for responses checkers
*/
export type PossibleExpectedMessageType = string | IMessage | RegExp;
export type PossibleExpectedMessageType = string | IMessage | RegExp | IEvent;

/**
* Response expectations area always collections. The collection is the set of possible responses, chosen at random. If the collection size
Expand Down
1 change: 0 additions & 1 deletion src/MessageService.ts
Expand Up @@ -87,7 +87,6 @@ export class MessageService {
} catch (e) {
return rej(e);
}

});

if (!outgoingMessageComparator.expectsAdditionalMessages()) {
Expand Down
13 changes: 13 additions & 0 deletions src/builtInMessageFilters.ts
@@ -0,0 +1,13 @@
import { IMessage } from 'botbuilder';

export function ignoreInternalSaveMessageFilter(message: IMessage): boolean {
return message.type !== '__save__';
}

export function ignoreEndOfConversationEventFilter(message: IMessage): boolean {
return message.type !== 'endOfConversation';
}

export function ignoreTypingEventFilter(message: IMessage): boolean {
return message.type !== 'typing';
}
10 changes: 10 additions & 0 deletions src/config.ts
Expand Up @@ -16,6 +16,16 @@ export interface IConfig {
*/
defaultAddress?: IAddress;

/**
* ignores typing event messages
*/
ignoreTypingEvent?: boolean;

/**
* ignores end of conversation event messages
*/
ignoreEndOfConversationEvent?: boolean;

/**
* filters for messages that the BotTester framework should use
*/
Expand Down
13 changes: 13 additions & 0 deletions test/BotTester.spec.ts
Expand Up @@ -388,6 +388,19 @@ describe('BotTester', () => {
.runTest()
).to.be.rejected.notify(done);
});

it('can ignore typing events', () => {
bot.dialog('/', (session) => {
session.send('hello');
session.sendTyping();
session.send('goodbye');
});

return new BotTester(bot)
.ignoreTypingEvent()
.sendMessageToBot('hey', 'hello', 'goodbye')
.runTest();
});
});
//```

Expand Down
35 changes: 35 additions & 0 deletions test/BotTesterFailure.spec.ts
Expand Up @@ -218,4 +218,39 @@ describe('BotTester', () => {
.sendMessageToBot('intro', 'hello', 'how', 'are', 'you?')
.runTest()).to.be.rejected.notify(done);
});

it('will fail if a endOfConversationEvent is seen with an ingoreEndOfConversationEventFilter', (done: Function) => {
bot.dialog('/', (session: Session) => {
session.send('hello');
session.endConversation();
});

expect(new BotTester(bot)
// need to timeout before the tests do to catch the error
.setTimeout(500)
.ignoreEndOfConversationEvent()
//tslint:disable
.sendMessageToBot('hey', 'hello', {type: 'endOfConversation'} as any)
//tslint:enable
.runTest())
.to.be.rejected.notify(done);
});

it('will fail if a typing event is seen with an ignoreTypingEventFilter', (done: Function) => {
bot.dialog('/', (session: Session) => {
session.send('hello');
session.sendTyping();
session.send('hey');
});

expect(new BotTester(bot)
// need to timeout before the tests do to catch the error
.setTimeout(500)
.ignoreTypingEvent()
//tslint:disable
.sendMessageToBot('hey', 'hello', {type: 'typing'} as any, 'hey')
//tslint:enable
.runTest())
.to.be.rejected.notify(done);
});
});

0 comments on commit 8b02cc6

Please sign in to comment.