mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-08-02 11:23:27 +00:00
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)
141 lines
4.6 KiB
Ruby
141 lines
4.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
describe Jobs::TopicsLocaleDetectionBackfill do
|
|
fab!(:topic) { Fabricate(:topic, 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::TopicLocaleDetector.expects(:detect_locale).never
|
|
|
|
job.execute({})
|
|
end
|
|
|
|
it "does nothing when content translation is disabled" do
|
|
SiteSetting.ai_translation_enabled = false
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).never
|
|
|
|
job.execute({})
|
|
end
|
|
|
|
it "does nothing when there are no topics to detect" do
|
|
Topic.update_all(locale: "en")
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).never
|
|
|
|
job.execute({})
|
|
end
|
|
|
|
it "detects locale for topics with nil locale" do
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(topic).once
|
|
job.execute({})
|
|
end
|
|
|
|
it "detects most recently updated topics first" do
|
|
topic_2 = Fabricate(:topic, locale: nil)
|
|
topic_3 = Fabricate(:topic, locale: nil)
|
|
|
|
topic.update!(updated_at: 3.days.ago)
|
|
topic_2.update!(updated_at: 2.day.ago)
|
|
topic_3.update!(updated_at: 4.day.ago)
|
|
|
|
SiteSetting.ai_translation_backfill_rate = 1
|
|
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(topic_2).once
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(topic).never
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(topic_3).never
|
|
|
|
job.execute({})
|
|
end
|
|
|
|
it "skips bot topics" do
|
|
topic.update!(user: Discourse.system_user)
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(topic).never
|
|
|
|
job.execute({})
|
|
end
|
|
|
|
it "handles detection errors gracefully" do
|
|
DiscourseAi::Translation::TopicLocaleDetector
|
|
.expects(:detect_locale)
|
|
.with(topic)
|
|
.raises(StandardError.new("jiboomz"))
|
|
.once
|
|
|
|
expect { job.execute({}) }.not_to raise_error
|
|
end
|
|
|
|
it "logs a summary after running" do
|
|
DiscourseAi::Translation::TopicLocaleDetector.stubs(:detect_locale)
|
|
DiscourseAi::Translation::VerboseLogger.expects(:log).with(includes("Detected 1 topic locales"))
|
|
|
|
job.execute({})
|
|
end
|
|
|
|
describe "with public content limitation" do
|
|
fab!(:private_category) { Fabricate(:private_category, group: Group[:staff]) }
|
|
fab!(:public_topic) { Fabricate(:topic, locale: nil) }
|
|
fab!(:private_topic) { Fabricate(:topic, category: private_category, locale: nil) }
|
|
|
|
before do
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).at_least_once
|
|
|
|
SiteSetting.ai_translation_backfill_limit_to_public_content = true
|
|
end
|
|
|
|
it "only processes topics from public categories" do
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(public_topic).once
|
|
DiscourseAi::Translation::TopicLocaleDetector
|
|
.expects(:detect_locale)
|
|
.with(private_topic)
|
|
.never
|
|
|
|
job.execute({})
|
|
end
|
|
|
|
it "processes all topics when setting is disabled" do
|
|
SiteSetting.ai_translation_backfill_limit_to_public_content = false
|
|
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(public_topic).once
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(private_topic).once
|
|
|
|
job.execute({})
|
|
end
|
|
end
|
|
|
|
describe "with max age limit" do
|
|
fab!(:old_topic) { Fabricate(:topic, locale: nil, created_at: 10.days.ago) }
|
|
fab!(:new_topic) { Fabricate(:topic, locale: nil, created_at: 2.days.ago) }
|
|
|
|
before do
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).at_least_once
|
|
|
|
SiteSetting.ai_translation_backfill_max_age_days = 5
|
|
end
|
|
|
|
it "only processes topics within the age limit" do
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(new_topic).once
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(old_topic).never
|
|
|
|
job.execute({})
|
|
end
|
|
|
|
it "processes all topics when setting is disabled" do
|
|
SiteSetting.ai_translation_backfill_max_age_days = 0
|
|
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(new_topic).once
|
|
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(old_topic).once
|
|
|
|
job.execute({})
|
|
end
|
|
end
|
|
end
|