"""Tests for display helper functions in talk client.

Tests format_session_header and format_turn_metadata edge cases.
"""

import pytest

from anima.shell.cli_commands.talk_display import (
    format_session_header,
    format_turn_metadata,
)


class TestFormatSessionHeader:
    """Tests for format_session_header function."""

    def test_all_params_provided(self):
        """All parameters provided - full format."""
        result = format_session_header(
            model="gpt-5",
            persona_name="assistant",
            current_turn=3,
            max_turns=10,
        )
        assert result == "gpt-5 · assistant · turn 3/10"

    def test_model_none_omitted(self):
        """model=None omits model segment."""
        result = format_session_header(
            model=None,
            persona_name="assistant",
            current_turn=3,
            max_turns=10,
        )
        assert result == "assistant · turn 3/10"

    def test_max_turns_none_no_fraction(self):
        """max_turns=None omits fraction from turn segment."""
        result = format_session_header(
            model="gpt-5",
            persona_name="assistant",
            current_turn=3,
            max_turns=None,
        )
        assert result == "gpt-5 · assistant · turn 3"

    def test_model_none_max_turns_none(self):
        """Both model and max_turns are None."""
        result = format_session_header(
            model=None,
            persona_name="assistant",
            current_turn=1,
            max_turns=None,
        )
        assert result == "assistant · turn 1"

    def test_turn_numbering_starts_at_one(self):
        """Turn numbering starts at 1."""
        result = format_session_header(
            model="claude-4",
            persona_name="coder",
            current_turn=1,
            max_turns=5,
        )
        assert "turn 1/5" in result

    def test_large_turn_numbers(self):
        """Large turn numbers work correctly."""
        result = format_session_header(
            model="gpt-5",
            persona_name="assistant",
            current_turn=100,
            max_turns=500,
        )
        assert result == "gpt-5 · assistant · turn 100/500"

    def test_special_characters_in_persona_name(self):
        """Persona names with special characters work."""
        result = format_session_header(
            model="gpt-5",
            persona_name="assistant-v2.0",
            current_turn=1,
            max_turns=10,
        )
        assert "assistant-v2.0" in result

    def test_unicode_model_name(self):
        """Unicode characters in model name work."""
        result = format_session_header(
            model="中文模型",
            persona_name="assistant",
            current_turn=1,
            max_turns=10,
        )
        assert "中文模型" in result


class TestFormatTurnMetadata:
    """Tests for format_turn_metadata function."""

    def test_turn_and_tokens_provided(self):
        """Both turn and tokens provided - full format."""
        result = format_turn_metadata(turn=3, total_tokens=1247, console_width=80)
        assert "turn 3" in result
        assert "1,247 tok" in result

    def test_tokens_only(self):
        """Only tokens provided - no turn segment."""
        result = format_turn_metadata(turn=None, total_tokens=5000, console_width=80)
        assert "turn" not in result
        assert "5,000 tok" in result

    def test_turn_only(self):
        """Only turn provided - no token segment."""
        result = format_turn_metadata(turn=5, total_tokens=0, console_width=80)
        assert "turn 5" in result
        assert "tok" not in result

    def test_neither_provided_returns_empty(self):
        """Neither turn nor tokens returns empty string."""
        result = format_turn_metadata(turn=None, total_tokens=0, console_width=80)
        assert result == ""

    def test_right_alignment_80_chars(self):
        """Content is right-aligned to 80 characters."""
        result = format_turn_metadata(turn=3, total_tokens=1247, console_width=80)
        assert len(result) == 80

    def test_right_alignment_100_chars(self):
        """Content is right-aligned to 100 characters."""
        result = format_turn_metadata(turn=3, total_tokens=1247, console_width=100)
        assert len(result) == 100

    def test_right_alignment_40_chars(self):
        """Content is right-aligned to 40 characters."""
        result = format_turn_metadata(turn=3, total_tokens=1247, console_width=40)
        assert len(result) == 40

    def test_no_padding_when_content_exceeds_width(self):
        """No padding when content exceeds console width."""
        result = format_turn_metadata(turn=999, total_tokens=999999, console_width=10)
        # Should not pad, content is longer than console width
        assert len(result) >= len("turn 999 · 999,999 tok")

    def test_small_token_count(self):
        """Small token counts are formatted correctly."""
        result = format_turn_metadata(turn=1, total_tokens=1, console_width=80)
        assert "1 tok" in result

    def test_large_token_count(self):
        """Large token counts are formatted with commas."""
        result = format_turn_metadata(turn=1, total_tokens=1000000, console_width=80)
        assert "1,000,000 tok" in result

    def test_console_width_zero(self):
        """Console width of 0 returns unpadded content."""
        result = format_turn_metadata(turn=1, total_tokens=100, console_width=0)
        assert result == "turn 1 · 100 tok"

    def test_console_width_exact_match(self):
        """Console width exactly matches content length."""
        content = "turn 1 · 100 tok"
        result = format_turn_metadata(
            turn=1, total_tokens=100, console_width=len(content)
        )
        assert len(result) == len(content)
        assert result.strip() == content
