from datetime import datetime from application.usecases.run_watch_cycle import RunWatchCycleUsecase from domain.entities import Article from domain.matcher import MatchingService from tests.fake_article_source import FakeArticleSource from tests.fake_notifier import FakeNotifier # Les Fixtures sont des objets ou des fonctions qui fournissent un contexte pour les tests. def make_article(title: str) -> Article: return Article( title=title, url="http://example.com/article", published_at= datetime(2024, 6, 1, 12, 0, 0), source="Example News" ) def make_usecase(articles: list[Article]) -> tuple[RunWatchCycleUsecase, FakeNotifier]: source = FakeArticleSource(articles) notifier = FakeNotifier() service = MatchingService() usecase = RunWatchCycleUsecase( source=source, notifier=notifier, matcher=service ) return usecase, notifier def test_article_matching_keyword_triggers_alert(): usecase, notifier = make_usecase([ make_article("Breaking: New Python Version Released") ]) result = usecase.execute(keywords=["python"]) assert result.alerts_sent == 1 assert result.processed == 1 assert "python" in notifier.alerts_sent[0].matched_keywords def test_article_without_keyword_triggers_no_alert(): usecase, notifier = make_usecase([ make_article("Sports Update: Local Team Wins Championship") ]) result = usecase.execute(keywords=["Python"]) assert result.alerts_sent == 0 assert result.processed == 1 assert len(notifier.alerts_sent) == 0 def test_matching_is_case_insensitive(): usecase, _ = make_usecase([ make_article("Breaking: New PYTHON Version Released") ]) result = usecase.execute(keywords=["python"]) assert result.alerts_sent == 1 def test_only_matching_articles_trigger_alerts(): articles = [ make_article("Python 3.13 : les nouveautés"), make_article("Recette de brioche au beurre"), make_article("Architecture hexagonale en Python"), make_article("Les chats dorment 16h par jour"), ] use_case, notifier = make_usecase(articles) result = use_case.execute(keywords=["python"]) assert result.processed == 4 assert result.alerts_sent == 2