Newer
Older
navi-1 / tests / clients / test_tool_result_renderer.py
@Eugene Sukhodolskiy Eugene Sukhodolskiy 4 hours ago 1 KB tui: minor correctness/perf fixes (Etap 5)
"""Tests for the generic tool result renderer."""

from __future__ import annotations

from rich.console import Console

from clients.terminal.tui.renderers.tool import ToolResultRenderer
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_tool_result_truncates_long_output() -> None:
    """A non-filesystem tool result with more than the line cap is truncated to
    the tail with a marker, so a huge output does not flood the chat bubble
    (regression for 2.P3)."""
    set_active_theme("gnexus-dark")
    renderer = ToolResultRenderer()
    big = "\n".join(f"line {i}" for i in range(300))
    msg = {
        "type": "tool_call",
        "tool": "some_tool",
        "result": big,
        "success": True,
    }
    panel = renderer.render(msg)
    text = _render_text(panel.renderable)
    assert "lines truncated]" in text
    # The tail is kept (last line present), the head is dropped (first line not).
    assert "line 299" in text
    assert "line 0" not in text


def test_tool_result_short_output_not_truncated() -> None:
    set_active_theme("gnexus-dark")
    renderer = ToolResultRenderer()
    msg = {
        "type": "tool_call",
        "tool": "some_tool",
        "result": "just a few\nlines",
        "success": True,
    }
    panel = renderer.render(msg)
    text = _render_text(panel.renderable)
    assert "truncated" not in text
    assert "just a few" in text