From a95c4e988dd0a15e67076a9344b2e32ffedb2761 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Fri, 18 Jan 2019 08:04:39 +0800 Subject: [PATCH] Refactoring: Migrate fifth test case in cti_caller_id_test.rb --- spec/models/cti/log_spec.rb | 64 +++++++++++++++++++++++++++++++++ test/unit/cti_caller_id_test.rb | 57 ----------------------------- 2 files changed, 64 insertions(+), 57 deletions(-) diff --git a/spec/models/cti/log_spec.rb b/spec/models/cti/log_spec.rb index ba8b299f7..cd7d3916f 100644 --- a/spec/models/cti/log_spec.rb +++ b/spec/models/cti/log_spec.rb @@ -72,6 +72,70 @@ RSpec.describe Cti::Log do end end + describe 'Callbacks -' do + describe 'Updating agent sessions:' do + before { allow(Sessions).to receive(:send_to).with(any_args) } + + context 'on creation' do + it 'pushes "cti_list_push" event' do + User.with_permissions('cti.agent').each do |u| + expect(Sessions).to receive(:send_to).with(u.id, { event: 'cti_list_push' }) + end + + create(:cti_log) + end + + context 'with over 60 existing Log records' do + before { create_list(:cti_log, 60) } + + it '(always) pushes "cti_list_push" event' do + User.with_permissions('cti.agent').each do |u| + expect(Sessions).to receive(:send_to).with(u.id, { event: 'cti_list_push' }) + end + + create(:cti_log) + end + end + end + + context 'on update' do + subject!(:log) { create(:cti_log) } + + it 'pushes "cti_list_push" event' do + User.with_permissions('cti.agent').each do |u| + expect(Sessions).to receive(:send_to).with(u.id, { event: 'cti_list_push' }) + end + + log.touch + end + + context 'when among the latest 60 Log records' do + before { create_list(:cti_log, 59) } + + it 'pushes "cti_list_push" event' do + User.with_permissions('cti.agent').each do |u| + expect(Sessions).to receive(:send_to).with(u.id, { event: 'cti_list_push' }) + end + + log.touch + end + end + + context 'when not among the latest 60 Log records' do + before { create_list(:cti_log, 60) } + + it 'does NOT push "cti_list_push" event' do + User.with_permissions('cti.agent').each do |u| + expect(Sessions).not_to receive(:send_to).with(u.id, { event: 'cti_list_push' }) + end + + log.touch + end + end + end + end + end + describe '#from_pretty' do context 'with complete, E164 international numbers' do subject(:log) { create(:cti_log, from: '4930609854180') } diff --git a/test/unit/cti_caller_id_test.rb b/test/unit/cti_caller_id_test.rb index b79b6f7f3..5037f2ee0 100644 --- a/test/unit/cti_caller_id_test.rb +++ b/test/unit/cti_caller_id_test.rb @@ -56,63 +56,6 @@ class CtiCallerIdTest < ActiveSupport::TestCase Scheduler.worker(true) end - test '5 probe if caller log need to be pushed' do - - Cti::Log.process( - 'cause' => '', - 'event' => 'newCall', - 'user' => 'user 1', - 'from' => '491111222222', - 'to' => '4930600000000', - 'callId' => 'touch-loop-0', - 'direction' => 'in', - ) - assert(Cti::Log.push_caller_list_update?(Cti::Log.last)) - - 65.times do |count| - travel 1.hour - Cti::Log.process( - 'cause' => '', - 'event' => 'newCall', - 'user' => 'user 1', - 'from' => '491111222222', - 'to' => '4930600000000', - 'callId' => "touch-loop-1-#{count}", - 'direction' => 'in', - ) - end - assert(Cti::Log.push_caller_list_update?(Cti::Log.last)) - assert_not(Cti::Log.push_caller_list_update?(Cti::Log.first)) - - 65.times do |count| - travel 1.minute - Cti::Log.process( - 'cause' => '', - 'event' => 'newCall', - 'user' => 'user 1', - 'from' => '491111222222', - 'to' => '4930600000000', - 'callId' => "touch-loop-2-#{count}", - 'direction' => 'in', - ) - end - assert(Cti::Log.push_caller_list_update?(Cti::Log.last)) - assert_not(Cti::Log.push_caller_list_update?(Cti::Log.first)) - - travel 2.seconds - Cti::Log.process( - 'cause' => '', - 'event' => 'newCall', - 'user' => 'user 1', - 'from' => '491111222222', - 'to' => '4930600000000', - 'callId' => 'touch-loop-3-1', - 'direction' => 'in', - ) - assert(Cti::Log.push_caller_list_update?(Cti::Log.last)) - - end - test 'user delete with caller log rebuild' do assert_equal(2, Cti::CallerId.where(user_id: @agent2.id).count)