discourse-translator/spec/services/amazon_spec.rb
Natalie Tay 7507795782
FIX: Don't double cook (#297)
This PR fixes the bug where we were double-cooking when saving `PostLocalization` when the AI provider is used.

The reason is due to the fact that we used to translate raw, then cook, and save the cooked. But now, we are required to save both the raw and cooked into `PostLocalization`.

This PR also splits `translate_translatable` into `translate_post` and `translate_topic`. Originally they were taking the same route, but it was proving to be a bit annoying because post uses raw and cooked, whereas topic uses title. It makes understanding any issues with a single model (e.g. Posts) easier to understand if it is a single code path. Lastly, PR also fixes an Amazon provider bug as it was never tested.
2025-05-13 14:39:01 +08:00

139 lines
3.6 KiB
Ruby

# frozen_string_literal: true
describe DiscourseTranslator::Provider::Amazon do
def new_translate_client
client = Aws::Translate::Client.new(stub_responses: true)
Aws::Translate::Client.stubs(:new).returns(client)
client
end
describe ".truncate" do
it "truncates text to 10000 bytes" do
text = "こんにちは" * (described_class::MAX_BYTES / 5)
truncated = described_class.truncate(text)
expect(truncated.bytesize).to be <= described_class::MAX_BYTES
expect(truncated.valid_encoding?).to eq(true)
expect(truncated[-1]).to eq ""
end
end
describe ".detect" do
let(:post) { Fabricate(:post) }
let(:text) { described_class.truncate(post.cooked) }
let(:detected_lang) { "en" }
it "should store the detected language" do
client = new_translate_client
client.stub_responses(
:translate_text,
{
translated_text: "translated text",
source_language_code: detected_lang,
target_language_code: "de",
},
)
expect(described_class.detect(post)).to eq(detected_lang)
expect(post.detected_locale).to eq(detected_lang)
end
it "should fail graciously when the cooked translated text is blank" do
post.raw = ""
expect(described_class.detect(post)).to be_nil
end
end
describe ".translate_post!" do
fab!(:post) { Fabricate(:post, raw: "rawraw rawrawraw", cooked: "coocoo coococooo") }
before do
post.set_detected_locale("en")
I18n.locale = :de
end
it "translates post with raw" do
client = new_translate_client
client.stub_responses(
:translate_text,
{
translated_text: "translated text",
source_language_code: "en",
target_language_code: "de",
},
)
expect(described_class.translate_post!(post, :de, { raw: true })).to eq("translated text")
end
it "translates post with cooked" do
client = new_translate_client
client.stub_responses(
:translate_text,
{
translated_text: "translated text",
source_language_code: "en",
target_language_code: "de",
},
)
expect(described_class.translate_post!(post, :de, { cooked: true })).to eq("translated text")
end
it "translates post with raw when unspecified" do
client = new_translate_client
client.stub_responses(
:translate_text,
{
translated_text: "translated text",
source_language_code: "en",
target_language_code: "de",
},
)
expect(described_class.translate_post!(post, :de)).to eq("translated text")
end
end
describe ".translate_topic!" do
fab!(:topic)
before do
topic.set_detected_locale("en")
I18n.locale = :de
end
it "translates topic's title" do
client = new_translate_client
client.stub_responses(
:translate_text,
{
translated_text: "translated text",
source_language_code: "en",
target_language_code: "de",
},
)
expect(described_class.translate_topic!(topic, :de)).to eq("translated text")
end
end
describe ".translate_text!" do
before { I18n.locale = :es }
it "translates the text" do
client = new_translate_client
client.stub_responses(
:translate_text,
{
translated_text: "translated text",
source_language_code: "en",
target_language_code: "es",
},
)
expect(described_class.translate_text!("derp")).to eq("translated text")
end
end
end