test: consolidate redundant compaction and tool tests

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-25 14:28:21 +08:00
1 parent 48443ece20
commit c406024b59
6 files changed
+39 -172

No files matched your search

-20
View File
@@ -259,26 +259,6 @@ class TestAgentLoopTTLParam:
class TestAutoCompact:
"""Test the _archive method."""
@pytest.mark.asyncio
async def test_is_expired_boundary(self, tmp_path):
"""Exactly at TTL boundary should be expired (>= not >)."""
loop = _make_loop(tmp_path, session_ttl_minutes=15)
ts = datetime.now() - timedelta(minutes=15)
assert loop.auto_compact._is_expired(ts) is True
ts2 = datetime.now() - timedelta(minutes=14, seconds=59)
assert loop.auto_compact._is_expired(ts2) is False
await loop.aclose()
@pytest.mark.asyncio
async def test_is_expired_string_timestamp(self, tmp_path):
"""_is_expired should parse ISO string timestamps."""
loop = _make_loop(tmp_path, session_ttl_minutes=15)
ts = (datetime.now() - timedelta(minutes=20)).isoformat()
assert loop.auto_compact._is_expired(ts) is True
assert loop.auto_compact._is_expired(None) is False
assert loop.auto_compact._is_expired("") is False
await loop.aclose()
@pytest.mark.asyncio
async def test_check_expired_only_archives_expired_sessions(self, tmp_path):
"""With multiple sessions, only the expired one should be archived."""
+12 -37
View File
@@ -58,45 +58,20 @@ def _add_turns(session: Session, turns: int, *, prefix: str = "msg") -> None:
session.add_message("assistant", f"{prefix} assistant {i}")
# ---------------------------------------------------------------------------
# __init__
# ---------------------------------------------------------------------------
def test_default_ttl_disables_idle_compaction():
sessions = MagicMock(spec=SessionManager)
sessions.list_sessions.return_value = [
{"key": "cli:idle", "updated_at": datetime.now() - timedelta(days=365)},
]
sessions.get_or_create.return_value = _make_session(
key="cli:idle", messages=[{"role": "user", "content": "pending"}],
)
ac = AutoCompact(sessions=sessions, consolidator=MagicMock())
schedule = MagicMock(side_effect=lambda pending: pending.close())
ac.check_expired(schedule, _runtime)
class TestInit:
"""Test AutoCompact.__init__ stores constructor arguments correctly."""
def test_stores_ttl(self):
"""_ttl should match session_ttl_minutes argument."""
ac = _make_autocompact(ttl=30)
assert ac._ttl == 30
def test_default_ttl_is_zero(self):
"""Default TTL should be 0."""
ac = _make_autocompact(ttl=0)
assert ac._ttl == 0
def test_archiving_set_is_empty(self):
"""_archiving should start as an empty set."""
ac = _make_autocompact()
assert ac._archiving == set()
def test_summaries_dict_is_empty(self):
"""_summaries should start as an empty dict."""
ac = _make_autocompact()
assert ac._summaries == {}
def test_stores_sessions_reference(self):
"""sessions attribute should reference the passed SessionManager."""
mock_sm = MagicMock(spec=SessionManager)
ac = _make_autocompact(sessions=mock_sm)
assert ac.sessions is mock_sm
def test_stores_consolidator_reference(self):
"""consolidator attribute should reference the passed Consolidator."""
mock_c = MagicMock()
ac = _make_autocompact(consolidator=mock_c)
assert ac.consolidator is mock_c
assert schedule.call_count == 0
# ---------------------------------------------------------------------------
-7
View File
@@ -902,13 +902,6 @@ class TestScratchpadInspection:
assert "step" in result
assert "2" in result
@pytest.mark.asyncio
async def test_inspect_nonexistent_still_returns_not_found(self):
tool = _make_tool()
result = await tool.execute(action="check", key="never_set_key_xyz")
assert "not found" in result
# ---------------------------------------------------------------------------
# sensitive sub-field blocking (Fix #3: API key leak prevention)
# ---------------------------------------------------------------------------