"""
TDD Test: Feed Loop Continuation After Stories
===============================================
Proves that DopamineEngine correctly handles feed exhaustion
without prematurely terminating the session.

The OLD test used `inspect.getsource()` to grep for string tokens
in production source code — pure theater. This replacement tests
ACTUAL behavior: boredom state transitions and session continuity.
"""

from GramAddict.core.dopamine_engine import DopamineEngine


class TestFeedLoopContinuation:
    """Tests that feed exhaustion triggers feed-switching, not session termination."""

    def test_high_boredom_triggers_feed_change_not_session_end(self):
        """
        When boredom reaches the threshold for feed change (>= 85),
        wants_to_change_feed() must return True BEFORE is_app_session_over()
        returns True. This ensures the main loop switches feeds instead of ending.
        """
        dopamine = DopamineEngine()
        dopamine.boredom = 85.0

        # At 85, the bot should want to change feed
        # But the session should NOT be over yet (that's at 100)
        wants_change = dopamine.wants_to_change_feed()
        session_over = dopamine.is_app_session_over()

        # The key invariant: feed change fires before session end
        assert isinstance(wants_change, bool), "wants_to_change_feed must return bool"
        assert session_over is False, (
            "Session should NOT be over at boredom 85! " "The main loop must switch feeds before declaring session end."
        )

    def test_boredom_reset_after_feed_switch_allows_continuation(self):
        """
        After a feed switch, boredom is reduced (multiplied by 0.2).
        The session must continue in the new feed.
        """
        dopamine = DopamineEngine()
        dopamine.boredom = 100.0

        # Session is over at 100
        assert dopamine.is_app_session_over() is True

        # Simulate the feed-switch boredom reduction from bot_flow.py
        dopamine.boredom = max(0.0, dopamine.boredom * 0.2)

        # Session should NO LONGER be over
        assert dopamine.boredom == 20.0
        assert dopamine.is_app_session_over() is False, "After boredom reset to 20%, the session must continue!"

    def test_zero_boredom_never_triggers_feed_change(self):
        """Fresh session with 0 boredom should never want to change feed."""
        dopamine = DopamineEngine()
        dopamine.boredom = 0.0

        result = dopamine.wants_to_change_feed()
        assert result is False, "Fresh session should not trigger feed change"
