Skip to content

Commit

Permalink
Add bot tests and improved bot methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenna committed Jan 22, 2017
1 parent 5359e34 commit 2b97c5c
Show file tree
Hide file tree
Showing 14 changed files with 288 additions and 41 deletions.
Binary file not shown.
Binary file modified public/img/avatars/231095546.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 5 additions & 6 deletions public/scripts/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
value: function newMessage(message) {
var activeChat = this.state.activeId;
var author = 'Telebot';
var type = 'user';
var type = 'client';
var text = message.message;
var sentAt = message.sentAt;

Expand Down Expand Up @@ -217,7 +217,6 @@
});
} else {

console.log('data', data.chats);
// Populate chats panel and messages
data.chats.forEach(function (chat, key) {
chats[chat.chatId] = chat;
Expand Down Expand Up @@ -30671,12 +30670,12 @@
sentAt = days + ', ' + time;
}

var isClient = this.props.type === 'client';
var isUser = this.props.type === 'user';

var isOrange = isClient ? 'is-orange' : '';
var nameStyle = isOrange + ' level-item client-name';
var isOrange = isUser ? 'is-orange' : '';
var nameStyle = isOrange + ' level-item user-name';

var avatar = isClient ? '/img/avatars/' + this.props.id + '.jpg' : '/img/telebot.jpg';
var avatar = isUser ? '/img/avatars/' + this.props.id + '.jpg' : '/img/telebot.jpg';

return React.createElement(
'div',
Expand Down
2 changes: 1 addition & 1 deletion public/styles/css/chatRoom.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/styles/css/chatRoom.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions public/styles/sass/chatRoom.sass
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ body
width: 26px
line-height: 0px

// Header with the client name
// Header with the user name
.header-container
border-bottom: 1px solid #dbdbdb

Expand Down Expand Up @@ -167,7 +167,7 @@ body
.message-info
line-height: 2.4rem

.client-name
.user-name
color: #56a9c0
font-size: 1.18rem
font-weight: 400
Expand Down
2 changes: 1 addition & 1 deletion public/views/chatRoom/components/chatRoom.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class ChatRoom extends React.Component {
newMessage(message) {
const activeChat = this.state.activeId;
const author = 'Telebot';
const type = 'user';
const type = 'client';
const text = message.message;
const sentAt = message.sentAt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ class MessagesSpace extends React.Component {
sentAt = days + ', ' + time
}

const isClient = this.props.type === 'client';
const isUser = this.props.type === 'user';

const isOrange = isClient ? 'is-orange' : '';
const nameStyle = `${isOrange} level-item client-name`;
const isOrange = isUser ? 'is-orange' : '';
const nameStyle = `${isOrange} level-item user-name`;

const avatar = isClient ? `/img/avatars/${this.props.id}.jpg` : `/img/telebot.jpg`;
const avatar = isUser ? `/img/avatars/${this.props.id}.jpg` : `/img/telebot.jpg`;

return (

Expand Down
7 changes: 4 additions & 3 deletions server/bot/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ bot.text((msg, reply) => {
const chatType = chat.type;
const firstname = chat.firstname;
const lastname = chat.lastname;
const type = 'client';
const type = 'user';
const message = msg.text;
const sentAt = new Date().getTime();
const sentAt = msg.date;

function saveMessage() {
Chat.insertMessage(chatId, type, firstname, message, sentAt)
Expand All @@ -55,5 +55,6 @@ bot.text((msg, reply) => {
}

saveMessage();

});

module.exports = { bot };
196 changes: 175 additions & 21 deletions server/test/bot.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,183 @@
/* eslint no-undef:off */

require('../config/config');
const expect = require('expect');
const { bot } = require('../bot/bot');
const chatSeed = require('./seed/chatSeed');
const utils = require('../bot/utils');
const botgramSeed = require('./seed/botgramSeed');
const botReply = require('../bot/botReply');
const web = require('../web/web');
const { Chat } = require('../db/model/chat');

describe('bot /start', () => {
it('should create chat in DB');
it('should send new chat to web');
it('should reply to user');
});

describe('bot /text', () => {
it('should save message in DB');
it('should send message to web');
it('should reply to user');
describe('bot', () => {
before(() => {
// Set spys
replySpy = expect.spyOn(botReply, 'send');
avatarSpy = expect.spyOn(utils, 'getUserAvatar');
sendSpy = expect.spyOn(web, 'sendMessage');

it('should create chat if inexitant');
it('should sae message after create inexistant chat');
});
// Get data
messageObj = botgramSeed.getData();
// Set data
const start = messageObj.start.chat;
startId = start.id;
startName = start.first_name;
startType = 'user'; // Set by botgram
startLast = start.last_name;

const text = messageObj.text;
textId = text.chat.id;
textType = 'user';
textName = text.chat.first_name;
textMessage = text.text;
textSentAt = new Date(text.date * 1000).toISOString(); // Response in Unix format (sec)

const noChat = messageObj.noChat;
noChatId = noChat.chat.id;
noChatType = 'user';
noChatName = noChat.chat.first_name;
noChatMessage = noChat.text;
noChatSentAt = new Date(noChat.date * 1000).toISOString(); // Response in Unix format (sec)
});

beforeEach((done) => {
// Refresh data
messageObj = botgramSeed.getData();
// Populate chats collection
chatSeed.populateChats().then(() => done());
});


describe('/start', () => {
beforeEach(() => {
bot.processMessage(messageObj.start);
});

it('should create chat in DB', (done) => {
setTimeout(() => {
Chat.findOne({ chatId: startId }).then((chat) => {
expect(chat.firstname).toBe(startName);
done();
});
}, 100);
});

it('should send new chat to web', (done) => {
const newChatSpy = expect.spyOn(web, 'newChat');
setTimeout(() => {
expect(newChatSpy).toHaveBeenCalled();
expect(newChatSpy).toHaveBeenCalledWith(startId, startType, startName, startLast);
done();
}, 100);
});

it('should reply to user', () => {
setTimeout(() => {
expect(replySpy).toHaveBeenCalled();
}, 100);
});

it('should request user avatar', () => {
expect(avatarSpy).toHaveBeenCalledWith(startId);
});
});

describe('/text', () => {
describe('with existant Chat', () => {
before(() => {
// Expect newChat isn't called
newChatSpy = expect.spyOn(Chat, 'insertChat');
});


beforeEach(() => {
// Process TEXT message
bot.processMessage(messageObj.text);
});

after(() => {
// Reset newChat behavior
newChatSpy.restore();
});

it('should save message in DB', (done) => {
setTimeout(() => {
Chat.findOne({ chatId: chatSeed.chat1.chatId }).then((chat) => {
expect(chat.messages.length).toBe(3);
expect(chat.messages[2].message).toBe(textMessage);
expect(newChatSpy).toNotHaveBeenCalled();
done();
});
}, 100);
});

it('should send message to web', (done) => {

setTimeout(() => {
expect(sendSpy).toHaveBeenCalled();
expect(sendSpy).toHaveBeenCalledWith(
textId, textType, textName, textMessage, new Date(textSentAt)
);
expect(newChatSpy).toNotHaveBeenCalled();
done();
}, 100);
});

it('should reply to user', (done) => {
setTimeout(() => {
expect(replySpy).toHaveBeenCalled();
expect(newChatSpy).toNotHaveBeenCalled();
done();
}, 100);
});
});

describe('NO previous chat', () => {
beforeEach(() => {
// Process NO CHAT message
bot.processMessage(messageObj.noChat);
});

it('should create chat if inexistant and save message', (done) => {
setTimeout(() => {
Chat.findOne({ chatId: noChatId }).then((chat) => {
expect(chat).toExist();
expect(chat.messages.length).toBe(1);
expect(chat.messages[0].message).toBe(noChatMessage);
done();
});
}, 100);
});

it('should send message after create inexistant chat', (done) => {
setTimeout(() => {
expect(sendSpy).toHaveBeenCalled();
expect(sendSpy).toHaveBeenCalledWith(
noChatId, noChatType, noChatName, noChatMessage, new Date(noChatSentAt)
);
done();
}, 100);
});

});
}); // describe(text)
}); // describe(bot)

describe('botReply send()', () => {
it('should reply client with msg param');
it('should send message to web');
it('should save message in db');
describe('botReply', () => {
describe('send()', () => {
it('should reply client with msg param');
it('should send message to web');
it('should save message in db');
});
});

describe('utils getUserAvatar()', () => {
it('should post /getUserProfilePhotos and return UserProfilePhotos obj (file_id, *_size, *_path)');
it('should post /getFile and return file_path');
it('should create write stream to /public/img/avatars');
it('should request /file/*/file_path and pipe response');
describe('utils', () => {
describe('getUserAvatar()', () => {
it('should post /getUserProfilePhotos and return UserProfilePhotos obj (file_id, *_size, *_path)');
it('should post /getFile and return file_path');
it('should create write stream to /public/img/avatars');
it('should request /file/*/file_path and pipe response');
});
});
66 changes: 66 additions & 0 deletions server/test/seed/botgramSeed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
function getData() {
const messageObj = {};

messageObj.start = {
message_id: 1345,
from: {
id: 3,
first_name: 'Juliano',
last_name: 'Penna'
},
chat: {
id: 3,
first_name: 'Juliano',
last_name: 'Penna',
type: 'private'
},
date: 1485103554,
text: '/start',
entities: [{
type: 'bot_command',
offset: 0,
length: 6
}]
};

messageObj.text = {
message_id: 1349,
from: {
id: 1,
first_name: 'Juliano',
last_name: 'Penna'
},
chat: {
id: 1,
first_name: 'Juliano',
last_name: 'Penna',
type: 'private'
},
date: 1485104036,
text: 'Zarathustra'
};

messageObj.noChat = {
message_id: 1349,
from: {
id: 99,
first_name: 'Gramps',
last_name: 'Yoho'
},
chat: {
id: 99,
first_name: 'Gramps',
last_name: 'Yoho',
type: 'private'
},
date: 1480104036,
text: 'Eat this hamburger!'
};

return messageObj;
}


module.exports = {
getData
};
2 changes: 1 addition & 1 deletion server/test/seed/chatSeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const chat2 = {
};

function populateChats() {
Chat.remove({}, () => {
return Chat.remove({}, () => {
new Chat(chat1).save();
new Chat(chat2).save();
});
Expand Down
Loading

0 comments on commit 2b97c5c

Please sign in to comment.