discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb
Natalie Tay 373e2305d6
FEATURE: Automatic translation and localization of posts, topics, categories (#1376)
Related: https://github.com/discourse/discourse-translator/pull/310

This commit includes all the jobs and event hooks to localize posts, topics, and categories.

A few notes:
- `feature_name: "translation"` because the site setting is `ai-translation` and module is `Translation`
- we will switch to proper ai-feature in the near future, and can consider using the persona_user as `localization.localizer_user_id`
- keeping things flat within the module for now as we will be moving to ai-feature soon and have to rearrange
- Settings renamed/introduced are:
  - ai_translation_backfill_rate (0)
  - ai_translation_backfill_limit_to_public_content (true)
  - ai_translation_backfill_max_age_days (5)
  - ai_translation_verbose_logs (false)
2025-05-29 17:28:06 +08:00

136 lines
4.5 KiB
Ruby

# frozen_string_literal: true
describe Jobs::PostsLocaleDetectionBackfill do
fab!(:post) { Fabricate(:post, locale: nil) }
subject(:job) { described_class.new }
before do
SiteSetting.discourse_ai_enabled = true
Fabricate(:fake_model).tap do |fake_llm|
SiteSetting.public_send("ai_translation_model=", "custom:#{fake_llm.id}")
end
SiteSetting.ai_translation_enabled = true
SiteSetting.ai_translation_backfill_rate = 100
end
it "does nothing when translator is disabled" do
SiteSetting.discourse_ai_enabled = false
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).never
job.execute({})
end
it "does nothing when content translation is disabled" do
SiteSetting.ai_translation_enabled = false
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).never
job.execute({})
end
it "does nothing when there are no posts to detect" do
Post.update_all(locale: "en")
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).never
job.execute({})
end
it "detects locale for posts with nil locale" do
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).once
job.execute({})
end
it "detects most recently updated posts first" do
post_2 = Fabricate(:post, locale: nil)
post_3 = Fabricate(:post, locale: nil)
post.update!(updated_at: 3.days.ago)
post_2.update!(updated_at: 2.day.ago)
post_3.update!(updated_at: 4.day.ago)
SiteSetting.ai_translation_backfill_rate = 1
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post_2).once
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).never
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post_3).never
job.execute({})
end
it "skips bot posts" do
post.update!(user: Discourse.system_user)
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).never
job.execute({})
end
it "handles detection errors gracefully" do
DiscourseAi::Translation::PostLocaleDetector
.expects(:detect_locale)
.with(post)
.raises(StandardError.new("jiboomz"))
.once
expect { job.execute({}) }.not_to raise_error
end
it "logs a summary after running" do
DiscourseAi::Translation::PostLocaleDetector.stubs(:detect_locale)
DiscourseAi::Translation::VerboseLogger.expects(:log).with(includes("Detected 1 post locales"))
job.execute({})
end
describe "with public content limitation" do
fab!(:private_category) { Fabricate(:private_category, group: Group[:staff]) }
fab!(:private_topic) { Fabricate(:topic, category: private_category) }
fab!(:private_post) { Fabricate(:post, topic: private_topic, locale: nil) }
before { SiteSetting.ai_translation_backfill_limit_to_public_content = true }
it "only processes posts from public categories" do
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).once
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(private_post).never
job.execute({})
end
it "processes all posts when setting is disabled" do
SiteSetting.ai_translation_backfill_limit_to_public_content = false
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).once
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(private_post).once
job.execute({})
end
end
describe "with max age limit" do
fab!(:old_post) { Fabricate(:post, locale: nil, created_at: 10.days.ago) }
fab!(:new_post) { Fabricate(:post, locale: nil, created_at: 2.days.ago) }
before { SiteSetting.ai_translation_backfill_max_age_days = 5 }
it "only processes posts within the age limit" do
# other posts
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).at_least_once
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(new_post).once
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(old_post).never
job.execute({})
end
it "processes all posts when setting is disabled" do
SiteSetting.ai_translation_backfill_max_age_days = 0
# other posts
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).at_least_once
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(new_post).once
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(old_post).once
job.execute({})
end
end
end