1- import { IEvent , IMessage } from 'botbuilder' ;
2- import { BotTesterExpectation } from './assertionLibraries/BotTesterExpectation' ;
3- import { IConfig } from './config' ;
1+ import { IEvent , IMessage } from 'botbuilder' ;
2+ import { assert } from 'chai' ;
3+ import { BotTesterExpectation } from './assertionLibraries/BotTesterExpectation' ;
4+ import { IConfig } from './config' ;
45
56export enum ExpectedMessageType {
67 String ,
78 IMessage ,
8- Regex
9+ Regex ,
10+ Function
911}
1012
1113/**
1214 * Types accepted for responses checkers
1315 */
14- export type PossibleExpectedMessageType = string | IMessage | RegExp | IEvent ;
16+ export type PossibleExpectedMessageType = string | IMessage | RegExp | IEvent | Function ;
1517
1618/**
1719 * Response expectations area always collections. The collection is the set of possible responses, chosen at random. If the collection size
@@ -26,8 +28,10 @@ function getExpectedMessageType(expectedResponseCollection: PossibleExpectedMess
2628 return ExpectedMessageType . String ;
2729 } else if ( firstElt . constructor . name === 'RegExp' ) {
2830 return ExpectedMessageType . Regex ;
29- } else {
31+ } else if ( firstElt . constructor . name === 'IMessage' ) {
3032 return ExpectedMessageType . IMessage ;
33+ } else {
34+ return ExpectedMessageType . Function ;
3135 }
3236}
3337
@@ -42,10 +46,8 @@ export class ExpectedMessage {
4246 */
4347 private readonly expectedResponseCollection : PossibleExpectedMessageCollections ;
4448
45- constructor (
46- config : IConfig ,
47- expectedResponseCollection : PossibleExpectedMessageType | PossibleExpectedMessageCollections
48- ) {
49+ constructor ( config : IConfig ,
50+ expectedResponseCollection : PossibleExpectedMessageType | PossibleExpectedMessageCollections ) {
4951 this . internalExpectation = new BotTesterExpectation ( config ) ;
5052
5153 if ( ! ( expectedResponseCollection instanceof Array ) ) {
@@ -73,6 +75,9 @@ export class ExpectedMessage {
7375 // doing this check will highlight if the diff in text instead of a large IMessage diff
7476 this . deepMessageMatchCheck ( outgoingMessage ) ;
7577 break ;
78+ case ExpectedMessageType . Function :
79+ this . deepMatchCheckWithFunction ( outgoingMessage ) ;
80+ break ;
7681 default :
7782 this . internalExpectation . expect ( outgoingMessage . type ) . toEqual ( 'save' ) ;
7883 }
@@ -111,7 +116,7 @@ export class ExpectedMessage {
111116 const regexCollection : RegExp [ ] = this . expectedResponseCollection as RegExp [ ] ;
112117
113118 this . internalExpectation . expect ( regexCollection . some ( ( regex : RegExp ) => regex . test ( text ) ) ,
114- `'${ text } ' did not match any regex in ${ regexCollection } ` ) . toBeTrue ( ) ;
119+ `'${ text } ' did not match any regex in ${ regexCollection } ` ) . toBeTrue ( ) ;
115120 }
116121
117122 /**
@@ -136,4 +141,30 @@ export class ExpectedMessage {
136141
137142 this . internalExpectation . expect ( expectedResponseCollectionAsIMessage ) . toDeeplyInclude ( outgoingMessage ) ;
138143 }
144+
145+ /**
146+ * Verfy the incoming message with custom test defined by tester
147+ * If the function that tester defined return an error, make the test break
148+ * If the function return anything else, the test is considered as good
149+ * I've tryed to use promise as parameter, but in a promise we change scope, so the assert doesn't work
150+ * @param {IMessage } outgoingMessage outgoing message being compared
151+ */
152+ private deepMatchCheckWithFunction ( outgoingMessage : IMessage ) : void {
153+ const functionCollection : Function [ ] = this . expectedResponseCollection as Function [ ] ;
154+ let errorString = '' ;
155+ let success = false ;
156+ functionCollection . forEach ( ( func : Function ) => {
157+ const result = func ( outgoingMessage ) ;
158+ if ( result instanceof Error ) {
159+ errorString += `\n -----------------ERROR-----------------\n\n\n'${ result . message } ' ` ;
160+ } else {
161+ success = true ;
162+ }
163+ } ) ;
164+ // ErrorString here, can hold multiples error, if the bot send multiples message in one batching
165+ const error = `Bot should have relied response that matches with function but respond '${ outgoingMessage } '` +
166+ ` that create the following error(s) '${ errorString } '` ;
167+ this . internalExpectation . expect ( success , error ) . toBeTrue ( ) ;
168+
169+ }
139170}
0 commit comments