Créer un bot de support IA pour votre boutique en ligne
Bot de support en Python : code source
Python
1import pytest23from store_assistant.bot import split_message, to_plain_text4from store_assistant.config import ConfigError, Settings5from store_assistant.memory import ConversationMemory6from store_assistant.prompts import build_system_prompt7from store_assistant.ratelimit import RateLimiter8910class FakeClock:11 def __init__(self) -> None:12 self.now = 1000.01314 def __call__(self) -> float:15 return self.now161718def test_rate_limiter_window() -> None:19 clock = FakeClock()20 limiter = RateLimiter(limit=2, window_s=60, clock=clock)21 assert limiter.check(1) == 022 assert limiter.check(1) == 023 assert limiter.check(1) == pytest.approx(60)24 assert limiter.check(2) == 0 # other users are not affected25 clock.now += 6126 assert limiter.check(1) == 0272829def test_memory_evicts_least_recent_chat() -> None:30 memory = ConversationMemory(max_messages=4, max_chats=2)31 memory.add_exchange(1, "q", "a")32 memory.add_exchange(2, "q", "a")33 memory.add_exchange(1, "q2", "a2") # chat 1 becomes the most recent34 memory.add_exchange(3, "q", "a")35 assert memory.history(2) == []36 assert len(memory.history(1)) == 4373839def test_split_message_short() -> None:40 assert split_message("hello") == ["hello"]414243def test_split_message_respects_limit_and_boundaries() -> None:44 text = "\n\n".join(["word " * 300] * 5)45 chunks = split_message(text, limit=4096)46 assert all(len(c) <= 4096 for c in chunks)47 assert "".join(chunks).replace(" ", "").replace("\n", "") == text.replace(" ", "").replace("\n", "")484950def test_split_message_without_spaces() -> None:51 chunks = split_message("x" * 10_000, limit=4096)52 assert [len(c) for c in chunks] == [4096, 4096, 1808]535455def test_system_prompt_names_store() -> None:56 assert "Nour Shop" in build_system_prompt("Nour Shop")575859def test_settings_require_api_key(monkeypatch) -> None:60 monkeypatch.setattr("store_assistant.config.load_dotenv", lambda *a, **k: None)61 monkeypatch.delenv("DEVUP_API_KEY", raising=False)62 with pytest.raises(ConfigError):63 Settings.from_env()64 assert Settings.from_env(require_api_key=False).devup_api_key == ""656667def test_settings_validate_numbers(monkeypatch) -> None:68 monkeypatch.setattr("store_assistant.config.load_dotenv", lambda *a, **k: None)69 monkeypatch.setenv("DEVUP_API_KEY", "test")70 monkeypatch.setenv("RATE_LIMIT_PER_MINUTE", "zero")71 with pytest.raises(ConfigError):72 Settings.from_env()73 monkeypatch.setenv("RATE_LIMIT_PER_MINUTE", "5")74 monkeypatch.setenv("ADMIN_CHAT_ID", "-1001234")75 settings = Settings.from_env()76 assert settings.rate_limit_per_minute == 577 assert settings.admin_chat_id == -1001234787980def test_settings_require_telegram_token_for_bot(monkeypatch) -> None:81 monkeypatch.setattr("store_assistant.config.load_dotenv", lambda *a, **k: None)82 monkeypatch.setenv("DEVUP_API_KEY", "test")83 monkeypatch.delenv("TELEGRAM_BOT_TOKEN", raising=False)84 with pytest.raises(ConfigError):85 Settings.from_env(require_telegram=True)868788def test_to_plain_text() -> None:89 raw = "## Commande\nStatut : **expédiée**\n- Total : 9 550 DA\nCode #TRK-1"90 assert to_plain_text(raw) == "Commande\nStatut : expédiée\n- Total : 9 550 DA\nCode #TRK-1"