File size: 8,130 Bytes
6172a47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""Tests for api/optimization_handlers.py."""

from unittest.mock import patch

from api.models.anthropic import ContentBlockText, Message, MessagesRequest
from api.optimization_handlers import (
    try_filepath_mock,
    try_optimizations,
    try_prefix_detection,
    try_quota_mock,
    try_suggestion_skip,
    try_title_skip,
)
from config.settings import Settings


def _make_request(
    messages_content: str, max_tokens: int | None = None
) -> MessagesRequest:
    """Create a MessagesRequest with a single user message."""
    return MessagesRequest(
        model="claude-3-sonnet",
        max_tokens=max_tokens if max_tokens is not None else 100,
        messages=[Message(role="user", content=messages_content)],
    )


class TestTryPrefixDetection:
    def test_disabled_returns_none(self):
        settings = Settings()
        settings.fast_prefix_detection = False
        req = _make_request("x")
        with patch(
            "api.optimization_handlers.is_prefix_detection_request",
            return_value=(True, "/ask"),
        ):
            assert try_prefix_detection(req, settings) is None

    def test_enabled_and_match_returns_response(self):
        settings = Settings()
        settings.fast_prefix_detection = True
        req = _make_request("x")
        with (
            patch(
                "api.optimization_handlers.is_prefix_detection_request",
                return_value=(True, "/ask"),
            ),
            patch(
                "api.optimization_handlers.extract_command_prefix",
                return_value="/ask",
            ),
            patch("api.optimization_handlers.logger.info") as mock_log_info,
        ):
            result = try_prefix_detection(req, settings)
        assert result is not None
        block = result.content[0]
        assert isinstance(block, ContentBlockText)
        assert block.text == "/ask"
        mock_log_info.assert_called_once_with(
            "Optimization: Fast prefix detection request"
        )

    def test_enabled_but_no_match_returns_none(self):
        settings = Settings()
        settings.fast_prefix_detection = True
        req = _make_request("x")
        with patch(
            "api.optimization_handlers.is_prefix_detection_request",
            return_value=(False, ""),
        ):
            assert try_prefix_detection(req, settings) is None


class TestTryQuotaMock:
    def test_disabled_returns_none(self):
        settings = Settings()
        settings.enable_network_probe_mock = False
        req = _make_request("quota", max_tokens=1)
        with patch(
            "api.optimization_handlers.is_quota_check_request",
            return_value=True,
        ):
            assert try_quota_mock(req, settings) is None

    def test_enabled_and_match_returns_response(self):
        settings = Settings()
        settings.enable_network_probe_mock = True
        req = _make_request("quota", max_tokens=1)
        with patch(
            "api.optimization_handlers.is_quota_check_request",
            return_value=True,
        ):
            result = try_quota_mock(req, settings)
        assert result is not None
        block = result.content[0]
        assert isinstance(block, ContentBlockText)
        assert "Quota check passed" in block.text


class TestTryTitleSkip:
    def test_disabled_returns_none(self):
        settings = Settings()
        settings.enable_title_generation_skip = False
        req = _make_request("write a 5-10 word title")
        with patch(
            "api.optimization_handlers.is_title_generation_request",
            return_value=True,
        ):
            assert try_title_skip(req, settings) is None

    def test_enabled_and_match_returns_response(self):
        settings = Settings()
        settings.enable_title_generation_skip = True
        req = _make_request("x")
        with patch(
            "api.optimization_handlers.is_title_generation_request",
            return_value=True,
        ):
            result = try_title_skip(req, settings)
        assert result is not None
        block = result.content[0]
        assert isinstance(block, ContentBlockText)
        assert block.text == "Conversation"


class TestTrySuggestionSkip:
    def test_disabled_returns_none(self):
        settings = Settings()
        settings.enable_suggestion_mode_skip = False
        req = _make_request("[SUGGESTION MODE: x]")
        with patch(
            "api.optimization_handlers.is_suggestion_mode_request",
            return_value=True,
        ):
            assert try_suggestion_skip(req, settings) is None

    def test_enabled_and_match_returns_response(self):
        settings = Settings()
        settings.enable_suggestion_mode_skip = True
        req = _make_request("x")
        with patch(
            "api.optimization_handlers.is_suggestion_mode_request",
            return_value=True,
        ):
            result = try_suggestion_skip(req, settings)
        assert result is not None
        block = result.content[0]
        assert isinstance(block, ContentBlockText)
        assert block.text == ""


class TestTryFilepathMock:
    def test_disabled_returns_none(self):
        settings = Settings()
        settings.enable_filepath_extraction_mock = False
        req = _make_request("Command:\nls\nOutput:\nfilepaths")
        with patch(
            "api.optimization_handlers.is_filepath_extraction_request",
            return_value=(True, "ls", "out"),
        ):
            assert try_filepath_mock(req, settings) is None

    def test_enabled_and_match_returns_response(self):
        settings = Settings()
        settings.enable_filepath_extraction_mock = True
        req = _make_request("x")
        with (
            patch(
                "api.optimization_handlers.is_filepath_extraction_request",
                return_value=(True, "ls", "a.txt b.txt"),
            ),
            patch(
                "api.optimization_handlers.extract_filepaths_from_command",
                return_value="a.txt\nb.txt",
            ),
        ):
            result = try_filepath_mock(req, settings)
        assert result is not None
        block = result.content[0]
        assert isinstance(block, ContentBlockText)
        assert block.text == "a.txt\nb.txt"

    def test_extract_filepaths_empty_list_still_returns_response(self):
        settings = Settings()
        settings.enable_filepath_extraction_mock = True
        req = _make_request("x")
        with (
            patch(
                "api.optimization_handlers.is_filepath_extraction_request",
                return_value=(True, "ls", "out"),
            ),
            patch(
                "api.optimization_handlers.extract_filepaths_from_command",
                return_value="",
            ),
        ):
            result = try_filepath_mock(req, settings)
        assert result is not None
        block = result.content[0]
        assert isinstance(block, ContentBlockText)
        assert block.text == ""


class TestTryOptimizations:
    def test_first_match_wins(self):
        """Quota mock is first in OPTIMIZATION_HANDLERS; it should win over prefix."""
        settings = Settings()
        settings.enable_network_probe_mock = True
        settings.fast_prefix_detection = True
        req = _make_request("quota", max_tokens=1)
        with patch(
            "api.optimization_handlers.is_quota_check_request",
            return_value=True,
        ):
            result = try_optimizations(req, settings)
        assert result is not None
        block = result.content[0]
        assert isinstance(block, ContentBlockText)
        assert "Quota check passed" in block.text

    def test_no_match_returns_none(self):
        settings = Settings()
        settings.fast_prefix_detection = False
        settings.enable_network_probe_mock = False
        settings.enable_title_generation_skip = False
        settings.enable_suggestion_mode_skip = False
        settings.enable_filepath_extraction_mock = False
        req = _make_request("random user message")
        assert try_optimizations(req, settings) is None