Skip to content
This repository has been archived by the owner on Aug 5, 2021. It is now read-only.

Commit

Permalink
update tests for the sentiment plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
AHaydar committed Jul 22, 2020
1 parent a9ea41d commit 71bb14c
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 14 deletions.
13 changes: 9 additions & 4 deletions plugins/sentiment.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ async function analyseSentiment (messages) {
return jsonResult
}

/**
* analyse the user messages in a channel or group
* @param {*} options
* @param {*} message
* @param {*} target
*/
async function analyse (options, message, target) {
try {
const user = await findUser(options.web.users, target)
Expand All @@ -71,19 +77,18 @@ async function analyse (options, message, target) {

const targetChannel = await getTargetChannel(options.web.conversations, message.channel)
if (!targetChannel) {
message.reply_thread('Are you in a channel or group? sentiment doesn\'t work in a direct message')
message.reply_thread('Are you in a channel or group? sentiment doesn\'t work in a direct message.')
return
}

const messages = await getUserMessagesInChannel(options.web.conversations, targetChannel, user)
if (messages.length !== 0) {
const response = await analyseSentiment(messages.map(m => m.text).join('\n'))
message.reply_thread(`${target} has recently been ${response.output.result}`)
message.reply_thread(`${target} has recently been ${response.output.result}.`)
} else {
message.reply_thread(`'User ${target} has not spoken recently'`)
message.reply_thread(`User ${target} has not spoken recently.`)
}
} catch (error) {
console.error(error)
message.reply_thread(`Something went wrong! this has nothing to do with the sentiments of ${target}. Please check the logs.`)
options.logger.error(`${module.exports.name} - something went wrong. Here's the error: ${pre(error)}`)
}
Expand Down
200 changes: 190 additions & 10 deletions test/sentiment.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// const { findUser, getTargetChannel, getUserMessagesInChannel, analyseSentiment, analyse } = require('../plugins/sentiment')
const sentiment = require('../plugins/sentiment')

jest.mock('node-fetch')
Expand Down Expand Up @@ -185,16 +184,197 @@ describe('analyseSentiment function', () => {
describe('analyse function', () => {
const reply_thread = jest.fn()
const message = {
reply_thread
reply_thread,
channel: '1234'
}
it('should call find user and return a message that the name passed does not belong to a user', async () => {
jest.spyOn(sentiment, 'findUser').mockImplementation(
() => {
console.log('kousa')

it('should reply that the name passed does not belong to a user', async () => {
const list = jest.fn().mockReturnValue({
members: []
})

const options = {
web : {
users: { list }
}
}
)
const analyseResult = await sentiment.analyse({}, message, 'John')
expect(sentiment.findUser).toHaveBeenCalled()
expect(json).toHaveBeenCalled()

const analyseResult = await sentiment.analyse(options, message, 'John')
expect(reply_thread).toHaveBeenCalledWith(`I don't know of a John. Please validate you entered the correct person's name.`)
})

it('should reply that the channel or group is invalid', async () => {
const listUsers = jest.fn().mockReturnValue({
members: [
{
profile: {
display_name: 'bosta'
}
},
{
profile: {
display_name: 'John'
}
}
]
})

const listChannels = jest.fn().mockReturnValue({
channels: []
})


const options = {
web : {
users: { list: listUsers },
conversations: { list: listChannels }
}
}

const analyseResult = await sentiment.analyse(options, message, 'John')
expect(reply_thread).toHaveBeenCalledWith(`Are you in a channel or group? sentiment doesn\'t work in a direct message.`)
})

it('should reply that the person has not spoken recently', async () => {
const listUsers = jest.fn().mockReturnValue({
members: [
{
profile: {
display_name: 'bosta'
}
},
{
profile: {
display_name: 'John'
}
}
]
})

const listChannels = jest.fn().mockReturnValue({
channels: [
{
id: '1234',
is_archived: false
},
{
id: '5677',
is_archived: false
},
{
id: '9999',
is_archived: true
}
]
})

const history = jest.fn().mockReturnValue({
messages: []
})

const options = {
web : {
users: { list: listUsers },
conversations: { list: listChannels, history }
}
}

const analyseResult = await sentiment.analyse(options, message, 'John')
expect(reply_thread).toHaveBeenCalledWith(`User John has not spoken recently.`)
})

it('should call the sentiments api and return a proper sentiment result', async () => {
const listUsers = jest.fn().mockReturnValue({
members: [
{
profile: {
display_name: 'bosta'
}
},
{
id: 'user1',
profile: {
display_name: 'John',
}
}
]
})

const listChannels = jest.fn().mockReturnValue({
channels: [
{
id: '1234',
is_archived: false
},
{
id: '5677',
is_archived: false
},
{
id: '9999',
is_archived: true
}
]
})

const history = jest.fn().mockReturnValue({
messages: [
{
user: '1'
},
{
user: 'user1'
},
{
user: 'user1'
},
{
user: 'user1'
},
{
user: 'user1'
},
{
user: 'user1'
},
{
user: 'user1'
},
{
user: 'user1'
},
{
user: 'user1'
},
{
user: 'user1'
},
{
user: 'user1'
},
{
user: 'user1'
}
]
})

const options = {
web : {
users: { list: listUsers },
conversations: { list: listChannels, history }
}
}

const json = jest.fn().mockReturnValue({output: {result: 'positive'}})
const fetch = require('node-fetch').mockImplementation(
() => {
return {
json
}
}
)

const analyseResult = await sentiment.analyse(options, message, 'John')
expect(reply_thread).toHaveBeenCalledWith(`John has recently been positive.`)
})
})

0 comments on commit 71bb14c

Please sign in to comment.