discourse-translator/spec/services/discourse_ai_spec.rb
Natalie Tay e42c0da8e7
DEV: Add the ability to translate a single piece of text (#281)
* DEV: Add the ability to translate a single piece of text

* rename method
2025-04-23 11:01:49 +08:00

92 lines
3 KiB
Ruby

# frozen_string_literal: true
describe DiscourseTranslator::Provider::DiscourseAi do
fab!(:post)
fab!(:topic)
before do
Fabricate(:fake_model).tap do |fake_llm|
SiteSetting.public_send("ai_helper_model=", "custom:#{fake_llm.id}")
end
SiteSetting.ai_helper_enabled = true
SiteSetting.translator_enabled = true
SiteSetting.translator_provider = "DiscourseAi"
end
describe ".language_supported?" do
it "returns true when detected language is different from i18n locale" do
I18n.stubs(:locale).returns(:xx)
expect(described_class.language_supported?("any-language")).to eq(true)
end
it "returns false when detected language is same base language as i18n locale" do
I18n.stubs(:locale).returns(:en_GB)
expect(described_class.language_supported?("en")).to eq(false)
end
end
describe ".detect!" do
it "returns the detected language" do
locale = "de"
DiscourseAi::Completions::Llm.with_prepared_responses([locale_json(locale)]) do
expect(DiscourseTranslator::Provider::DiscourseAi.detect!(post)).to eq locale
end
end
end
describe ".translate_translatable!" do
before do
post.set_detected_locale("de")
topic.set_detected_locale("de")
end
it "translates the post and returns [locale, translated_text]" do
DiscourseAi::Completions::Llm.with_prepared_responses(
[translation_json("some translated text")],
) do
translated_text = DiscourseTranslator::Provider::DiscourseAi.translate_translatable!(post)
expect(translated_text).to eq "<p>some translated text</p>"
end
end
it "translates the topic" do
allow(::DiscourseAi::TopicTranslator).to receive(:new).and_call_original
DiscourseAi::Completions::Llm.with_prepared_responses(
[translation_json("some translated text")],
) do
translated_text = DiscourseTranslator::Provider::DiscourseAi.translate_translatable!(topic)
expect(translated_text).to eq "some translated text"
end
end
it "sends the content for splitting and the split content for translation" do
post.update(raw: "#{"a" * 3000} #{"b" * 3000}")
DiscourseAi::Completions::Llm.with_prepared_responses(
%w[lol wut].map { |content| translation_json(content) },
) do
expect(
DiscourseTranslator::Provider::DiscourseAi.translate_translatable!(post),
).to eq "<p>lolwut</p>"
end
end
end
describe ".translate_text!" do
it "returns the translated text" do
DiscourseAi::Completions::Llm.with_prepared_responses(
[translation_json("some translated text")],
) do
translated_text = DiscourseTranslator::Provider::DiscourseAi.translate_text!("derp")
expect(translated_text).to eq "some translated text"
end
end
end
def locale_json(content)
{ locale: content }.to_json
end
def translation_json(content)
{ translation: content }.to_json
end
end