From b28d00a117a5a44cb0b8ff1f6a057027f8352e4b Mon Sep 17 00:00:00 2001 From: Tanmay Deep Sharma Date: Thu, 4 Sep 2025 16:21:29 +0700 Subject: [PATCH] fix rubocop --- .../auto_assignment/balanced_selector_spec.rb | 43 ++++++++-------- .../auto_assignment/assignment_job_spec.rb | 10 ++-- .../periodic_assignment_job_spec.rb | 4 +- .../assignment_service_spec.rb | 50 ++++++++++--------- .../inbox_round_robin_service_spec.rb | 4 +- .../round_robin_selector_spec.rb | 21 ++++---- 6 files changed, 66 insertions(+), 66 deletions(-) diff --git a/spec/enterprise/services/enterprise/auto_assignment/balanced_selector_spec.rb b/spec/enterprise/services/enterprise/auto_assignment/balanced_selector_spec.rb index 5a75ac969..abc4c5f82 100644 --- a/spec/enterprise/services/enterprise/auto_assignment/balanced_selector_spec.rb +++ b/spec/enterprise/services/enterprise/auto_assignment/balanced_selector_spec.rb @@ -7,29 +7,26 @@ RSpec.describe Enterprise::AutoAssignment::BalancedSelector do let(:agent1) { create(:user, account: account, role: :agent, availability: :online) } let(:agent2) { create(:user, account: account, role: :agent, availability: :online) } let(:agent3) { create(:user, account: account, role: :agent, availability: :online) } - - before do - @member1 = create(:inbox_member, inbox: inbox, user: agent1) - @member2 = create(:inbox_member, inbox: inbox, user: agent2) - @member3 = create(:inbox_member, inbox: inbox, user: agent3) - end + let(:member1) { create(:inbox_member, inbox: inbox, user: agent1) } + let(:member2) { create(:inbox_member, inbox: inbox, user: agent2) } + let(:member3) { create(:inbox_member, inbox: inbox, user: agent3) } describe '#select_agent' do context 'when selecting based on workload' do - let(:available_agents) { [@member1, @member2, @member3] } + let(:available_agents) { [member1, member2, member3] } it 'selects the agent with least open conversations' do # Agent1 has 3 open conversations 3.times { create(:conversation, inbox: inbox, assignee: agent1, status: 'open') } - + # Agent2 has 1 open conversation create(:conversation, inbox: inbox, assignee: agent2, status: 'open') - + # Agent3 has 2 open conversations 2.times { create(:conversation, inbox: inbox, assignee: agent3, status: 'open') } - + selected_agent = selector.select_agent(available_agents) - + # Should select agent2 as they have the least conversations expect(selected_agent).to eq(agent2) end @@ -38,29 +35,29 @@ RSpec.describe Enterprise::AutoAssignment::BalancedSelector do # Agent1 has 1 open and 3 resolved conversations create(:conversation, inbox: inbox, assignee: agent1, status: 'open') 3.times { create(:conversation, inbox: inbox, assignee: agent1, status: 'resolved') } - + # Agent2 has 2 open conversations 2.times { create(:conversation, inbox: inbox, assignee: agent2, status: 'open') } - - selected_agent = selector.select_agent([member1, @member2]) - + + selected_agent = selector.select_agent([member1, member2]) + # Should select agent1 as they have fewer open conversations expect(selected_agent).to eq(agent1) end it 'uses round robin as fallback when agents have equal workload' do # All agents have same number of conversations - [@member1, @member2, @member3].each do |member| + [member1, member2, member3].each do |member| create(:conversation, inbox: inbox, assignee: member.user, status: 'open') end - + round_robin_service = instance_double(AutoAssignment::InboxRoundRobinService) allow(AutoAssignment::InboxRoundRobinService).to receive(:new).and_return(round_robin_service) - + expect(round_robin_service).to receive(:available_agent).with( allowed_agent_ids: [agent1.id.to_s, agent2.id.to_s, agent3.id.to_s] ) - + selector.select_agent(available_agents) end end @@ -74,7 +71,7 @@ RSpec.describe Enterprise::AutoAssignment::BalancedSelector do context 'when one agent is available' do it 'returns that agent' do - selected_agent = selector.select_agent([@member1]) + selected_agent = selector.select_agent([member1]) expect(selected_agent).to eq(agent1) end end @@ -84,12 +81,12 @@ RSpec.describe Enterprise::AutoAssignment::BalancedSelector do # Agent1 and 2 have conversations create(:conversation, inbox: inbox, assignee: agent1, status: 'open') create(:conversation, inbox: inbox, assignee: agent2, status: 'open') - + # Agent3 is new with no conversations selected_agent = selector.select_agent(available_agents) - + expect(selected_agent).to eq(agent3) end end end -end \ No newline at end of file +end diff --git a/spec/jobs/auto_assignment/assignment_job_spec.rb b/spec/jobs/auto_assignment/assignment_job_spec.rb index f1366a966..9cda78b83 100644 --- a/spec/jobs/auto_assignment/assignment_job_spec.rb +++ b/spec/jobs/auto_assignment/assignment_job_spec.rb @@ -32,7 +32,7 @@ RSpec.describe AutoAssignment::AssignmentJob, type: :job do it 'uses custom bulk limit from environment' do allow(ENV).to receive(:fetch).with('AUTO_ASSIGNMENT_BULK_LIMIT', 100).and_return('50') - + service = instance_double(AutoAssignment::AssignmentService) allow(AutoAssignment::AssignmentService).to receive(:new).with(inbox: inbox).and_return(service) expect(service).to receive(:perform_bulk_assignment).with(limit: 50).and_return(2) @@ -56,7 +56,7 @@ RSpec.describe AutoAssignment::AssignmentJob, type: :job do it 'returns early without processing' do expect(AutoAssignment::AssignmentService).not_to receive(:new) - described_class.new.perform(inbox_id: 999999) + described_class.new.perform(inbox_id: 999_999) end end @@ -68,9 +68,9 @@ RSpec.describe AutoAssignment::AssignmentJob, type: :job do expect(Rails.logger).to receive(:error).with("Bulk assignment failed for inbox #{inbox.id}: Something went wrong") - expect { + expect do described_class.new.perform(inbox_id: inbox.id) - }.to raise_error(StandardError, 'Something went wrong') + end.to raise_error(StandardError, 'Something went wrong') end end end @@ -80,4 +80,4 @@ RSpec.describe AutoAssignment::AssignmentJob, type: :job do expect(described_class.queue_name).to eq('default') end end -end \ No newline at end of file +end diff --git a/spec/jobs/auto_assignment/periodic_assignment_job_spec.rb b/spec/jobs/auto_assignment/periodic_assignment_job_spec.rb index 69efcb6a0..230ce774a 100644 --- a/spec/jobs/auto_assignment/periodic_assignment_job_spec.rb +++ b/spec/jobs/auto_assignment/periodic_assignment_job_spec.rb @@ -69,7 +69,7 @@ RSpec.describe AutoAssignment::PeriodicAssignmentJob, type: :job do context 'with batch processing' do it 'processes accounts in batches' do # Create multiple accounts - 5.times do |i| + 5.times do |_i| acc = create(:account) inb = create(:inbox, account: acc, enable_auto_assignment: true) create(:assignment_policy, inbox: inb) @@ -89,4 +89,4 @@ RSpec.describe AutoAssignment::PeriodicAssignmentJob, type: :job do expect(described_class.queue_name).to eq('scheduled_jobs') end end -end \ No newline at end of file +end diff --git a/spec/services/auto_assignment/assignment_service_spec.rb b/spec/services/auto_assignment/assignment_service_spec.rb index 2c9c26758..ea03ae46d 100644 --- a/spec/services/auto_assignment/assignment_service_spec.rb +++ b/spec/services/auto_assignment/assignment_service_spec.rb @@ -46,7 +46,7 @@ RSpec.describe AutoAssignment::AssignmentService do it 'only assigns open conversations' do resolved_conversation = create(:conversation, inbox: inbox, assignee: nil, status: 'resolved') - assigned_count = service.perform_bulk_assignment(limit: 10) + service.perform_bulk_assignment(limit: 10) expect(conversation.reload.assignee).to eq(agent) expect(resolved_conversation.reload.assignee).to be_nil @@ -54,7 +54,7 @@ RSpec.describe AutoAssignment::AssignmentService do it 'does not reassign already assigned conversations' do assigned_conversation = create(:conversation, inbox: inbox, assignee: agent) - unassigned_conversation = create(:conversation, inbox: inbox, assignee: nil) + create(:conversation, inbox: inbox, assignee: nil) assigned_count = service.perform_bulk_assignment(limit: 10) @@ -95,16 +95,16 @@ RSpec.describe AutoAssignment::AssignmentService do end it 'assigns conversations with oldest last_activity_at first' do - old_conversation = create(:conversation, - inbox: inbox, - assignee: nil, - created_at: 2.hours.ago, - last_activity_at: 2.hours.ago) - new_conversation = create(:conversation, - inbox: inbox, - assignee: nil, - created_at: 1.hour.ago, - last_activity_at: 1.hour.ago) + old_conversation = create(:conversation, + inbox: inbox, + assignee: nil, + created_at: 2.hours.ago, + last_activity_at: 2.hours.ago) + new_conversation = create(:conversation, + inbox: inbox, + assignee: nil, + created_at: 1.hour.ago, + last_activity_at: 1.hour.ago) service.perform_bulk_assignment(limit: 1) @@ -130,23 +130,27 @@ RSpec.describe AutoAssignment::AssignmentService do before do create(:inbox_member, inbox: inbox, user: agent2) allow(OnlineStatusTracker).to receive(:get_available_users).and_return({ - agent.id.to_s => 'online', - agent2.id.to_s => 'online' - }) + agent.id.to_s => 'online', + agent2.id.to_s => 'online' + }) end context 'when fair distribution is enabled' do before do allow(inbox).to receive(:auto_assignment_config).and_return({ - 'fair_distribution_limit' => 2, - 'fair_distribution_window' => 3600 - }) + 'fair_distribution_limit' => 2, + 'fair_distribution_window' => 3600 + }) end it 'respects the assignment limit per agent' do # Mock agent1 at limit - allow_any_instance_of(AutoAssignment::RateLimiter).to receive(:within_limit?) do |limiter| - limiter.agent == agent ? false : true + instance_double(AutoAssignment::RateLimiter) + allow(AutoAssignment::RateLimiter).to receive(:new) do |args| + limiter = instance_double(AutoAssignment::RateLimiter) + allow(limiter).to receive(:within_limit?).and_return(args[:agent] != agent) + allow(limiter).to receive(:track_assignment) + limiter end unassigned_conversation = create(:conversation, inbox: inbox, assignee: nil) @@ -176,7 +180,7 @@ RSpec.describe AutoAssignment::AssignmentService do end # Move forward past the window - Timecop.freeze(Time.current + 2.hours) do + Timecop.freeze(2.hours.from_now) do new_convo = create(:conversation, inbox: inbox, assignee: nil) service.perform_bulk_assignment(limit: 1) expect(new_convo.reload.assignee).not_to be_nil @@ -197,7 +201,7 @@ RSpec.describe AutoAssignment::AssignmentService do context 'with round robin assignment' do it 'distributes conversations evenly among agents' do - conversations = 4.times.map { create(:conversation, inbox: inbox, assignee: nil) } + conversations = Array.new(4) { create(:conversation, inbox: inbox, assignee: nil) } service.perform_bulk_assignment(limit: 4) @@ -210,4 +214,4 @@ RSpec.describe AutoAssignment::AssignmentService do end end end -end \ No newline at end of file +end diff --git a/spec/services/auto_assignment/inbox_round_robin_service_spec.rb b/spec/services/auto_assignment/inbox_round_robin_service_spec.rb index 721f2bab8..abf6e42cc 100644 --- a/spec/services/auto_assignment/inbox_round_robin_service_spec.rb +++ b/spec/services/auto_assignment/inbox_round_robin_service_spec.rb @@ -1,4 +1,6 @@ -RSpec.describe AutoAssignment::InboxRoundRobinService do +require 'rails_helper' + +describe AutoAssignment::InboxRoundRobinService do subject(:inbox_round_robin_service) { described_class.new(inbox: inbox) } let!(:account) { create(:account) } diff --git a/spec/services/auto_assignment/round_robin_selector_spec.rb b/spec/services/auto_assignment/round_robin_selector_spec.rb index 94030753e..bf086abe8 100644 --- a/spec/services/auto_assignment/round_robin_selector_spec.rb +++ b/spec/services/auto_assignment/round_robin_selector_spec.rb @@ -7,20 +7,17 @@ RSpec.describe AutoAssignment::RoundRobinSelector do let(:agent1) { create(:user, account: account, role: :agent, availability: :online) } let(:agent2) { create(:user, account: account, role: :agent, availability: :online) } let(:agent3) { create(:user, account: account, role: :agent, availability: :online) } - - before do - @member1 = create(:inbox_member, inbox: inbox, user: agent1) - @member2 = create(:inbox_member, inbox: inbox, user: agent2) - @member3 = create(:inbox_member, inbox: inbox, user: agent3) - end + let(:member1) { create(:inbox_member, inbox: inbox, user: agent1) } + let(:member2) { create(:inbox_member, inbox: inbox, user: agent2) } + let(:member3) { create(:inbox_member, inbox: inbox, user: agent3) } describe '#select_agent' do context 'when agents are available' do - let(:available_agents) { [@member1, @member2, @member3] } + let(:available_agents) { [member1, member2, member3] } it 'returns an agent from the available list' do selected_agent = selector.select_agent(available_agents) - + expect(selected_agent).not_to be_nil expect([agent1, agent2, agent3]).to include(selected_agent) end @@ -28,11 +25,11 @@ RSpec.describe AutoAssignment::RoundRobinSelector do it 'uses round robin service for selection' do round_robin_service = instance_double(AutoAssignment::InboxRoundRobinService) allow(AutoAssignment::InboxRoundRobinService).to receive(:new).and_return(round_robin_service) - + expect(round_robin_service).to receive(:available_agent).with( allowed_agent_ids: [agent1.id.to_s, agent2.id.to_s, agent3.id.to_s] ).and_return(agent1) - + selected_agent = selector.select_agent(available_agents) expect(selected_agent).to eq(agent1) end @@ -47,9 +44,9 @@ RSpec.describe AutoAssignment::RoundRobinSelector do context 'when one agent is available' do it 'returns that agent' do - selected_agent = selector.select_agent([@member1]) + selected_agent = selector.select_agent([member1]) expect(selected_agent).to eq(agent1) end end end -end \ No newline at end of file +end