about summary refs log tree commit diff
path: root/spec/lib/translation_service/libre_translate_spec.rb
blob: a6cb01884a2f18e3805d8203d11467227117e7db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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(
      body: '[{"code": "en","name": "English","targets": ["de","es"]},{"code": "da","name": "Danish","targets": ["en","de"]}]'
    )
  end

  describe '#supported?' do
    it 'supports included language pair' do
      expect(service.supported?('en', 'de')).to be true
    end

    it 'does not support reversed language pair' do
      expect(service.supported?('de', 'en')).to be false
    end

    it 'supports auto-detecting source language' do
      expect(service.supported?(nil, 'de')).to be true
    end

    it 'does not support auto-detecting for unsupported target language' do
      expect(service.supported?(nil, 'pt')).to be false
    end
  end

  describe '#languages' do
    subject(:languages) { service.send(:languages) }

    it 'includes supported source languages' do
      expect(languages.keys).to eq ['en', 'da', nil]
    end

    it 'includes supported target languages for source language' do
      expect(languages['en']).to eq %w(de es)
    end

    it 'includes supported target languages for auto-detected language' do
      expect(languages[nil]).to eq %w(de es en)
    end
  end

  describe '#translate' do
    it 'returns translation with specified source language' do
      stub_request(:post, 'https://libretranslate.example.com/translate')
        .with(body: '{"q":"Hasta la vista","source":"es","target":"en","format":"html","api_key":"my-api-key"}')
        .to_return(body: '{"translatedText": "See you"}')

      translation = service.translate('Hasta la vista', 'es', 'en')
      expect(translation.detected_source_language).to eq 'es'
      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')
        .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"}')

      translation = service.translate('Guten Morgen', nil, 'en')
      expect(translation.detected_source_language).to be_nil
      expect(translation.provider).to eq 'LibreTranslate'
      expect(translation.text).to eq 'Good morning'
    end
  end
end