"""Tests for Timmy's self-knowledge capabilities (Issues #78 and #80)."""

from unittest.mock import MagicMock, patch

import pytest

from timmy.prompts import get_system_prompt


class TestSelfKnowledgeInPrompts:
    """Verify that system prompts contain self-knowledge sections."""

    @pytest.fixture(autouse=True)
    def mock_settings(self):
        """Mock config.settings.ollama_model for all tests."""
        # The settings import happens inside get_system_prompt function,
        # so we mock the config module's settings attribute
        mock_settings = MagicMock()
        mock_settings.ollama_model = "test-model"

        with patch("config.settings", mock_settings):
            yield mock_settings

    def test_full_prompt_contains_architecture_header(self, mock_settings):
        """Full prompt should contain 'ARCHITECTURE MAP' section."""
        prompt = get_system_prompt(tools_enabled=True)
        assert "ARCHITECTURE MAP" in prompt

    def test_full_prompt_contains_tool_safety_reference(self, mock_settings):
        """Full prompt should mention tool_safety.py specifically."""
        prompt = get_system_prompt(tools_enabled=True)
        assert "tool_safety.py" in prompt

    def test_full_prompt_contains_known_limitations(self, mock_settings):
        """Full prompt should contain 'KNOWN LIMITATIONS' section."""
        prompt = get_system_prompt(tools_enabled=True)
        assert "KNOWN LIMITATIONS" in prompt

    def test_full_prompt_contains_specific_limitation(self, mock_settings):
        """Full prompt should mention inability to run test suite autonomously."""
        prompt = get_system_prompt(tools_enabled=True)
        assert "Cannot run your own test suite" in prompt

    def test_lite_prompt_contains_architecture_header(self, mock_settings):
        """Lite prompt should contain 'ARCHITECTURE' section."""
        prompt = get_system_prompt(tools_enabled=False)
        assert "ARCHITECTURE" in prompt

    def test_lite_prompt_contains_known_limitations(self, mock_settings):
        """Lite prompt should also contain 'KNOWN LIMITATIONS' section."""
        prompt = get_system_prompt(tools_enabled=False)
        assert "KNOWN LIMITATIONS" in prompt

    def test_lite_prompt_is_shorter_than_full_prompt(self, mock_settings):
        """Lite prompt's self-knowledge section should be shorter than full prompt's."""
        full_prompt = get_system_prompt(tools_enabled=True)
        lite_prompt = get_system_prompt(tools_enabled=False)

        # Lite prompt should be shorter overall
        assert len(lite_prompt) < len(full_prompt), (
            f"Lite prompt ({len(lite_prompt)} chars) should be shorter than "
            f"full prompt ({len(full_prompt)} chars)"
        )

    def test_full_prompt_contains_architecture_layers(self, mock_settings):
        """Full prompt should describe architecture layers."""
        prompt = get_system_prompt(tools_enabled=True)

        # Should describe key architectural layers
        assert "Config layer" in prompt
        assert "Agent layer" in prompt
        assert "Memory layer" in prompt
        assert "agents.yaml" in prompt

    def test_full_prompt_contains_self_modification(self, mock_settings):
        """Full prompt should describe self-modification pathway."""
        prompt = get_system_prompt(tools_enabled=True)
        assert "SELF-MODIFICATION" in prompt
        assert "agents.yaml" in prompt
        assert "explain proposed changes" in prompt

    def test_lite_prompt_contains_self_modification(self, mock_settings):
        """Lite prompt should mention self-modification ability."""
        prompt = get_system_prompt(tools_enabled=False)
        assert "SELF-MODIFICATION" in prompt

    def test_full_prompt_contains_capabilities(self, mock_settings):
        """Full prompt should list current capabilities."""
        prompt = get_system_prompt(tools_enabled=True)
        assert "YOUR CURRENT CAPABILITIES" in prompt

    def test_lite_prompt_is_condensed(self, mock_settings):
        """Lite prompt should have condensed self-knowledge (no detailed descriptions)."""
        prompt = get_system_prompt(tools_enabled=False)

        # Should have the key sections but in condensed form
        assert "ARCHITECTURE" in prompt
        assert "YOUR CURRENT CAPABILITIES" in prompt
        assert "YOUR KNOWN LIMITATIONS" in prompt
