File size: 4,299 Bytes
b5b9c2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""Tests for /compress <focus> — guided compression with focus topic.

Inspired by Claude Code's /compact <focus> feature.
"""

from unittest.mock import MagicMock, patch

from tests.cli.test_cli_init import _make_cli


def _make_history() -> list[dict[str, str]]:
    return [
        {"role": "user", "content": "one"},
        {"role": "assistant", "content": "two"},
        {"role": "user", "content": "three"},
        {"role": "assistant", "content": "four"},
    ]


def test_focus_topic_extracted_and_passed(capsys):
    """Focus topic is extracted from the command and passed to _compress_context."""
    shell = _make_cli()
    history = _make_history()
    compressed = [history[0], history[-1]]
    shell.conversation_history = history
    shell.agent = MagicMock()
    shell.agent.compression_enabled = True
    shell.agent._cached_system_prompt = ""
    shell.agent._compress_context.return_value = (compressed, "")

    def _estimate(messages):
        if messages is history:
            return 100
        return 50

    with patch("agent.model_metadata.estimate_messages_tokens_rough", side_effect=_estimate):
        shell._manual_compress("/compress database schema")

    output = capsys.readouterr().out
    assert 'focus: "database schema"' in output

    # Verify focus_topic was passed through
    shell.agent._compress_context.assert_called_once()
    call_kwargs = shell.agent._compress_context.call_args
    assert call_kwargs.kwargs.get("focus_topic") == "database schema"


def test_no_focus_topic_when_bare_command(capsys):
    """When no focus topic is provided, None is passed."""
    shell = _make_cli()
    history = _make_history()
    shell.conversation_history = history
    shell.agent = MagicMock()
    shell.agent.compression_enabled = True
    shell.agent._cached_system_prompt = ""
    shell.agent._compress_context.return_value = (list(history), "")

    with patch("agent.model_metadata.estimate_messages_tokens_rough", return_value=100):
        shell._manual_compress("/compress")

    shell.agent._compress_context.assert_called_once()
    call_kwargs = shell.agent._compress_context.call_args
    assert call_kwargs.kwargs.get("focus_topic") is None


def test_empty_focus_after_command_treated_as_none(capsys):
    """Trailing whitespace after /compress does not produce a focus topic."""
    shell = _make_cli()
    history = _make_history()
    shell.conversation_history = history
    shell.agent = MagicMock()
    shell.agent.compression_enabled = True
    shell.agent._cached_system_prompt = ""
    shell.agent._compress_context.return_value = (list(history), "")

    with patch("agent.model_metadata.estimate_messages_tokens_rough", return_value=100):
        shell._manual_compress("/compress   ")

    shell.agent._compress_context.assert_called_once()
    call_kwargs = shell.agent._compress_context.call_args
    assert call_kwargs.kwargs.get("focus_topic") is None


def test_focus_topic_printed_in_compression_banner(capsys):
    """The focus topic shows in the compression progress banner."""
    shell = _make_cli()
    history = _make_history()
    compressed = [history[0], history[-1]]
    shell.conversation_history = history
    shell.agent = MagicMock()
    shell.agent.compression_enabled = True
    shell.agent._cached_system_prompt = ""
    shell.agent._compress_context.return_value = (compressed, "")

    with patch("agent.model_metadata.estimate_messages_tokens_rough", return_value=100):
        shell._manual_compress("/compress API endpoints")

    output = capsys.readouterr().out
    assert 'focus: "API endpoints"' in output


def test_no_focus_prints_standard_banner(capsys):
    """Without focus, the standard banner (no focus: line) is printed."""
    shell = _make_cli()
    history = _make_history()
    compressed = [history[0], history[-1]]
    shell.conversation_history = history
    shell.agent = MagicMock()
    shell.agent.compression_enabled = True
    shell.agent._cached_system_prompt = ""
    shell.agent._compress_context.return_value = (compressed, "")

    with patch("agent.model_metadata.estimate_messages_tokens_rough", return_value=100):
        shell._manual_compress("/compress")

    output = capsys.readouterr().out
    assert "focus:" not in output
    assert "Compressing" in output