|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rails_helper" |
| 4 | + |
| 5 | +describe Jobs::TranslateCategories do |
| 6 | + let(:translator) { mock } |
| 7 | + |
| 8 | + def localize_all_categories(*locales) |
| 9 | + Category.all.each do |category| |
| 10 | + locales.each { |locale| Fabricate(:category_localization, category:, locale:, name: "x") } |
| 11 | + end |
| 12 | + end |
| 13 | + |
| 14 | + before do |
| 15 | + SiteSetting.translator_enabled = true |
| 16 | + SiteSetting.experimental_category_translation = true |
| 17 | + SiteSetting.automatic_translation_backfill_rate = 100 |
| 18 | + SiteSetting.automatic_translation_target_languages = "pt|zh_CN" |
| 19 | + |
| 20 | + DiscourseTranslator::Provider.stubs(:get).returns(translator) |
| 21 | + Jobs.run_immediately! |
| 22 | + end |
| 23 | + |
| 24 | + it "does nothing when translator is disabled" do |
| 25 | + SiteSetting.translator_enabled = false |
| 26 | + |
| 27 | + translator.expects(:translate_text!).never |
| 28 | + |
| 29 | + subject.execute({}) |
| 30 | + end |
| 31 | + |
| 32 | + it "does nothing when experimental_category_translation is disabled" do |
| 33 | + SiteSetting.experimental_category_translation = false |
| 34 | + |
| 35 | + translator.expects(:translate_text!).never |
| 36 | + |
| 37 | + subject.execute({}) |
| 38 | + end |
| 39 | + |
| 40 | + it "does nothing when no target languages are configured" do |
| 41 | + SiteSetting.automatic_translation_target_languages = "" |
| 42 | + |
| 43 | + translator.expects(:translate_text!).never |
| 44 | + |
| 45 | + subject.execute({}) |
| 46 | + end |
| 47 | + |
| 48 | + it "does nothing when no categories exist" do |
| 49 | + Category.destroy_all |
| 50 | + |
| 51 | + translator.expects(:translate_text!).never |
| 52 | + |
| 53 | + subject.execute({}) |
| 54 | + end |
| 55 | + |
| 56 | + it "translates categories to the configured locales" do |
| 57 | + number_of_categories = Category.count |
| 58 | + DiscourseTranslator::CategoryTranslator |
| 59 | + .expects(:translate) |
| 60 | + .with(is_a(Category), "pt") |
| 61 | + .times(number_of_categories) |
| 62 | + DiscourseTranslator::CategoryTranslator |
| 63 | + .expects(:translate) |
| 64 | + .with(is_a(Category), "zh_CN") |
| 65 | + .times(number_of_categories) |
| 66 | + |
| 67 | + subject.execute({}) |
| 68 | + end |
| 69 | + |
| 70 | + it "skips categories that already have localizations" do |
| 71 | + localize_all_categories("pt", "zh_CN") |
| 72 | + |
| 73 | + category1 = |
| 74 | + Fabricate(:category, name: "First Category", description: "First category description") |
| 75 | + Fabricate(:category_localization, category: category1, locale: "pt", name: "Primeira Categoria") |
| 76 | + |
| 77 | + # It should only translate to Chinese, not Portuguese |
| 78 | + DiscourseTranslator::CategoryTranslator.expects(:translate).with(category1, "pt").never |
| 79 | + DiscourseTranslator::CategoryTranslator.expects(:translate).with(category1, "zh_CN").once |
| 80 | + |
| 81 | + subject.execute({}) |
| 82 | + end |
| 83 | + |
| 84 | + it "continues from a specified category ID" do |
| 85 | + category1 = Fabricate(:category, name: "First", description: "First description") |
| 86 | + category2 = Fabricate(:category, name: "Second", description: "Second description") |
| 87 | + |
| 88 | + DiscourseTranslator::CategoryTranslator |
| 89 | + .expects(:translate) |
| 90 | + .with(category1, any_parameters) |
| 91 | + .never |
| 92 | + DiscourseTranslator::CategoryTranslator |
| 93 | + .expects(:translate) |
| 94 | + .with(category2, any_parameters) |
| 95 | + .twice |
| 96 | + |
| 97 | + subject.execute(from_category_id: category2.id) |
| 98 | + end |
| 99 | + |
| 100 | + it "handles translation errors gracefully" do |
| 101 | + localize_all_categories("pt", "zh_CN") |
| 102 | + |
| 103 | + category1 = Fabricate(:category, name: "First", description: "First description") |
| 104 | + DiscourseTranslator::CategoryTranslator |
| 105 | + .expects(:translate) |
| 106 | + .with(category1, "pt") |
| 107 | + .raises(StandardError.new("API error")) |
| 108 | + DiscourseTranslator::CategoryTranslator.expects(:translate).with(category1, "zh_CN").once |
| 109 | + |
| 110 | + expect { subject.execute({}) }.not_to raise_error |
| 111 | + end |
| 112 | + |
| 113 | + it "enqueues the next batch when there are more categories" do |
| 114 | + Jobs::TranslateCategories.const_set(:BATCH_SIZE, 1) |
| 115 | + |
| 116 | + Jobs |
| 117 | + .expects(:enqueue_in) |
| 118 | + .with(10.seconds, :translate_categories, from_category_id: any_parameters) |
| 119 | + .times(Category.count) |
| 120 | + |
| 121 | + subject.execute({}) |
| 122 | + |
| 123 | + # Reset the constant |
| 124 | + Jobs::TranslateCategories.send(:remove_const, :BATCH_SIZE) |
| 125 | + Jobs::TranslateCategories.const_set(:BATCH_SIZE, 50) |
| 126 | + end |
| 127 | +end |
0 commit comments