Skip to content

Commit

Permalink
[ARO] Add unit test + interpretation of string return
Browse files Browse the repository at this point in the history
  • Loading branch information
erbizard committed Jul 25, 2018
1 parent f2c97dc commit 9a7064f
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 68 deletions.
48 changes: 48 additions & 0 deletions npm-debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli 'C:\\Users\\arthur.rouet\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli 'run',
1 verbose cli 'lint:fix' ]
2 info using npm@4.0.5
3 info using node@v8.9.1
4 verbose run-script [ 'prelint:fix', 'lint:fix', 'postlint:fix' ]
5 info lifecycle bot-tester@3.4.5~prelint:fix: bot-tester@3.4.5
6 silly lifecycle bot-tester@3.4.5~prelint:fix: no script for prelint:fix, continuing
7 info lifecycle bot-tester@3.4.5~lint:fix: bot-tester@3.4.5
8 verbose lifecycle bot-tester@3.4.5~lint:fix: unsafe-perm in lifecycle true
9 verbose lifecycle bot-tester@3.4.5~lint:fix: PATH: C:\Users\arthur.rouet\AppData\Roaming\npm\node_modules\npm\bin\node-gyp-bin;C:\Users\arthur.rouet\Desktop\Angie-bot\BOT_Hello_Eng-master\BotTester\node_modules\.bin;C:\Users\arthur.rouet\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\local\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\arthur.rouet\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files\Docker\Docker\Resources\bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files\Java\jdk1.7.0_80;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\Python27;C:\Users\arthur.rouet\AppData\Local\Programs\Python\Python36-32\Scripts;C:\Users\arthur.rouet\AppData\Local\Programs\Python\Python36-32;C:\Program Files (x86)\Symantec\VIP Access Client;C:\Users\arthur.rouet\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\Program Files\Git\cmd;C:\Program Files\TortoiseGit\bin;C:\Users\arthur.rouet\AppData\Local\Programs\Python\Launcher;C:\Python27\script;C:\Python27;C:\Program Files\Git\;C:\Users\arthur.rouet\AppData\Local\Programs\Python\Python36-32\Scripts;C:\Users\arthur.rouet\AppData\Local\Programs\Python\Python36-32;C:\Program Files\Java\jre1.8.0_131\bin;C:\Users\arthur.rouet\AppData\Roaming\npm;C:\Program Files\Apache\Maven\bin;C:\Program Files\Microsoft VS Code\bin;C:\Users\arthur.rouet\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\Users\arthur.rouet\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Git\usr\bin\vendor_perl;C:\Program Files\Git\usr\bin\core_perl
10 verbose lifecycle bot-tester@3.4.5~lint:fix: CWD: C:\Users\arthur.rouet\Desktop\Angie-bot\BOT_Hello_Eng-master\BotTester
11 silly lifecycle bot-tester@3.4.5~lint:fix: Args: [ '/d /s /c', 'tslint \'src/**/*.ts\' --fix' ]
12 silly lifecycle bot-tester@3.4.5~lint:fix: Returned: code: 2 signal: null
13 info lifecycle bot-tester@3.4.5~lint:fix: Failed to exec lint:fix script
14 verbose stack Error: bot-tester@3.4.5 lint:fix: `tslint 'src/**/*.ts' --fix`
14 verbose stack Exit status 2
14 verbose stack at EventEmitter.<anonymous> (C:\Users\arthur.rouet\AppData\Roaming\npm\node_modules\npm\lib\utils\lifecycle.js:279:16)
14 verbose stack at emitTwo (events.js:126:13)
14 verbose stack at EventEmitter.emit (events.js:214:7)
14 verbose stack at ChildProcess.<anonymous> (C:\Users\arthur.rouet\AppData\Roaming\npm\node_modules\npm\lib\utils\spawn.js:40:14)
14 verbose stack at emitTwo (events.js:126:13)
14 verbose stack at ChildProcess.emit (events.js:214:7)
14 verbose stack at maybeClose (internal/child_process.js:925:16)
14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
15 verbose pkgid bot-tester@3.4.5
16 verbose cwd C:\Users\arthur.rouet\Desktop\Angie-bot\BOT_Hello_Eng-master\BotTester
17 error Windows_NT 10.0.16299
18 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\arthur.rouet\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "run" "lint:fix"
19 error node v8.9.1
20 error npm v4.0.5
21 error code ELIFECYCLE
22 error bot-tester@3.4.5 lint:fix: `tslint 'src/**/*.ts' --fix`
22 error Exit status 2
23 error Failed at the bot-tester@3.4.5 lint:fix script 'tslint 'src/**/*.ts' --fix'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the bot-tester package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error tslint 'src/**/*.ts' --fix
23 error You can get information on how to open an issue for this project with:
23 error npm bugs bot-tester
23 error Or if that isn't available, you can get their info via:
23 error npm owner ls bot-tester
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]
14 changes: 9 additions & 5 deletions src/ExpectedMessage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {IEvent, IMessage} from 'botbuilder';
import {assert} from 'chai';
import {BotTesterExpectation} from './assertionLibraries/BotTesterExpectation';
import {IConfig} from './config';

Expand Down Expand Up @@ -116,7 +115,7 @@ export class ExpectedMessage {
const regexCollection: RegExp[] = this.expectedResponseCollection as RegExp[];

this.internalExpectation.expect(regexCollection.some((regex: RegExp) => regex.test(text)),
`'${text}' did not match any regex in ${regexCollection}`).toBeTrue();
`'${text}' did not match any regex in ${regexCollection}`).toBeTrue();
}

/**
Expand Down Expand Up @@ -146,25 +145,30 @@ export class ExpectedMessage {
* Verfy the incoming message with custom test defined by tester
* If the function that tester defined return an error, make the test break
* If the function return anything else, the test is considered as good
* I've tryed to use promise as parameter, but in a promise we change scope, so the assert doesn't work
* @param {IMessage} outgoingMessage outgoing message being compared
*/
private deepMatchCheckWithFunction(outgoingMessage: IMessage): void {
const functionCollection: Function[] = this.expectedResponseCollection as Function[];
let errorString = '';
const exceptedResponsesStrings = [];
let success = false;
functionCollection.forEach((func: Function) => {
const result = func(outgoingMessage);
if (result instanceof Error) {
errorString += `\n -----------------ERROR-----------------\n\n\n'${result.message}' `;
} else if (typeof result === 'string') {
exceptedResponsesStrings.push(result);
} else {
success = true;
}
});
// ErrorString here, can hold multiples error, if the bot send multiples message in one batching
const error = `Bot should have relied response that matches with function but respond '${outgoingMessage}'` +
` that create the following error(s) '${errorString}'`;
this.internalExpectation.expect(success, error).toBeTrue();

if (exceptedResponsesStrings.length > 0) {
this.checkMessageTextForExactStringMatch(outgoingMessage, exceptedResponsesStrings);
} else {
this.internalExpectation.expect(success, error).toBeTrue();
}
}
}
106 changes: 68 additions & 38 deletions test/ava/BotTester.ava.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import test from 'ava';
import { Context, TestContext } from 'ava';
import { IAddress, IMessage, Message, Prompts, Session, UniversalBot } from 'botbuilder';
import { BotTester } from './../../src/BotTester';
import { IConfig } from './../../src/config';
import { TestConnector } from './../../src/TestConnector';
import { getAdaptiveCard, getAdaptiveCardAttachment, getAdaptiveCardMessage } from './../adaptiveCardProvider';
import {Context, TestContext} from 'ava';
import {IAddress, IMessage, Message, Prompts, Session, UniversalBot} from 'botbuilder';
import {BotTester} from './../../src/BotTester';
import {IConfig} from './../../src/config';
import {TestConnector} from './../../src/TestConnector';
import {getAdaptiveCard, getAdaptiveCardAttachment, getAdaptiveCardMessage} from './../adaptiveCardProvider';

const connector = new TestConnector();

Expand All @@ -14,6 +14,7 @@ interface IBotTestContext {

//tslint:disable
type AvaTestContext = TestContext & Context<any>;

//tslint:enable

interface IAvaBotTest extends AvaTestContext {
Expand Down Expand Up @@ -110,13 +111,13 @@ test('can inspect session state', (t: IAvaBotTest) => {
bot.dialog('/', [(session: Session) => {
new Prompts.text(session, 'What would you like to set data to?');
}, (session: Session, results) => {
session.userData = { data: results.response };
session.userData = {data: results.response};
session.save();
}]);
//tslint:enable

return new BotTester(bot)
.sendMessageToBot('Start this thing!', 'What would you like to set data to?')
.sendMessageToBot('Start this thing!', 'What would you like to set data to?')
.sendMessageToBotAndExpectSaveWithNoResponse('This is data!')
.checkSession((session: Session) => {
t.not(session.userData, null);
Expand Down Expand Up @@ -163,16 +164,18 @@ test('address/multiuser can ensure proper address being used four routing. inclu

addressMultiUserSetup(bot);

const defaultAddress = { channelId: 'console',
user: { id: 'customUser1', name: 'A' },
bot: { id: 'customBot1', name: 'Bot1' },
conversation: { id: 'customUser1Conversation' }
const defaultAddress = {
channelId: 'console',
user: {id: 'customUser1', name: 'A'},
bot: {id: 'customBot1', name: 'Bot1'},
conversation: {id: 'customUser1Conversation'}
};

const user2Address = { channelId: 'console',
user: { id: 'user2', name: 'B' },
bot: { id: 'bot', name: 'Bot' },
conversation: { id: 'user2Conversation' }
const user2Address = {
channelId: 'console',
user: {id: 'user2', name: 'B'},
bot: {id: 'bot', name: 'Bot'},
conversation: {id: 'user2Conversation'}
};

const askForUser1Name = new Message()
Expand Down Expand Up @@ -206,16 +209,18 @@ test('address/multiuser Can have a default address assigned to it and communicat

addressMultiUserSetup(bot);

const defaultAddress = { channelId: 'console',
user: { id: 'customUser1', name: 'A' },
bot: { id: 'customBot1', name: 'Bot1' },
conversation: { id: 'customUser1Conversation' }
const defaultAddress = {
channelId: 'console',
user: {id: 'customUser1', name: 'A'},
bot: {id: 'customBot1', name: 'Bot1'},
conversation: {id: 'customUser1Conversation'}
};

const user2Address = { channelId: 'console',
user: { id: 'user2', name: 'B' },
bot: { id: 'bot', name: 'Bot' },
conversation: { id: 'user2Conversation' }
const user2Address = {
channelId: 'console',
user: {id: 'user2', name: 'B'},
bot: {id: 'bot', name: 'Bot'},
conversation: {id: 'user2Conversation'}
};

const askForUser1Name = new Message()
Expand All @@ -239,8 +244,8 @@ test('address/multiuser Can have a default address assigned to it and communicat
.toMessage();

// when testing for an address that is not the default for the bot, the address must be passed in
return new BotTester(bot, Object.assign({ defaultAddress }, getTestOptions(t)))
// because user 1 is the default address, the expected responses can be a string
return new BotTester(bot, Object.assign({defaultAddress}, getTestOptions(t)))
// because user 1 is the default address, the expected responses can be a string
.sendMessageToBot(askForUser1Name, 'A')
.sendMessageToBot('What is my name?', user1ExpectedResponse)
.sendMessageToBot(askForUser1Name, user1ExpectedResponse)
Expand All @@ -251,10 +256,11 @@ test('address/multiuser Can have a default address assigned to it and communicat
test('can handle batch responses', (t: IAvaBotTest) => {
const bot = t.context.bot;

const CUSTOMER_ADDRESS: IAddress = { channelId: 'console',
user: { id: 'userId1', name: 'user1' },
bot: { id: 'bot', name: 'Bot' },
conversation: { id: 'user1Conversation' }
const CUSTOMER_ADDRESS: IAddress = {
channelId: 'console',
user: {id: 'userId1', name: 'user1'},
bot: {id: 'bot', name: 'Bot'},
conversation: {id: 'user1Conversation'}
};

const msg1 = new Message()
Expand All @@ -271,7 +277,7 @@ test('can handle batch responses', (t: IAvaBotTest) => {
bot.send([msg1, msg2]);
});

return new BotTester(bot, Object.assign({ defaultAddress: CUSTOMER_ADDRESS }, getTestOptions(t)))
return new BotTester(bot, Object.assign({defaultAddress: CUSTOMER_ADDRESS}, getTestOptions(t)))
.sendMessageToBot('anything', 'hello', 'there')
.runTest();
});
Expand Down Expand Up @@ -319,10 +325,10 @@ test('can do arbitrary work between test steps', (t: IAvaBotTest) => {
});

return new BotTester(bot, getTestOptions(t))
.sendMessageToBot('you say', 'goodbye')
.then(() => responseString = 'hello')
.sendMessageToBot('and i say', 'hello')
.runTest();
.sendMessageToBot('you say', 'goodbye')
.then(() => responseString = 'hello')
.sendMessageToBot('and i say', 'hello')
.runTest();
});

test('can wait between test steps', (t: IAvaBotTest) => {
Expand Down Expand Up @@ -374,7 +380,7 @@ test('can apply one or more message filters in the BotTester options for message
const ignoreHowMessage = (message: IMessage) => !message.text.includes('how');
const ignoreAreMessage = (message: IMessage) => !message.text.includes('are');

return new BotTester(bot, Object.assign({ messageFilters: [ignoreHowMessage, ignoreAreMessage]}, getTestOptions(t)))
return new BotTester(bot, Object.assign({messageFilters: [ignoreHowMessage, ignoreAreMessage]}, getTestOptions(t)))
.sendMessageToBot('intro', 'hello', 'you?')
.runTest();
});
Expand All @@ -400,10 +406,10 @@ test('change timeout time', (t: IAvaBotTest) => {

const timeout = 750;
bot.dialog('/', (session: Session) => {
setTimeout(() => session.send('hi there'), timeout * 2 );
setTimeout(() => session.send('hi there'), timeout * 2);
});

return t.throws(new BotTester(bot, getTestOptions(t))
return t.throws(new BotTester(bot, getTestOptions(t))
.setTimeout(timeout)
.sendMessageToBot('hey', 'hi there')
.runTest());
Expand Down Expand Up @@ -461,3 +467,27 @@ test('can ensure adaptive cards are present, regardless of order', (t: IAvaBotTe
.sendMessageToBot('anything', message2)
.runTest();
});

test('can correctly process function as expected message', (t: IAvaBotTest) => {
const bot = t.context.bot;

bot.dialog('/', (session: Session) => {
session.send('hello!');
session.send('12');
});

return new BotTester(bot, getTestOptions(t))
.sendMessageToBot('Hi', (message: IMessage) => {
if (message.text === 'Hello') {
return true;
} else {
return new Error(`Message : '${message.text}' is not equal to 'Hello'`);
}
}, (message: IMessage) => {
if (parseInt(message.text, 0) % 2 === 0) {
return true;
} else {
return new Error(`Message is not an even number : '${message.text}'`);
}
}).runTest();
});
24 changes: 24 additions & 0 deletions test/ava/BotTesterFailure.ava.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,27 @@ test('will fail when there are no attachments when they are expected', (t: IAvaB
.sendMessageToBot('anything', expectedMessage)
.runTest());
});

test('Will fail when excepted function throw an error', (t: IAvaBotTest) => {
const bot = t.context.bot;

bot.dialog('/', (session: Session) => {
session.send('hello!');
session.send('13');
});

return t.throws(new BotTester(bot, getTestOptions(t))
.sendMessageToBot('Hi', (message: IMessage) => {
if (message.text === 'Hello') {
return true;
} else {
return new Error(`Message : '${message.text}' is not equal to 'Hello'`);
}
}, (message: IMessage) => {
if (parseInt(message.text, 0) % 2 === 0) {
return true;
} else {
return new Error(`Message is not an even number : '${message.text}'`);
}
}).runTest());
});
Loading

0 comments on commit 9a7064f

Please sign in to comment.