diff --git a/tests/unit/python/utils/i18n/test_damage.py b/tests/unit/python/utils/i18n/test_damage.py new file mode 100644 index 0000000000..80c9ddfa67 --- /dev/null +++ b/tests/unit/python/utils/i18n/test_damage.py @@ -0,0 +1,46 @@ +"""Unit tests for the criterion that condemns an answer carrying its source. + +The shape is one the gateway produced across twenty languages: the English +source, a markdown rule, then a rendering. Every other criterion passed it, +because the rendering keeps the spans and the names and the separator is made +of characters the markup comparison does not count. +""" + +from __future__ import annotations + +import unittest +from typing import ClassVar + +from utils.i18n.damage import reason + + +class TestEchoAndContinue(unittest.TestCase): + """A translation holding its whole source is the server failing, not translating.""" + + SOURCE: ClassVar[str] = ( + "Corporate design injection for NGINX-served apps, unifying light and " + "dark theming from OKLCH design tokens" + ) + RENDERING: ClassVar[str] = ( + "Corporate-Design-Injection für NGINX-bediente Anwendungen, vereinheitlicht " + "helles und dunkles Theming aus OKLCH-Design-Tokens" + ) + + def test_an_answer_that_prepends_its_source_is_condemned(self) -> None: + answer = f"{self.SOURCE}\n---\n\n{self.RENDERING}" + self.assertEqual(reason(self.SOURCE, answer, "de"), "echo") + + def test_an_answer_that_appends_its_source_is_condemned(self) -> None: + answer = f"{self.RENDERING}\n\n{self.SOURCE}" + self.assertEqual(reason(self.SOURCE, answer, "de"), "echo") + + def test_a_rendering_that_drops_the_source_is_kept(self) -> None: + self.assertEqual(reason(self.SOURCE, self.RENDERING, "de"), "") + + def test_a_short_source_may_live_inside_a_longer_rendering(self) -> None: + source = "Mail" + self.assertEqual(reason(source, "Mail-Konto des Benutzers", "de"), "") + + +if __name__ == "__main__": + unittest.main() diff --git a/utils/i18n/damage.py b/utils/i18n/damage.py index 2f02e8ccd3..f868c62163 100644 --- a/utils/i18n/damage.py +++ b/utils/i18n/damage.py @@ -204,6 +204,27 @@ def collapsed(source: str, translation: str) -> bool: ) +def echoes(source: str, translation: str) -> bool: + """Return whether the translation carries its whole source and continues. + + A server that answers ``\\n---\\n`` passes every other + rule: the rendering it appended keeps the spans, the names and the markup, + and the separator is made of characters ``structure`` does not count. The + reader is then served the English text as its own translation. + + The floor is the one :func:`reason` applies to a plain echo, because the + risk it guards against is the same: a short source legitimately survives + inside a longer rendering, a sentence does not. + + Args: + source: the source message. + translation: what came back for it. + """ + if source not in translation: + return False + return sum(character.isalpha() for character in prose(source)) >= ECHO_FLOOR + + def structure(text: str) -> Counter: """Return the markup characters of ``text`` with their multiplicity. @@ -250,6 +271,8 @@ def reason(source: str, translation: str, language: str = "") -> str: if translation == source: alphabetic = sum(character.isalpha() for character in prose(source)) return "echo" if alphabetic >= ECHO_FLOOR else "" + if echoes(source, translation): + return "echo" if protected_spans(source) - protected_spans(translation): return "protected-span" if added_spans(source, translation):