libre_translate_spec.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe TranslationService::LibreTranslate do
  4. subject(:service) { described_class.new('https://libretranslate.example.com', 'my-api-key') }
  5. before do
  6. stub_request(:get, 'https://libretranslate.example.com/languages').to_return(
  7. body: '[{"code": "en","name": "English","targets": ["de","en","es"]},{"code": "da","name": "Danish","targets": ["en","pt"]}]'
  8. )
  9. end
  10. describe '#languages' do
  11. subject(:languages) { service.languages }
  12. it 'returns source languages' do
  13. expect(languages.keys).to eq ['en', 'da', nil]
  14. end
  15. it 'returns target languages for each source language' do
  16. expect(languages['en']).to eq %w(de es)
  17. expect(languages['da']).to eq %w(en pt)
  18. end
  19. it 'returns target languages for auto-detected language' do
  20. expect(languages[nil]).to eq %w(de en es pt)
  21. end
  22. end
  23. describe '#translate' do
  24. it 'returns translation with specified source language' do
  25. stub_request(:post, 'https://libretranslate.example.com/translate')
  26. .with(body: '{"q":["Hasta la vista"],"source":"es","target":"en","format":"html","api_key":"my-api-key"}')
  27. .to_return(body: '{"translatedText": ["See you"]}')
  28. translations = service.translate(['Hasta la vista'], 'es', 'en')
  29. expect(translations.size).to eq 1
  30. translation = translations.first
  31. expect(translation.detected_source_language).to be 'es'
  32. expect(translation.provider).to eq 'LibreTranslate'
  33. expect(translation.text).to eq 'See you'
  34. end
  35. it 'returns translation with auto-detected source language' do
  36. stub_request(:post, 'https://libretranslate.example.com/translate')
  37. .with(body: '{"q":["Guten Morgen"],"source":"auto","target":"en","format":"html","api_key":"my-api-key"}')
  38. .to_return(body: '{"detectedLanguage": [{"confidence": 92, "language": "de"}], "translatedText": ["Good morning"]}')
  39. translations = service.translate(['Guten Morgen'], nil, 'en')
  40. expect(translations.size).to eq 1
  41. translation = translations.first
  42. expect(translation.detected_source_language).to eq 'de'
  43. expect(translation.provider).to eq 'LibreTranslate'
  44. expect(translation.text).to eq 'Good morning'
  45. end
  46. it 'returns translation of multiple texts' do
  47. stub_request(:post, 'https://libretranslate.example.com/translate')
  48. .with(body: '{"q":["Guten Morgen","Gute Nacht"],"source":"de","target":"en","format":"html","api_key":"my-api-key"}')
  49. .to_return(body: '{"translatedText": ["Good morning", "Good night"]}')
  50. translations = service.translate(['Guten Morgen', 'Gute Nacht'], 'de', 'en')
  51. expect(translations.size).to eq 2
  52. expect(translations.first.text).to eq 'Good morning'
  53. expect(translations.last.text).to eq 'Good night'
  54. end
  55. end
  56. end