|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import asyncio |
4 | 3 | from unittest.mock import create_autospec |
5 | 4 |
|
6 | 5 | import pytest |
7 | 6 | from agents.models.interface import Model |
8 | 7 |
|
9 | 8 | from bot.agents import DEFAULT_INSTRUCTIONS |
10 | | -from bot.agents import MAX_CHATS |
11 | 9 | from bot.agents import MAX_TURNS |
12 | 10 | from bot.agents import OpenAIAgent |
13 | 11 |
|
@@ -154,74 +152,3 @@ def test_no_truncation_when_under_limit(self): |
154 | 152 | msgs = agent.get_messages(chat_id=100) |
155 | 153 | user_msgs = [m for m in msgs if m["role"] == "user"] |
156 | 154 | assert len(user_msgs) == 3 |
157 | | - |
158 | | - |
159 | | -class TestChatEviction: |
160 | | - def test_default_max_chats(self): |
161 | | - assert MAX_CHATS == 200 |
162 | | - |
163 | | - def test_evicts_oldest_chat_when_limit_exceeded(self, monkeypatch): |
164 | | - monkeypatch.setattr("bot.agents.MAX_CHATS", 3) |
165 | | - agent = OpenAIAgent(name="test") |
166 | | - |
167 | | - agent.set_messages(100, [{"role": "user", "content": "a"}]) |
168 | | - agent.set_messages(200, [{"role": "user", "content": "b"}]) |
169 | | - agent.set_messages(300, [{"role": "user", "content": "c"}]) |
170 | | - # At limit — all present |
171 | | - assert len(agent._conversations) == 3 |
172 | | - |
173 | | - # Adding a 4th should evict the oldest (100) |
174 | | - agent.set_messages(400, [{"role": "user", "content": "d"}]) |
175 | | - assert 100 not in agent._conversations |
176 | | - assert len(agent._conversations) == 3 |
177 | | - assert set(agent._conversations.keys()) == {200, 300, 400} |
178 | | - |
179 | | - def test_updating_existing_chat_does_not_evict(self, monkeypatch): |
180 | | - monkeypatch.setattr("bot.agents.MAX_CHATS", 2) |
181 | | - agent = OpenAIAgent(name="test") |
182 | | - |
183 | | - agent.set_messages(100, [{"role": "user", "content": "a"}]) |
184 | | - agent.set_messages(200, [{"role": "user", "content": "b"}]) |
185 | | - # Updating chat 100 should not trigger eviction |
186 | | - agent.set_messages(100, [{"role": "user", "content": "updated"}]) |
187 | | - assert len(agent._conversations) == 2 |
188 | | - assert agent.get_messages(100)[0]["content"] == "updated" |
189 | | - |
190 | | - def test_append_to_new_chat_triggers_eviction(self, monkeypatch): |
191 | | - monkeypatch.setattr("bot.agents.MAX_CHATS", 2) |
192 | | - agent = OpenAIAgent(name="test") |
193 | | - |
194 | | - agent.set_messages(100, [{"role": "user", "content": "a"}]) |
195 | | - agent.set_messages(200, [{"role": "user", "content": "b"}]) |
196 | | - agent.append_user_message(300, "c") |
197 | | - |
198 | | - assert 100 not in agent._conversations |
199 | | - assert len(agent._conversations) == 2 |
200 | | - |
201 | | - def test_accessing_chat_refreshes_its_position(self, monkeypatch): |
202 | | - monkeypatch.setattr("bot.agents.MAX_CHATS", 3) |
203 | | - agent = OpenAIAgent(name="test") |
204 | | - |
205 | | - agent.set_messages(100, [{"role": "user", "content": "a"}]) |
206 | | - agent.set_messages(200, [{"role": "user", "content": "b"}]) |
207 | | - agent.set_messages(300, [{"role": "user", "content": "c"}]) |
208 | | - |
209 | | - # Access chat 100 to refresh it (move to end) |
210 | | - agent.set_messages(100, [{"role": "user", "content": "refreshed"}]) |
211 | | - |
212 | | - # Now 200 is oldest — adding 400 should evict 200, not 100 |
213 | | - agent.set_messages(400, [{"role": "user", "content": "d"}]) |
214 | | - assert 200 not in agent._conversations |
215 | | - assert 100 in agent._conversations |
216 | | - |
217 | | - def test_eviction_also_cleans_up_lock(self, monkeypatch): |
218 | | - monkeypatch.setattr("bot.agents.MAX_CHATS", 2) |
219 | | - agent = OpenAIAgent(name="test") |
220 | | - |
221 | | - agent._locks[100] = asyncio.Lock() |
222 | | - agent.set_messages(100, [{"role": "user", "content": "a"}]) |
223 | | - agent._locks[200] = asyncio.Lock() |
224 | | - agent.set_messages(200, [{"role": "user", "content": "b"}]) |
225 | | - |
226 | | - agent.set_messages(300, [{"role": "user", "content": "c"}]) |
227 | | - assert 100 not in agent._locks |
0 commit comments