"""Unit tests for BranchAwarePromptBuilder — pure function, no DB needed."""
import pytest
from app.services.branch_aware_prompt_builder import BranchAwarePromptBuilder


def test_build_basic():
    """Basic build with no cross-branch context."""
    builder = BranchAwarePromptBuilder()
    result = builder.build(
        branch_messages=[
            {"role": "user", "content": "DNS not resolving"},
            {"role": "assistant", "content": "Let's check DNS config"},
        ],
        sibling_summaries="",
        session_context="Problem: DNS resolution failure. Domain: networking.",
        attachments=[],
        new_message="I ran nslookup and got timeout",
    )
    assert "system_base" in result
    assert "rag_context" in result
    assert "history" in result
    assert "new_message" in result
    assert "images" in result
    assert result["new_message"] == "I ran nslookup and got timeout"
    assert len(result["history"]) == 2


def test_build_with_cross_branch_context():
    """Cross-branch summaries go into rag_context, not system_base."""
    builder = BranchAwarePromptBuilder()
    sibling_ctx = "\n## Cross-Branch Context\n- **Network** [dead_end]: Network was fine."
    result = builder.build(
        branch_messages=[],
        sibling_summaries=sibling_ctx,
        session_context="Problem: test",
        attachments=[],
        new_message="test message",
    )
    assert "Cross-Branch Context" in result["rag_context"]
    assert "Cross-Branch Context" not in result["system_base"]


def test_build_with_images():
    """Image attachments are passed through."""
    builder = BranchAwarePromptBuilder()
    result = builder.build(
        branch_messages=[],
        sibling_summaries="",
        session_context="Problem: test",
        attachments=[{"media_type": "image/png", "data": "base64data"}],
        new_message="check this screenshot",
    )
    assert len(result["images"]) == 1
    assert result["images"][0]["media_type"] == "image/png"


def test_build_with_revival_context():
    """Revival context is prepended to rag_context."""
    builder = BranchAwarePromptBuilder()
    result = builder.build(
        branch_messages=[],
        sibling_summaries="",
        session_context="Problem: test",
        attachments=[],
        new_message="test",
        revival_context="New evidence: the error appears when VPN is active",
    )
    assert "New evidence" in result["rag_context"]


def test_history_filters_to_user_assistant():
    """Only user and assistant messages appear in history."""
    builder = BranchAwarePromptBuilder()
    result = builder.build(
        branch_messages=[
            {"role": "user", "content": "first"},
            {"role": "assistant", "content": "second"},
            {"role": "system", "content": "should be excluded"},
        ],
        sibling_summaries="",
        session_context="Problem: test",
        attachments=[],
        new_message="third",
    )
    assert len(result["history"]) == 2
