#!/usr/bin/env python3
"""Tests pour la CORRECTION 1 — Heuristique source croisee."""
import sys
import os
import unittest

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "agents_python"))
from media_fetcher import _is_trusted_source_domain


class TestIsTrustedSourceDomain(unittest.TestCase):
    """Tests unitaires pour _is_trusted_source_domain()."""

    def test_matching_domain_str_list(self):
        """Domaine present dans une liste de strings."""
        sources = [
            "https://psychomotricite-cenon.fr/marie-le-boiteux",
            "https://booknode.com/auteur/marie-le-boiteux",
        ]
        self.assertTrue(_is_trusted_source_domain(
            "https://psychomotricite-cenon.fr/page-accueil", sources))

    def test_matching_domain_dict_list(self):
        """Domaine present dans une liste de dicts."""
        sources = [
            {"url": "https://psychomotricite-cenon.fr/page", "domain": "psychomotricite-cenon.fr"},
            {"url": "https://booknode.com/auteur/x", "domain": "booknode.com"},
        ]
        self.assertTrue(_is_trusted_source_domain(
            "https://psychomotricite-cenon.fr/accueil", sources))

    def test_no_match(self):
        """Domaine absent des sources."""
        sources = [
            "https://psychomotricite-cenon.fr/page",
        ]
        self.assertFalse(_is_trusted_source_domain(
            "https://random-site.com/photo.jpg", sources))

    def test_www_normalization(self):
        """www vs sans www doivent matcher."""
        sources = [
            "https://humanite.fr/herve-bokobza-article",
        ]
        self.assertTrue(_is_trusted_source_domain(
            "https://www.humanite.fr/article", sources))

    def test_www_normalization_reverse(self):
        """Source avec www, image sans www."""
        sources = [
            "https://www.humanite.fr/article",
        ]
        self.assertTrue(_is_trusted_source_domain(
            "https://humanite.fr/photo.jpg", sources))

    def test_empty_sources(self):
        """Liste vide → False."""
        self.assertFalse(_is_trusted_source_domain(
            "https://example.com/photo.jpg", []))

    def test_none_sources(self):
        """None → False."""
        self.assertFalse(_is_trusted_source_domain(
            "https://example.com/photo.jpg", None))

    def test_empty_url(self):
        """URL vide → False."""
        self.assertFalse(_is_trusted_source_domain(
            "", ["https://example.com"]))

    def test_subdomain_no_match(self):
        """Sous-domaine different ne matche pas."""
        sources = [
            "https://blogs.mediapart.fr/herve-bokobza",
        ]
        # blogs.mediapart.fr != mediapart.fr
        self.assertFalse(_is_trusted_source_domain(
            "https://mediapart.fr/article", sources))

    def test_linkedin_domain_not_in_sources(self):
        """LinkedIn ne doit pas matcher si pas dans sources."""
        sources = [
            "https://lemonde.fr/article",
            "https://humanite.fr/article",
        ]
        self.assertFalse(_is_trusted_source_domain(
            "https://fr.linkedin.com/in/marie-boiteux", sources))

    def test_linkedin_domain_in_sources_still_rejected(self):
        """LinkedIn est generique : rejete meme si present dans sources."""
        sources = [
            "https://fr.linkedin.com/in/marie-le-boiteux",
            "https://psychomotricite-cenon.fr/page",
        ]
        self.assertFalse(_is_trusted_source_domain(
            "https://fr.linkedin.com/in/marie-boiteux-autre", sources))

    def test_facebook_generic_rejected(self):
        """Facebook est generique : rejete meme si present dans sources."""
        sources = [
            "https://facebook.com/marie.leboiteux",
        ]
        self.assertFalse(_is_trusted_source_domain(
            "https://facebook.com/marie.boiteux.photo", sources))

    def test_mixed_dict_and_empty_url(self):
        """Dicts avec url vide sont ignores."""
        sources = [
            {"url": "", "domain": ""},
            {"url": "https://psychomotricite-cenon.fr/page", "domain": "psychomotricite-cenon.fr"},
        ]
        self.assertTrue(_is_trusted_source_domain(
            "https://psychomotricite-cenon.fr/img.jpg", sources))


if __name__ == "__main__":
    unittest.main(verbosity=2)
