"""Tests for None guard on response.choices[0].message.content.strip().

OpenAI-compatible APIs return ``message.content = None`` when the model
responds with tool calls only or reasoning-only output (e.g. DeepSeek-R1,
Qwen-QwQ via OpenRouter with ``reasoning.enabled = True``).  Calling
``.strip()`` on ``None`` raises ``AttributeError``.

These tests verify that every call site handles ``content is None`` safely,
and that ``extract_content_or_reasoning()`` falls back to structured
reasoning fields when content is empty.
"""

import asyncio
import types

import pytest

from agent.auxiliary_client import extract_content_or_reasoning


# ── helpers ────────────────────────────────────────────────────────────────

def _make_response(content, **msg_attrs):
    """Build a minimal OpenAI-compatible ChatCompletion response stub.

    Extra keyword args are set as attributes on the message object
    (e.g. reasoning="...", reasoning_content="...", reasoning_details=[...]).
    """
    message = types.SimpleNamespace(content=content, tool_calls=None, **msg_attrs)
    choice = types.SimpleNamespace(message=message)
    return types.SimpleNamespace(choices=[choice])


def _run(coro):
    """Run an async coroutine synchronously."""
    return asyncio.get_event_loop().run_until_complete(coro)


# ── web_tools — LLM content processor (line 419) ─────────────────────────

class TestWebToolsProcessorContentNone:
    """tools/web_tools.py — _process_with_llm() return line"""

    def test_none_content_raises_before_fix(self):
        response = _make_response(None)

        with pytest.raises(AttributeError):
            response.choices[0].message.content.strip()

    def test_none_content_safe_with_or_guard(self):
        response = _make_response(None)

        content = (response.choices[0].message.content or "").strip()
        assert content == ""


# ── web_tools — synthesis/summarization (line 538) ────────────────────────

class TestWebToolsSynthesisContentNone:
    """tools/web_tools.py — synthesize_content() final_summary line"""

    def test_none_content_raises_before_fix(self):
        response = _make_response(None)

        with pytest.raises(AttributeError):
            response.choices[0].message.content.strip()

    def test_none_content_safe_with_or_guard(self):
        response = _make_response(None)

        content = (response.choices[0].message.content or "").strip()
        assert content == ""


# ── vision_tools (line 350) ───────────────────────────────────────────────

class TestVisionToolsContentNone:
    """tools/vision_tools.py — analyze_image() analysis extraction"""

    def test_none_content_raises_before_fix(self):
        response = _make_response(None)

        with pytest.raises(AttributeError):
            response.choices[0].message.content.strip()

    def test_none_content_safe_with_or_guard(self):
        response = _make_response(None)

        content = (response.choices[0].message.content or "").strip()
        assert content == ""


# ── skills_guard (line 963) ───────────────────────────────────────────────

class TestSkillsGuardContentNone:
    """tools/skills_guard.py — _llm_audit_skill() llm_text extraction"""

    def test_none_content_raises_before_fix(self):
        response = _make_response(None)

        with pytest.raises(AttributeError):
            response.choices[0].message.content.strip()

    def test_none_content_safe_with_or_guard(self):
        response = _make_response(None)

        content = (response.choices[0].message.content or "").strip()
        assert content == ""


# ── integration: verify the actual source lines are guarded ───────────────

class TestSourceLinesAreGuarded:
    """Read the actual source files and verify the fix is applied.

    These tests will FAIL before the fix (bare .content.strip()) and
    PASS after ((.content or "").strip()).
    """

    @staticmethod
    def _read_file(rel_path: str) -> str:
        import os
        base = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
        with open(os.path.join(base, rel_path)) as f:
            return f.read()

    def test_web_tools_guarded(self):
        src = self._read_file("tools/web_tools.py")
        assert ".message.content.strip()" not in src, (
            "tools/web_tools.py still has unguarded "
            ".content.strip() — apply `(... or \"\").strip()` guard"
        )

    def test_vision_tools_guarded(self):
        src = self._read_file("tools/vision_tools.py")
        assert ".message.content.strip()" not in src, (
            "tools/vision_tools.py still has unguarded "
            ".content.strip() — apply `(... or \"\").strip()` guard"
        )

    def test_skills_guard_guarded(self):
        src = self._read_file("tools/skills_guard.py")
        assert ".message.content.strip()" not in src, (
            "tools/skills_guard.py still has unguarded "
            ".content.strip() — apply `(... or \"\").strip()` guard"
        )


# ── extract_content_or_reasoning() ────────────────────────────────────────

class TestExtractContentOrReasoning:
    """agent/auxiliary_client.py — extract_content_or_reasoning()"""

    def test_normal_content_returned(self):
        response = _make_response("  Hello world  ")
        assert extract_content_or_reasoning(response) == "Hello world"

    def test_none_content_returns_empty(self):
        response = _make_response(None)
        assert extract_content_or_reasoning(response) == ""


    def test_think_blocks_stripped_with_remaining_content(self):
        response = _make_response("<think>internal reasoning</think>The answer is 42.")
        assert extract_content_or_reasoning(response) == "The answer is 42."

    def test_think_only_content_falls_back_to_reasoning_field(self):
        """When content is only think blocks, fall back to structured reasoning."""
        response = _make_response(
            "<think>some reasoning</think>",
            reasoning="The actual reasoning output",
        )
        assert extract_content_or_reasoning(response) == "The actual reasoning output"


    def test_content_preferred_over_reasoning(self):
        """When both content and reasoning exist, content wins."""
        response = _make_response("Actual answer", reasoning="Internal reasoning")
        assert extract_content_or_reasoning(response) == "Actual answer"
