Trim Platega descriptions by byte length

This commit is contained in:
Egor
2025-11-20 01:16:25 +03:00
parent ec71fa78c0
commit 3935813336
2 changed files with 46 additions and 10 deletions

View File

@@ -181,19 +181,30 @@ class PlategaService:
return None, raw_text
@staticmethod
def _sanitize_description(description: str, max_length: int) -> str:
"""Обрезает описание до максимально допустимой длины."""
def _sanitize_description(description: str, max_bytes: int) -> str:
"""Обрезает описание с учётом байтового лимита Platega."""
cleaned = (description or "").strip()
if max_length and len(cleaned) > max_length:
logger.debug(
"Platega description trimmed from %s to %s characters",
len(cleaned),
max_length,
)
return cleaned[:max_length]
if not max_bytes:
return cleaned
encoded = cleaned.encode("utf-8")
if len(encoded) <= max_bytes:
return cleaned
logger.debug(
"Platega description trimmed from %s to %s bytes",
len(encoded),
max_bytes,
)
trimmed_bytes = encoded[:max_bytes]
while True:
try:
return trimmed_bytes.decode("utf-8")
except UnicodeDecodeError:
trimmed_bytes = trimmed_bytes[:-1]
return cleaned
@staticmethod
def parse_expires_at(expires_in: Optional[str]) -> Optional[datetime]:

View File

@@ -0,0 +1,25 @@
import logging
import pytest
from app.services.platega_service import PlategaService
def test_sanitize_description_limits_utf8_bytes(caplog: pytest.LogCaptureFixture) -> None:
caplog.set_level(logging.DEBUG)
original = "Интернет-сервис - Пополнение баланса на 50 ₽ и ещё чуть-чуть"
trimmed = PlategaService._sanitize_description(original, 64)
assert len(trimmed.encode("utf-8")) <= 64
assert trimmed != original
assert any("trimmed" in record.message for record in caplog.records)
def test_sanitize_description_returns_clean_value() -> None:
original = " Обычное описание "
trimmed = PlategaService._sanitize_description(original, 64)
assert trimmed == "Обычное описание"
assert len(trimmed.encode("utf-8")) <= 64