|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +describe Jobs::DetectTranslateTopic do |
| 4 | + fab!(:topic) |
| 5 | + subject(:job) { described_class.new } |
| 6 | + |
| 7 | + let(:locales) { %w[en ja] } |
| 8 | + |
| 9 | + before do |
| 10 | + SiteSetting.translator_enabled = true |
| 11 | + SiteSetting.experimental_content_translation = true |
| 12 | + SiteSetting.automatic_translation_backfill_rate = 1 |
| 13 | + SiteSetting.automatic_translation_target_languages = locales.join("|") |
| 14 | + end |
| 15 | + |
| 16 | + it "does nothing when translator is disabled" do |
| 17 | + SiteSetting.translator_enabled = false |
| 18 | + DiscourseTranslator::TopicLocaleDetector.expects(:detect_locale).never |
| 19 | + DiscourseTranslator::TopicTranslator.expects(:translate).never |
| 20 | + |
| 21 | + job.execute({ topic_id: topic.id }) |
| 22 | + end |
| 23 | + |
| 24 | + it "does nothing when content translation is disabled" do |
| 25 | + SiteSetting.experimental_content_translation = false |
| 26 | + DiscourseTranslator::TopicLocaleDetector.expects(:detect_locale).never |
| 27 | + DiscourseTranslator::TopicTranslator.expects(:translate).never |
| 28 | + |
| 29 | + job.execute({ topic_id: topic.id }) |
| 30 | + end |
| 31 | + |
| 32 | + it "detects locale" do |
| 33 | + SiteSetting.translator_enabled = true |
| 34 | + DiscourseTranslator::TopicLocaleDetector.expects(:detect_locale).with(topic).once |
| 35 | + DiscourseTranslator::TopicTranslator.expects(:translate).twice |
| 36 | + |
| 37 | + job.execute({ topic_id: topic.id }) |
| 38 | + end |
| 39 | + |
| 40 | + it "skips bot topics" do |
| 41 | + topic.update!(user: Discourse.system_user) |
| 42 | + DiscourseTranslator::TopicTranslator.expects(:translate).never |
| 43 | + |
| 44 | + job.execute({ topic_id: topic.id }) |
| 45 | + end |
| 46 | + |
| 47 | + it "does not translate when no target languages are configured" do |
| 48 | + SiteSetting.automatic_translation_target_languages = "" |
| 49 | + DiscourseTranslator::TopicLocaleDetector.expects(:detect_locale).with(topic).returns("en") |
| 50 | + DiscourseTranslator::TopicTranslator.expects(:translate).never |
| 51 | + |
| 52 | + job.execute({ topic_id: topic.id }) |
| 53 | + end |
| 54 | + |
| 55 | + it "skips translating to the topic's language" do |
| 56 | + topic.update(locale: "en") |
| 57 | + DiscourseTranslator::TopicLocaleDetector.expects(:detect_locale).with(topic).returns("en") |
| 58 | + DiscourseTranslator::TopicTranslator.expects(:translate).with(topic, "en").never |
| 59 | + DiscourseTranslator::TopicTranslator.expects(:translate).with(topic, "ja").once |
| 60 | + |
| 61 | + job.execute({ topic_id: topic.id }) |
| 62 | + end |
| 63 | + |
| 64 | + it "handles translation errors gracefully" do |
| 65 | + topic.update(locale: "en") |
| 66 | + DiscourseTranslator::TopicLocaleDetector.expects(:detect_locale).with(topic).returns("en") |
| 67 | + DiscourseTranslator::TopicTranslator.expects(:translate).raises(StandardError.new("API error")) |
| 68 | + |
| 69 | + expect { job.execute({ topic_id: topic.id }) }.not_to raise_error |
| 70 | + end |
| 71 | +end |
0 commit comments