"""Tests for response_builder.build_response_parts."""

from ccgram.handlers.response_builder import build_response_parts
from ccgram.expandable_quote import EXPANDABLE_QUOTE_END as EXP_END
from ccgram.expandable_quote import EXPANDABLE_QUOTE_START as EXP_START


class TestBuildResponseParts:
    def test_user_message_has_emoji_prefix(self):
        parts = build_response_parts("hello", is_complete=True, role="user")
        assert len(parts) == 1
        assert "\U0001f464" in parts[0]

    def test_user_message_truncated_at_3000_chars(self):
        long_text = "a" * 4000
        parts = build_response_parts(long_text, is_complete=True, role="user")
        assert len(parts) == 1
        # Should contain truncation marker
        assert "\u2026" in parts[0]
        # Should be shorter than original
        assert len(parts[0]) < len(long_text)

    def test_thinking_content_truncated_at_500_chars(self):
        inner = "x" * 800
        text = f"{EXP_START}{inner}{EXP_END}"
        parts = build_response_parts(text, is_complete=True, content_type="thinking")
        assert len(parts) == 1
        assert "truncated" in parts[0].lower()

    def test_plain_text_single_part(self):
        parts = build_response_parts("short text", is_complete=True)
        assert len(parts) == 1

    def test_plain_text_multi_part_has_page_suffix(self):
        long_text = "\n".join(f"line {i} " + "padding" * 50 for i in range(200))
        parts = build_response_parts(long_text, is_complete=True)
        assert len(parts) > 1
        assert "1/" in parts[0]

    def test_expandable_quote_stays_atomic(self):
        inner = "thought " * 100
        text = f"{EXP_START}{inner}{EXP_END}"
        parts = build_response_parts(text, is_complete=False, content_type="thinking")
        assert len(parts) == 1

    def test_thinking_has_prefix(self):
        parts = build_response_parts(
            "some thought", is_complete=True, content_type="thinking"
        )
        assert len(parts) == 1
        assert "Thinking" in parts[0]

    def test_assistant_text_no_prefix(self):
        parts = build_response_parts(
            "hello world", is_complete=True, content_type="text", role="assistant"
        )
        assert len(parts) == 1
        assert "\U0001f464" not in parts[0]
        assert "Thinking" not in parts[0]

    def test_returns_raw_markdown(self):
        """Response builder should return raw markdown (entity conversion at send time)."""
        parts = build_response_parts("**bold** text", is_complete=True)
        assert len(parts) == 1
        # Raw markdown keeps ** markers (entity conversion happens at send time)
        assert "**bold**" in parts[0]

    def test_expandable_quote_sentinels_preserved(self):
        """Sentinels should pass through for conversion at send time."""
        text = f"{EXP_START}quote content{EXP_END}"
        parts = build_response_parts(text, is_complete=True)
        assert len(parts) == 1
        assert EXP_START in parts[0]
        assert EXP_END in parts[0]
