fix specs

This commit is contained in:
Tanmay Deep Sharma
2025-09-04 19:11:18 +07:00
parent d483974ef1
commit b015c61d71
3 changed files with 109 additions and 55 deletions
@@ -14,24 +14,27 @@ RSpec.describe AutoAssignment::AssignmentService do
describe '#perform_bulk_assignment' do
context 'when auto assignment is enabled' do
let(:rate_limiter) { instance_double(AutoAssignment::RateLimiter) }
before do
allow(OnlineStatusTracker).to receive(:get_available_users).and_return({ agent.id.to_s => 'online' })
# Mock RoundRobinSelector to return the agent
round_robin_selector = instance_double(AutoAssignment::RoundRobinSelector)
allow(AutoAssignment::RoundRobinSelector).to receive(:new).and_return(round_robin_selector)
allow(round_robin_selector).to receive(:select_agent).and_return(agent)
# Mock RateLimiter to allow all assignments by default
allow_any_instance_of(AutoAssignment::RateLimiter).to receive(:within_limit?).and_return(true)
allow_any_instance_of(AutoAssignment::RateLimiter).to receive(:track_assignment)
allow(AutoAssignment::RateLimiter).to receive(:new).and_return(rate_limiter)
allow(rate_limiter).to receive(:within_limit?).and_return(true)
allow(rate_limiter).to receive(:track_assignment)
end
it 'assigns conversations to available agents' do
# Create conversation and ensure it's unassigned
conv = create(:conversation, inbox: inbox, status: 'open')
conv.update!(assignee_id: nil)
assigned_count = service.perform_bulk_assignment(limit: 1)
expect(assigned_count).to eq(1)
@@ -88,7 +91,7 @@ RSpec.describe AutoAssignment::AssignmentService do
it 'dispatches assignee changed event' do
conversation # ensure it exists
conversation.update!(assignee_id: nil)
# The conversation model also dispatches a conversation.updated event
allow(Rails.configuration.dispatcher).to receive(:dispatch)
expect(Rails.configuration.dispatcher).to receive(:dispatch).with(
@@ -113,17 +116,20 @@ RSpec.describe AutoAssignment::AssignmentService do
end
context 'with conversation priority' do
let(:rate_limiter) { instance_double(AutoAssignment::RateLimiter) }
before do
allow(OnlineStatusTracker).to receive(:get_available_users).and_return({ agent.id.to_s => 'online' })
# Mock RoundRobinSelector to return the agent
round_robin_selector = instance_double(AutoAssignment::RoundRobinSelector)
allow(AutoAssignment::RoundRobinSelector).to receive(:new).and_return(round_robin_selector)
allow(round_robin_selector).to receive(:select_agent).and_return(agent)
# Mock RateLimiter to allow all assignments by default
allow_any_instance_of(AutoAssignment::RateLimiter).to receive(:within_limit?).and_return(true)
allow_any_instance_of(AutoAssignment::RateLimiter).to receive(:track_assignment)
allow(AutoAssignment::RateLimiter).to receive(:new).and_return(rate_limiter)
allow(rate_limiter).to receive(:within_limit?).and_return(true)
allow(rate_limiter).to receive(:track_assignment)
end
context 'when priority is longest_waiting' do
@@ -189,12 +195,17 @@ RSpec.describe AutoAssignment::AssignmentService do
round_robin_selector = instance_double(AutoAssignment::RoundRobinSelector)
allow(AutoAssignment::RoundRobinSelector).to receive(:new).and_return(round_robin_selector)
allow(round_robin_selector).to receive(:select_agent).and_return(agent2)
# Mock agent1 at limit, agent2 not at limit
allow_any_instance_of(AutoAssignment::RateLimiter).to receive(:within_limit?) do |limiter|
limiter.instance_variable_get(:@agent) != agent
end
allow_any_instance_of(AutoAssignment::RateLimiter).to receive(:track_assignment)
agent1_limiter = instance_double(AutoAssignment::RateLimiter)
agent2_limiter = instance_double(AutoAssignment::RateLimiter)
allow(AutoAssignment::RateLimiter).to receive(:new).with(inbox: inbox, agent: agent).and_return(agent1_limiter)
allow(AutoAssignment::RateLimiter).to receive(:new).with(inbox: inbox, agent: agent2).and_return(agent2_limiter)
allow(agent1_limiter).to receive(:within_limit?).and_return(false)
allow(agent2_limiter).to receive(:within_limit?).and_return(true)
allow(agent2_limiter).to receive(:track_assignment)
unassigned_conversation = create(:conversation, inbox: inbox, status: 'open')
unassigned_conversation.update!(assignee_id: nil)
@@ -207,14 +218,16 @@ RSpec.describe AutoAssignment::AssignmentService do
it 'tracks assignments in Redis' do
conversation # ensure it exists
conversation.update!(assignee_id: nil)
# Mock RoundRobinSelector
round_robin_selector = instance_double(AutoAssignment::RoundRobinSelector)
allow(AutoAssignment::RoundRobinSelector).to receive(:new).and_return(round_robin_selector)
allow(round_robin_selector).to receive(:select_agent).and_return(agent)
allow_any_instance_of(AutoAssignment::RateLimiter).to receive(:within_limit?).and_return(true)
expect_any_instance_of(AutoAssignment::RateLimiter).to receive(:track_assignment)
limiter = instance_double(AutoAssignment::RateLimiter)
allow(AutoAssignment::RateLimiter).to receive(:new).and_return(limiter)
allow(limiter).to receive(:within_limit?).and_return(true)
expect(limiter).to receive(:track_assignment)
service.perform_bulk_assignment(limit: 1)
end
@@ -224,13 +237,15 @@ RSpec.describe AutoAssignment::AssignmentService do
round_robin_selector = instance_double(AutoAssignment::RoundRobinSelector)
allow(AutoAssignment::RoundRobinSelector).to receive(:new).and_return(round_robin_selector)
allow(round_robin_selector).to receive(:select_agent).and_return(agent, agent2)
# Mock RateLimiter to allow all
allow_any_instance_of(AutoAssignment::RateLimiter).to receive(:within_limit?).and_return(true)
allow_any_instance_of(AutoAssignment::RateLimiter).to receive(:track_assignment)
limiter = instance_double(AutoAssignment::RateLimiter)
allow(AutoAssignment::RateLimiter).to receive(:new).and_return(limiter)
allow(limiter).to receive(:within_limit?).and_return(true)
allow(limiter).to receive(:track_assignment)
# Simulate time passing for rate limit window
travel_to(Time.current) do
freeze_time do
2.times do
conversation_new = create(:conversation, inbox: inbox, status: 'open')
conversation_new.update!(assignee_id: nil)
@@ -255,15 +270,17 @@ RSpec.describe AutoAssignment::AssignmentService do
conv = create(:conversation, inbox: inbox, status: 'open')
conv.update!(assignee_id: nil)
end
# Mock RoundRobinSelector
round_robin_selector = instance_double(AutoAssignment::RoundRobinSelector)
allow(AutoAssignment::RoundRobinSelector).to receive(:new).and_return(round_robin_selector)
allow(round_robin_selector).to receive(:select_agent).and_return(agent)
# Mock RateLimiter to allow all
allow_any_instance_of(AutoAssignment::RateLimiter).to receive(:within_limit?).and_return(true)
allow_any_instance_of(AutoAssignment::RateLimiter).to receive(:track_assignment)
limiter = instance_double(AutoAssignment::RateLimiter)
allow(AutoAssignment::RateLimiter).to receive(:new).and_return(limiter)
allow(limiter).to receive(:within_limit?).and_return(true)
allow(limiter).to receive(:track_assignment)
assigned_count = service.perform_bulk_assignment(limit: 5)
expect(assigned_count).to eq(5)
@@ -7,16 +7,33 @@ 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) }
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) }
let(:round_robin_service) { instance_double(AutoAssignment::InboxRoundRobinService) }
let(:member1) do
allow(AutoAssignment::InboxRoundRobinService).to receive(:new).and_return(round_robin_service)
allow(round_robin_service).to receive(:add_agent_to_queue)
create(:inbox_member, inbox: inbox, user: agent1)
end
let(:member2) do
allow(AutoAssignment::InboxRoundRobinService).to receive(:new).and_return(round_robin_service)
allow(round_robin_service).to receive(:add_agent_to_queue)
create(:inbox_member, inbox: inbox, user: agent2)
end
let(:member3) do
allow(AutoAssignment::InboxRoundRobinService).to receive(:new).and_return(round_robin_service)
allow(round_robin_service).to receive(:add_agent_to_queue)
create(:inbox_member, inbox: inbox, user: agent3)
end
before do
# Mock the round robin service to avoid Redis calls
allow_any_instance_of(AutoAssignment::InboxRoundRobinService).to receive(:add_agent_to_queue)
allow_any_instance_of(AutoAssignment::InboxRoundRobinService).to receive(:reset_queue)
allow_any_instance_of(AutoAssignment::InboxRoundRobinService).to receive(:validate_queue?).and_return(true)
allow_any_instance_of(AutoAssignment::InboxRoundRobinService).to receive(:available_agent).and_return(nil)
allow(AutoAssignment::InboxRoundRobinService).to receive(:new).and_return(round_robin_service)
allow(round_robin_service).to receive(:add_agent_to_queue)
allow(round_robin_service).to receive(:reset_queue)
allow(round_robin_service).to receive(:validate_queue?).and_return(true)
end
describe '#select_agent' do
@@ -24,12 +41,8 @@ RSpec.describe AutoAssignment::RoundRobinSelector do
let(:available_agents) { [member1, member2, member3] }
it 'returns an agent from the available list' do
# Mock the round robin service to return an agent
round_robin_service = instance_double(AutoAssignment::InboxRoundRobinService)
allow(round_robin_service).to receive(:add_agent_to_queue)
allow(AutoAssignment::InboxRoundRobinService).to receive(:new).and_return(round_robin_service)
allow(round_robin_service).to receive(:available_agent).and_return(agent1)
selected_agent = selector.select_agent(available_agents)
expect(selected_agent).not_to be_nil
@@ -37,10 +50,6 @@ RSpec.describe AutoAssignment::RoundRobinSelector do
end
it 'uses round robin service for selection' do
round_robin_service = instance_double(AutoAssignment::InboxRoundRobinService)
allow(round_robin_service).to receive(:add_agent_to_queue)
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)
@@ -59,12 +68,8 @@ RSpec.describe AutoAssignment::RoundRobinSelector do
context 'when one agent is available' do
it 'returns that agent' do
# Mock the round robin service to return the agent
round_robin_service = instance_double(AutoAssignment::InboxRoundRobinService)
allow(round_robin_service).to receive(:add_agent_to_queue)
allow(AutoAssignment::InboxRoundRobinService).to receive(:new).and_return(round_robin_service)
allow(round_robin_service).to receive(:available_agent).and_return(agent1)
selected_agent = selector.select_agent([member1])
expect(selected_agent).to eq(agent1)
end