11import test from 'ava' ;
2- import { Context , TestContext } from 'ava' ;
3- import { IAddress , IMessage , Message , Prompts , Session , UniversalBot } from 'botbuilder' ;
4- import { BotTester } from './../../src/BotTester' ;
5- import { IConfig } from './../../src/config' ;
6- import { TestConnector } from './../../src/TestConnector' ;
7- import { getAdaptiveCard , getAdaptiveCardAttachment , getAdaptiveCardMessage } from './../adaptiveCardProvider' ;
2+ import { Context , TestContext } from 'ava' ;
3+ import { IAddress , IMessage , Message , Prompts , Session , UniversalBot } from 'botbuilder' ;
4+ import { BotTester } from './../../src/BotTester' ;
5+ import { IConfig } from './../../src/config' ;
6+ import { TestConnector } from './../../src/TestConnector' ;
7+ import { getAdaptiveCard , getAdaptiveCardAttachment , getAdaptiveCardMessage } from './../adaptiveCardProvider' ;
88
99const connector = new TestConnector ( ) ;
1010
@@ -14,6 +14,7 @@ interface IBotTestContext {
1414
1515//tslint:disable
1616type AvaTestContext = TestContext & Context < any > ;
17+
1718//tslint:enable
1819
1920interface IAvaBotTest extends AvaTestContext {
@@ -110,13 +111,13 @@ test('can inspect session state', (t: IAvaBotTest) => {
110111 bot . dialog ( '/' , [ ( session : Session ) => {
111112 new Prompts . text ( session , 'What would you like to set data to?' ) ;
112113 } , ( session : Session , results ) => {
113- session . userData = { data : results . response } ;
114+ session . userData = { data : results . response } ;
114115 session . save ( ) ;
115116 } ] ) ;
116117 //tslint:enable
117118
118119 return new BotTester ( bot )
119- . sendMessageToBot ( 'Start this thing!' , 'What would you like to set data to?' )
120+ . sendMessageToBot ( 'Start this thing!' , 'What would you like to set data to?' )
120121 . sendMessageToBotAndExpectSaveWithNoResponse ( 'This is data!' )
121122 . checkSession ( ( session : Session ) => {
122123 t . not ( session . userData , null ) ;
@@ -163,16 +164,18 @@ test('address/multiuser can ensure proper address being used four routing. inclu
163164
164165 addressMultiUserSetup ( bot ) ;
165166
166- const defaultAddress = { channelId : 'console' ,
167- user : { id : 'customUser1' , name : 'A' } ,
168- bot : { id : 'customBot1' , name : 'Bot1' } ,
169- conversation : { id : 'customUser1Conversation' }
167+ const defaultAddress = {
168+ channelId : 'console' ,
169+ user : { id : 'customUser1' , name : 'A' } ,
170+ bot : { id : 'customBot1' , name : 'Bot1' } ,
171+ conversation : { id : 'customUser1Conversation' }
170172 } ;
171173
172- const user2Address = { channelId : 'console' ,
173- user : { id : 'user2' , name : 'B' } ,
174- bot : { id : 'bot' , name : 'Bot' } ,
175- conversation : { id : 'user2Conversation' }
174+ const user2Address = {
175+ channelId : 'console' ,
176+ user : { id : 'user2' , name : 'B' } ,
177+ bot : { id : 'bot' , name : 'Bot' } ,
178+ conversation : { id : 'user2Conversation' }
176179 } ;
177180
178181 const askForUser1Name = new Message ( )
@@ -206,16 +209,18 @@ test('address/multiuser Can have a default address assigned to it and communicat
206209
207210 addressMultiUserSetup ( bot ) ;
208211
209- const defaultAddress = { channelId : 'console' ,
210- user : { id : 'customUser1' , name : 'A' } ,
211- bot : { id : 'customBot1' , name : 'Bot1' } ,
212- conversation : { id : 'customUser1Conversation' }
212+ const defaultAddress = {
213+ channelId : 'console' ,
214+ user : { id : 'customUser1' , name : 'A' } ,
215+ bot : { id : 'customBot1' , name : 'Bot1' } ,
216+ conversation : { id : 'customUser1Conversation' }
213217 } ;
214218
215- const user2Address = { channelId : 'console' ,
216- user : { id : 'user2' , name : 'B' } ,
217- bot : { id : 'bot' , name : 'Bot' } ,
218- conversation : { id : 'user2Conversation' }
219+ const user2Address = {
220+ channelId : 'console' ,
221+ user : { id : 'user2' , name : 'B' } ,
222+ bot : { id : 'bot' , name : 'Bot' } ,
223+ conversation : { id : 'user2Conversation' }
219224 } ;
220225
221226 const askForUser1Name = new Message ( )
@@ -239,8 +244,8 @@ test('address/multiuser Can have a default address assigned to it and communicat
239244 . toMessage ( ) ;
240245
241246 // when testing for an address that is not the default for the bot, the address must be passed in
242- return new BotTester ( bot , Object . assign ( { defaultAddress } , getTestOptions ( t ) ) )
243- // because user 1 is the default address, the expected responses can be a string
247+ return new BotTester ( bot , Object . assign ( { defaultAddress} , getTestOptions ( t ) ) )
248+ // because user 1 is the default address, the expected responses can be a string
244249 . sendMessageToBot ( askForUser1Name , 'A' )
245250 . sendMessageToBot ( 'What is my name?' , user1ExpectedResponse )
246251 . sendMessageToBot ( askForUser1Name , user1ExpectedResponse )
@@ -251,10 +256,11 @@ test('address/multiuser Can have a default address assigned to it and communicat
251256test ( 'can handle batch responses' , ( t : IAvaBotTest ) => {
252257 const bot = t . context . bot ;
253258
254- const CUSTOMER_ADDRESS : IAddress = { channelId : 'console' ,
255- user : { id : 'userId1' , name : 'user1' } ,
256- bot : { id : 'bot' , name : 'Bot' } ,
257- conversation : { id : 'user1Conversation' }
259+ const CUSTOMER_ADDRESS : IAddress = {
260+ channelId : 'console' ,
261+ user : { id : 'userId1' , name : 'user1' } ,
262+ bot : { id : 'bot' , name : 'Bot' } ,
263+ conversation : { id : 'user1Conversation' }
258264 } ;
259265
260266 const msg1 = new Message ( )
@@ -271,7 +277,7 @@ test('can handle batch responses', (t: IAvaBotTest) => {
271277 bot . send ( [ msg1 , msg2 ] ) ;
272278 } ) ;
273279
274- return new BotTester ( bot , Object . assign ( { defaultAddress : CUSTOMER_ADDRESS } , getTestOptions ( t ) ) )
280+ return new BotTester ( bot , Object . assign ( { defaultAddress : CUSTOMER_ADDRESS } , getTestOptions ( t ) ) )
275281 . sendMessageToBot ( 'anything' , 'hello' , 'there' )
276282 . runTest ( ) ;
277283} ) ;
@@ -319,10 +325,10 @@ test('can do arbitrary work between test steps', (t: IAvaBotTest) => {
319325 } ) ;
320326
321327 return new BotTester ( bot , getTestOptions ( t ) )
322- . sendMessageToBot ( 'you say' , 'goodbye' )
323- . then ( ( ) => responseString = 'hello' )
324- . sendMessageToBot ( 'and i say' , 'hello' )
325- . runTest ( ) ;
328+ . sendMessageToBot ( 'you say' , 'goodbye' )
329+ . then ( ( ) => responseString = 'hello' )
330+ . sendMessageToBot ( 'and i say' , 'hello' )
331+ . runTest ( ) ;
326332} ) ;
327333
328334test ( 'can wait between test steps' , ( t : IAvaBotTest ) => {
@@ -374,7 +380,7 @@ test('can apply one or more message filters in the BotTester options for message
374380 const ignoreHowMessage = ( message : IMessage ) => ! message . text . includes ( 'how' ) ;
375381 const ignoreAreMessage = ( message : IMessage ) => ! message . text . includes ( 'are' ) ;
376382
377- return new BotTester ( bot , Object . assign ( { messageFilters : [ ignoreHowMessage , ignoreAreMessage ] } , getTestOptions ( t ) ) )
383+ return new BotTester ( bot , Object . assign ( { messageFilters : [ ignoreHowMessage , ignoreAreMessage ] } , getTestOptions ( t ) ) )
378384 . sendMessageToBot ( 'intro' , 'hello' , 'you?' )
379385 . runTest ( ) ;
380386} ) ;
@@ -400,10 +406,10 @@ test('change timeout time', (t: IAvaBotTest) => {
400406
401407 const timeout = 750 ;
402408 bot . dialog ( '/' , ( session : Session ) => {
403- setTimeout ( ( ) => session . send ( 'hi there' ) , timeout * 2 ) ;
409+ setTimeout ( ( ) => session . send ( 'hi there' ) , timeout * 2 ) ;
404410 } ) ;
405411
406- return t . throws ( new BotTester ( bot , getTestOptions ( t ) )
412+ return t . throws ( new BotTester ( bot , getTestOptions ( t ) )
407413 . setTimeout ( timeout )
408414 . sendMessageToBot ( 'hey' , 'hi there' )
409415 . runTest ( ) ) ;
@@ -461,3 +467,27 @@ test('can ensure adaptive cards are present, regardless of order', (t: IAvaBotTe
461467 . sendMessageToBot ( 'anything' , message2 )
462468 . runTest ( ) ;
463469} ) ;
470+
471+ test ( 'can correctly process function as expected message' , ( t : IAvaBotTest ) => {
472+ const bot = t . context . bot ;
473+
474+ bot . dialog ( '/' , ( session : Session ) => {
475+ session . send ( 'hello!' ) ;
476+ session . send ( '12' ) ;
477+ } ) ;
478+
479+ return new BotTester ( bot , getTestOptions ( t ) )
480+ . sendMessageToBot ( 'Hi' , ( message : IMessage ) => {
481+ if ( message . text === 'Hello' ) {
482+ return true ;
483+ } else {
484+ return new Error ( `Message : '${ message . text } ' is not equal to 'Hello'` ) ;
485+ }
486+ } , ( message : IMessage ) => {
487+ if ( parseInt ( message . text , 0 ) % 2 === 0 ) {
488+ return true ;
489+ } else {
490+ return new Error ( `Message is not an even number : '${ message . text } '` ) ;
491+ }
492+ } ) . runTest ( ) ;
493+ } ) ;
0 commit comments