From 14aa478e77f7f4dddaa6ae230419ede625bb085e Mon Sep 17 00:00:00 2001 From: Matthew Schwartz Date: Wed, 6 Dec 2017 18:34:52 -0800 Subject: [PATCH 1/3] v3.3.0 (#45) * Add message filter and in-line options update (#28) * added ability to filter messages * added in line options modifier * add builtin message filters * add builtin message filters to options/config (#30) * v3.1.0 (#32) * add builtin message filters * bump version for v3.1.0 * updated docs for publish * added ignoreInternalSaveMessage option * Add internal save ignore option (#35) * v3.2.0 * fixed a redundant definition that came from a recursive merge strategy * fix spelling error with ignoreInternalSaveMessage (#38) * 3.2.1 * remove typescript notation from docs * forcibly remove docs folder when building typeDocs * 3.2.2 * add adaptive cards (and attachment) support (#44) * add adaptiveCardProvider * add adaptivecards package * add adaptive cards implementation * add adaptive cards tests * update docs * include ts-node require * update node version * update mocha version * add yarn to circle config * v3.3.0 --- README.md | 14 ++++++ circle.yml | 11 +++- ...ries_chaiexpectation_.chaiexpectation.html | 46 ++++++++++++++--- docs/classes/_bottester_.bottester.html | 38 +++++++------- .../_expectedmessage_.expectedmessage.html | 14 +++--- .../_messageservice_.messageservice.html | 14 +++--- ...comparator_.outgoingmessagecomparator.html | 18 +++---- .../_sessionservice_.sessionservice.html | 20 ++++---- .../_testconnector_.testconnector.html | 14 +++--- ..._expectedmessage_.expectedmessagetype.html | 6 +-- ...nlibraries_iexpectation_.iexpectation.html | 10 ++-- docs/interfaces/_bottester_.ibottester.html | 14 +++--- .../_bottester_.ioptionsmodifier.html | 8 +-- docs/interfaces/_config_.iconfig.html | 12 ++--- .../_assertionlibraries_iexpectation_.html | 2 +- docs/modules/_bottester_.html | 2 +- docs/modules/_builtinmessagefilters_.html | 6 +-- docs/modules/_config_.html | 6 +-- docs/modules/_expectedmessage_.html | 4 +- docs/modules/_messageservice_.html | 2 +- package.json | 9 ++-- src/assertionLibraries/ChaiExpectation.ts | 50 ++++++++++++++++--- test/BotTester.spec.ts | 36 +++++++++++++ test/BotTesterFailure.spec.ts | 31 ++++++++++++ test/adaptiveCardProvider.ts | 47 +++++++++++++++++ yarn.lock | 10 ++-- 26 files changed, 327 insertions(+), 117 deletions(-) create mode 100644 test/adaptiveCardProvider.ts 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/docs/classes/_assertionlibraries_chaiexpectation_.chaiexpectation.html b/docs/classes/_assertionlibraries_chaiexpectation_.chaiexpectation.html index 0693f98..98c4f25 100644 --- a/docs/classes/_assertionlibraries_chaiexpectation_.chaiexpectation.html +++ b/docs/classes/_assertionlibraries_chaiexpectation_.chaiexpectation.html @@ -108,6 +108,7 @@

Properties

Methods

@@ -165,13 +166,39 @@

Private subject

subject: __type

Methods

+
+ +

Private checkForMatchingAttachments

+
    +
  • checkForMatchingAttachments(outgoingAttachments: IAttachment[], expectedAttachments: IAttachment[]): boolean
  • +
+ +

notToBeEmpty

@@ -183,7 +210,7 @@

notToBeEmpty

Returns void

@@ -201,7 +228,7 @@

toBeTrue

Returns void

@@ -219,7 +246,7 @@

toDeeplyInclude

@@ -252,7 +279,7 @@

toEqual

Parameters

@@ -276,7 +303,7 @@

toInclude

Parameters

@@ -354,6 +381,9 @@

Returns void subject +
  • + checkForMatchingAttachments +
  • notToBeEmpty
  • diff --git a/docs/classes/_bottester_.bottester.html b/docs/classes/_bottester_.bottester.html index 1059bed..7f529fd 100644 --- a/docs/classes/_bottester_.bottester.html +++ b/docs/classes/_bottester_.bottester.html @@ -142,7 +142,7 @@

    constructor

  • Parameters

    @@ -167,7 +167,7 @@

    Private bot

    bot: UniversalBot
  • @@ -177,7 +177,7 @@

    Private config

    config: IConfig
    @@ -187,7 +187,7 @@

    Private messageService

    messageService: MessageService @@ -197,7 +197,7 @@

    Private sessionLoader

    sessionLoader: SessionService
    @@ -207,7 +207,7 @@

    Private testSteps

    testSteps: TestStep[]
    @@ -225,7 +225,7 @@

    addMessageFilter

    Parameters

    @@ -249,7 +249,7 @@

    checkSession

    Parameters

    @@ -275,7 +275,7 @@

    Private convertToIMessage<
  • Parameters

    @@ -299,7 +299,7 @@

    ignoreEndOfConversationEvent

    Returns BotTester

    @@ -317,7 +317,7 @@

    ignoreTypingEvent

    Returns BotTester

    @@ -335,7 +335,7 @@

    runTest

    @@ -358,7 +358,7 @@

    sendMessageToBot

    Parameters

    @@ -385,7 +385,7 @@

    sendMessageToBotAndExpectSaveWithNoRespon

    Parameters

    @@ -409,7 +409,7 @@

    sendMessageToBotIgnoringResponseOrder

    Parameters

    @@ -435,7 +435,7 @@

    Private sendMessageTo
  • @@ -476,7 +476,7 @@

    setTimeout

    Parameters

    @@ -500,7 +500,7 @@

    then

    Parameters

    @@ -524,7 +524,7 @@

    wait

    Parameters

    diff --git a/docs/classes/_expectedmessage_.expectedmessage.html b/docs/classes/_expectedmessage_.expectedmessage.html index def33d3..94c8359 100644 --- a/docs/classes/_expectedmessage_.expectedmessage.html +++ b/docs/classes/_expectedmessage_.expectedmessage.html @@ -124,7 +124,7 @@

    constructor

  • Parameters

    @@ -146,7 +146,7 @@

    Private expectedResponseexpectedResponseCollection: PossibleExpectedMessageCollections

  • @@ -168,7 +168,7 @@

    checkForMessageMatch

  • @@ -200,7 +200,7 @@

    Private checkMessageT
  • @@ -237,7 +237,7 @@

    Private checkMessageT
  • @@ -269,7 +269,7 @@

    Private deepMessageMa
  • @@ -303,7 +303,7 @@

    toString

  • Returns string

    diff --git a/docs/classes/_messageservice_.messageservice.html b/docs/classes/_messageservice_.messageservice.html index d5b35e8..1ce7784 100644 --- a/docs/classes/_messageservice_.messageservice.html +++ b/docs/classes/_messageservice_.messageservice.html @@ -124,7 +124,7 @@

    constructor

  • Parameters

    @@ -149,7 +149,7 @@

    Private bot

    bot: UniversalBot
    @@ -159,7 +159,7 @@

    Private botToUserMess
    botToUserMessageChecker: botToUserMessageCheckerFunction
    @@ -169,7 +169,7 @@

    Private config

    config: IConfig
    @@ -186,7 +186,7 @@

    Private applyOutgoing
  • @@ -208,7 +208,7 @@

    sendMessageToBot

  • @@ -248,7 +248,7 @@

    Private setBotToUser<
  • diff --git a/docs/classes/_outgoingmessagecomparator_.outgoingmessagecomparator.html b/docs/classes/_outgoingmessagecomparator_.outgoingmessagecomparator.html index 1695f92..486ff23 100644 --- a/docs/classes/_outgoingmessagecomparator_.outgoingmessagecomparator.html +++ b/docs/classes/_outgoingmessagecomparator_.outgoingmessagecomparator.html @@ -125,7 +125,7 @@

    constructor

  • Parameters

    @@ -150,7 +150,7 @@

    Private expectedMessagesexpectedMessages: ExpectedMessage[]

  • @@ -160,7 +160,7 @@

    Private ignoreOrder

    ignoreOrder: boolean
    @@ -177,7 +177,7 @@

    compareOutgoingMessageToExpectedResponses

  • @@ -205,7 +205,7 @@

    Private compareOutgoing

    Parameters

    @@ -228,7 +228,7 @@

    Private compareOutgoing

    Parameters

    @@ -251,7 +251,7 @@

    Private dequeueNextEx
  • @@ -273,7 +273,7 @@

    expectsAdditionalMessages

  • @@ -295,7 +295,7 @@

    getTimeoutErrorMessage

  • Returns string

    diff --git a/docs/classes/_sessionservice_.sessionservice.html b/docs/classes/_sessionservice_.sessionservice.html index fced763..aed1430 100644 --- a/docs/classes/_sessionservice_.sessionservice.html +++ b/docs/classes/_sessionservice_.sessionservice.html @@ -126,7 +126,7 @@

    constructor

  • Parameters

    @@ -148,7 +148,7 @@

    Private bot

    bot: UniversalBot
    @@ -158,7 +158,7 @@

    Private currentSessioncurrentSessionLoadResolver: function

  • @@ -189,7 +189,7 @@

    Private savePerformed

    savePerformed: Promise<any>
    @@ -214,7 +214,7 @@

    Private applySessionL
  • @@ -238,7 +238,7 @@

    Private applySessionS
  • @@ -263,7 +263,7 @@

    Private createSession
  • @@ -296,7 +296,7 @@

    getInternalSaveMessage

  • @@ -329,7 +329,7 @@

    getSession

  • @@ -360,7 +360,7 @@

    Private loadSession

  • diff --git a/docs/classes/_testconnector_.testconnector.html b/docs/classes/_testconnector_.testconnector.html index e251080..c6123c6 100644 --- a/docs/classes/_testconnector_.testconnector.html +++ b/docs/classes/_testconnector_.testconnector.html @@ -122,7 +122,7 @@

    constructor

  • Returns TestConnector

    @@ -138,7 +138,7 @@

    Private newConvoCount
    newConvoCount: number
    @@ -148,7 +148,7 @@

    Private onEventHandle
    onEventHandler: function
    @@ -200,7 +200,7 @@

    Private onInvokeHandl
    onInvokeHandler: function
    @@ -265,7 +265,7 @@

    onEvent

  • Parameters

    @@ -327,7 +327,7 @@

    send

  • Parameters

    @@ -374,7 +374,7 @@

    startConversation

  • Parameters

    diff --git a/docs/enums/_expectedmessage_.expectedmessagetype.html b/docs/enums/_expectedmessage_.expectedmessagetype.html index d1c5519..9c02a30 100644 --- a/docs/enums/_expectedmessage_.expectedmessagetype.html +++ b/docs/enums/_expectedmessage_.expectedmessagetype.html @@ -90,7 +90,7 @@

    IMessage

    IMessage:
    @@ -100,7 +100,7 @@

    Regex

    Regex:
    @@ -110,7 +110,7 @@

    String

    String:
    diff --git a/docs/interfaces/_assertionlibraries_iexpectation_.iexpectation.html b/docs/interfaces/_assertionlibraries_iexpectation_.iexpectation.html index 99d2432..5965628 100644 --- a/docs/interfaces/_assertionlibraries_iexpectation_.iexpectation.html +++ b/docs/interfaces/_assertionlibraries_iexpectation_.iexpectation.html @@ -117,7 +117,7 @@

    notToBeEmpty

  • Returns void

    @@ -134,7 +134,7 @@

    toBeTrue

  • Returns void

    @@ -151,7 +151,7 @@

    toDeeplyInclude

  • Parameters

    @@ -174,7 +174,7 @@

    toEqual

  • Parameters

    @@ -197,7 +197,7 @@

    toInclude

  • Parameters

    diff --git a/docs/interfaces/_bottester_.ibottester.html b/docs/interfaces/_bottester_.ibottester.html index fcbf781..76c0aaa 100644 --- a/docs/interfaces/_bottester_.ibottester.html +++ b/docs/interfaces/_bottester_.ibottester.html @@ -119,7 +119,7 @@

    checkSession

  • @@ -156,7 +156,7 @@

    runTest

  • @@ -178,7 +178,7 @@

    sendMessageToBot

  • @@ -217,7 +217,7 @@

    sendMessageToBotAndExpectSaveWithNoRespon
  • @@ -249,7 +249,7 @@

    sendMessageToBotIgnoringResponseOrder

  • @@ -281,7 +281,7 @@

    then

  • @@ -313,7 +313,7 @@

    wait

  • diff --git a/docs/interfaces/_bottester_.ioptionsmodifier.html b/docs/interfaces/_bottester_.ioptionsmodifier.html index e920cd5..8948010 100644 --- a/docs/interfaces/_bottester_.ioptionsmodifier.html +++ b/docs/interfaces/_bottester_.ioptionsmodifier.html @@ -116,7 +116,7 @@

    addMessageFilter

  • @@ -147,7 +147,7 @@

    ignoreEndOfConversationEvent

  • @@ -169,7 +169,7 @@

    ignoreTypingEvent

  • @@ -191,7 +191,7 @@

    setTimeout

  • diff --git a/docs/interfaces/_config_.iconfig.html b/docs/interfaces/_config_.iconfig.html index b7d7fb0..612c59d 100644 --- a/docs/interfaces/_config_.iconfig.html +++ b/docs/interfaces/_config_.iconfig.html @@ -101,7 +101,7 @@

    Optional defaultAddressdefaultAddress: IAddress

    @@ -116,7 +116,7 @@

    Optional ignoreEndOf
    ignoreEndOfConversationEvent: boolean
    @@ -131,7 +131,7 @@

    Optional ignoreInternalignoreInternalSaveMessage: boolean

    @@ -146,7 +146,7 @@

    Optional ignoreTypingignoreTypingEvent: boolean

    @@ -161,7 +161,7 @@

    Optional messageFiltersmessageFilters: MessageFilter[]

    @@ -176,7 +176,7 @@

    Optional timeout

    timeout: number
    diff --git a/docs/modules/_assertionlibraries_iexpectation_.html b/docs/modules/_assertionlibraries_iexpectation_.html index f91faed..c8e1bc1 100644 --- a/docs/modules/_assertionlibraries_iexpectation_.html +++ b/docs/modules/_assertionlibraries_iexpectation_.html @@ -95,7 +95,7 @@

    expect

  • diff --git a/docs/modules/_bottester_.html b/docs/modules/_bottester_.html index 3618309..63fb812 100644 --- a/docs/modules/_bottester_.html +++ b/docs/modules/_bottester_.html @@ -98,7 +98,7 @@

    checkSessionFunction

    checkSessionFunction: function
    diff --git a/docs/modules/_builtinmessagefilters_.html b/docs/modules/_builtinmessagefilters_.html index c0ba873..db51cd4 100644 --- a/docs/modules/_builtinmessagefilters_.html +++ b/docs/modules/_builtinmessagefilters_.html @@ -91,7 +91,7 @@

    ignoreEndOfConversationEventFilter

  • Parameters

    @@ -114,7 +114,7 @@

    ignoreInternalSaveMessageFilter

  • Parameters

    @@ -137,7 +137,7 @@

    ignoreTypingEventFilter

  • Parameters

    diff --git a/docs/modules/_config_.html b/docs/modules/_config_.html index 8fb9837..fb09b5b 100644 --- a/docs/modules/_config_.html +++ b/docs/modules/_config_.html @@ -98,7 +98,7 @@

    MessageFilter

    MessageFilter: function
    @@ -137,7 +137,7 @@

    NO_TIMEOUT

    NO_TIMEOUT: -1 = -1
    @@ -152,7 +152,7 @@

    config

    config: IConfig = configInternal
    diff --git a/docs/modules/_expectedmessage_.html b/docs/modules/_expectedmessage_.html index f75cf18..25cc25c 100644 --- a/docs/modules/_expectedmessage_.html +++ b/docs/modules/_expectedmessage_.html @@ -98,7 +98,7 @@

    PossibleExpectedMessageCollections

    PossibleExpectedMessageCollections: PossibleExpectedMessageType[]
    @@ -114,7 +114,7 @@

    PossibleExpectedMessageType

    PossibleExpectedMessageType: string | IMessage | RegExp | IEvent
    diff --git a/docs/modules/_messageservice_.html b/docs/modules/_messageservice_.html index 9637c51..7cec6dc 100644 --- a/docs/modules/_messageservice_.html +++ b/docs/modules/_messageservice_.html @@ -91,7 +91,7 @@

    botToUserMessageCheckerFunction

    botToUserMessageCheckerFunction: function
    diff --git a/package.json b/package.json index 08274fa..8a03d55 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bot-tester", - "version": "3.2.2", + "version": "3.3.0", "description": "Easily test bots made with the bot builder framework", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -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" From c27949a41d725a36c5e6a321b2a88a74a48d4cd3 Mon Sep 17 00:00:00 2001 From: Matt Schwartz Date: Wed, 20 Dec 2017 17:28:53 -0800 Subject: [PATCH 2/3] create getConfig function --- src/BotTester.ts | 6 +++--- src/config.ts | 29 +++++++++++++++++++---------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/BotTester.ts b/src/BotTester.ts index c4207ba..d297f3b 100644 --- a/src/BotTester.ts +++ b/src/BotTester.ts @@ -1,7 +1,7 @@ import * as Promise from 'bluebird'; import { IAddress, IMessage, Message, Session, UniversalBot } from 'botbuilder'; import { ignoreEndOfConversationEventFilter, ignoreTypingEventFilter } from './builtInMessageFilters'; -import { config, IConfig, MessageFilter } from './config'; +import { getConfig, IConfig, MessageFilter } from './config'; import { ExpectedMessage, PossibleExpectedMessageCollections, PossibleExpectedMessageType } from './ExpectedMessage'; import { botToUserMessageCheckerFunction, MessageService } from './MessageService'; import { SessionService } from './SessionService'; @@ -109,8 +109,8 @@ export class BotTester implements IBotTester, IOptionsModifier { private testSteps: TestStep[]; private config: IConfig; - constructor(bot: UniversalBot, options: IConfig = config) { - this.config = Object.assign({}, config, options); + constructor(bot: UniversalBot, options?: IConfig) { + this.config = Object.assign({}, getConfig(), options); this.config.messageFilters = this.config.messageFilters.slice(); this.bot = bot; this.messageService = new MessageService(bot, this.config); diff --git a/src/config.ts b/src/config.ts index 923317e..af07fe7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -40,14 +40,13 @@ export interface IConfig { } const configFilePath = `bot-tester.json`; -const configFileExists = fs.existsSync(configFilePath); /** * default value for timeout. If config/options are set to this value, no timeout will be used */ export const NO_TIMEOUT = -1; -let configInternal: IConfig = { +const defaultConfig: IConfig = { timeout: NO_TIMEOUT, defaultAddress: { channelId: 'console', @@ -57,14 +56,24 @@ let configInternal: IConfig = { } }; -if (configFileExists) { - configInternal = JSON.parse(fs.readFileSync(configFilePath, { encoding: 'utf8' })); -} +export function getConfig(): IConfig { + let configInternal: IConfig; -configInternal.messageFilters = []; + if (configInternal) { + return configInternal; + } -if (configInternal.ignoreInternalSaveMessage) { - configInternal.messageFilters.push(ignoreInternalSaveMessageFilter); -} + const configFileExists = fs.existsSync(configFilePath); + + if (configFileExists) { + configInternal = JSON.parse(fs.readFileSync(configFilePath, { encoding: 'utf8' })); + } -export const config = configInternal; + configInternal.messageFilters = []; + + if (configInternal.ignoreInternalSaveMessage) { + configInternal.messageFilters.push(ignoreInternalSaveMessageFilter); + } + + return configInternal; +} From b136bca5fa98532184c61a18bdbb99b38cde87b4 Mon Sep 17 00:00:00 2001 From: Matt Schwartz Date: Wed, 20 Dec 2017 17:35:56 -0800 Subject: [PATCH 3/3] set internal config to have a default value if undef --- src/config.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/config.ts b/src/config.ts index af07fe7..04697b0 100644 --- a/src/config.ts +++ b/src/config.ts @@ -63,6 +63,8 @@ export function getConfig(): IConfig { return configInternal; } + configInternal = defaultConfig; + const configFileExists = fs.existsSync(configFilePath); if (configFileExists) {