# frozen_string_literal: true require 'rails_helper' shared_examples_for 'round_robin_handler' do describe '#round robin' do let(:account) { create(:account) } let(:agent) { create(:user, email: 'agent1@example.com', account: account) } let(:inbox) { create(:inbox, account: account) } let(:conversation) do create( :conversation, account: account, contact: create(:contact, account: account), inbox: inbox, assignee: nil ) end before do create(:inbox_member, inbox: inbox, user: agent) allow(Redis::Alfred).to receive(:rpoplpush).and_return(agent.id) end it 'runs round robin on after_save callbacks' do # run_round_robin expect(conversation.reload.assignee).to eq(agent) end it 'will not auto assign agent if enable_auto_assignment is false' do inbox.update(enable_auto_assignment: false) # run_round_robin expect(conversation.reload.assignee).to eq(nil) end it 'will not auto assign agent if its a bot conversation' do conversation = create( :conversation, account: account, contact: create(:contact, account: account), inbox: inbox, status: 'pending', assignee: nil ) # run_round_robin expect(conversation.reload.assignee).to eq(nil) end it 'gets triggered on update only when status changes to open' do conversation.status = 'resolved' conversation.save! expect(conversation.reload.assignee).to eq(agent) inbox.inbox_members.where(user_id: agent.id).first.destroy! # round robin changes assignee in this case since agent doesn't have access to inbox agent2 = create(:user, email: 'agent2@example.com', account: account) create(:inbox_member, inbox: inbox, user: agent2) allow(Redis::Alfred).to receive(:rpoplpush).and_return(agent2.id) conversation.status = 'open' conversation.save! expect(conversation.reload.assignee).to eq(agent2) end end end