diff --git a/README.md b/README.md index 76c670a..55a3860 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ For a more in depth view, check out [the Bot Tester Framework Config doc](https: import { IAddress, IMessage, Message, Prompts, Session, UniversalBot } from 'botbuilder'; import { expect } from 'chai'; import { BotTester, TestConnector } from 'bot-tester'; +import { getAdaptiveCard, getAdaptiveCardAttachment, getAdaptiveCardMessage } from './adaptiveCardProvider'; const connector = new TestConnector(); @@ -113,6 +114,19 @@ describe('BotTester', () => { }); ``` +# Test Adaptive Cards +``` javascript + it('can correctly check against adaptive cards', () => { + bot.dialog('/', (session) => { + session.send(getAdaptiveCardMessage()); + }); + + return new BotTester(bot) + .sendMessageToBot('anything', getAdaptiveCardMessage()) + .runTest(); + }); +``` + # Inspect session ```javascript it('can inspect session state', () => { diff --git a/circle.yml b/circle.yml index bf43b1c..2b6bd96 100644 --- a/circle.yml +++ b/circle.yml @@ -1,7 +1,16 @@ machine: node: - version: 6.1.0 + version: 8.0.0 + environment: + PATH: "${PATH}:${HOME}/${CIRCLE_PROJECT_REPONAME}/node_modules/.bin" +dependencies: + override: + - yarn + cache_directories: + - ~/.cache/yarn + test: override: - yarn lint && yarn coverage + diff --git a/package.json b/package.json index 08274fa..f821380 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "buildTypeDocs": "yarn cleanBuild && rm -rf docs && typedoc --excludeNotExported --out docs --hideGenerator src/BotTester.ts --module commonjs --gaID UA-105358964-1 --readme none && touch docs/.nojekyll", "ci": "npm run lint && npm run test", "coverage": "npm run coverageTests && nyc report --reporter=text-lcov | node_modules/.bin/coveralls", - "coverageTests": "nyc node_modules/.bin/_mocha --require source-map-support/register test/**/*.spec.ts" + "coverageTests": "nyc node_modules/.bin/_mocha --require source-map-support/register --require node_modules/ts-node/register test/**/*.spec.ts" }, "nyc": { "extension": [ @@ -74,8 +74,9 @@ "@types/chai": "^4.0.1", "@types/chai-as-promised": "^0.0.31", "@types/chai-subset": "^1.3.1", - "@types/mocha": "^2.2.41", + "@types/mocha": "^2.2.44", "@types/node": "^8.0.26", + "adaptivecards": "^1.0.0-beta9", "chai-as-promised": "^7.1.1", "coveralls": "^2.13.1", "istanbul": "^0.4.5", @@ -83,8 +84,8 @@ "nyc": "^11.2.1", "source-map-support": "^0.4.17", "ts-node": "^3.1.0", - "typedoc": "^0.8.0", "tslint": "^5.7.0", + "typedoc": "^0.8.0", "typescript": "^2.4.1" }, "files": [ diff --git a/src/assertionLibraries/ChaiExpectation.ts b/src/assertionLibraries/ChaiExpectation.ts index 98bb635..646a144 100644 --- a/src/assertionLibraries/ChaiExpectation.ts +++ b/src/assertionLibraries/ChaiExpectation.ts @@ -1,4 +1,4 @@ -import { IMessage } from 'botbuilder'; +import { IAttachment, IMessage } from 'botbuilder'; import * as chai from 'chai'; import { IExpectation } from './IExpectation'; @@ -72,20 +72,32 @@ export class ChaiExpectation implements IExpectation { return; } - const clone = Object.assign({}, outgoingMessage); + const outgoingMessageClone = Object.assign({}, outgoingMessage); + const expectedResponseClone = Object.assign({}, expectedResponse); + + // attachments are compared separately + const outgoingMessageAttachments = outgoingMessageClone.attachments || []; + const expectedResponseAttachments = expectedResponseClone.attachments || []; + + outgoingMessageClone.attachments = []; + expectedResponseClone.attachments = []; // ignore source event (not added to message until after sending) - delete expectedResponse.source; + delete expectedResponseClone.source; // auto added by prompts, not needed - delete outgoingMessage.inputHint; + delete outgoingMessageClone.inputHint; // always botbuilder - delete expectedResponse.agent; + delete expectedResponseClone.agent; try { - expect(clone).to.containSubset(expectedResponse); + expect(outgoingMessageClone).to.containSubset(expectedResponseClone); matchExists = true; + + if (matchExists && expectedResponseAttachments.length) { + matchExists = this.checkForMatchingAttachments(outgoingMessageAttachments, expectedResponseAttachments); + } } catch (e) { // continue, no match found } @@ -96,4 +108,30 @@ export class ChaiExpectation implements IExpectation { to be a subset of one of ${JSON.stringify(expectedResponseCollectionAsIMessage, null, 2)}`); } } + + private checkForMatchingAttachments(outgoingAttachments: IAttachment[], expectedAttachments: IAttachment[]): boolean { + let matchExists: boolean = false; + + outgoingAttachments.forEach((outgoingAttachment: IAttachment) => { + if (matchExists) { + return; + } + + expectedAttachments.forEach((expectedAttachment: IAttachment) => { + if (matchExists) { + return; + } + + try { + expect(outgoingAttachment).to.containSubset(expectedAttachment); + + matchExists = true; + } catch (e) { + // continue + } + }); + }); + + return matchExists; + } } diff --git a/test/BotTester.spec.ts b/test/BotTester.spec.ts index ef9cb55..4f8285b 100644 --- a/test/BotTester.spec.ts +++ b/test/BotTester.spec.ts @@ -4,6 +4,7 @@ import * as chai from 'chai'; import * as chaiAsPromised from 'chai-as-promised'; import { BotTester } from './../src/BotTester'; import { TestConnector } from './../src/TestConnector'; +import { getAdaptiveCard, getAdaptiveCardAttachment, getAdaptiveCardMessage } from './adaptiveCardProvider'; chai.use(chaiAsPromised); const expect = chai.expect; @@ -85,6 +86,19 @@ describe('BotTester', () => { }); //``` +//# Test Adaptive Cards +//``` javascript + it('can correctly check against adaptive cards', () => { + bot.dialog('/', (session) => { + session.send(getAdaptiveCardMessage()); + }); + + return new BotTester(bot) + .sendMessageToBot('anything', getAdaptiveCardMessage()) + .runTest(); + }); +//``` + //# Inspect session //```javascript it('can inspect session state', () => { @@ -416,5 +430,27 @@ describe('BotTester', () => { .sendMessageToBot('this could be anything!', 'hello') .runTest(); }); + + it('can ensure adaptive cards are present, regardless of order', () => { + bot.dialog('/', (session: Session) => { + session.send(getAdaptiveCardMessage()); + }); + + const matchingCard = getAdaptiveCard(); + const nonMatchingCard = getAdaptiveCard(); + + nonMatchingCard.actions = [{title: 'this is not the correct title', type: 'this is no the correct type'}]; + + const message1 = getAdaptiveCardMessage(nonMatchingCard); + const message2 = getAdaptiveCardMessage(matchingCard); + + message1.attachments.push(getAdaptiveCardAttachment(matchingCard)); + message2.attachments.push(getAdaptiveCardAttachment(nonMatchingCard)); + + return new BotTester(bot) + .sendMessageToBot('anything', message1) + .sendMessageToBot('anything', message2) + .runTest(); + }); }); }); diff --git a/test/BotTesterFailure.spec.ts b/test/BotTesterFailure.spec.ts index 37dcbd4..0f3f10a 100644 --- a/test/BotTesterFailure.spec.ts +++ b/test/BotTesterFailure.spec.ts @@ -3,6 +3,7 @@ import * as chai from 'chai'; import * as chaiAsPromised from 'chai-as-promised'; import { BotTester } from './../src/BotTester'; import { TestConnector } from './../src/TestConnector'; +import { getAdaptiveCard, getAdaptiveCardAttachment, getAdaptiveCardMessage } from './adaptiveCardProvider'; chai.use(chaiAsPromised); @@ -253,4 +254,34 @@ describe('BotTester', () => { .runTest()) .to.be.rejected.notify(done); }); + + it('will fail when there are no matching adaptive cards', (done: Function) => { + bot.dialog('/', (session: Session) => { + session.send(getAdaptiveCardMessage()); + }); + + const card = getAdaptiveCard(); + + card.actions = [{title: 'this is not the correct title', type: 'this is no the correct type'}]; + + const message = getAdaptiveCardMessage(card); + + expect(new BotTester(bot) + .sendMessageToBot('anything', message) + .runTest()).to.be.rejected.notify(done); + }); + + it('will fail when there are no attachments when they are expected', (done: Function) => { + bot.dialog('/', (session: Session) => { + session.send('hello'); + }); + + const expectedMessage = getAdaptiveCardMessage(); + + expectedMessage.text = 'hello'; + + expect(new BotTester(bot) + .sendMessageToBot('anything', expectedMessage) + .runTest()).to.be.rejected.notify(done); + }); }); diff --git a/test/adaptiveCardProvider.ts b/test/adaptiveCardProvider.ts new file mode 100644 index 0000000..da5b4b5 --- /dev/null +++ b/test/adaptiveCardProvider.ts @@ -0,0 +1,47 @@ +import { IAdaptiveCard } from 'adaptivecards'; +import { IAttachment, IMessage, Message} from 'botbuilder'; + +export function getAdaptiveCard() : IAdaptiveCard { + const action = { + type: 'Action.OpenUrl', + url: 'http://adaptivecards.io', + title: 'Learn More' + }; + + return { + type: 'AdaptiveCard', + version: '1.0', + body: [ + { + type: 'TextBlock', + text: 'Hello World!', + size: 'large' + }, + { + type: 'TextBlock', + text: '*Sincerely yours,*' + }, + { + type: 'TextBlock', + text: 'Adaptive Cards', + separation: 'none' + } + ], + actions: [ action ] + }; +} + +export function getAdaptiveCardAttachment(adaptiveCard?: IAdaptiveCard): IAttachment { + adaptiveCard = adaptiveCard || getAdaptiveCard(); + + return { + contentType: 'application/vnd.microsoft.card.adaptive', + content: adaptiveCard + }; +} + +export function getAdaptiveCardMessage(adaptiveCard?: IAdaptiveCard): IMessage { + return new Message() + .addAttachment(getAdaptiveCardAttachment(adaptiveCard)) + .toMessage(); +} diff --git a/yarn.lock b/yarn.lock index 93452bb..2ecc5a4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -63,9 +63,9 @@ version "2.0.29" resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-2.0.29.tgz#5002e14f75e2d71e564281df0431c8c1b4a2a36a" -"@types/mocha@^2.2.41": - version "2.2.42" - resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.42.tgz#ab769f51d37646b6fe8d4a086a98c285b1fab3f5" +"@types/mocha@^2.2.44": + version "2.2.44" + resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.44.tgz#1d4a798e53f35212fd5ad4d04050620171cd5b5e" "@types/node@*", "@types/node@^8.0.26": version "8.0.27" @@ -86,6 +86,10 @@ abbrev@1.0.x: version "1.0.9" resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135" +adaptivecards@^1.0.0-beta9: + version "1.0.0-beta9" + resolved "https://registry.yarnpkg.com/adaptivecards/-/adaptivecards-1.0.0-beta9.tgz#bc22f61978c58666c8e862c9ad5b4b43b0b61fa8" + ajv@^4.9.1: version "4.11.8" resolved "https://registry.yarnpkg.com/ajv/-/ajv-4.11.8.tgz#82ffb02b29e662ae53bdc20af15947706739c536"