Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬
Ruby actioncablechat-widgetconversationcustomer-supportdashboarddesigndockerdocker-imageherokuintercomjavascriptlivechatopensourcerailsrubysassvuejswhatsappzendesk
require 'rails_helper'
RSpec.describe Captain::Tools::ResolveConversationTool do
let(:account) { create(:account) }
let(:assistant) { create(:captain_assistant, account: account) }
let(:inbox) { create(:inbox, account: account) }
let(:conversation) { create(:conversation, account: account, inbox: inbox, status: :open) }
let(:tool) { described_class.new(assistant) }
let(:tool_context) { Struct.new(:state).new({ conversation: { id: conversation.id } }) }
before do
Current.executed_by = assistant
end
after do
Current.reset
end
describe 'resolving a conversation' do
before { account.enable_features('captain_integration') }
it 'marks resolved and enqueues an activity message with the reason' do
tool.perform(tool_context, reason: 'Possible spam')
expect(conversation.reload).to be_resolved
expect(Conversations::ActivityMessageJob).to have_been_enqueued.with(
conversation,
hash_including(
content: I18n.t('conversations.activity.captain.resolved_by_tool', user_name: assistant.name, reason: 'Possible spam')
)
)
end
it 'creates the configured resolution message' do
assistant.update!(config: assistant.config.merge('resolution_message' => 'Thanks for contacting us.'))
tool.perform(tool_context, reason: 'Issue resolved')
resolution_message = conversation.messages.outgoing.where(private: false).last
expect(resolution_message).to have_attributes(content: 'Thanks for contacting us.', sender: assistant)
end
it 'rolls back the resolution message when resolving fails' do
assistant.update!(config: assistant.config.merge('resolution_message' => 'Thanks for contacting us.'))
allow(tool).to receive(:find_conversation).and_return(conversation)
allow(conversation).to receive(:resolved!).and_raise(ActiveRecord::RecordInvalid.new(conversation))
expect do
tool.perform(tool_context, reason: 'Issue resolved')
end.to raise_error(ActiveRecord::RecordInvalid)
.and(not_change { conversation.messages.outgoing.where(private: false).count })
expect(conversation.reload).to be_open
end
it 'resolves without a message when resolution messages are disabled' do
assistant.update!(send_inactivity_resolution_message: false)
expect do
tool.perform(tool_context, reason: 'Issue resolved')
end.not_to(change { conversation.messages.outgoing.where(private: false).count })
expect(conversation.reload).to be_resolved
end
it 'emits a captain resolution event with the tool source' do
expect(Captain::ConversationEvents).to receive(:resolved)
.with(conversation: conversation, assistant: assistant, source: 'tool', at: kind_of(Time))
tool.perform(tool_context, reason: 'Possible spam')
end
it 'creates a conversation_resolved reporting event' do
create(:captain_inbox, captain_assistant: assistant, inbox: inbox)
expect do
perform_enqueued_jobs do
tool.perform(tool_context, reason: 'Possible spam')
end
end.to change { ReportingEvent.where(conversation_id: conversation.id, name: 'conversation_resolved').count }.by(1)
end
end
describe 'when auto-resolve is disabled for the assistant' do
before { assistant.update!(auto_resolve_mode: 'disabled') }
it 'does not resolve and returns a disabled message' do
result = tool.perform(tool_context, reason: 'Possible spam')
expect(result).to eq('Auto-resolve is disabled for this assistant')
expect(conversation.reload).not_to be_resolved
end
end
describe 'resolving an already resolved conversation' do
let(:conversation) { create(:conversation, account: account, inbox: inbox, status: :resolved) }
it 'does not emit a captain resolution event' do
expect(Captain::ConversationEvents).not_to receive(:resolved)
tool.perform(tool_context, reason: 'Possible spam')
end
it 'does not re-resolve and returns an already resolved message' do
queue_adapter = ActiveJob::Base.queue_adapter
queue_adapter.enqueued_jobs.clear
result = tool.perform(tool_context, reason: 'Possible spam')
expect(result).to include('already resolved')
expect(Conversations::ActivityMessageJob).not_to have_been_enqueued
end
end
describe 'when a playground call cannot resolve a conversation' do
let(:tool_context) { Struct.new(:state).new({ source: 'playground' }) }
it 'marks the result as an error for run details' do
result = tool.perform(tool_context, reason: 'Possible spam')
expect(result).to eq('ERROR: Conversation not found')
end
end
end