2021-05-05 15:36:11 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Integrations::Dialogflow::ProcessorService do
|
|
|
|
let(:account) { create(:account) }
|
2021-11-12 12:23:03 +00:00
|
|
|
let(:inbox) { create(:inbox, account: account) }
|
|
|
|
let(:hook) { create(:integrations_hook, :dialogflow, inbox: inbox, account: account) }
|
2021-07-21 16:32:43 +00:00
|
|
|
let(:conversation) { create(:conversation, account: account, status: :pending) }
|
2021-05-05 15:36:11 +00:00
|
|
|
let(:message) { create(:message, account: account, conversation: conversation) }
|
|
|
|
let(:event_name) { 'message.created' }
|
|
|
|
let(:event_data) { { message: message } }
|
2021-07-07 04:34:48 +00:00
|
|
|
let(:dialogflow_text_double) { double }
|
2021-05-05 15:36:11 +00:00
|
|
|
|
|
|
|
describe '#perform' do
|
|
|
|
let(:dialogflow_service) { double }
|
|
|
|
let(:dialogflow_response) do
|
|
|
|
ActiveSupport::HashWithIndifferentAccess.new(
|
2021-07-07 04:34:48 +00:00
|
|
|
fulfillment_messages: [
|
|
|
|
{ text: dialogflow_text_double }
|
|
|
|
]
|
2021-05-05 15:36:11 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:processor) { described_class.new(event_name: event_name, hook: hook, event_data: event_data) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(dialogflow_service).to receive(:query_result).and_return(dialogflow_response)
|
|
|
|
allow(processor).to receive(:get_dialogflow_response).and_return(dialogflow_service)
|
2021-07-07 04:34:48 +00:00
|
|
|
allow(dialogflow_text_double).to receive(:to_h).and_return({ text: ['hello payload'] })
|
2021-05-05 15:36:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when valid message and dialogflow returns fullfillment text' do
|
|
|
|
it 'creates the response message' do
|
2021-07-07 04:34:48 +00:00
|
|
|
processor.perform
|
|
|
|
expect(conversation.reload.messages.last.content).to eql('hello payload')
|
2021-05-05 15:36:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when dialogflow returns fullfillment text to be empty' do
|
|
|
|
let(:dialogflow_response) do
|
|
|
|
ActiveSupport::HashWithIndifferentAccess.new(
|
2021-07-07 04:34:48 +00:00
|
|
|
fulfillment_messages: [{ payload: { content: 'hello payload random' } }]
|
2021-05-05 15:36:11 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates the response message based on fulfillment messages' do
|
2021-07-07 04:34:48 +00:00
|
|
|
processor.perform
|
|
|
|
expect(conversation.reload.messages.last.content).to eql('hello payload random')
|
2021-05-05 15:36:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when dialogflow returns action' do
|
|
|
|
let(:dialogflow_response) do
|
|
|
|
ActiveSupport::HashWithIndifferentAccess.new(
|
|
|
|
fulfillment_messages: [{ payload: { action: 'handoff' } }]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handsoff the conversation to agent' do
|
|
|
|
processor.perform
|
|
|
|
expect(conversation.status).to eql('open')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-07 04:34:48 +00:00
|
|
|
context 'when dialogflow returns action and messages if available' do
|
|
|
|
let(:dialogflow_response) do
|
|
|
|
ActiveSupport::HashWithIndifferentAccess.new(
|
|
|
|
fulfillment_messages: [{ payload: { action: 'handoff' } }, { text: dialogflow_text_double }]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handsoff the conversation to agent' do
|
|
|
|
processor.perform
|
|
|
|
expect(conversation.reload.status).to eql('open')
|
|
|
|
expect(conversation.messages.last.content).to eql('hello payload')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-03 06:35:39 +00:00
|
|
|
context 'when dialogflow returns resolve action' do
|
|
|
|
let(:dialogflow_response) do
|
|
|
|
ActiveSupport::HashWithIndifferentAccess.new(
|
|
|
|
fulfillment_messages: [{ payload: { action: 'resolve' } }, { text: dialogflow_text_double }]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'resolves the conversation without moving it to an agent' do
|
|
|
|
processor.perform
|
|
|
|
expect(conversation.reload.status).to eql('resolved')
|
|
|
|
expect(conversation.messages.last.content).to eql('hello payload')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-05 15:36:11 +00:00
|
|
|
context 'when conversation is not bot' do
|
|
|
|
let(:conversation) { create(:conversation, account: account, status: :open) }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(processor.perform).to be(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when message is private' do
|
|
|
|
let(:message) { create(:message, account: account, conversation: conversation, private: true) }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(processor.perform).to be(nil)
|
|
|
|
end
|
|
|
|
end
|
2021-10-14 07:47:09 +00:00
|
|
|
|
|
|
|
context 'when message updated' do
|
|
|
|
let(:message) do
|
|
|
|
create(:message, account: account, conversation: conversation, private: true,
|
|
|
|
submitted_values: [{ 'title' => 'Support', 'value' => 'selected_gas' }])
|
|
|
|
end
|
|
|
|
let(:event_name) { 'message.updated' }
|
|
|
|
|
|
|
|
it 'returns submitted value for message content' do
|
|
|
|
expect(processor.send(:message_content, message)).to eql('selected_gas')
|
|
|
|
end
|
|
|
|
end
|
2021-05-05 15:36:11 +00:00
|
|
|
end
|
|
|
|
end
|