discourse/plugins/discourse-ai/spec/lib/translation/localizable_quota_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

67 lines
2.3 KiB
Ruby
Vendored

# frozen_string_literal: true
describe DiscourseAi::Translation::LocalizableQuota do
let(:test_class) do
Class.new do
include DiscourseAi::Translation::LocalizableQuota
def self.model_name
"post"
end
end
end
let(:model) { OpenStruct.new(id: 123) }
let(:locale) { "en" }
after { Discourse.redis.del(test_class.relocalize_key(model, locale)) }
describe ".has_relocalize_quota?" do
it "returns false if quota is at MAX_QUOTA_PER_DAY" do
Discourse.redis.set(test_class.relocalize_key(model, locale), 2, ex: 10)
expect(test_class.has_relocalize_quota?(model, locale)).to eq(false)
end
it "returns false if quota is above MAX_QUOTA_PER_DAY" do
Discourse.redis.set(test_class.relocalize_key(model, locale), 3, ex: 10)
expect(test_class.has_relocalize_quota?(model, locale)).to eq(false)
end
it "returns true if quota is below MAX_QUOTA_PER_DAY and increments quota" do
Discourse.redis.set(test_class.relocalize_key(model, locale), 1, ex: 10)
expect(test_class.has_relocalize_quota?(model, locale)).to eq(true)
expect(Discourse.redis.get(test_class.relocalize_key(model, locale))).to eq("2")
end
it "does not increment quota if skip_incr is true" do
Discourse.redis.set(test_class.relocalize_key(model, locale), 1, ex: 10)
test_class.has_relocalize_quota?(model, locale, skip_incr: true)
expect(Discourse.redis.get(test_class.relocalize_key(model, locale))).to eq("1")
end
it "initializes quota to 1 if not set before" do
test_class.has_relocalize_quota?(model, locale)
expect(Discourse.redis.get(test_class.relocalize_key(model, locale))).to eq("1")
end
it "sets expiry to 1 day when initializing quota" do
test_class.has_relocalize_quota?(model, locale)
ttl = Discourse.redis.ttl(test_class.relocalize_key(model, locale))
expect(ttl).to be_within(5).of(1.day.to_i)
end
it "preserves TTL when incrementing existing quota" do
Discourse.redis.set(test_class.relocalize_key(model, locale), 1, ex: 3600)
original_ttl = Discourse.redis.ttl(test_class.relocalize_key(model, locale))
test_class.has_relocalize_quota?(model, locale)
new_ttl = Discourse.redis.ttl(test_class.relocalize_key(model, locale))
expect(new_ttl).to be_within(2).of(original_ttl)
end
end
end