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