Files
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

352 lines
13 KiB
TypeScript

/* eslint-disable new-cap */
import { expect } from 'chai';
import moment from 'moment-timezone';
import sinon from 'sinon';
import { conversations } from './mockData';
import { OverviewData } from '../../../../../server/services/omnichannel-analytics/OverviewData';
const analytics = (date: { gte: Date; lte: Date }) => {
// filter the mockData array with the date param with moment
return conversations.filter((c) => moment(c.ts).isBetween(date.gte, date.lte, undefined, '[]'));
};
describe('OverviewData Analytics', () => {
describe('isActionAllowed', () => {
it('should return false if no action is provided', () => {
const overview = new OverviewData({} as any);
expect(overview.isActionAllowed(undefined)).to.be.false;
});
it('should return false if an invalid action is provided', () => {
const overview = new OverviewData({} as any);
expect(overview.isActionAllowed('invalid_action')).to.be.false;
});
it('should return true if a valid action is provided', () => {
const overview = new OverviewData({} as any);
expect(overview.isActionAllowed('Conversations')).to.be.true;
});
});
describe('callAction', () => {
it('should fail if the action is invalid', async () => {
const overview = new OverviewData({} as any);
try {
// @ts-expect-error - Invalid action
await overview.callAction('invalid_action', {} as any);
} catch (e: any) {
expect(e.message).to.be.equal('Invalid action');
}
});
it('should call the correct action with the correct parameters', async () => {
const overview = new OverviewData({
getAnalyticsBetweenDate: () => [],
getOnHoldConversationsBetweenDate: () => 0,
getAnalyticsMetricsBetweenDate: () => [],
} as any);
const spy = sinon.spy(overview, 'Conversations');
const spy2 = sinon.spy(overview, 'Productivity');
await overview.callAction('Conversations', moment(), moment(), '', 'UTC');
expect(spy.calledOnce).to.be.true;
await overview.callAction('Productivity', moment(), moment(), '', 'UTC', (v: string): string => v);
expect(spy2.calledOnce).to.be.true;
});
});
describe('getKeyHavingMaxValue', () => {
it('should return the key with the max value', () => {
const overview = new OverviewData({} as any);
const map = new Map();
map.set('a', 1);
map.set('b', 2);
map.set('c', 3);
expect(overview.getKeyHavingMaxValue(map, 'd')).to.be.equal('c');
});
it('should return the default key if the map is empty', () => {
const overview = new OverviewData({} as any);
const map = new Map();
expect(overview.getKeyHavingMaxValue(map, 'd')).to.be.equal('d');
});
});
describe('getAllMapKeysSize', () => {
it('should return the sum of all map keys', () => {
const overview = new OverviewData({} as any);
const map = new Map();
map.set('a', 1);
map.set('b', 2);
map.set('c', 3);
expect(overview.sumAllMapKeys(map)).to.be.equal(6);
});
it('should return 0 if the map is empty', () => {
const overview = new OverviewData({} as any);
const map = new Map();
expect(overview.sumAllMapKeys(map)).to.be.equal(0);
});
});
describe('getBusiestDay', () => {
it('should return the day with the most messages', () => {
const overview = new OverviewData({} as any);
const map = new Map();
map.set(
'Monday',
new Map([
['1', 1],
['2', 2],
]),
);
map.set(
'Tuesday',
new Map([
['13', 1],
['15', 2],
]),
);
map.set(
'Sunday',
new Map([
['12', 2],
['23', 2],
]),
);
expect(overview.getBusiestDay(map)).to.be.equal('Sunday');
});
it('should return the first day with the most messages if theres a tie', () => {
const overview = new OverviewData({} as any);
const map = new Map();
map.set(
'Monday',
new Map([
['1', 1],
['2', 2],
]),
);
map.set(
'Tuesday',
new Map([
['13', 1],
['15', 2],
]),
);
map.set(
'Sunday',
new Map([
['12', 1],
['23', 2],
]),
);
expect(overview.getBusiestDay(map)).to.be.equal('Monday');
});
it('should return the default key if the map is empty', () => {
const overview = new OverviewData({} as any);
const map = new Map();
expect(overview.getBusiestDay(map)).to.be.equal('-');
});
});
describe('sumAllMapKeys', () => {
it('should return the sum of all map keys', () => {
const overview = new OverviewData({} as any);
const map = new Map();
map.set('a', 1);
map.set('b', 2);
map.set('c', 3);
expect(overview.sumAllMapKeys(map)).to.be.equal(6);
});
it('should return 0 if the map is empty', () => {
const overview = new OverviewData({} as any);
const map = new Map();
expect(overview.sumAllMapKeys(map)).to.be.equal(0);
});
});
describe('Conversations', () => {
it('should return all values as 0 when theres no data', async () => {
const overview = new OverviewData({
getAnalyticsBetweenDate: () => [],
getOnHoldConversationsBetweenDate: () => 0,
} as any);
const result = await overview.Conversations(moment(), moment(), '', 'UTC', (v: string): string => v, {});
expect(result).to.be.deep.equal([
{ title: 'Total_conversations', value: 0 },
{ title: 'Open_conversations', value: 0 },
{ title: 'On_Hold_conversations', value: 0 },
{ title: 'Total_messages', value: 0 },
{ title: 'Busiest_day', value: '-' },
{ title: 'Conversations_per_day', value: '0.00' },
{ title: 'Busiest_time', value: '-' },
]);
});
it('should return all values as 0 when theres data but not on the period we pass', async () => {
const overview = new OverviewData({
getAnalyticsBetweenDate: () => analytics({ gte: moment().set('month', 9).toDate(), lte: moment().set('month', 9).toDate() }),
getOnHoldConversationsBetweenDate: () => 0,
} as any);
const result = await overview.Conversations(moment(), moment(), '', 'UTC', (v: string): string => v, {});
expect(result).to.be.deep.equal([
{ title: 'Total_conversations', value: 0 },
{ title: 'Open_conversations', value: 0 },
{ title: 'On_Hold_conversations', value: 0 },
{ title: 'Total_messages', value: 0 },
{ title: 'Busiest_day', value: '-' },
{ title: 'Conversations_per_day', value: '0.00' },
{ title: 'Busiest_time', value: '-' },
]);
});
it('should return the correct values when theres data on the period we pass', async () => {
const overview = new OverviewData({
getAnalyticsBetweenDate: (date: { gte: Date; lte: Date }) => analytics(date),
getOnHoldConversationsBetweenDate: () => 1,
} as any);
// Fixed date to assure we get the same data
const result = await overview.Conversations(
moment.utc().set('month', 10).set('year', 2023).set('date', 12).startOf('day'),
moment.utc().set('month', 10).set('year', 2023).set('date', 12).endOf('day'),
'',
'UTC',
(v: string): string => v,
{},
);
expect(result).to.be.deep.equal([
{ title: 'Total_conversations', value: 1 },
{ title: 'Open_conversations', value: 0 },
{ title: 'On_Hold_conversations', value: 1 },
{ title: 'Total_messages', value: 93 },
{ title: 'Busiest_day', value: 'Sunday' },
{ title: 'Conversations_per_day', value: '1.00' },
{ title: 'Busiest_time', value: '11AM - 12PM' },
]);
});
it('should only return conversation metrics related to the provided period, and not consider previous or following days', async () => {
const overview = new OverviewData({
getAnalyticsBetweenDate: (date: { gte: Date; lte: Date }) => analytics(date),
getOnHoldConversationsBetweenDate: () => 1,
} as any);
// choosing this specific date since the day before and after are not empty
const targetDate = moment.utc().set('month', 10).set('year', 2023).set('date', 23);
// Fixed date to assure we get the same data
const result = await overview.Conversations(targetDate.startOf('day'), targetDate.endOf('day'), '', 'UTC');
expect(result).to.be.deep.equal([
{ title: 'Total_conversations', value: 1 },
{ title: 'Open_conversations', value: 0 },
{ title: 'On_Hold_conversations', value: 1 },
{ title: 'Total_messages', value: 14 },
{ title: 'Busiest_day', value: 'Thursday' },
{ title: 'Conversations_per_day', value: '1.00' },
{ title: 'Busiest_time', value: '7AM - 8AM' },
]);
});
it('should return all values as 0 when there is no data in the provided period, but there is data in the previous and following days', async () => {
const overview = new OverviewData({
getAnalyticsBetweenDate: (date: { gte: Date; lte: Date }) => analytics(date),
getOnHoldConversationsBetweenDate: () => 0,
} as any);
// choosing this specific date since the day before and after are not empty
const targetDate = moment.utc().set('month', 10).set('year', 2023).set('date', 13);
const result = await overview.Conversations(targetDate.startOf('day'), targetDate.endOf('day'), '', 'UTC');
expect(result).to.be.deep.equal([
{ title: 'Total_conversations', value: 0 },
{ title: 'Open_conversations', value: 0 },
{ title: 'On_Hold_conversations', value: 0 },
{ title: 'Total_messages', value: 0 },
{ title: 'Busiest_day', value: '-' },
{ title: 'Conversations_per_day', value: '0.00' },
{ title: 'Busiest_time', value: '-' },
]);
});
});
describe('Productivity', () => {
it('should return all values as 0 when theres no data', async () => {
const overview = new OverviewData({
getAnalyticsMetricsBetweenDate: () => ({
forEach: () => [],
}),
} as any);
const result = await overview.Productivity(moment(), moment(), '', 'UTC', (v: string): string => v, {});
expect(result).to.be.deep.equal([
{ title: 'Avg_response_time', value: '00:00:00' },
{ title: 'Avg_first_response_time', value: '00:00:00' },
{ title: 'Avg_reaction_time', value: '00:00:00' },
]);
});
it('should return all values as 0 when theres data but not on the period we pass', async () => {
const overview = new OverviewData({
getAnalyticsMetricsBetweenDate: (_: any, date: { gte: Date; lte: Date }) => analytics(date),
} as any);
const result = await overview.Productivity(
moment().set('month', 9),
moment().set('month', 9),
'',
'UTC',
(v: string): string => v,
{},
);
expect(result).to.be.deep.equal([
{ title: 'Avg_response_time', value: '00:00:00' },
{ title: 'Avg_first_response_time', value: '00:00:00' },
{ title: 'Avg_reaction_time', value: '00:00:00' },
]);
});
it('should return the correct values when theres data on the period we pass', async () => {
const overview = new OverviewData({
getAnalyticsMetricsBetweenDate: (_: any, date: { gte: Date; lte: Date }) => analytics(date),
} as any);
const result = await overview.Productivity(
moment().set('month', 10).set('year', 2023).startOf('month'),
moment().set('month', 10).set('year', 2023).endOf('month'),
'',
'UTC',
);
expect(result).to.be.deep.equal([
{ title: 'Avg_response_time', value: '00:00:07' },
{ title: 'Avg_first_response_time', value: '00:00:10' },
{ title: 'Avg_reaction_time', value: '00:00:49' },
]);
});
it('should only return productivity metrics related to the provided period, and not consider previous or following days', async () => {
const overview = new OverviewData({
getAnalyticsMetricsBetweenDate: (_: any, date: { gte: Date; lte: Date }) => analytics(date),
} as any);
// choosing this specific date since the day before and after are not empty
const targetDate = moment().set('month', 10).set('year', 2023).set('date', 25);
const result = await overview.Productivity(targetDate.startOf('day'), targetDate.clone().endOf('day'), '', 'UTC');
expect(result).to.be.deep.equal([
{ title: 'Avg_response_time', value: '00:00:01' },
{ title: 'Avg_first_response_time', value: '00:00:04' },
{ title: 'Avg_reaction_time', value: '00:02:03' },
]);
});
it('should return all values as 0 when there is no data in the provided period, but there is data in the previous and following days', async () => {
const overview = new OverviewData({
getAnalyticsMetricsBetweenDate: (_: any, date: { gte: Date; lte: Date }) => analytics(date),
} as any);
// choosing this specific date since the day before and after are not empty
const targetDate = moment.utc().set('month', 10).set('year', 2023).set('date', 13);
const result = await overview.Productivity(targetDate.startOf('day'), targetDate.endOf('day'), '', 'UTC');
expect(result).to.be.deep.equal([
{ title: 'Avg_response_time', value: '00:00:00' },
{ title: 'Avg_first_response_time', value: '00:00:00' },
{ title: 'Avg_reaction_time', value: '00:00:00' },
]);
});
});
});