From 4025517f090a122e84805b8ff697d7ad2094f6bc Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Mon, 20 Jan 2025 14:33:58 +0530 Subject: [PATCH] feat: add specs for response builder --- .../conversation/response_builder_job_spec.rb | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 spec/enterprise/jobs/captain/conversation/response_builder_job_spec.rb diff --git a/spec/enterprise/jobs/captain/conversation/response_builder_job_spec.rb b/spec/enterprise/jobs/captain/conversation/response_builder_job_spec.rb new file mode 100644 index 000000000..8dc20a29c --- /dev/null +++ b/spec/enterprise/jobs/captain/conversation/response_builder_job_spec.rb @@ -0,0 +1,62 @@ +require 'rails_helper' + +RSpec.describe Captain::Conversation::ResponseBuilderJob, type: :job do + let(:account) { create(:account, custom_attributes: { plan_name: 'startups' }) } + let(:inbox) { create(:inbox, account: account) } + let(:assistant) { create(:captain_assistant, account: account) } + let!(:captain_inbox_association) { create(:captain_inbox, captain_assistant: assistant, inbox: inbox) } + + describe '#perform' do + context 'when limits are reached' do + let(:conversation) { create(:conversation, inbox: inbox, account: account) } + + before do + allow(inbox).to receive(:captain_active?).and_return(false) + allow(account).to receive(:increment_response_usage).and_return(true) + end + + it 'processes handoff' do + # ensure that a assistant is actually present but is inactive + expect(inbox.captain_assistant.present?).to be_truthy + expect(inbox.captain_active?).to be_falsey + + expect { described_class.perform_now(conversation, assistant) }.to change { conversation.messages.count } + expect(conversation.messages.count).to eq(1) + expect(conversation.messages.last.content).to eq('Transferring to another agent for further assistance.') + end + + it 'does not increment usage' do + # ensure that a assistant is actually present but is inactive + expect(inbox.captain_assistant.present?).to be_truthy + expect(inbox.captain_active?).to be_falsey + expect(account).not_to receive(:increment_response_usage) + expect { described_class.perform_now(conversation, assistant) }.to change { conversation.messages.count } + end + end + + context 'when limits are not reached' do + let(:conversation) { create(:conversation, inbox: inbox, account: account) } + let!(:message) { create(:message, conversation: conversation, content: 'Hello', message_type: :incoming) } + let(:mock_llm_chat_service) { double('Captain::Llm::AssistantChatService') } + + before do + allow(inbox).to receive(:captain_active?).and_return(true) + allow(Captain::Llm::AssistantChatService).to receive(:new).and_return(mock_llm_chat_service) + allow(mock_llm_chat_service).to receive(:generate_response).and_return({ 'response' => 'Hey, welcome to Captain Specs' }) + end + + it 'generates and processes response' do + expect { described_class.perform_now(conversation, assistant) }.to change { conversation.messages.count } + expect(conversation.messages.count).to eq(2) + expect(conversation.messages.outgoing.count).to eq(1) + expect(conversation.messages.last.content).to eq('Hey, welcome to Captain Specs') + end + + it 'increments usage response' do + described_class.perform_now(conversation, assistant) + account.reload + expect(account.usage_limits[:captain][:generated_responses][:consumed]).to eq(1) + end + end + end +end