2021-05-05 15:36:11 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Integrations::Dialogflow::ProcessorService do
|
|
|
|
let(:account) { create(:account) }
|
2021-05-17 05:02:59 +00:00
|
|
|
let(:hook) { create(:integrations_hook, :dialogflow, 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
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|