about summary refs log tree commit diff
path: root/spec
diff options
context:
space:
mode:
authorChristian Schmidt <github@chsc.dk>2023-03-16 11:07:24 +0100
committerGitHub <noreply@github.com>2023-03-16 11:07:24 +0100
commitbd047acc356671727c112336bb237f979bba517d (patch)
treef65ea64df4dcf6bb93a5561bb237caf78c54153c /spec
parent630436ab2d84b5ed2334ad510cb9ad6253050ae5 (diff)
Replace `Status#translatable?` with language matrix in separate endpoint (#24037)
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/api/v1/instances/translation_languages_controller_spec.rb31
-rw-r--r--spec/controllers/api/v1/statuses/translations_controller_spec.rb3
-rw-r--r--spec/lib/translation_service/deepl_spec.rb36
-rw-r--r--spec/lib/translation_service/libre_translate_spec.rb31
-rw-r--r--spec/models/status_spec.rb79
-rw-r--r--spec/presenters/instance_presenter_spec.rb2
6 files changed, 50 insertions, 132 deletions
diff --git a/spec/controllers/api/v1/instances/translation_languages_controller_spec.rb b/spec/controllers/api/v1/instances/translation_languages_controller_spec.rb
new file mode 100644
index 000000000..5b7e4abb6
--- /dev/null
+++ b/spec/controllers/api/v1/instances/translation_languages_controller_spec.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+describe Api::V1::Instances::TranslationLanguagesController do
+  describe 'GET #show' do
+    context 'when no translation service is configured' do
+      it 'returns empty language matrix' do
+        get :show
+
+        expect(response).to have_http_status(200)
+        expect(body_as_json).to eq({})
+      end
+    end
+
+    context 'when a translation service is configured' do
+      before do
+        service = instance_double(TranslationService::DeepL, languages: { nil => %w(en de), 'en' => ['de'] })
+        allow(TranslationService).to receive(:configured?).and_return(true)
+        allow(TranslationService).to receive(:configured).and_return(service)
+      end
+
+      it 'returns language matrix' do
+        get :show
+
+        expect(response).to have_http_status(200)
+        expect(body_as_json).to eq({ und: %w(en de), en: ['de'] })
+      end
+    end
+  end
+end
diff --git a/spec/controllers/api/v1/statuses/translations_controller_spec.rb b/spec/controllers/api/v1/statuses/translations_controller_spec.rb
index 2deea9fc0..8495779bf 100644
--- a/spec/controllers/api/v1/statuses/translations_controller_spec.rb
+++ b/spec/controllers/api/v1/statuses/translations_controller_spec.rb
@@ -19,9 +19,10 @@ describe Api::V1::Statuses::TranslationsController do
 
       before do
         translation = TranslationService::Translation.new(text: 'Hello')
-        service = instance_double(TranslationService::DeepL, translate: translation, supported?: true)
+        service = instance_double(TranslationService::DeepL, translate: translation)
         allow(TranslationService).to receive(:configured?).and_return(true)
         allow(TranslationService).to receive(:configured).and_return(service)
+        Rails.cache.write('translation_service/languages', { 'es' => ['en'] })
         post :create, params: { status_id: status.id }
       end
 
diff --git a/spec/lib/translation_service/deepl_spec.rb b/spec/lib/translation_service/deepl_spec.rb
index aa2473186..2363f8f13 100644
--- a/spec/lib/translation_service/deepl_spec.rb
+++ b/spec/lib/translation_service/deepl_spec.rb
@@ -16,29 +16,6 @@ RSpec.describe TranslationService::DeepL do
     )
   end
 
-  describe '#supported?' do
-    it 'supports included languages as source and target languages' do
-      expect(service.supported?('uk', 'en')).to be true
-    end
-
-    it 'supports auto-detecting source language' do
-      expect(service.supported?(nil, 'en')).to be true
-    end
-
-    it 'supports "en" and "pt" as target languages though not included in language list' do
-      expect(service.supported?('uk', 'en')).to be true
-      expect(service.supported?('uk', 'pt')).to be true
-    end
-
-    it 'does not support non-included language as target language' do
-      expect(service.supported?('uk', 'nl')).to be false
-    end
-
-    it 'does not support non-included language as source language' do
-      expect(service.supported?('da', 'en')).to be false
-    end
-  end
-
   describe '#translate' do
     it 'returns translation with specified source language' do
       stub_request(:post, 'https://api.deepl.com/v2/translate')
@@ -63,13 +40,18 @@ RSpec.describe TranslationService::DeepL do
     end
   end
 
-  describe '#languages?' do
+  describe '#languages' do
     it 'returns source languages' do
-      expect(service.send(:languages, 'source')).to eq ['en', 'uk', nil]
+      expect(service.languages.keys).to eq [nil, 'en', 'uk']
+    end
+
+    it 'returns target languages for each source language' do
+      expect(service.languages['en']).to eq %w(pt en-GB zh)
+      expect(service.languages['uk']).to eq %w(en pt en-GB zh)
     end
 
-    it 'returns target languages' do
-      expect(service.send(:languages, 'target')).to eq %w(en-gb zh en pt)
+    it 'returns target languages for auto-detection' do
+      expect(service.languages[nil]).to eq %w(en pt en-GB zh)
     end
   end
 
diff --git a/spec/lib/translation_service/libre_translate_spec.rb b/spec/lib/translation_service/libre_translate_spec.rb
index a6cb01884..fbd726a7e 100644
--- a/spec/lib/translation_service/libre_translate_spec.rb
+++ b/spec/lib/translation_service/libre_translate_spec.rb
@@ -7,41 +7,24 @@ RSpec.describe TranslationService::LibreTranslate do
 
   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"]}]'
+      body: '[{"code": "en","name": "English","targets": ["de","en","es"]},{"code": "da","name": "Danish","targets": ["en","pt"]}]'
     )
   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) }
+    subject(:languages) { service.languages }
 
-    it 'includes supported source languages' do
+    it 'returns source languages' do
       expect(languages.keys).to eq ['en', 'da', nil]
     end
 
-    it 'includes supported target languages for source language' do
+    it 'returns target languages for each source language' do
       expect(languages['en']).to eq %w(de es)
+      expect(languages['da']).to eq %w(en pt)
     end
 
-    it 'includes supported target languages for auto-detected language' do
-      expect(languages[nil]).to eq %w(de es en)
+    it 'returns target languages for auto-detected language' do
+      expect(languages[nil]).to eq %w(de en es pt)
     end
   end
 
diff --git a/spec/models/status_spec.rb b/spec/models/status_spec.rb
index 1f6cfc796..1e58c6d0d 100644
--- a/spec/models/status_spec.rb
+++ b/spec/models/status_spec.rb
@@ -114,85 +114,6 @@ RSpec.describe Status, type: :model do
     end
   end
 
-  describe '#translatable?' do
-    before do
-      allow(TranslationService).to receive(:configured?).and_return(true)
-      allow(TranslationService).to receive(:configured).and_return(TranslationService.new)
-      allow(TranslationService.configured).to receive(:supported?).with('es', 'en').and_return(true)
-
-      subject.language = 'es'
-      subject.visibility = :public
-    end
-
-    context 'all conditions are satisfied' do
-      it 'returns true' do
-        expect(subject.translatable?).to be true
-      end
-    end
-
-    context 'translation service is not configured' do
-      it 'returns false' do
-        allow(TranslationService).to receive(:configured?).and_return(false)
-        allow(TranslationService).to receive(:configured).and_raise(TranslationService::NotConfiguredError)
-        expect(subject.translatable?).to be false
-      end
-    end
-
-    context 'status language is nil' do
-      it 'returns true' do
-        subject.language = nil
-        allow(TranslationService.configured).to receive(:supported?).with(nil, 'en').and_return(true)
-        expect(subject.translatable?).to be true
-      end
-    end
-
-    context 'status language is same as default locale' do
-      it 'returns false' do
-        subject.language = I18n.locale
-        expect(subject.translatable?).to be false
-      end
-    end
-
-    context 'status language is unsupported' do
-      it 'returns false' do
-        subject.language = 'af'
-        allow(TranslationService.configured).to receive(:supported?).with('af', 'en').and_return(false)
-        expect(subject.translatable?).to be false
-      end
-    end
-
-    context 'default locale is unsupported' do
-      it 'returns false' do
-        allow(TranslationService.configured).to receive(:supported?).with('es', 'af').and_return(false)
-        I18n.with_locale('af') do
-          expect(subject.translatable?).to be false
-        end
-      end
-    end
-
-    context 'default locale has region' do
-      it 'returns true' do
-        I18n.with_locale('en-GB') do
-          expect(subject.translatable?).to be true
-        end
-      end
-    end
-
-    context 'status text is blank' do
-      it 'returns false' do
-        subject.text = ' '
-        expect(subject.translatable?).to be false
-      end
-    end
-
-    context 'status visiblity is hidden' do
-      it 'returns false' do
-        subject.visibility = 'limited'
-        expect(subject.translatable?).to be false
-      end
-    end
-  end
-
   describe '#content' do
     it 'returns the text of the status if it is not a reblog' do
       expect(subject.content).to eql subject.text
diff --git a/spec/presenters/instance_presenter_spec.rb b/spec/presenters/instance_presenter_spec.rb
index 795abd8b4..2a1d668ce 100644
--- a/spec/presenters/instance_presenter_spec.rb
+++ b/spec/presenters/instance_presenter_spec.rb
@@ -3,7 +3,7 @@
 require 'rails_helper'
 
 describe InstancePresenter do
-  let(:instance_presenter) { InstancePresenter.new }
+  let(:instance_presenter) { described_class.new }
 
   describe '#description' do
     around do |example|