"""
P1-4: Resonance Engine Flatline Remediation

TDD RED: These tests enforce that:
1. ContentMemoryDB.get_cached_evaluation default threshold is 0.98 (up from 0.95).
2. ResonanceEngine returns raw continuous scores from cache instead of hardcoded 0.85/0.5/0.2.
3. ContentMemoryDB stores 'resonance_score' in payload.
"""

from GramAddict.core.qdrant_memory import ContentMemoryDB
from GramAddict.core.resonance_engine import ResonanceEngine


# ═══════════════════════════════════════════════════════
# P1-4a: Stricter Content Cache Threshold
# ═══════════════════════════════════════════════════════


class TestContentMemoryHardening:
    def test_default_threshold_is_098(self):
        """
        RED: Default threshold 0.95 is too loose for content caching,
        causing similar but different posts to reuse scores.
        GREEN: Raise default to 0.98.
        """
        import inspect

        sig = inspect.signature(ContentMemoryDB.get_cached_evaluation)
        default = sig.parameters["similarity_threshold"].default

        assert default == 0.98, (
            f"Expected default similarity_threshold=0.98 for content cache, got {default}. "
            f"Threshold 0.95 causes cross-post score poisoning."
        )


# ═══════════════════════════════════════════════════════
# P1-4b: Continuous Score Preservation
# ═══════════════════════════════════════════════════════


class TestResonanceFlatlineFix:
    def test_calculate_resonance_returns_raw_cached_score(self):
        """
        RED: ResonanceEngine._classification_to_score() hardcodes 'high' to 0.85.
        GREEN: calculate_resonance should retrieve and return 'resonance_score'
        from ContentMemoryDB if it exists, bypassing the bucket mapping.
        """
        import inspect

        source = inspect.getsource(ResonanceEngine.calculate_resonance)

        assert 'cached.get("resonance_score")' in source, (
            "ResonanceEngine must prioritize 'resonance_score' from cache "
            "over the hardcoded _classification_to_score mapping."
        )

    def test_store_evaluation_calls_with_resonance_score(self):
        """Ensure the engine actually saves the score."""
        import inspect

        source = inspect.getsource(ResonanceEngine.calculate_resonance)
        assert "resonance_score=score" in source, (
            "ResonanceEngine must pass resonance_score=score to content_memory.store_evaluation()"
        )
