fix: backTest
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Vicent Llopis 2022-06-03 13:36:21 +02:00
parent 3eac002fb4
commit fa27e6dd58
6 changed files with 79 additions and 72 deletions

View File

@ -42,7 +42,7 @@ module.exports = Self => {
return true; return true;
} }
};
return false; return false;
};
}; };

View File

@ -43,7 +43,7 @@ module.exports = Self => {
if (!recipient) if (!recipient)
throw new Error(`Could not send message "${message}" to worker id ${recipientId} from user ${userId}`); throw new Error(`Could not send message "${message}" to worker id ${recipientId} from user ${userId}`);
return models.Chat.create({ await models.Chat.create({
senderFk: sender.id, senderFk: sender.id,
recipient: `@${recipient.name}`, recipient: `@${recipient.name}`,
dated: new Date(), dated: new Date(),
@ -52,5 +52,7 @@ module.exports = Self => {
status: 0, status: 0,
attempts: 0 attempts: 0
}); });
return true;
}; };
}; };

View File

@ -30,14 +30,14 @@ module.exports = Self => {
for (let chat of chats) { for (let chat of chats) {
if (chat.checkUserStatus) { if (chat.checkUserStatus) {
try { try {
await sendCheckingUserStatus(chat); await Self.sendCheckingUserStatus(chat);
await updateChat(chat, sentStatus); await updateChat(chat, sentStatus);
} catch (error) { } catch (error) {
await updateChat(chat, errorStatus); await updateChat(chat, errorStatus);
} }
} else { } else {
try { try {
await sendMessage(chat.senderFk, chat.recipient, chat.message); await Self.sendMessage(chat.senderFk, chat.recipient, chat.message);
await updateChat(chat, sentStatus); await updateChat(chat, sentStatus);
} catch (error) { } catch (error) {
await updateChat(chat, errorStatus); await updateChat(chat, errorStatus);
@ -46,7 +46,7 @@ module.exports = Self => {
} }
}; };
async function sendCheckingUserStatus(chat) { Self.sendCheckingUserStatus = async function sendCheckingUserStatus(chat) {
const models = Self.app.models; const models = Self.app.models;
const recipientName = chat.recipient.slice(1); const recipientName = chat.recipient.slice(1);
@ -56,7 +56,7 @@ module.exports = Self => {
} }
}); });
const {data} = await getUserStatus(recipient.name); const {data} = await Self.getUserStatus(recipient.name);
if (data) { if (data) {
if (data.status === 'offline' || data.status === 'busy') { if (data.status === 'offline' || data.status === 'busy') {
// Send message to department room // Send message to department room
@ -75,7 +75,8 @@ module.exports = Self => {
} else } else
return sendMessage(chat.senderFk, `@${recipient.name}`, chat.message); return sendMessage(chat.senderFk, `@${recipient.name}`, chat.message);
} }
} };
/** /**
* Update status and attempts of a chat * Update status and attempts of a chat
* *
@ -84,7 +85,7 @@ module.exports = Self => {
* @param {string} message - The message to send * @param {string} message - The message to send
* @return {Promise} - The request promise * @return {Promise} - The request promise
*/ */
async function sendMessage(senderFk, recipient, message) { Self.sendMessage = async function sendMessage(senderFk, recipient, message) {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
return new Promise(resolve => { return new Promise(resolve => {
return resolve({ return resolve({
@ -113,15 +114,15 @@ module.exports = Self => {
'alias': sender.nickname, 'alias': sender.nickname,
'text': message 'text': message
}, options); }, options);
} };
/** /**
* Update status and attempts of a chat * Update status and attempts of a chat
* *
* @param {object} chat - The chat * @param {object} chat - The chat
* @param {string} status - The new status * @param {string} status - The new status
* @return {Promise} - The request promise * @return {Promise} - The request promise
*/ */
async function updateChat(chat, status) { async function updateChat(chat, status) {
return chat.updateAttributes({ return chat.updateAttributes({
status: status, status: status,
@ -130,12 +131,12 @@ module.exports = Self => {
} }
/** /**
* Returns the current user status on Rocketchat * Returns the current user status on Rocketchat
* *
* @param {string} username - The recipient user name * @param {string} username - The recipient user name
* @return {Promise} - The request promise * @return {Promise} - The request promise
*/ */
async function getUserStatus(username) { Self.getUserStatus = async function getUserStatus(username) {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
return new Promise(resolve => { return new Promise(resolve => {
return resolve({ return resolve({
@ -157,5 +158,5 @@ module.exports = Self => {
}; };
return axios.get(`${login.api}/users.getStatus`, options); return axios.get(`${login.api}/users.getStatus`, options);
} };
}; };

View File

@ -1,14 +1,14 @@
const app = require('vn-loopback/server/server'); const app = require('vn-loopback/server/server');
describe('Chat send()', () => { describe('Chat send()', () => {
it('should return a "Fake notification sent" as response', async() => { it('should return true as response', async() => {
let ctx = {req: {accessToken: {userId: 1}}}; let ctx = {req: {accessToken: {userId: 1}}};
let response = await app.models.Chat.send(ctx, '@salesPerson', 'I changed something'); let response = await app.models.Chat.send(ctx, '@salesPerson', 'I changed something');
expect(response).toEqual(true); expect(response).toEqual(true);
}); });
it('should retrun false as response', async() => { it('should return false as response', async() => {
let ctx = {req: {accessToken: {userId: 18}}}; let ctx = {req: {accessToken: {userId: 18}}};
let response = await app.models.Chat.send(ctx, '@salesPerson', 'I changed something'); let response = await app.models.Chat.send(ctx, '@salesPerson', 'I changed something');

View File

@ -1,58 +1,21 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
describe('Chat sendCheckingPresence()', () => { describe('Chat sendCheckingPresence()', () => {
const today = new Date(); it('should return true as response', async() => {
today.setHours(6, 0); const workerId = 1107;
const ctx = {req: {accessToken: {userId: 1}}};
const chatModel = models.Chat;
const departmentId = 23;
const workerId = 1107;
it(`should call to send() method with "@HankPym" as recipient argument`, async() => { let ctx = {req: {accessToken: {userId: 1}}};
spyOn(chatModel, 'send').and.callThrough(); let response = await models.Chat.sendCheckingPresence(ctx, workerId, 'I changed something');
spyOn(chatModel, 'getUserStatus').and.returnValue(
new Promise(resolve => {
return resolve({
data: {
status: 'online'
}
});
})
);
await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something'); expect(response).toEqual(true);
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', 'I changed something');
}); });
it(`should call to send() method with "#cooler" as recipient argument`, async() => { it('should return false as response', async() => {
spyOn(chatModel, 'send').and.callThrough(); const salesPersonId = 18;
spyOn(chatModel, 'getUserStatus').and.returnValue(
new Promise(resolve => {
return resolve({
data: {
status: 'offline'
}
});
})
);
const tx = await models.Claim.beginTransaction({}); let ctx = {req: {accessToken: {userId: 18}}};
let response = await models.Chat.sendCheckingPresence(ctx, salesPersonId, 'I changed something');
try { expect(response).toEqual(false);
const options = {transaction: tx};
const department = await models.Department.findById(departmentId, null, options);
await department.updateAttribute('chatName', 'cooler');
await chatModel.sendCheckingPresence(ctx, workerId, 'I changed something');
expect(chatModel.send).toHaveBeenCalledWith(ctx, '#cooler', '@HankPym ➔ I changed something');
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
}); });

View File

@ -0,0 +1,41 @@
const models = require('vn-loopback/server/server').models;
describe('Chat sendCheckingPresence()', () => {
const today = new Date();
today.setHours(6, 0);
const chatModel = models.Chat;
it(`should call to sendCheckingUserStatus()`, async() => {
spyOn(chatModel, 'sendCheckingUserStatus').and.callThrough();
const chat = {
checkUserStatus: 1,
status: 0,
attempts: 0
};
await chatModel.destroyAll();
await chatModel.create(chat);
await chatModel.sendQueued();
expect(chatModel.sendCheckingUserStatus).toHaveBeenCalled();
});
it(`should call to sendMessage() method`, async() => {
spyOn(chatModel, 'sendMessage').and.callThrough();
const chat = {
checkUserStatus: 0,
status: 0,
attempts: 0
};
await chatModel.destroyAll();
await chatModel.create(chat);
await chatModel.sendQueued();
expect(chatModel.sendMessage).toHaveBeenCalled();
});
});