|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +describe Jobs::TranslatePosts do |
| 4 | + fab!(:post) |
| 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::PostTranslator.expects(:translate).never |
| 19 | + |
| 20 | + job.execute({}) |
| 21 | + end |
| 22 | + |
| 23 | + it "does nothing when content translation is disabled" do |
| 24 | + SiteSetting.experimental_content_translation = false |
| 25 | + DiscourseTranslator::PostTranslator.expects(:translate).never |
| 26 | + |
| 27 | + job.execute({}) |
| 28 | + end |
| 29 | + |
| 30 | + it "does nothing when no target languages are configured" do |
| 31 | + SiteSetting.automatic_translation_target_languages = "" |
| 32 | + DiscourseTranslator::PostTranslator.expects(:translate).never |
| 33 | + |
| 34 | + job.execute({}) |
| 35 | + end |
| 36 | + |
| 37 | + it "does nothing when there are no posts to translate" do |
| 38 | + Post.destroy_all |
| 39 | + DiscourseTranslator::PostTranslator.expects(:translate).never |
| 40 | + |
| 41 | + job.execute({}) |
| 42 | + end |
| 43 | + |
| 44 | + it "translates posts to the configured locales" do |
| 45 | + DiscourseTranslator::PostTranslator.expects(:translate).with(post, "en").at_least_once |
| 46 | + DiscourseTranslator::PostTranslator.expects(:translate).with(post, "ja").at_least_once |
| 47 | + |
| 48 | + job.execute({}) |
| 49 | + end |
| 50 | + |
| 51 | + it "skips posts that already have localizations" do |
| 52 | + Post.all.each do |post| |
| 53 | + Fabricate(:post_localization, post:, locale: "en") |
| 54 | + Fabricate(:post_localization, post:, locale: "ja") |
| 55 | + end |
| 56 | + DiscourseTranslator::PostTranslator.expects(:translate).never |
| 57 | + |
| 58 | + job.execute({}) |
| 59 | + end |
| 60 | + |
| 61 | + it "skips bot posts" do |
| 62 | + post.update!(user: Discourse.system_user) |
| 63 | + DiscourseTranslator::PostTranslator.expects(:translate).with(post, "en").never |
| 64 | + DiscourseTranslator::PostTranslator.expects(:translate).with(post, "ja").never |
| 65 | + |
| 66 | + job.execute({}) |
| 67 | + end |
| 68 | + |
| 69 | + it "handles translation errors gracefully" do |
| 70 | + DiscourseTranslator::PostTranslator |
| 71 | + .expects(:translate) |
| 72 | + .with(post, "en") |
| 73 | + .raises(StandardError.new("API error")) |
| 74 | + DiscourseTranslator::PostTranslator.expects(:translate).with(post, "ja").once |
| 75 | + |
| 76 | + expect { job.execute({}) }.not_to raise_error |
| 77 | + end |
| 78 | + |
| 79 | + it "logs a summary after translation" do |
| 80 | + DiscourseTranslator::PostTranslator.stubs(:translate) |
| 81 | + DiscourseTranslator::VerboseLogger.expects(:log).with(includes("Translated 1 posts to en, ja")) |
| 82 | + |
| 83 | + job.execute({}) |
| 84 | + end |
| 85 | +end |
0 commit comments