"""
Tests for services/quality/pattern_leak.py
"""
import pytest

from services.quality.pattern_leak import check


class TestPromptLeak:
    def test_english_translation_prefix(self):
        r = check("Translation: Bonjour le monde")
        assert r["issue"] == "prompt_leak"

    def test_here_is_translation(self):
        r = check("Here is the translation: Bonjour")
        assert r["issue"] == "prompt_leak"

    def test_french_voici_traduction(self):
        r = check("Voici la traduction : Bonjour le monde")
        assert r["issue"] == "prompt_leak"

    def test_chinese_translation_prefix(self):
        r = check("翻译：你好世界")
        assert r["issue"] == "prompt_leak"

    def test_sure_heres_translation(self):
        r = check("Sure, here's the translation: Hello world")
        assert r["issue"] == "prompt_leak"

    def test_of_course_heres(self):
        r = check("Of course, here you go: Bonjour")
        assert r["issue"] == "prompt_leak"

    def test_markdown_bold_translation(self):
        r = check("**Translation** Bonjour le monde")
        assert r["issue"] == "prompt_leak"

    def test_normal_text_passes(self):
        r = check("Bonjour le monde, comment allez-vous?")
        assert r["issue"] is None

    def test_text_with_translation_word_in_middle_passes(self):
        r = check("Voici ce que je pense de la traduction de ce texte")
        # The word "traduction" appears but not as a prefix
        assert r["issue"] is None


class TestRepetitionHallucination:
    def test_long_repetition_detected(self):
        r = check("the the the the the the the the")
        assert r["issue"] == "repetition_hallucination"
        assert r["repetition_count"] >= 5

    def test_short_repetition_passes(self):
        # 4 times is within tolerance
        r = check("I think I think I think I think")
        assert r["issue"] is None

    def test_normal_text_passes(self):
        r = check("This is a normal sentence with no repetition issues")
        assert r["issue"] is None

    def test_mixed_repetition_in_long_text_passes(self):
        # Repetition is local, not a hallucination
        r = check("The cat sat on the mat. The cat was happy. The dog was sad.")
        assert r["issue"] is None

    def test_repetition_with_punctuation(self):
        # "the, the, the, the, the" should still be detected
        r = check("the, the, the, the, the, the")
        assert r["issue"] == "repetition_hallucination"


class TestEmptyAndEdgeCases:
    def test_empty_text(self):
        r = check("")
        assert r["issue"] is None

    def test_whitespace_only(self):
        r = check("   \n  \t  ")
        assert r["issue"] is None

    def test_none_input(self):
        r = check(None)
        assert r["issue"] is None
