discourse/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rb
Natalie Tay b7d7f99c04
FEATURE: Allow re-localization twice a day if post version has changed (#34023)
This commit is a continuation of
https://github.com/discourse/discourse-ai/pull/1422.

Previously, we entirely skipped / disallowed localization. With this PR,
- For topics, we will only enqueue the translate title job if the post
revisor indicates there is a title change
- For posts, we will only enqueue the translate post job if there is a
post version change

Both jobs will be enqueued with a delay of
`SiteSetting.editing_grace_period` or `5 minutes`, whichever is larger.
Each topic or post may be retranslated to a locale at a maximum of twice
a day.
2025-08-04 10:58:30 +08:00

131 lines
4.1 KiB
Ruby
Vendored

# frozen_string_literal: true
describe DiscourseAi::Translation::EntryPoint do
before do
assign_fake_provider_to(:ai_default_llm_model)
enable_current_plugin
SiteSetting.ai_translation_enabled = true
SiteSetting.content_localization_supported_locales = "en"
end
describe "upon post process cooked" do
it "enqueues detect post locale and translate post job" do
post =
PostCreator.create!(Fabricate(:user), raw: "post", title: "topic", skip_validations: true)
expect_job_enqueued(job: :detect_translate_post, args: { post_id: post.id })
end
it "does not enqueue if setting disabled" do
SiteSetting.ai_translation_enabled = false
post = Fabricate(:post)
CookedPostProcessor.new(post).post_process
expect(job_enqueued?(job: :detect_translate_post, args: { post_id: post.id })).to eq false
end
end
describe "upon topic created" do
it "enqueues detect topic locale and translate topic job" do
topic =
PostCreator.create!(
Fabricate(:admin),
raw: "post",
title: "topic",
skip_validations: true,
).topic
expect_job_enqueued(job: :detect_translate_topic, args: { topic_id: topic.id })
end
it "does not enqueue if setting disabled" do
SiteSetting.ai_translation_enabled = false
topic =
PostCreator.create!(
Fabricate(:admin),
raw: "post",
title: "topic",
skip_validations: true,
).topic
expect(job_enqueued?(job: :detect_translate_topic, args: { topic_id: topic.id })).to eq false
end
end
describe "upon topic edited" do
fab!(:post) { Fabricate(:post, post_number: 1) }
fab!(:non_first_post) { Fabricate(:post, post_number: 2) }
before do
enable_current_plugin
assign_fake_provider_to(:ai_default_llm_model)
end
it "enqueues in grace period detect translate topic job if title changed" do
freeze_time
SiteSetting.editing_grace_period = 10.minutes
SiteSetting.ai_translation_enabled = true
topic = post.topic
revisor = PostRevisor.new(post, topic)
revisor.revise!(post.user, { title: "A whole new hole" }, { skip_validations: true })
revisor.post_process_post
expect_job_enqueued(
job: :detect_translate_topic,
args: {
topic_id: topic.id,
},
at: 10.minutes.from_now,
)
expect(job_enqueued?(job: :detect_translate_post)).to eq false
end
it "does not enqueue detect translate topic job if title did not change" do
new_category = Fabricate(:category)
SiteSetting.ai_translation_enabled = true
topic = post.topic
post.revise(post.user, category_id: new_category.id)
expect(job_enqueued?(job: :detect_translate_topic, args: { topic_id: topic.id })).to eq false
expect(job_enqueued?(job: :detect_translate_post)).to eq false
end
it "does not enqueue if setting disabled" do
SiteSetting.ai_translation_enabled = false
expect(
job_enqueued?(job: :detect_translate_topic, args: { topic_id: post.topic_id }),
).to eq false
expect(job_enqueued?(job: :detect_translate_post)).to eq false
end
end
describe "upon post edited" do
it "enqueues detect translate post job in grace period" do
freeze_time
SiteSetting.editing_grace_period = 10.minutes
SiteSetting.ai_translation_enabled = true
post = Fabricate(:post, post_number: 1)
post.revise(post.user, { raw: "new raw" }, { force_new_version: true })
expect_job_enqueued(
job: :detect_translate_post,
args: {
post_id: post.id,
},
at: 10.minutes.from_now,
)
expect(job_enqueued?(job: :detect_translate_topic)).to eq false
end
it "does not enqueue detect translate post job if no new version of post" do
SiteSetting.ai_translation_enabled = true
post = Fabricate(:post, post_number: 1)
post.revise(post.user, { raw: post.raw + "A" })
expect(job_enqueued?(job: :detect_translate_post)).to eq false
expect(job_enqueued?(job: :detect_translate_topic)).to eq false
end
end
end