From f4b5549dd043d9f338a63bc16a9911a3a892fc63 Mon Sep 17 00:00:00 2001 From: Nat Date: Fri, 17 Jan 2025 16:06:57 +0800 Subject: [PATCH] FIX: Strip detection text before truncation --- app/services/discourse_translator/base.rb | 5 +++-- spec/services/base_spec.rb | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/services/discourse_translator/base.rb b/app/services/discourse_translator/base.rb index a931925d..ac78e99e 100644 --- a/app/services/discourse_translator/base.rb +++ b/app/services/discourse_translator/base.rb @@ -79,8 +79,9 @@ def self.strip_tags_for_detection(detection_text) end def self.text_for_detection(topic_or_post) - strip_tags_for_detection( - get_text(topic_or_post).truncate(DETECTION_CHAR_LIMIT, omission: nil), + strip_tags_for_detection(get_text(topic_or_post)).truncate( + DETECTION_CHAR_LIMIT, + omission: nil, ) end diff --git a/spec/services/base_spec.rb b/spec/services/base_spec.rb index c1609d0d..eb917c31 100644 --- a/spec/services/base_spec.rb +++ b/spec/services/base_spec.rb @@ -82,6 +82,11 @@ class EmptyTranslator < DiscourseTranslator::Base post.cooked = text expect(DiscourseTranslator::Base.text_for_detection(post)).to eq(text) end + + it "strips text before truncation" do + post.cooked = "" + "a" * 1000 + expect(DiscourseTranslator::Base.text_for_detection(post)).to eq("a" * 1000) + end end describe ".text_for_translation" do