Skip to content

Commit

Permalink
test: Improve tests for legacy omnichannel analytics (#30379)
Browse files Browse the repository at this point in the history
  • Loading branch information
murtaza98 authored Oct 6, 2023
1 parent 75f0ae3 commit df5c496
Show file tree
Hide file tree
Showing 6 changed files with 523 additions and 42 deletions.
26 changes: 8 additions & 18 deletions apps/meteor/tests/data/livechat/department.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { faker } from '@faker-js/faker';
import { expect } from 'chai';
import type { ILivechatDepartment, IUser, LivechatDepartmentDTO } from '@rocket.chat/core-typings';
import { api, credentials, methodCall, request } from '../api-data';
import { IUserCredentialsHeader, password } from '../user';
import { login } from '../users.helper';
import { createAgent, makeAgentAvailable } from './rooms';
import { IUserCredentialsHeader } from '../user';
import { createAnOnlineAgent } from './users';
import { WithRequiredProperty } from './utils';

export const NewDepartmentData = ((): Partial<ILivechatDepartment> => ({
enabled: true,
Expand Down Expand Up @@ -59,29 +59,19 @@ new Promise((resolve, reject) => {

export const createDepartmentWithAnOnlineAgent = async (): Promise<{department: ILivechatDepartment, agent: {
credentials: IUserCredentialsHeader;
user: IUser;
user: WithRequiredProperty<IUser, 'username'>;
}}> => {
// TODO moving here for tests
const username = `user.test.${Date.now()}`;
const email = `${username}@rocket.chat`;
const { body } = await request
.post(api('users.create'))
.set(credentials)
.send({ email, name: username, username, password });
const agent = body.user;
const createdUserCredentials = await login(agent.username, password);
await createAgent(agent.username);
await makeAgentAvailable(createdUserCredentials);
const { user, credentials } = await createAnOnlineAgent();

const department = await createDepartmentWithMethod() as ILivechatDepartment;

await addOrRemoveAgentFromDepartment(department._id, {agentId: agent._id, username: (agent.username as string)}, true);
await addOrRemoveAgentFromDepartment(department._id, {agentId: user._id, username: user.username}, true);

return {
department,
agent: {
credentials: createdUserCredentials,
user: agent,
credentials,
user,
}
};
};
Expand Down
3 changes: 3 additions & 0 deletions apps/meteor/tests/data/livechat/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ export const getLivechatRoomInfo = (roomId: string): Promise<IOmnichannelRoom> =
});
};

/**
* @summary Sends message as visitor
*/
export const sendMessage = (roomId: string, message: string, visitorToken: string): Promise<IMessage> => {
return new Promise((resolve, reject) => {
request
Expand Down
23 changes: 22 additions & 1 deletion apps/meteor/tests/data/livechat/users.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { faker } from "@faker-js/faker";
import type { IUser } from "@rocket.chat/core-typings";
import { password } from "../user";
import { IUserCredentialsHeader, password } from "../user";
import { createUser, login } from "../users.helper";
import { createAgent, makeAgentAvailable } from "./rooms";
import { api, credentials, request } from "../api-data";
Expand Down Expand Up @@ -29,3 +29,24 @@ export const removeAgent = async (userId: string): Promise<void> => {
.set(credentials)
.expect(200);
}

export const createAnOnlineAgent = async (): Promise<{
credentials: IUserCredentialsHeader;
user: IUser & { username: string };
}> => {
const username = `user.test.${Date.now()}`;
const email = `${username}@rocket.chat`;
const { body } = await request
.post(api('users.create'))
.set(credentials)
.send({ email, name: username, username, password });
const agent = body.user;
const createdUserCredentials = await login(agent.username, password);
await createAgent(agent.username);
await makeAgentAvailable(createdUserCredentials);

return {
credentials: createdUserCredentials,
user: agent,
};
}
4 changes: 4 additions & 0 deletions apps/meteor/tests/data/livechat/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export type DummyResponse<T, E = 'wrapped'> =
E extends 'wrapped' ? { body: { [k: string]: T } } : { body: T };

export type WithRequiredProperty<Type, Key extends keyof Type> = Type & {
[Property in Key]-?: Type[Property];
};

export const sleep = (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Loading

0 comments on commit df5c496

Please sign in to comment.