2023-03-03 15:06:31 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe TranslationService::LibreTranslate do
|
|
|
|
subject(:service) { described_class.new('https://libretranslate.example.com', 'my-api-key') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_request(:get, 'https://libretranslate.example.com/languages').to_return(
|
2023-03-16 06:07:24 -04:00
|
|
|
body: '[{"code": "en","name": "English","targets": ["de","en","es"]},{"code": "da","name": "Danish","targets": ["en","pt"]}]'
|
2023-03-03 15:06:31 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#languages' do
|
2023-03-16 06:07:24 -04:00
|
|
|
subject(:languages) { service.languages }
|
2023-03-03 15:06:31 -05:00
|
|
|
|
2023-03-16 06:07:24 -04:00
|
|
|
it 'returns source languages' do
|
2023-03-03 15:06:31 -05:00
|
|
|
expect(languages.keys).to eq ['en', 'da', nil]
|
|
|
|
end
|
|
|
|
|
2023-03-16 06:07:24 -04:00
|
|
|
it 'returns target languages for each source language' do
|
2023-03-03 15:06:31 -05:00
|
|
|
expect(languages['en']).to eq %w(de es)
|
2023-03-16 06:07:24 -04:00
|
|
|
expect(languages['da']).to eq %w(en pt)
|
2023-03-03 15:06:31 -05:00
|
|
|
end
|
|
|
|
|
2023-03-16 06:07:24 -04:00
|
|
|
it 'returns target languages for auto-detected language' do
|
|
|
|
expect(languages[nil]).to eq %w(de en es pt)
|
2023-03-03 15:06:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#translate' do
|
|
|
|
it 'returns translation with specified source language' do
|
|
|
|
stub_request(:post, 'https://libretranslate.example.com/translate')
|
2023-05-31 18:10:21 -04:00
|
|
|
.with(body: '{"q":["Hasta la vista"],"source":"es","target":"en","format":"html","api_key":"my-api-key"}')
|
|
|
|
.to_return(body: '{"translatedText": ["See you"]}')
|
2023-03-03 15:06:31 -05:00
|
|
|
|
2023-05-31 18:10:21 -04:00
|
|
|
translations = service.translate(['Hasta la vista'], 'es', 'en')
|
|
|
|
expect(translations.size).to eq 1
|
|
|
|
|
|
|
|
translation = translations.first
|
|
|
|
expect(translation.detected_source_language).to be 'es'
|
2023-03-03 15:06:31 -05:00
|
|
|
expect(translation.provider).to eq 'LibreTranslate'
|
|
|
|
expect(translation.text).to eq 'See you'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns translation with auto-detected source language' do
|
|
|
|
stub_request(:post, 'https://libretranslate.example.com/translate')
|
2023-05-31 18:10:21 -04:00
|
|
|
.with(body: '{"q":["Guten Morgen"],"source":"auto","target":"en","format":"html","api_key":"my-api-key"}')
|
|
|
|
.to_return(body: '{"detectedLanguage": [{"confidence": 92, "language": "de"}], "translatedText": ["Good morning"]}')
|
|
|
|
|
|
|
|
translations = service.translate(['Guten Morgen'], nil, 'en')
|
|
|
|
expect(translations.size).to eq 1
|
2023-03-03 15:06:31 -05:00
|
|
|
|
2023-05-31 18:10:21 -04:00
|
|
|
translation = translations.first
|
|
|
|
expect(translation.detected_source_language).to eq 'de'
|
2023-03-03 15:06:31 -05:00
|
|
|
expect(translation.provider).to eq 'LibreTranslate'
|
|
|
|
expect(translation.text).to eq 'Good morning'
|
|
|
|
end
|
2023-05-31 18:10:21 -04:00
|
|
|
|
|
|
|
it 'returns translation of multiple texts' do
|
|
|
|
stub_request(:post, 'https://libretranslate.example.com/translate')
|
|
|
|
.with(body: '{"q":["Guten Morgen","Gute Nacht"],"source":"de","target":"en","format":"html","api_key":"my-api-key"}')
|
|
|
|
.to_return(body: '{"translatedText": ["Good morning", "Good night"]}')
|
|
|
|
|
|
|
|
translations = service.translate(['Guten Morgen', 'Gute Nacht'], 'de', 'en')
|
|
|
|
expect(translations.size).to eq 2
|
|
|
|
|
|
|
|
expect(translations.first.text).to eq 'Good morning'
|
|
|
|
expect(translations.last.text).to eq 'Good night'
|
|
|
|
end
|
2023-03-03 15:06:31 -05:00
|
|
|
end
|
|
|
|
end
|