Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬
Ruby actioncablechat-widgetconversationcustomer-supportdashboarddesigndockerdocker-imageherokuintercomjavascriptlivechatopensourcerailsrubysassvuejswhatsappzendesk
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Voice::EndConferenceJob do
let(:account) { create(:account) }
let(:channel) { create(:channel_twilio_sms, :with_voice, account: account, phone_number: '+15551239999') }
let(:conversation) { create(:conversation, account: account, inbox: channel.inbox) }
let(:call) { create(:call, conversation: conversation, status: 'completed') }
let(:conference) { instance_double(Voice::Provider::Twilio::ConferenceService, end_conference: nil, agents_remain?: false) }
before do
allow(Twilio::VoiceWebhookSetupService).to receive(:new)
.and_return(instance_double(Twilio::VoiceWebhookSetupService, perform: "AP#{SecureRandom.hex(8)}"))
real = Voice::Provider::Twilio::ConferenceService.method(:new)
allow(Voice::Provider::Twilio::ConferenceService).to receive(:new) do |call:|
allow(conference).to receive(:complete_unless_agent_joining) do |leaving_call_sid:, since:|
real.call(call: call).complete_unless_agent_joining(leaving_call_sid: leaving_call_sid, since: since)
end
conference
end
end
it 'hangs up an outbound contact still being dialled once no agent is left' do
call.update!(status: 'in_progress', direction: :outgoing)
expect { described_class.perform_now(call.id, leaving_call_sid: 'CA-left') }
.to have_enqueued_job(Voice::HangUpContactJob).with(call.id)
expect(call.reload.status).to eq('completed')
end
it 'still hangs up an outbound contact when its last round cannot end the conference' do
call.update!(status: 'in_progress', direction: :outgoing)
allow(conference).to receive(:end_conference).and_raise(Twilio::REST::TwilioError)
expect { described_class.new.send(:give_up, call, described_class::LATE_ROUNDS, nil) }
.to have_enqueued_job(Voice::HangUpContactJob).with(call.id)
expect(call.reload.status).to eq('completed')
end
it 'still hangs up an outbound contact when its last round cannot check who is on the call' do
call.update!(status: 'in_progress', direction: :outgoing)
allow(conference).to receive(:agents_remain?).and_raise(Twilio::REST::TwilioError)
expect { described_class.new.send(:give_up, call, described_class::LATE_ROUNDS, 'CA-left') }
.to have_enqueued_job(Voice::HangUpContactJob).with(call.id)
expect(call.reload.status).to eq('completed')
end
it 'ends the conference of the call' do
described_class.perform_now(call.id)
expect(Voice::Provider::Twilio::ConferenceService).to have_received(:new).with(call: call)
expect(conference).to have_received(:end_conference)
end
it 'ignores a call that no longer exists' do
described_class.perform_now(call.id + 1000)
expect(conference).not_to have_received(:end_conference)
end
it 'leaves the conference running when another agent is still on the call' do
allow(conference).to receive(:agents_remain?).and_return(true)
described_class.perform_now(call.id, leaving_call_sid: 'CA-agent-1')
expect(conference).to have_received(:agents_remain?).with(leaving_call_sid: 'CA-agent-1')
expect(conference).not_to have_received(:end_conference)
end
it 'ends the conference and completes the call once the last agent is confirmed gone' do
live = create(:call, conversation: conversation, status: 'in_progress', started_at: 1.minute.ago)
allow(ActionCable.server).to receive(:broadcast)
described_class.perform_now(live.id, leaving_call_sid: 'CA-agent-1')
expect(conference).to have_received(:end_conference)
expect(live.reload.status).to eq('completed')
end
it 'waits for an agent connection still on its way in' do
live = create(:call, conversation: conversation, status: 'in_progress', started_at: 1.minute.ago,
meta: { 'agent_legs' => { 'CA-new' => { 'state' => 'joining', 'at' => Time.zone.now.to_i } } })
expect { described_class.perform_now(live.id, leaving_call_sid: 'CA-agent-1') }
.to have_enqueued_job(described_class).with(live.id, leaving_call_sid: 'CA-agent-1')
expect(conference).not_to have_received(:end_conference)
expect(live.reload.status).to eq('in_progress')
end
it 'retries when Twilio cannot be reached' do
allow(conference).to receive(:end_conference).and_raise(Twilio::REST::TwilioError)
expect { described_class.perform_now(call.id) }.to have_enqueued_job(described_class).with(call.id)
end
it 'completes the call once the retries are spent, after a last attempt to end the conference' do
live = create(:call, conversation: conversation, status: 'in_progress', started_at: 1.minute.ago)
allow(ActionCable.server).to receive(:broadcast)
job = described_class.new(live.id)
job.exception_executions = { '[StandardError]' => 4 }
allow(job).to receive(:perform).and_raise(Twilio::REST::TwilioError)
job.perform_now
expect(conference).to have_received(:end_conference)
expect(live.reload.status).to eq('completed')
end
it 'checks for an agent still on the call before a last attempt, and carries the leg into a later round' do
live = create(:call, conversation: conversation, status: 'in_progress', started_at: 1.minute.ago)
allow(conference).to receive(:agents_remain?).and_raise(Twilio::REST::TwilioError)
job = described_class.new(live.id, leaving_call_sid: 'CA-agent-1')
job.exception_executions = { '[StandardError]' => 4 }
expect { job.perform_now }.to have_enqueued_job(described_class).with(live.id, leaving_call_sid: 'CA-agent-1', round: 1)
expect(conference).not_to have_received(:end_conference)
expect(live.reload.status).to eq('in_progress')
end
it 'leaves the conference running when the last attempt finds another agent on it' do
live = create(:call, conversation: conversation, status: 'in_progress', started_at: 1.minute.ago)
job = described_class.new(live.id, leaving_call_sid: 'CA-agent-1')
job.exception_executions = { '[StandardError]' => 4 }
allow(job).to receive(:perform).and_raise(Twilio::REST::TwilioError)
allow(conference).to receive(:agents_remain?).and_return(true)
job.perform_now
expect(conference).not_to have_received(:end_conference)
expect(live.reload.status).to eq('in_progress')
end
it 'queues a later round when Twilio still will not end the conference after the retries' do
allow(conference).to receive(:end_conference).and_raise(Twilio::REST::TwilioError)
job = described_class.new(call.id)
job.exception_executions = { '[StandardError]' => 4 }
expect { job.perform_now }.to have_enqueued_job(described_class).with(call.id, leaving_call_sid: nil, round: 1)
end
it 'stops after the last round' do
allow(conference).to receive(:end_conference).and_raise(Twilio::REST::TwilioError)
job = described_class.new(call.id, round: described_class::LATE_ROUNDS)
job.exception_executions = { '[StandardError]' => 4 }
expect { job.perform_now }.not_to have_enqueued_job(described_class)
end
end