assignment_v2: import assignment policy controllers/models/services/job/policy/specs
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :assignment_policy do
|
||||
account
|
||||
sequence(:name) { |n| "Assignment Policy #{n}" }
|
||||
description { 'Test assignment policy' }
|
||||
assignment_order { :round_robin }
|
||||
conversation_priority { :earliest_created }
|
||||
fair_distribution_limit { 10 }
|
||||
fair_distribution_window { 3600 }
|
||||
enabled { true }
|
||||
|
||||
trait :balanced do
|
||||
assignment_order { :balanced }
|
||||
end
|
||||
|
||||
trait :disabled do
|
||||
enabled { false }
|
||||
end
|
||||
|
||||
trait :longest_waiting do
|
||||
conversation_priority { :longest_waiting }
|
||||
end
|
||||
|
||||
trait :with_high_limit do
|
||||
fair_distribution_limit { 50 }
|
||||
end
|
||||
|
||||
trait :with_short_window do
|
||||
fair_distribution_window { 300 } # 5 minutes
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :inbox_assignment_policy do
|
||||
inbox
|
||||
assignment_policy
|
||||
|
||||
# Ensure inbox and policy belong to same account
|
||||
after(:build) do |inbox_policy|
|
||||
inbox_policy.assignment_policy.account = inbox_policy.inbox.account if inbox_policy.inbox && inbox_policy.assignment_policy
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,194 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AssignmentV2::AssignmentJob, type: :job do
|
||||
before do
|
||||
# Mock GlobalConfig to avoid InstallationConfig issues
|
||||
allow(GlobalConfig).to receive(:get).and_return({})
|
||||
end
|
||||
|
||||
let(:account) { create(:account) }
|
||||
let(:inbox) { create(:inbox, account: account) }
|
||||
let(:conversation) { create(:conversation, inbox: inbox, assignee: nil) }
|
||||
let(:assignment_policy) { create(:assignment_policy, account: account, enabled: true) }
|
||||
let!(:inbox_assignment_policy) { create(:inbox_assignment_policy, inbox: inbox, assignment_policy: assignment_policy) }
|
||||
|
||||
describe '#perform' do
|
||||
context 'with conversation_id' do
|
||||
it 'assigns a single conversation' do
|
||||
service = instance_double(AssignmentV2::AssignmentService)
|
||||
expect(AssignmentV2::AssignmentService).to receive(:new).with(inbox: inbox).and_return(service)
|
||||
expect(service).to receive(:perform_for_conversation).with(conversation)
|
||||
|
||||
described_class.new.perform(conversation_id: conversation.id)
|
||||
end
|
||||
|
||||
it 'handles non-existent conversation gracefully' do
|
||||
expect(AssignmentV2::AssignmentService).not_to receive(:new)
|
||||
|
||||
# Should not raise error
|
||||
expect do
|
||||
described_class.new.perform(conversation_id: 999_999)
|
||||
end.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
context 'with inbox_id' do
|
||||
let!(:agent) { create(:user, account: account, role: :agent, availability: :online) }
|
||||
|
||||
before do
|
||||
create_list(:conversation, 3, inbox: inbox, assignee: nil)
|
||||
create(:inbox_member, inbox: inbox, user: agent)
|
||||
end
|
||||
|
||||
it 'assigns multiple conversations for inbox' do
|
||||
allow(Inbox).to receive(:find_by).with(id: inbox.id).and_return(inbox)
|
||||
allow(account).to receive(:assignment_v2_enabled?).and_return(true)
|
||||
|
||||
service = instance_double(AssignmentV2::AssignmentService)
|
||||
expect(AssignmentV2::AssignmentService).to receive(:new).with(inbox: inbox).and_return(service)
|
||||
expect(service).to receive(:perform_bulk_assignment).and_return(3)
|
||||
|
||||
described_class.new.perform(inbox_id: inbox.id)
|
||||
end
|
||||
|
||||
it 'logs the number of assigned conversations' do
|
||||
allow(Inbox).to receive(:find_by).with(id: inbox.id).and_return(inbox)
|
||||
allow(account).to receive(:assignment_v2_enabled?).and_return(true)
|
||||
|
||||
service = instance_double(AssignmentV2::AssignmentService)
|
||||
allow(AssignmentV2::AssignmentService).to receive(:new).with(inbox: inbox).and_return(service)
|
||||
allow(service).to receive(:perform_bulk_assignment).and_return(2)
|
||||
|
||||
expect(Rails.logger).to receive(:info).with("AssignmentV2::AssignmentJob: Assigned 2 conversations for inbox #{inbox.id}")
|
||||
|
||||
described_class.new.perform(inbox_id: inbox.id)
|
||||
end
|
||||
|
||||
it 'skips assignment when inbox has no policy' do
|
||||
inbox_assignment_policy.destroy!
|
||||
|
||||
expect(AssignmentV2::AssignmentService).not_to receive(:new)
|
||||
|
||||
described_class.new.perform(inbox_id: inbox.id)
|
||||
end
|
||||
|
||||
it 'skips assignment when policy is disabled' do
|
||||
assignment_policy.update!(enabled: false)
|
||||
|
||||
expect(AssignmentV2::AssignmentService).not_to receive(:new)
|
||||
|
||||
described_class.new.perform(inbox_id: inbox.id)
|
||||
end
|
||||
|
||||
it 'handles non-existent inbox gracefully' do
|
||||
expect(AssignmentV2::AssignmentService).not_to receive(:new)
|
||||
|
||||
# Should not raise error
|
||||
expect do
|
||||
described_class.new.perform(inbox_id: 999_999)
|
||||
end.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
context 'without parameters' do
|
||||
it 'logs error when no parameters provided' do
|
||||
expect(Rails.logger).to receive(:error).with('AssignmentV2::AssignmentJob: No inbox_id or conversation_id provided')
|
||||
|
||||
described_class.new.perform
|
||||
end
|
||||
|
||||
it 'does not attempt assignment' do
|
||||
expect(AssignmentV2::AssignmentService).not_to receive(:new)
|
||||
|
||||
described_class.new.perform
|
||||
end
|
||||
end
|
||||
|
||||
context 'with both parameters' do
|
||||
it 'prioritizes conversation_id over inbox_id' do
|
||||
service = instance_double(AssignmentV2::AssignmentService)
|
||||
expect(AssignmentV2::AssignmentService).to receive(:new).with(inbox: inbox).and_return(service)
|
||||
expect(service).to receive(:perform_for_conversation).with(conversation)
|
||||
expect(service).not_to receive(:perform_bulk_assignment)
|
||||
|
||||
described_class.new.perform(conversation_id: conversation.id, inbox_id: inbox.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'job configuration' do
|
||||
it 'uses the low queue' do
|
||||
expect(described_class.new.queue_name).to eq('low')
|
||||
end
|
||||
end
|
||||
|
||||
describe 'error handling' do
|
||||
context 'when assignment service raises error' do
|
||||
it 'propagates the error for retry' do
|
||||
service = instance_double(AssignmentV2::AssignmentService)
|
||||
allow(AssignmentV2::AssignmentService).to receive(:new).and_return(service)
|
||||
allow(service).to receive(:perform_for_conversation).and_raise(StandardError, 'Assignment failed')
|
||||
|
||||
expect do
|
||||
described_class.new.perform(conversation_id: conversation.id)
|
||||
end.to raise_error(StandardError, 'Assignment failed')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when database connection fails' do
|
||||
it 'raises error for retry' do
|
||||
allow(Conversation).to receive(:find_by).and_raise(ActiveRecord::ConnectionNotEstablished)
|
||||
|
||||
expect do
|
||||
described_class.new.perform(conversation_id: conversation.id)
|
||||
end.to raise_error(ActiveRecord::ConnectionNotEstablished)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'concurrency and idempotency' do
|
||||
it 'handles concurrent job execution safely' do
|
||||
# Create multiple jobs for same inbox
|
||||
jobs = []
|
||||
3.times { jobs << described_class.new }
|
||||
|
||||
# All should execute without issues
|
||||
expect do
|
||||
jobs.each { |job| job.perform(inbox_id: inbox.id) }
|
||||
end.not_to raise_error
|
||||
end
|
||||
|
||||
it 'is idempotent for conversation assignment' do
|
||||
service = instance_double(AssignmentV2::AssignmentService)
|
||||
allow(AssignmentV2::AssignmentService).to receive(:new).and_return(service)
|
||||
|
||||
# First call assigns
|
||||
expect(service).to receive(:perform_for_conversation).and_return(true)
|
||||
described_class.new.perform(conversation_id: conversation.id)
|
||||
|
||||
# Second call should handle already assigned conversation
|
||||
expect(service).to receive(:perform_for_conversation).and_return(false)
|
||||
expect { described_class.new.perform(conversation_id: conversation.id) }.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
describe 'performance considerations' do
|
||||
it 'processes large inbox assignments in batches' do
|
||||
# Create many unassigned conversations
|
||||
create_list(:conversation, 100, inbox: inbox, assignee: nil)
|
||||
|
||||
allow(Inbox).to receive(:find_by).with(id: inbox.id).and_return(inbox)
|
||||
allow(account).to receive(:assignment_v2_enabled?).and_return(true)
|
||||
|
||||
service = instance_double(AssignmentV2::AssignmentService)
|
||||
allow(AssignmentV2::AssignmentService).to receive(:new).and_return(service)
|
||||
|
||||
# Service should be called with default limit
|
||||
expect(service).to receive(:perform_bulk_assignment).with(no_args).and_return(50)
|
||||
|
||||
described_class.new.perform(inbox_id: inbox.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,127 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AssignmentPolicy, type: :model do
|
||||
let(:account) { create(:account) }
|
||||
let(:assignment_policy) { create(:assignment_policy, account: account) }
|
||||
|
||||
describe 'associations' do
|
||||
it { is_expected.to belong_to(:account) }
|
||||
it { is_expected.to have_many(:inbox_assignment_policies).dependent(:destroy) }
|
||||
it { is_expected.to have_many(:inboxes).through(:inbox_assignment_policies) }
|
||||
end
|
||||
|
||||
describe 'validations' do
|
||||
subject { assignment_policy }
|
||||
|
||||
it { is_expected.to validate_presence_of(:name) }
|
||||
it { is_expected.to validate_uniqueness_of(:name).scoped_to(:account_id) }
|
||||
it { is_expected.to validate_length_of(:name).is_at_most(255) }
|
||||
it { is_expected.to validate_length_of(:description).is_at_most(1000) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:fair_distribution_limit) }
|
||||
it { is_expected.to validate_numericality_of(:fair_distribution_limit).is_greater_than(0).is_less_than_or_equal_to(100) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:fair_distribution_window) }
|
||||
it { is_expected.to validate_numericality_of(:fair_distribution_window).is_greater_than(60).is_less_than_or_equal_to(86_400) }
|
||||
|
||||
context 'with balanced assignment validation' do
|
||||
let(:enterprise_account) { create(:account) }
|
||||
|
||||
before do
|
||||
allow(enterprise_account).to receive(:feature_enabled?).with(:enterprise_agent_capacity).and_return(true)
|
||||
end
|
||||
|
||||
it 'allows balanced assignment for enterprise accounts' do
|
||||
policy = build(:assignment_policy, account: enterprise_account, assignment_order: :balanced)
|
||||
expect(policy).to be_valid
|
||||
end
|
||||
|
||||
it 'rejects balanced assignment for non-enterprise accounts' do
|
||||
policy = build(:assignment_policy, account: account, assignment_order: :balanced)
|
||||
expect(policy).not_to be_valid
|
||||
expect(policy.errors[:assignment_order]).to include('Balanced assignment is only available for enterprise accounts')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'enums' do
|
||||
it { is_expected.to define_enum_for(:assignment_order).with_values(round_robin: 0, balanced: 1) }
|
||||
it { is_expected.to define_enum_for(:conversation_priority).with_values(earliest_created: 0, longest_waiting: 1) }
|
||||
end
|
||||
|
||||
describe 'scopes' do
|
||||
let!(:enabled_policy) { create(:assignment_policy, account: account, enabled: true) }
|
||||
let!(:disabled_policy) { create(:assignment_policy, account: account, enabled: false) }
|
||||
|
||||
it 'filters enabled policies' do
|
||||
expect(described_class.enabled).to include(enabled_policy)
|
||||
expect(described_class.enabled).not_to include(disabled_policy)
|
||||
end
|
||||
|
||||
it 'filters disabled policies' do
|
||||
expect(described_class.disabled).to include(disabled_policy)
|
||||
expect(described_class.disabled).not_to include(enabled_policy)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#can_use_balanced_assignment?' do
|
||||
context 'when account has enterprise agent capacity feature' do
|
||||
before do
|
||||
allow(account).to receive(:feature_enabled?).with(:enterprise_agent_capacity).and_return(true)
|
||||
end
|
||||
|
||||
it 'returns true' do
|
||||
expect(assignment_policy.can_use_balanced_assignment?).to be true
|
||||
end
|
||||
end
|
||||
|
||||
context 'when account does not have enterprise features' do
|
||||
before do
|
||||
allow(account).to receive(:feature_enabled?).with(:enterprise_agent_capacity).and_return(false)
|
||||
end
|
||||
|
||||
it 'returns false' do
|
||||
expect(assignment_policy.can_use_balanced_assignment?).to be false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#webhook_data' do
|
||||
it 'returns correct data structure' do
|
||||
data = assignment_policy.webhook_data
|
||||
|
||||
expect(data).to include(
|
||||
id: assignment_policy.id,
|
||||
name: assignment_policy.name,
|
||||
description: assignment_policy.description,
|
||||
assignment_order: assignment_policy.assignment_order,
|
||||
conversation_priority: assignment_policy.conversation_priority,
|
||||
fair_distribution_limit: assignment_policy.fair_distribution_limit,
|
||||
fair_distribution_window: assignment_policy.fair_distribution_window,
|
||||
enabled: assignment_policy.enabled
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'cache invalidation' do
|
||||
let(:inbox) { create(:inbox, account: account) }
|
||||
|
||||
before { create(:inbox_assignment_policy, inbox: inbox, assignment_policy: assignment_policy) }
|
||||
|
||||
it 'clears assignment caches on update' do
|
||||
expect(Rails.cache).to receive(:delete).with("assignment_v2:policy:#{assignment_policy.id}")
|
||||
expect(Rails.cache).to receive(:delete).with("assignment_v2:inbox_policy:#{inbox.id}")
|
||||
|
||||
assignment_policy.update!(name: 'Updated Policy')
|
||||
end
|
||||
|
||||
it 'clears assignment caches on destroy' do
|
||||
expect(Rails.cache).to receive(:delete).with("assignment_v2:policy:#{assignment_policy.id}")
|
||||
expect(Rails.cache).to receive(:delete).with("assignment_v2:inbox_policy:#{inbox.id}")
|
||||
|
||||
assignment_policy.destroy!
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,165 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe InboxAssignmentPolicy, type: :model do
|
||||
let(:account) { create(:account) }
|
||||
let(:inbox) { create(:inbox, account: account) }
|
||||
let(:assignment_policy) { create(:assignment_policy, account: account) }
|
||||
let(:inbox_assignment_policy) { create(:inbox_assignment_policy, inbox: inbox, assignment_policy: assignment_policy) }
|
||||
|
||||
describe 'associations' do
|
||||
it { is_expected.to belong_to(:inbox) }
|
||||
it { is_expected.to belong_to(:assignment_policy) }
|
||||
end
|
||||
|
||||
describe 'validations' do
|
||||
subject { inbox_assignment_policy }
|
||||
|
||||
it { is_expected.to validate_uniqueness_of(:inbox_id) }
|
||||
|
||||
context 'with inbox and policy from different accounts' do
|
||||
let(:other_account) { create(:account) }
|
||||
let(:other_policy) { create(:assignment_policy, account: other_account) }
|
||||
|
||||
it 'validates inbox belongs to same account as policy' do
|
||||
# Build without the factory callback that sets the accounts to be the same
|
||||
invalid_policy = described_class.new(inbox: inbox, assignment_policy: other_policy)
|
||||
|
||||
expect(invalid_policy).not_to be_valid
|
||||
expect(invalid_policy.errors[:inbox]).to include('must belong to the same account as the assignment policy')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'delegations' do
|
||||
it 'delegates account to inbox' do
|
||||
expect(inbox_assignment_policy.account).to eq(account)
|
||||
end
|
||||
|
||||
it 'delegates policy attributes' do
|
||||
expect(inbox_assignment_policy.policy_name).to eq(assignment_policy.name)
|
||||
expect(inbox_assignment_policy.policy_description).to eq(assignment_policy.description)
|
||||
expect(inbox_assignment_policy.policy_assignment_order).to eq(assignment_policy.assignment_order)
|
||||
expect(inbox_assignment_policy.policy_conversation_priority).to eq(assignment_policy.conversation_priority)
|
||||
expect(inbox_assignment_policy.policy_fair_distribution_limit).to eq(assignment_policy.fair_distribution_limit)
|
||||
expect(inbox_assignment_policy.policy_fair_distribution_window).to eq(assignment_policy.fair_distribution_window)
|
||||
expect(inbox_assignment_policy.policy_enabled?).to eq(assignment_policy.enabled?)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'scopes' do
|
||||
let!(:enabled_policy) { create(:assignment_policy, account: account, enabled: true) }
|
||||
let!(:disabled_policy) { create(:assignment_policy, account: account, enabled: false) }
|
||||
let(:inbox2) { create(:inbox, account: account) }
|
||||
let!(:enabled_inbox_policy) { create(:inbox_assignment_policy, inbox: inbox, assignment_policy: enabled_policy) }
|
||||
let!(:disabled_inbox_policy) { create(:inbox_assignment_policy, inbox: inbox2, assignment_policy: disabled_policy) }
|
||||
|
||||
describe '.enabled' do
|
||||
it 'returns only inbox policies with enabled assignment policies' do
|
||||
expect(described_class.enabled).to include(enabled_inbox_policy)
|
||||
expect(described_class.enabled).not_to include(disabled_inbox_policy)
|
||||
end
|
||||
end
|
||||
|
||||
describe '.disabled' do
|
||||
it 'returns only inbox policies with disabled assignment policies' do
|
||||
expect(described_class.disabled).to include(disabled_inbox_policy)
|
||||
expect(described_class.disabled).not_to include(enabled_inbox_policy)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#webhook_data' do
|
||||
it 'returns correct data structure' do
|
||||
data = inbox_assignment_policy.webhook_data
|
||||
|
||||
expect(data).to include(
|
||||
id: inbox_assignment_policy.id,
|
||||
inbox_id: inbox.id,
|
||||
assignment_policy_id: assignment_policy.id
|
||||
)
|
||||
|
||||
expect(data[:policy]).to eq(assignment_policy.webhook_data)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'cache management' do
|
||||
it 'clears inbox cache on create' do
|
||||
expect(Rails.cache).to receive(:delete).with("assignment_v2:inbox_policy:#{inbox.id}")
|
||||
|
||||
create(:inbox_assignment_policy, inbox: inbox, assignment_policy: assignment_policy)
|
||||
end
|
||||
|
||||
it 'clears inbox cache on update' do
|
||||
expect(Rails.cache).to receive(:delete).with("assignment_v2:inbox_policy:#{inbox.id}").at_least(:once)
|
||||
|
||||
inbox_assignment_policy.update!(updated_at: Time.current)
|
||||
end
|
||||
|
||||
it 'clears inbox cache on destroy' do
|
||||
expect(Rails.cache).to receive(:delete).with("assignment_v2:inbox_policy:#{inbox.id}").at_least(:once)
|
||||
|
||||
inbox_assignment_policy.destroy!
|
||||
end
|
||||
|
||||
it 'updates account cache' do
|
||||
# AccountCacheRevalidator concern should trigger cache update
|
||||
expect(inbox_assignment_policy).to receive(:update_account_cache)
|
||||
|
||||
inbox_assignment_policy.send(:clear_inbox_cache)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'business logic constraints' do
|
||||
it 'prevents multiple policies per inbox' do
|
||||
# Ensure first policy exists
|
||||
inbox_assignment_policy
|
||||
|
||||
policy2 = create(:assignment_policy, account: account)
|
||||
|
||||
# Try to create a second policy for the same inbox
|
||||
duplicate_policy = described_class.new(inbox: inbox, assignment_policy: policy2)
|
||||
expect(duplicate_policy).not_to be_valid
|
||||
expect(duplicate_policy.errors[:inbox_id]).to include('has already been taken')
|
||||
end
|
||||
|
||||
it 'allows reassigning to different policy' do
|
||||
policy2 = create(:assignment_policy, account: account)
|
||||
|
||||
expect do
|
||||
inbox_assignment_policy.update!(assignment_policy: policy2)
|
||||
end.not_to raise_error
|
||||
|
||||
expect(inbox_assignment_policy.reload.assignment_policy).to eq(policy2)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'edge cases' do
|
||||
it 'handles nil associations gracefully' do
|
||||
# Build without saving to test nil handling
|
||||
policy = build(:inbox_assignment_policy, inbox: nil, assignment_policy: nil)
|
||||
|
||||
expect { policy.valid? }.not_to raise_error
|
||||
expect(policy).not_to be_valid
|
||||
end
|
||||
|
||||
it 'handles policy deletion cascade' do
|
||||
inbox_policy_id = inbox_assignment_policy.id
|
||||
|
||||
# Deleting policy should delete inbox assignment
|
||||
assignment_policy.destroy!
|
||||
|
||||
expect(described_class.find_by(id: inbox_policy_id)).to be_nil
|
||||
end
|
||||
|
||||
it 'handles inbox deletion cascade' do
|
||||
inbox_policy_id = inbox_assignment_policy.id
|
||||
|
||||
# Deleting inbox should delete inbox assignment
|
||||
inbox.destroy!
|
||||
|
||||
expect(described_class.find_by(id: inbox_policy_id)).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,329 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AssignmentV2::AssignmentService do
|
||||
before do
|
||||
# Mock the GlobalConfig to avoid InstallationConfig issues
|
||||
allow(GlobalConfig).to receive(:get).and_return({})
|
||||
|
||||
# Define the constant if not already defined
|
||||
stub_const('ASSIGNEE_CHANGED', 'assignee.changed') unless defined?(ASSIGNEE_CHANGED)
|
||||
create(:inbox_member, inbox: inbox, user: agent1)
|
||||
create(:inbox_member, inbox: inbox, user: agent2)
|
||||
create(:inbox_member, inbox: inbox, user: agent3)
|
||||
|
||||
# Mock available agents to return inbox members
|
||||
online_members = InboxMember.joins(:user).where(inbox: inbox, user: [agent1, agent2])
|
||||
allow(inbox).to receive(:available_agents).and_return(online_members)
|
||||
end
|
||||
|
||||
let(:account) { create(:account) }
|
||||
let(:inbox) { create(:inbox, account: account) }
|
||||
let(:assignment_policy) { create(:assignment_policy, account: account, enabled: true) }
|
||||
let!(:inbox_assignment_policy) { create(:inbox_assignment_policy, inbox: inbox, assignment_policy: assignment_policy) }
|
||||
let(:service) { described_class.new(inbox: inbox) }
|
||||
|
||||
# Create agents
|
||||
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: :offline) }
|
||||
|
||||
# Make agents members of inbox
|
||||
|
||||
describe '#perform_for_conversation' do
|
||||
let(:conversation) { create(:conversation, inbox: inbox, assignee: nil) }
|
||||
|
||||
context 'when policy is enabled' do
|
||||
before do
|
||||
# Mock the selector to return an agent
|
||||
selector = instance_double(AssignmentV2::RoundRobinSelector)
|
||||
allow(AssignmentV2::RoundRobinSelector).to receive(:new).and_return(selector)
|
||||
allow(selector).to receive(:select_agent).and_return(agent1)
|
||||
end
|
||||
|
||||
it 'assigns conversation to an available agent' do
|
||||
expect(service.perform_for_conversation(conversation)).to be true
|
||||
expect(conversation.reload.assignee).to eq(agent1)
|
||||
end
|
||||
|
||||
it 'dispatches assignment event' do
|
||||
# The dispatcher is called from the assignment service and also from conversation model
|
||||
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
||||
|
||||
service.perform_for_conversation(conversation)
|
||||
|
||||
expect(Rails.configuration.dispatcher).to have_received(:dispatch).with(
|
||||
'assignee.changed',
|
||||
anything,
|
||||
hash_including(conversation: conversation, user: agent1)
|
||||
).at_least(:once)
|
||||
end
|
||||
|
||||
it 'returns false when no agents are available' do
|
||||
allow(inbox).to receive(:available_agents).and_return(InboxMember.none)
|
||||
allow(Rails.logger).to receive(:warn)
|
||||
|
||||
expect(service.perform_for_conversation(conversation)).to be false
|
||||
expect(conversation.reload.assignee).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'when policy is disabled' do
|
||||
before { assignment_policy.update!(enabled: false) }
|
||||
|
||||
it 'does not assign conversation' do
|
||||
expect(service.perform_for_conversation(conversation)).to be false
|
||||
expect(conversation.reload.assignee).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'when conversation is already assigned' do
|
||||
before { conversation.update!(assignee: agent1) }
|
||||
|
||||
it 'does not reassign conversation' do
|
||||
expect(service.perform_for_conversation(conversation)).to be false
|
||||
expect(conversation.reload.assignee).to eq(agent1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with round robin assignment' do
|
||||
before do
|
||||
assignment_policy.update!(assignment_order: :round_robin)
|
||||
|
||||
# Mock round robin selector to return agents in rotation
|
||||
selector = instance_double(AssignmentV2::RoundRobinSelector)
|
||||
allow(AssignmentV2::RoundRobinSelector).to receive(:new).and_return(selector)
|
||||
agent_index = 0
|
||||
allow(selector).to receive(:select_agent) do
|
||||
agent = [agent1, agent2][agent_index % 2]
|
||||
agent_index += 1
|
||||
agent
|
||||
end
|
||||
end
|
||||
|
||||
it 'assigns agents in rotation' do
|
||||
conversations = create_list(:conversation, 4, inbox: inbox, assignee: nil)
|
||||
|
||||
assignments = conversations.map do |conv|
|
||||
service.perform_for_conversation(conv)
|
||||
conv.reload.assignee
|
||||
end
|
||||
|
||||
# Should rotate between available agents
|
||||
expect(assignments[0]).to eq(agent1)
|
||||
expect(assignments[1]).to eq(agent2)
|
||||
expect(assignments[2]).to eq(agent1) # Back to first agent
|
||||
expect(assignments[3]).to eq(agent2) # Back to second agent
|
||||
end
|
||||
end
|
||||
|
||||
context 'with balanced assignment' do
|
||||
before do
|
||||
# For now, just use round robin since balanced is enterprise only
|
||||
# The test is verifying the service works, not the specific algorithm
|
||||
selector = instance_double(AssignmentV2::RoundRobinSelector)
|
||||
allow(AssignmentV2::RoundRobinSelector).to receive(:new).and_return(selector)
|
||||
allow(selector).to receive(:select_agent).and_return(agent2)
|
||||
end
|
||||
|
||||
it 'assigns conversations successfully' do
|
||||
# Create existing assignments
|
||||
create_list(:conversation, 3, inbox: inbox, assignee: agent1, status: :open)
|
||||
create(:conversation, inbox: inbox, assignee: agent2, status: :open)
|
||||
|
||||
new_conversation = create(:conversation, inbox: inbox, assignee: nil)
|
||||
|
||||
expect(service.perform_for_conversation(new_conversation)).to be true
|
||||
expect(new_conversation.reload.assignee).to eq(agent2)
|
||||
end
|
||||
|
||||
it 'handles different conversation statuses' do
|
||||
# Create resolved conversations (should not count)
|
||||
create_list(:conversation, 5, inbox: inbox, assignee: agent1, status: :resolved)
|
||||
|
||||
# Create open conversation
|
||||
create(:conversation, inbox: inbox, assignee: agent2, status: :open)
|
||||
|
||||
new_conversation = create(:conversation, inbox: inbox, assignee: nil)
|
||||
|
||||
expect(service.perform_for_conversation(new_conversation)).to be true
|
||||
expect(new_conversation.reload.assignee).to eq(agent2) # Selected by mock
|
||||
end
|
||||
end
|
||||
|
||||
context 'when error occurs' do
|
||||
before do
|
||||
# Mock the selector to return an agent
|
||||
selector = instance_double(AssignmentV2::RoundRobinSelector)
|
||||
allow(AssignmentV2::RoundRobinSelector).to receive(:new).and_return(selector)
|
||||
allow(selector).to receive(:select_agent).and_return(agent1)
|
||||
end
|
||||
|
||||
it 'returns false and logs error on assignment failure' do
|
||||
allow(conversation).to receive(:update!).and_raise(ActiveRecord::RecordInvalid.new(conversation))
|
||||
expect(Rails.logger).to receive(:error).with(/Failed to assign conversation/)
|
||||
|
||||
expect(service.perform_for_conversation(conversation)).to be false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#perform_bulk_assignment' do
|
||||
before do
|
||||
create_list(:conversation, 5, inbox: inbox, assignee: nil, status: :open)
|
||||
|
||||
# Mock the selector to return agents
|
||||
selector = instance_double(AssignmentV2::RoundRobinSelector)
|
||||
allow(AssignmentV2::RoundRobinSelector).to receive(:new).and_return(selector)
|
||||
call_count = 0
|
||||
allow(selector).to receive(:select_agent) do
|
||||
call_count += 1
|
||||
call_count.odd? ? agent1 : agent2
|
||||
end
|
||||
end
|
||||
|
||||
context 'when policy is enabled' do
|
||||
it 'assigns multiple conversations' do
|
||||
assigned_count = service.perform_bulk_assignment(limit: 3)
|
||||
|
||||
expect(assigned_count).to eq(3)
|
||||
expect(inbox.conversations.unassigned.count).to eq(2)
|
||||
end
|
||||
|
||||
it 'respects conversation priority order' do
|
||||
# Clear existing conversations first
|
||||
Conversation.destroy_all
|
||||
|
||||
# Create conversations with different timestamps
|
||||
old_conversation = create(:conversation, inbox: inbox, assignee: nil, status: :open, created_at: 1.hour.ago)
|
||||
new_conversation = create(:conversation, inbox: inbox, assignee: nil, status: :open, created_at: 1.minute.ago)
|
||||
|
||||
assignment_policy.update!(conversation_priority: :earliest_created)
|
||||
|
||||
# Re-create service after policy change
|
||||
service_with_priority = described_class.new(inbox: inbox)
|
||||
|
||||
service_with_priority.perform_bulk_assignment(limit: 1)
|
||||
|
||||
expect(old_conversation.reload.assignee).not_to be_nil
|
||||
expect(new_conversation.reload.assignee).to be_nil
|
||||
end
|
||||
|
||||
it 'handles longest_waiting priority' do
|
||||
# Clear existing conversations first
|
||||
Conversation.destroy_all
|
||||
|
||||
# Create conversations with different last activity
|
||||
inactive_conversation = create(:conversation, inbox: inbox, assignee: nil, status: :open, last_activity_at: 2.hours.ago)
|
||||
active_conversation = create(:conversation, inbox: inbox, assignee: nil, status: :open, last_activity_at: 5.minutes.ago)
|
||||
|
||||
assignment_policy.update!(conversation_priority: :longest_waiting)
|
||||
|
||||
# Re-create service after policy change
|
||||
service_with_priority = described_class.new(inbox: inbox)
|
||||
|
||||
service_with_priority.perform_bulk_assignment(limit: 1)
|
||||
|
||||
expect(inactive_conversation.reload.assignee).not_to be_nil
|
||||
expect(active_conversation.reload.assignee).to be_nil
|
||||
end
|
||||
|
||||
it 'returns 0 when no conversations to assign' do
|
||||
Conversation.find_each { |c| c.update!(assignee_id: agent1.id) }
|
||||
|
||||
expect(service.perform_bulk_assignment).to eq(0)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when policy is disabled' do
|
||||
before { assignment_policy.update!(enabled: false) }
|
||||
|
||||
it 'does not assign any conversations' do
|
||||
expect(service.perform_bulk_assignment).to eq(0)
|
||||
expect(inbox.conversations.unassigned.count).to eq(5)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'enterprise capacity features' do
|
||||
let(:conversation) { create(:conversation, inbox: inbox, assignee: nil) }
|
||||
|
||||
before do
|
||||
# Mock enterprise availability
|
||||
stub_const('Enterprise', Module.new)
|
||||
stub_const('Enterprise::AssignmentV2::CapacityManager', Class.new)
|
||||
|
||||
# Mock the selector to return agent1
|
||||
selector = instance_double(AssignmentV2::RoundRobinSelector)
|
||||
allow(AssignmentV2::RoundRobinSelector).to receive(:new).and_return(selector)
|
||||
allow(selector).to receive(:select_agent).and_return(agent1)
|
||||
end
|
||||
|
||||
it 'uses round robin when enterprise features are available' do
|
||||
expect(service.perform_for_conversation(conversation)).to be true
|
||||
expect(conversation.reload.assignee).to eq(agent1)
|
||||
end
|
||||
|
||||
it 'handles absence of enterprise features gracefully' do
|
||||
# Remove enterprise constant
|
||||
hide_const('Enterprise')
|
||||
|
||||
# Service should still work with round robin
|
||||
expect(service.perform_for_conversation(conversation)).to be true
|
||||
expect(conversation.reload.assignee).to eq(agent1)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'cache management' do
|
||||
it 'uses cache for round robin state' do
|
||||
assignment_policy.update!(assignment_order: :round_robin)
|
||||
|
||||
# Mock the selector and round robin service
|
||||
selector = instance_double(AssignmentV2::RoundRobinSelector)
|
||||
allow(AssignmentV2::RoundRobinSelector).to receive(:new).and_return(selector)
|
||||
allow(selector).to receive(:select_agent).and_return(agent1)
|
||||
|
||||
# Create and assign conversations
|
||||
conversation1 = create(:conversation, inbox: inbox, assignee: nil)
|
||||
service.perform_for_conversation(conversation1)
|
||||
|
||||
conversation2 = create(:conversation, inbox: inbox, assignee: nil)
|
||||
service.perform_for_conversation(conversation2)
|
||||
|
||||
# Just verify assignments worked
|
||||
expect(conversation1.reload.assignee).to eq(agent1)
|
||||
expect(conversation2.reload.assignee).to eq(agent1)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'edge cases' do
|
||||
it 'handles inbox without policy gracefully' do
|
||||
inbox_assignment_policy.destroy!
|
||||
conversation = create(:conversation, inbox: inbox, assignee: nil)
|
||||
|
||||
expect(service.perform_for_conversation(conversation)).to be false
|
||||
end
|
||||
|
||||
it 'handles empty agent list' do
|
||||
allow(inbox).to receive(:available_agents).and_return(InboxMember.none)
|
||||
conversation = create(:conversation, inbox: inbox, assignee: nil)
|
||||
|
||||
expect(service.perform_for_conversation(conversation)).to be false
|
||||
end
|
||||
|
||||
it 'filters out agents without inbox membership' do
|
||||
non_member_agent = create(:user, account: account, role: :agent, availability: :online)
|
||||
conversation = create(:conversation, inbox: inbox, assignee: nil)
|
||||
|
||||
# Mock selector to return agent1 (who is a member)
|
||||
selector = instance_double(AssignmentV2::RoundRobinSelector)
|
||||
allow(AssignmentV2::RoundRobinSelector).to receive(:new).and_return(selector)
|
||||
allow(selector).to receive(:select_agent).and_return(agent1)
|
||||
|
||||
expect(service.perform_for_conversation(conversation)).to be true
|
||||
expect(conversation.reload.assignee).not_to eq(non_member_agent)
|
||||
expect(conversation.reload.assignee).to eq(agent1)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,293 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AssignmentV2::RateLimiter, type: :service do
|
||||
before do
|
||||
# Mock GlobalConfig to avoid InstallationConfig issues
|
||||
allow(GlobalConfig).to receive(:get).and_return({})
|
||||
redis = Redis.new(Redis::Config.app)
|
||||
redis.flushdb if Rails.env.test?
|
||||
|
||||
# Ensure inbox_assignment_policy exists so the inbox has a policy
|
||||
inbox_assignment_policy
|
||||
end
|
||||
|
||||
let(:account) { create(:account) }
|
||||
let(:policy) { create(:assignment_policy, account: account, fair_distribution_limit: 5, fair_distribution_window: 3600) }
|
||||
let(:agent) { create(:user, account: account) }
|
||||
let(:inbox) { create(:inbox, account: account) }
|
||||
let(:inbox_assignment_policy) { create(:inbox_assignment_policy, inbox: inbox, assignment_policy: policy) }
|
||||
let(:rate_limiter) { described_class.new(inbox: inbox, user: agent) }
|
||||
|
||||
describe '#initialize' do
|
||||
it 'sets up rate limiter with inbox and user' do
|
||||
expect(rate_limiter.instance_variable_get(:@inbox)).to eq(inbox)
|
||||
expect(rate_limiter.instance_variable_get(:@user)).to eq(agent)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#within_limits?' do
|
||||
context 'when agent has no assignments in current window' do
|
||||
it 'returns true' do
|
||||
expect(rate_limiter.within_limits?).to be true
|
||||
end
|
||||
end
|
||||
|
||||
context 'when agent is below limit' do
|
||||
before do
|
||||
# Simulate 3 assignments in current window
|
||||
3.times { rate_limiter.record_assignment(create(:conversation)) }
|
||||
end
|
||||
|
||||
it 'returns true' do
|
||||
expect(rate_limiter.within_limits?).to be true
|
||||
end
|
||||
end
|
||||
|
||||
context 'when agent reaches limit' do
|
||||
before do
|
||||
# Simulate reaching the limit (5 assignments)
|
||||
5.times { rate_limiter.record_assignment(create(:conversation)) }
|
||||
end
|
||||
|
||||
it 'returns false' do
|
||||
expect(rate_limiter.within_limits?).to be false
|
||||
end
|
||||
end
|
||||
|
||||
context 'when agent exceeds limit' do
|
||||
before do
|
||||
# Simulate exceeding the limit
|
||||
6.times { rate_limiter.record_assignment(create(:conversation)) }
|
||||
end
|
||||
|
||||
it 'returns false' do
|
||||
expect(rate_limiter.within_limits?).to be false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#record_assignment' do
|
||||
let(:conversation) { create(:conversation, inbox: inbox) }
|
||||
|
||||
it 'increments assignment count for agent' do
|
||||
initial_status = rate_limiter.status
|
||||
rate_limiter.record_assignment(conversation)
|
||||
new_status = rate_limiter.status
|
||||
|
||||
expect(new_status[:current_count]).to eq(initial_status[:current_count] + 1)
|
||||
end
|
||||
|
||||
it 'sets expiration on the key' do
|
||||
rate_limiter.record_assignment(conversation)
|
||||
|
||||
current_window = (Time.current.to_i / 3600) * 3600
|
||||
key = "assignment_v2:rate_limit:#{agent.id}:#{current_window}"
|
||||
|
||||
redis = Redis.new(Redis::Config.app)
|
||||
ttl = redis.ttl(key)
|
||||
expect(ttl).to be > 0
|
||||
expect(ttl).to be <= 3600
|
||||
end
|
||||
|
||||
context 'when Redis fails' do
|
||||
before do
|
||||
redis_double = instance_double(Redis)
|
||||
allow(Redis).to receive(:new).and_return(redis_double)
|
||||
allow(redis_double).to receive(:multi).and_raise(Redis::ConnectionError)
|
||||
allow(Rails.logger).to receive(:error)
|
||||
end
|
||||
|
||||
it 'raises error' do
|
||||
expect { rate_limiter.record_assignment(conversation) }.to raise_error(Redis::ConnectionError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#status' do
|
||||
it 'returns correct status for agent with no assignments' do
|
||||
status = rate_limiter.status
|
||||
expect(status[:current_count]).to eq(0)
|
||||
expect(status[:within_limits]).to be true
|
||||
expect(status[:limit]).to eq(5)
|
||||
end
|
||||
|
||||
it 'returns correct count after assignments' do
|
||||
3.times { rate_limiter.record_assignment(create(:conversation)) }
|
||||
status = rate_limiter.status
|
||||
expect(status[:current_count]).to eq(3)
|
||||
expect(status[:within_limits]).to be true
|
||||
end
|
||||
|
||||
context 'when Redis fails' do
|
||||
before do
|
||||
redis_double = instance_double(Redis)
|
||||
allow(Redis).to receive(:new).and_return(redis_double)
|
||||
allow(redis_double).to receive(:get).and_raise(Redis::ConnectionError)
|
||||
allow(Rails.logger).to receive(:error)
|
||||
end
|
||||
|
||||
it 'raises error' do
|
||||
expect { rate_limiter.status }.to raise_error(Redis::ConnectionError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'remaining assignments' do
|
||||
it 'returns full limit when no assignments made' do
|
||||
status = rate_limiter.status
|
||||
expect(status[:limit] - status[:current_count]).to eq(5)
|
||||
end
|
||||
|
||||
it 'returns correct remaining count' do
|
||||
2.times { rate_limiter.record_assignment(create(:conversation)) }
|
||||
status = rate_limiter.status
|
||||
expect(status[:limit] - status[:current_count]).to eq(3)
|
||||
end
|
||||
|
||||
it 'returns 0 when limit reached' do
|
||||
5.times { rate_limiter.record_assignment(create(:conversation)) }
|
||||
status = rate_limiter.status
|
||||
expect(status[:limit] - status[:current_count]).to eq(0)
|
||||
end
|
||||
|
||||
it 'returns negative when limit exceeded' do
|
||||
6.times { rate_limiter.record_assignment(create(:conversation)) }
|
||||
status = rate_limiter.status
|
||||
expect(status[:limit] - status[:current_count]).to eq(-1)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'assignment capacity checks' do
|
||||
it 'returns true when agent has remaining capacity' do
|
||||
2.times { rate_limiter.record_assignment(create(:conversation)) }
|
||||
expect(rate_limiter.within_limits?).to be true
|
||||
end
|
||||
|
||||
it 'returns false when agent has no capacity' do
|
||||
5.times { rate_limiter.record_assignment(create(:conversation)) }
|
||||
expect(rate_limiter.within_limits?).to be false
|
||||
end
|
||||
|
||||
it 'correctly tracks multiple assignments' do
|
||||
3.times { rate_limiter.record_assignment(create(:conversation)) }
|
||||
|
||||
status = rate_limiter.status
|
||||
expect(status[:current_count]).to eq(3)
|
||||
expect(status[:within_limits]).to be true
|
||||
expect(status[:limit] - status[:current_count]).to eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'multiple agents' do
|
||||
let(:agent2) { create(:user, account: account) }
|
||||
let(:rate_limiter2) { described_class.new(inbox: inbox, user: agent2) }
|
||||
|
||||
before do
|
||||
2.times { rate_limiter.record_assignment(create(:conversation)) }
|
||||
4.times { rate_limiter2.record_assignment(create(:conversation)) }
|
||||
end
|
||||
|
||||
it 'tracks status independently for each agent' do
|
||||
status1 = rate_limiter.status
|
||||
status2 = rate_limiter2.status
|
||||
|
||||
expect(status1[:current_count]).to eq(2)
|
||||
expect(status1[:within_limits]).to be true
|
||||
expect(status1[:limit] - status1[:current_count]).to eq(3)
|
||||
|
||||
expect(status2[:current_count]).to eq(4)
|
||||
expect(status2[:within_limits]).to be true
|
||||
expect(status2[:limit] - status2[:current_count]).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'reset functionality' do
|
||||
before do
|
||||
3.times { rate_limiter.record_assignment(create(:conversation)) }
|
||||
end
|
||||
|
||||
it 'can be reset by clearing Redis key' do
|
||||
status_before = rate_limiter.status
|
||||
expect(status_before[:current_count]).to eq(3)
|
||||
|
||||
# Manually clear the key
|
||||
redis = Redis.new(Redis::Config.app)
|
||||
current_window = (Time.current.to_i / 3600) * 3600
|
||||
key = "assignment_v2:rate_limit:#{agent.id}:#{current_window}"
|
||||
redis.del(key)
|
||||
|
||||
status_after = rate_limiter.status
|
||||
expect(status_after[:current_count]).to eq(0)
|
||||
end
|
||||
|
||||
context 'when Redis fails' do
|
||||
before do
|
||||
redis_double = instance_double(Redis)
|
||||
allow(Redis).to receive(:new).and_return(redis_double)
|
||||
allow(redis_double).to receive(:del).and_raise(Redis::ConnectionError)
|
||||
allow(Rails.logger).to receive(:error)
|
||||
end
|
||||
|
||||
it 'raises error' do
|
||||
redis = Redis.new(Redis::Config.app)
|
||||
current_window = (Time.current.to_i / 3600) * 3600
|
||||
key = "assignment_v2:rate_limit:#{agent.id}:#{current_window}"
|
||||
expect { redis.del(key) }.to raise_error(Redis::ConnectionError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'window timing' do
|
||||
it 'calculates reset time correctly' do
|
||||
# Mock current time to make test predictable
|
||||
travel_to(Time.zone.parse('2024-01-01 10:30:00')) do
|
||||
status = rate_limiter.status
|
||||
reset_time = status[:reset_at]
|
||||
|
||||
expect(reset_time).to be_a(Time)
|
||||
expect(reset_time).to be > Time.current
|
||||
expect(reset_time - Time.current).to be <= 3600
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'window boundaries' do
|
||||
it 'resets count in new window' do
|
||||
# Set up assignment in current window
|
||||
2.times { rate_limiter.record_assignment(create(:conversation)) }
|
||||
expect(rate_limiter.status[:current_count]).to eq(2)
|
||||
|
||||
# Travel to next window (advance by window size)
|
||||
travel(3601.seconds) do
|
||||
expect(rate_limiter.status[:current_count]).to eq(0)
|
||||
expect(rate_limiter.within_limits?).to be true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'concurrent access' do
|
||||
it 'handles concurrent increments correctly' do
|
||||
threads = []
|
||||
results = []
|
||||
mutex = Mutex.new
|
||||
|
||||
# Simulate concurrent assignment requests
|
||||
5.times do
|
||||
threads << Thread.new do
|
||||
within_limits = rate_limiter.within_limits?
|
||||
mutex.synchronize { results << within_limits }
|
||||
rate_limiter.record_assignment(create(:conversation)) if within_limits
|
||||
end
|
||||
end
|
||||
|
||||
threads.each(&:join)
|
||||
|
||||
# Final count should not exceed the limit
|
||||
final_count = rate_limiter.status[:current_count]
|
||||
expect(final_count).to be <= 5
|
||||
expect(results.count(true)).to eq(final_count)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,145 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AssignmentV2::RoundRobinSelector, type: :service do
|
||||
before do
|
||||
# Mock GlobalConfig to avoid InstallationConfig issues
|
||||
allow(GlobalConfig).to receive(:get).and_return({})
|
||||
create(:inbox_member, inbox: inbox, user: user1)
|
||||
create(:inbox_member, inbox: inbox, user: user2)
|
||||
create(:inbox_member, inbox: inbox, user: user3)
|
||||
end
|
||||
|
||||
let(:account) { create(:account) }
|
||||
let(:inbox) { create(:inbox, account: account) }
|
||||
let(:policy) { create(:assignment_policy, account: account) }
|
||||
let(:user1) { create(:user, account: account, availability: :online) }
|
||||
let(:user2) { create(:user, account: account, availability: :online) }
|
||||
let(:user3) { create(:user, account: account, availability: :offline) }
|
||||
|
||||
describe '#select_agent' do
|
||||
let(:selector) { described_class.new(inbox: inbox) }
|
||||
let(:round_robin_service) { instance_double(AutoAssignment::InboxRoundRobinService) }
|
||||
let(:available_agents) { InboxMember.where(inbox: inbox, user: [user1, user2]) }
|
||||
|
||||
before do
|
||||
allow(AutoAssignment::InboxRoundRobinService).to receive(:new).with(inbox: inbox).and_return(round_robin_service)
|
||||
end
|
||||
|
||||
context 'when Redis is available' do
|
||||
before do
|
||||
allow(round_robin_service).to receive(:available_agent).with(allowed_agent_ids: [user1.id.to_s, user2.id.to_s]).and_return(user1.id.to_s)
|
||||
end
|
||||
|
||||
it 'returns an online agent' do
|
||||
result = selector.select_agent(available_agents)
|
||||
expect(result).to eq(user1)
|
||||
end
|
||||
|
||||
it 'excludes offline agents' do
|
||||
result = selector.select_agent(available_agents)
|
||||
expect(result).not_to eq(user3)
|
||||
end
|
||||
|
||||
it 'handles no available agent gracefully' do
|
||||
allow(round_robin_service).to receive(:available_agent).with(allowed_agent_ids: [user1.id.to_s, user2.id.to_s]).and_return(nil)
|
||||
result = selector.select_agent(available_agents)
|
||||
expect(result).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'when Redis fails' do
|
||||
before do
|
||||
allow(round_robin_service).to receive(:available_agent).and_raise(Redis::CannotConnectError)
|
||||
end
|
||||
|
||||
it 'raises the error' do
|
||||
expect { selector.select_agent(available_agents) }.to raise_error(Redis::CannotConnectError)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with empty available agents' do
|
||||
it 'returns nil when no agents are available' do
|
||||
result = selector.select_agent(InboxMember.none)
|
||||
expect(result).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'with different user IDs' do
|
||||
it 'correctly finds the inbox member by user_id' do
|
||||
allow(round_robin_service).to receive(:available_agent).with(allowed_agent_ids: [user1.id.to_s, user2.id.to_s]).and_return(user2.id.to_s)
|
||||
|
||||
result = selector.select_agent(available_agents)
|
||||
expect(result).to eq(user2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#add_agent_to_queue' do
|
||||
let(:selector) { described_class.new(inbox: inbox) }
|
||||
let(:round_robin_service) { instance_double(AutoAssignment::InboxRoundRobinService) }
|
||||
|
||||
before do
|
||||
allow(AutoAssignment::InboxRoundRobinService).to receive(:new).with(inbox: inbox).and_return(round_robin_service)
|
||||
end
|
||||
|
||||
it 'delegates to round robin service' do
|
||||
expect(round_robin_service).to receive(:add_agent_to_queue).with(user1.id)
|
||||
selector.add_agent_to_queue(user1.id)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#remove_agent_from_queue' do
|
||||
let(:selector) { described_class.new(inbox: inbox) }
|
||||
let(:round_robin_service) { instance_double(AutoAssignment::InboxRoundRobinService) }
|
||||
|
||||
before do
|
||||
allow(AutoAssignment::InboxRoundRobinService).to receive(:new).with(inbox: inbox).and_return(round_robin_service)
|
||||
end
|
||||
|
||||
it 'delegates to round robin service' do
|
||||
expect(round_robin_service).to receive(:remove_agent_from_queue).with(user1.id)
|
||||
selector.remove_agent_from_queue(user1.id)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#reset_queue' do
|
||||
let(:selector) { described_class.new(inbox: inbox) }
|
||||
let(:round_robin_service) { instance_double(AutoAssignment::InboxRoundRobinService) }
|
||||
|
||||
before do
|
||||
allow(AutoAssignment::InboxRoundRobinService).to receive(:new).with(inbox: inbox).and_return(round_robin_service)
|
||||
end
|
||||
|
||||
it 'delegates to round robin service' do
|
||||
expect(round_robin_service).to receive(:reset_queue)
|
||||
selector.reset_queue
|
||||
end
|
||||
end
|
||||
|
||||
describe 'edge cases' do
|
||||
let(:selector) { described_class.new(inbox: inbox) }
|
||||
let(:round_robin_service) { instance_double(AutoAssignment::InboxRoundRobinService) }
|
||||
let(:available_agents) { InboxMember.where(inbox: inbox, user: [user1, user2]) }
|
||||
|
||||
before do
|
||||
allow(AutoAssignment::InboxRoundRobinService).to receive(:new).with(inbox: inbox).and_return(round_robin_service)
|
||||
end
|
||||
|
||||
it 'handles invalid user_id from round robin service' do
|
||||
allow(round_robin_service).to receive(:available_agent).and_return('invalid_id')
|
||||
|
||||
result = selector.select_agent(available_agents)
|
||||
expect(result).to be_nil
|
||||
end
|
||||
|
||||
it 'handles user_id not in available agents' do
|
||||
other_user = create(:user, account: account)
|
||||
allow(round_robin_service).to receive(:available_agent).and_return(other_user.id.to_s)
|
||||
|
||||
result = selector.select_agent(available_agents)
|
||||
expect(result).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user