import { IEvent , IMessage } from 'botbuilder' ; import { BotTesterExpectation } from './assertionLibraries/BotTesterExpectation' ; import { IConfig } from './config' ; export enum ExpectedMessageType { String , IMessage , Regex } /**
 * Types accepted for responses checkers
 */ 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
 * is 1, then there is only one response that is expected
 */ export type PossibleExpectedMessageCollections = PossibleExpectedMessageType [ ] ; function getExpectedMessageType ( expectedResponseCollection ) { const firstElt = expectedResponseCollection [ 0 ] ; if ( typeof firstElt === 'string' ) { return ExpectedMessageType . String ; } else if ( firstElt . constructor . name === 'RegExp' ) { return ExpectedMessageType . Regex ; } else { return ExpectedMessageType . IMessage ; } } /**
 * Class that wraps expectedResponseCollections for comparison against an outgoing message. One instance of ExpectedMessage represents one
 * response from the bot.
 */ export class ExpectedMessage { private readonly internalExpectation : BotTesterExpectation ; /**
     * set of possible responses, chosen at random. If the collection size is 1, then there is only one response that is expected
     */ private readonly expectedResponseCollection : PossibleExpectedMessageCollections ; constructor ( config , expectedResponseCollection ) { this . internalExpectation = new BotTesterExpectation ( config ) ; if ( ! ( expectedResponseCollection instanceof Array ) ) { this . expectedResponseCollection = [ expectedResponseCollection as PossibleExpectedMessageType ] ; } else { this . expectedResponseCollection = expectedResponseCollection as PossibleExpectedMessageCollections ; } } param outgoingMessage public checkForMessageMatch ( outgoingMessage ) { switch ( getExpectedMessageType ( this . expectedResponseCollection ) ) { case ExpectedMessageType . String : this . checkMessageTextForExactStringMatch ( outgoingMessage , this . expectedResponseCollection as string [ ] ) ; break ; case ExpectedMessageType . Regex : this . checkMessageTextForRegex ( outgoingMessage ) ; break ; case ExpectedMessageType . IMessage : this . deepMessageMatchCheck ( outgoingMessage ) ; break ; default : this . internalExpectation . expect ( outgoingMessage . type ) . toEqual ( 'save' ) ; } } public toString ( ) { return JSON . stringify ( this . expectedResponseCollection , null , 2 ) ; } param outgoingMessage param expectedResponseStrings private checkMessageTextForExactStringMatch ( outgoingMessage , expectedResponseStrings ) { const outgoingText = outgoingMessage . text ; const errorStringExpectedResponseText = expectedResponseStrings . length > 1 ? `one of ${ expectedResponseStrings }` : expectedResponseStrings [ 0 ] ; const errorString = `Bot should have responded with '${ errorStringExpectedResponseText }', but was '${ outgoingText }'` ; this . internalExpectation . expect ( expectedResponseStrings , errorString ) . toInclude ( outgoingText ) ; } param outgoingMessage private checkMessageTextForRegex ( outgoingMessage ) { const text = outgoingMessage . text ; const regexCollection = this . expectedResponseCollection as RegExp [ ] ; this . internalExpectation . expect ( regexCollection . some ( ( regex ) => regex . test ( text ) ) , `'${ text }' did not match any regex in ${ regexCollection }` ) . toBeTrue ( ) ; } param outgoingMessage private deepMessageMatchCheck ( outgoingMessage ) { const expectedResponseCollectionAsIMessage = this . expectedResponseCollection as IMessage [ ] ; const expectedResponseStrings = expectedResponseCollectionAsIMessage . map ( ( expectedResponse ) => expectedResponse . text ) . filter ( ( text ) => text ) ; if ( expectedResponseStrings . length ) { this . checkMessageTextForExactStringMatch ( outgoingMessage , expectedResponseStrings ) ; } this . internalExpectation . expect ( expectedResponseCollectionAsIMessage ) . toDeeplyInclude ( outgoingMessage ) ; } }