-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bot tests and improved bot methods
- Loading branch information
Showing
14 changed files
with
288 additions
and
41 deletions.
There are no files selected for viewing
Binary file modified
BIN
-10 Bytes
(100%)
.sass-cache/9396ae2e0c0f2d234b4aea05b65689732de12f24/chatRoom.sassc
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.