Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with greeting [BOTKIT] #59

Merged
merged 11 commits into from
Dec 3, 2019
Merged
5 changes: 4 additions & 1 deletion features/greeting/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ const replies = require('./replies.js')
const randomArrayItem = array => array[Math.floor(Math.random() * array.length)]

module.exports = (controller) => {
const matchKeys = queries.join('|')
const regexp = new RegExp(`\\b(${matchKeys})\\b`, 'i')

controller.hears(
queries,
regexp,
['message', 'direct_message'],
async (bot, message) => {
await bot.reply(message, { text: randomArrayItem(replies) })
Expand Down
2 changes: 1 addition & 1 deletion features/greeting/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = [
'Hello',
'Good morning',
'G\'day',
'\^Hi\$',
'Hi',
'Morning',
'Hey',
]
3,396 changes: 722 additions & 2,674 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"botbuilder-adapter-slack": "^1.0.2",
"botbuilder-adapter-slack": "^1.0.7",
"botbuilder-storage-mongodb": "^0.9.5",
"botkit": "^4.0.1",
"botkit-mock": "^0.1.11",
"botkit": "^4.6.1",
"botkit-mock": "^4.0.0",
"botkit-plugin-cms": "^1.0.0",
"dotenv": "^7.0.0"
},
Expand Down
46 changes: 29 additions & 17 deletions specs/greeting_spec.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
const assert = require('assert')
const Botmock = require('botkit-mock')

const { getBasicController } = require('./helpers')
const greetingController = require('../features/greeting')
const replies = require('../features/greeting/replies.js')

describe('Sample hears controller', () => {
beforeEach(() => {
this.controller = Botmock({})
this.bot = this.controller.spawn({ type: 'slack' })
this.controller = getBasicController()
greetingController(this.controller)
})

it(
'Should return any greeting if user types `hi`',
() => this.bot.usersInput([{
it('Should return any greeting if user types `hi`', async () => {
nikitasakau marked this conversation as resolved.
Show resolved Hide resolved
await this.controller.usersInput([{
type: 'message',
channel: 'channel',
messages: [{
text: 'hi', isAssertion: true,
}],
}]).then(message => assert(replies.includes(message.text)))
)
})

it('Shouldn\'t return any greeting if user types `Morning` in one line code block', async () => {
await this.controller.usersInput([{
type: 'message',
channel: 'channel',
messages: [{
text: '`Morning`', isAssertion: true,
}],
}]).then(message => assert.deepEqual(message, {}))
})

it(
"Shouldn't return any greeting if user types hi in one line code block",
() =>this.bot.usersInput([{
it('Shouldn\'t return any greeting if user types `Morning` in multiline code block', async () => {
await this.controller.usersInput([{
type: 'message',
channel: 'channel',
messages: [{
text: '`hi`', isAssertion: true,
text: '```Morning```', isAssertion: true,
}],
}]).then(message => assert.deepEqual(message, {}))
)
})

it(
"Shouldn't return any greeting if user types hi in multiline code block",
() => this.bot.usersInput([{
it('Shouldn\'t return any greeting if user types a word that includes greeting word', async () => {
await this.controller.usersInput([{
type: 'message',
channel: 'channel',
messages: [{
text: '```hi```', isAssertion: true,
text: 'they', isAssertion: true,
}],
}]).then(message => assert.deepEqual(message, {}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thoughts about moving this method to a helper method for tests? It's duplicated lot of times

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AleksSenkou Do you mean message => assert.deepEqual(message, {}) only or the whole usersInput?

Copy link
Contributor

@AleksSenkou AleksSenkou Dec 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikitasakov I'm thinking about:

await userInputs('they').then(message => assert.deepEqual(message, {}))

thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you use await on promise?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or:

await userInputs('they').then(noAnswerFromBotExpected)

Copy link
Member Author

@nikitasakau nikitasakau Dec 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lazycoder9 I did as in docs - https://github.com/gratifyguy/botkit-mock. Or it is not the same?

)
})
})
26 changes: 26 additions & 0 deletions specs/helpers/getBasicController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { BotMock, SlackApiMock } = require('botkit-mock')
const { SlackAdapter, SlackMessageTypeMiddleware, SlackEventMiddleware } = require('botbuilder-adapter-slack');

const { removeCodeFromMessage } = require('../../lib/middleware');

module.exports = () => {
const adapter = new SlackAdapter({
clientSigningSecret: "secret",
botToken: "token",
debug: true
});

adapter.use(new SlackEventMiddleware())
adapter.use(new SlackMessageTypeMiddleware())

const controller = new BotMock({
adapter: adapter,
disable_webserver: true
});

controller.middleware.ingest.use(removeCodeFromMessage)

SlackApiMock.bindMockApi(controller)

return controller
}
5 changes: 5 additions & 0 deletions specs/helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const getBasicController = require('./getBasicController')

module.exports = {
getBasicController,
}
15 changes: 8 additions & 7 deletions specs/mention_spec.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
const { BotMock, SlackApiMock } = require('botkit-mock')
const assert = require('assert')
const Botmock = require('botkit-mock')

const { getBasicController } = require('./helpers')
const mentionController = require('../features/mention.js')
const replies = require('../features/mention/replies')
const { removeCodeFromMessage } = require('../lib/middleware')

describe('Mention controller', () => {
beforeEach(() => {
this.controller = Botmock({})
this.bot = this.controller.spawn({ type: 'slack' })
this.controller = getBasicController()
mentionController(this.controller)
})

it(
'should return one of mention responds if user mentions bot',
() => this.bot.usersInput([{
it('should return one of mention responds if user mentions bot', async () => {
await this.controller.usersInput([{
type: 'direct_mention',
channel: 'channel',
messages: [{
text: 'bot', isAssertion: true,
}],
}]).then(message => assert(replies.includes(message.text)))
)
})
})