"""Tests for the context-compression summary renderer."""

from __future__ import annotations

from rich.console import Console

from clients.terminal.tui.renderers.summary import ContextSummaryRenderer
from clients.terminal.tui.themes import set_active_theme


def _render_text(renderable) -> str:
    console = Console(record=True, width=80, force_terminal=True, color_system=None)
    console.print(renderable)
    return console.export_text()


def test_summary_renderer_accepts_context_summary() -> None:
    set_active_theme("gnexus-dark")
    renderer = ContextSummaryRenderer()
    msg = {
        "type": "context_summary",
        "content": "Earlier the user asked to build a TUI client.",
        "messages_before": 42,
        "messages_after": 8,
    }
    assert renderer.accepts(msg)
    assert not renderer.accepts({"type": "status", "content": "x"})


def test_summary_renderer_header_has_counts() -> None:
    set_active_theme("gnexus-dark")
    renderer = ContextSummaryRenderer()
    msg = {
        "type": "context_summary",
        "content": "Earlier the user asked to build a TUI client.",
        "messages_before": 42,
        "messages_after": 8,
    }
    panel = renderer.render(msg)
    assert panel.title == "Context compressed: 42 → 8 messages"


def test_summary_renderer_renders_summary_text() -> None:
    set_active_theme("gnexus-dark")
    renderer = ContextSummaryRenderer()
    msg = {
        "type": "context_summary",
        "content": "## Summary\n- Built a TUI client\n- Added /compact",
        "messages_before": 42,
        "messages_after": 8,
    }
    text = _render_text(renderer.render(msg))
    assert "Summary" in text
    assert "Built a TUI client" in text


def test_summary_renderer_handles_missing_counts() -> None:
    set_active_theme("gnexus-dark")
    renderer = ContextSummaryRenderer()
    msg = {"type": "context_summary", "content": "text", "messages_before": None, "messages_after": None}
    panel = renderer.render(msg)
    assert panel.title == "Context compressed"