mirror of
https://github.com/BEDOLAGA-DEV/remnawave-bedolaga-telegram-bot.git
synced 2026-01-19 19:32:10 +00:00
Trim Platega descriptions by byte length
This commit is contained in:
@@ -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]:
|
||||
|
||||
25
tests/services/test_platega_service.py
Normal file
25
tests/services/test_platega_service.py
Normal 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
|
||||
Reference in New Issue
Block a user