11import type { ILivechatVisitor } from '@rocket.chat/core-typings' ;
22import { expect } from 'chai' ;
3- import { before , describe , it } from 'mocha' ;
3+ import { before , afterEach , after , describe , it } from 'mocha' ;
44import moment from 'moment' ;
55
66import { api , getCredentials , request , credentials } from '../../../data/api-data' ;
@@ -13,6 +13,7 @@ import {
1313 getLivechatRoomInfo ,
1414 fetchInquiry ,
1515 closeOmnichannelRoom ,
16+ deleteVisitor ,
1617} from '../../../data/livechat/rooms' ;
1718
1819describe ( 'MAC' , ( ) => {
@@ -25,14 +26,29 @@ describe('MAC', () => {
2526
2627 describe ( 'MAC rooms' , ( ) => {
2728 let visitor : ILivechatVisitor ;
28- it ( 'Should create an innactive room by default' , async ( ) => {
29- const visitor = await createVisitor ( ) ;
29+ let multipleContactsVisitor : ILivechatVisitor ;
30+
31+ afterEach ( ( ) => deleteVisitor ( visitor . token ) ) ;
32+
33+ after ( ( ) => deleteVisitor ( multipleContactsVisitor . token ) ) ;
34+
35+ it ( 'Should create an innactive room and contact by default' , async ( ) => {
36+ visitor = await createVisitor ( ) ;
3037 const room = await createLivechatRoom ( visitor . token ) ;
3138
3239 expect ( room ) . to . be . an ( 'object' ) ;
3340 expect ( room . v . activity ) . to . be . undefined ;
3441 } ) ;
3542
43+ it ( 'Should create an innactive contact by default' , async ( ) => {
44+ visitor = await createVisitor ( ) ;
45+ const room = await createLivechatRoom ( visitor . token ) ;
46+
47+ const res = await request . get ( api ( `omnichannel/contacts.get` ) ) . set ( credentials ) . query ( { contactId : room . contactId } ) ;
48+ expect ( res . body . contact . channels [ 0 ] . visitor . visitorId ) . to . be . equal ( visitor . _id ) ;
49+ expect ( res . body . contact ) . not . to . have . property ( 'activity' ) ;
50+ } ) ;
51+
3652 it ( 'should mark room as active when agent sends a message' , async ( ) => {
3753 visitor = await createVisitor ( ) ;
3854 const room = await createLivechatRoom ( visitor . token ) ;
@@ -44,50 +60,75 @@ describe('MAC', () => {
4460 expect ( updatedRoom ) . to . have . nested . property ( 'v.activity' ) . and . to . be . an ( 'array' ) ;
4561 } ) ;
4662
47- it ( 'should mark multiple rooms as active when they come from same visitor after an agent sends a message' , async ( ) => {
48- const room = await createLivechatRoom ( visitor . token ) ;
63+ it ( 'should mark contact as active when agent sends a message' , async ( ) => {
64+ multipleContactsVisitor = await createVisitor ( ) ;
65+ const room = await createLivechatRoom ( multipleContactsVisitor . token ) ;
66+
67+ await sendAgentMessage ( room . _id ) ;
68+
69+ const res = await request . get ( api ( `omnichannel/contacts.get` ) ) . set ( credentials ) . query ( { contactId : room . contactId } ) ;
70+ expect ( res . body . contact . channels [ 0 ] . visitor . visitorId ) . to . be . equal ( multipleContactsVisitor . _id ) ;
71+ expect ( res . body . contact ) . to . have . property ( 'activity' ) . that . is . an ( 'array' ) . with . lengthOf ( 1 ) ;
72+ expect ( res . body . contact . activity [ 0 ] ) . to . equal ( moment . utc ( ) . format ( 'YYYY-MM' ) ) ;
73+ } ) ;
74+
75+ it ( 'should mark multiple rooms as active when they come from same contact after an agent sends a message' , async ( ) => {
76+ const room = await createLivechatRoom ( multipleContactsVisitor . token ) ;
4977
5078 await sendAgentMessage ( room . _id ) ;
5179
5280 const updatedRoom = await getLivechatRoomInfo ( room . _id ) ;
5381
5482 expect ( updatedRoom ) . to . have . nested . property ( 'v.activity' ) . and . to . be . an ( 'array' ) ;
83+ await closeOmnichannelRoom ( room . _id ) ;
84+ } ) ;
85+
86+ it ( 'should keep contact active when reusing it and an agent response is received' , async ( ) => {
87+ const room = await createLivechatRoom ( multipleContactsVisitor . token ) ;
88+
89+ await sendAgentMessage ( room . _id ) ;
5590
91+ const res = await request . get ( api ( `omnichannel/contacts.get` ) ) . set ( credentials ) . query ( { contactId : room . contactId } ) ;
92+ expect ( res . body . contact . channels [ 0 ] . visitor . visitorId ) . to . be . equal ( multipleContactsVisitor . _id ) ;
93+ expect ( res . body . contact ) . to . have . property ( 'activity' ) . that . is . an ( 'array' ) . with . lengthOf ( 1 ) ;
94+ expect ( res . body . contact . activity [ 0 ] ) . to . equal ( moment . utc ( ) . format ( 'YYYY-MM' ) ) ;
5695 await closeOmnichannelRoom ( room . _id ) ;
5796 } ) ;
5897
59- it ( 'should mark room as active when it comes from same visitor on same period, even without agent interaction' , async ( ) => {
60- const room = await createLivechatRoom ( visitor . token ) ;
98+ it ( 'should mark room as active when it comes from same contact on same period, even without agent interaction' , async ( ) => {
99+ const room = await createLivechatRoom ( multipleContactsVisitor . token ) ;
61100
62101 expect ( room ) . to . have . nested . property ( 'v.activity' ) . and . to . be . an ( 'array' ) ;
63102 expect ( room . v . activity ?. includes ( moment . utc ( ) . format ( 'YYYY-MM' ) ) ) . to . be . true ;
64-
65103 await closeOmnichannelRoom ( room . _id ) ;
66104 } ) ;
67105
68- it ( 'should mark an inquiry as active when it comes from same visitor on same period, even without agent interaction' , async ( ) => {
69- const room = await createLivechatRoom ( visitor . token ) ;
106+ it ( 'should mark an inquiry as active when it comes from same contact on same period, even without agent interaction' , async ( ) => {
107+ const room = await createLivechatRoom ( multipleContactsVisitor . token ) ;
70108 const inquiry = await fetchInquiry ( room . _id ) ;
71109
72110 expect ( inquiry ) . to . have . nested . property ( 'v.activity' ) . and . to . be . an ( 'array' ) ;
73111 expect ( inquiry . v . activity ?. includes ( moment . utc ( ) . format ( 'YYYY-MM' ) ) ) . to . be . true ;
74112 expect ( room . v . activity ?. includes ( moment . utc ( ) . format ( 'YYYY-MM' ) ) ) . to . be . true ;
75-
76113 await closeOmnichannelRoom ( room . _id ) ;
77114 } ) ;
78115
79- it ( 'visitor should be marked as active for period' , async ( ) => {
80- const { body } = await request
81- . get ( api ( 'livechat/visitors.info' ) )
82- . query ( { visitorId : visitor . _id } )
83- . set ( credentials )
84- . expect ( 'Content-Type' , 'application/json' )
85- . expect ( 200 ) ;
86-
87- expect ( body ) . to . have . nested . property ( 'visitor' ) . and . to . be . an ( 'object' ) ;
88- expect ( body . visitor ) . to . have . nested . property ( 'activity' ) . and . to . be . an ( 'array' ) ;
89- expect ( body . visitor . activity ) . to . have . lengthOf ( 1 ) ;
90- expect ( body . visitor . activity [ 0 ] ) . to . equal ( moment . utc ( ) . format ( 'YYYY-MM' ) ) ;
116+ it ( 'contact should be marked as active for period' , async ( ) => {
117+ visitor = await createVisitor ( ) ;
118+ const room = await createLivechatRoom ( visitor . token ) ;
119+ await sendAgentMessage ( room . _id ) ;
120+ const res = await request . get ( api ( `omnichannel/contacts.get` ) ) . set ( credentials ) . query ( { contactId : room . contactId } ) ;
121+
122+ expect ( res . status ) . to . be . equal ( 200 ) ;
123+ expect ( res . body ) . to . have . property ( 'success' , true ) ;
124+ expect ( res . body ) . to . have . nested . property ( 'contact' ) . and . to . be . an ( 'object' ) ;
125+ expect ( res . body . contact . channels ) . to . be . an ( 'array' ) . with . lengthOf ( 1 ) ;
126+ expect ( res . body . contact . channels [ 0 ] . name ) . to . be . equal ( 'api' ) ;
127+ expect ( res . body . contact . channels [ 0 ] . visitor . visitorId ) . to . be . equal ( visitor . _id ) ;
128+
129+ expect ( res . body . contact ) . to . have . nested . property ( 'activity' ) . and . to . be . an ( 'array' ) . with . lengthOf ( 1 ) ;
130+ expect ( res . body . contact . activity [ 0 ] ) . to . equal ( moment . utc ( ) . format ( 'YYYY-MM' ) ) ;
131+ await closeOmnichannelRoom ( room . _id ) ;
91132 } ) ;
92133 } ) ;
93134} ) ;
0 commit comments