Feat: detect language of the message content (#6660)

This commit is contained in:
Tejaswini Chile
2023-04-04 08:57:27 +05:30
committed by GitHub
parent 268eababa3
commit 6a0ca35de4
14 changed files with 178 additions and 7 deletions

View File

@@ -0,0 +1,44 @@
require 'rails_helper'
require 'google/cloud/translate/v3'
describe Integrations::GoogleTranslate::DetectLanguageService do
let(:account) { create(:account) }
let(:message) { create(:message, account: account, content: 'muchas muchas gracias') }
let(:hook) { create(:integrations_hook, :google_translate, account: account) }
let(:translate_client) { double }
before do
allow(::Google::Cloud::Translate).to receive(:translation_service).and_return(translate_client)
allow(translate_client).to receive(:detect_language).and_return(::Google::Cloud::Translate::V3::DetectLanguageResponse
.new({ languages: [{ language_code: 'es', confidence: 0.71875 }] }))
end
describe '#perform' do
it 'detects and updates the conversation language' do
described_class.new(hook: hook, message: message).perform
expect(translate_client).to have_received(:detect_language)
expect(message.conversation.reload.additional_attributes['conversation_language']).to eq('es')
end
it 'will not update the conversation language if it is already present' do
message.conversation.update!(additional_attributes: { conversation_language: 'en' })
described_class.new(hook: hook, message: message).perform
expect(translate_client).not_to have_received(:detect_language)
expect(message.conversation.reload.additional_attributes['conversation_language']).to eq('en')
end
it 'will not update the conversation language if the message is not incoming' do
message.update!(message_type: :outgoing)
described_class.new(hook: hook, message: message).perform
expect(translate_client).not_to have_received(:detect_language)
expect(message.conversation.reload.additional_attributes['conversation_language']).to be_nil
end
it 'will not execute if the message content is blank' do
message.update!(content: nil)
described_class.new(hook: hook, message: message).perform
expect(translate_client).not_to have_received(:detect_language)
expect(message.conversation.reload.additional_attributes['conversation_language']).to be_nil
end
end
end