discourse/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rb
Natalie Tay a444091293
FIX: Retranslate topic excerpts and avoid using post versions to indicate change (#36382)
There are three bugs fixed here:

## bug 1
`revisor.should_create_new_version?` does not trigger when an upload is
added. This means localizations will not be updated. This is now fixed
by not using `should_create_new_version` but `raw_changed` instead.

m/t/389685/15

## bug 2
When the topic's first post is updated, it does not update the
topic_localization excerpt. This is now fixed.

t/169370/3

## bug 3
When the topic title is edited (entry_point.rb), the
`detect_translate_topic` job is enqueued, however, localization does not
happen because it already exists. The job now is consistent and
relocalizes based on a daily quota.

t/169370/3
2025-12-02 22:45:11 +08:00

146 lines
4.4 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 "enqueues in grace period detect translate topic job if first post (excerpt) 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,
{ raw: post.raw + " Additional content." },
{ 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 true
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: 2)
post.revise(post.user, { raw: "new raw" })
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
end
end