Skip to content

Commit 39b12ed

Browse files
author
Your Name
committed
test: add tests for compaction skipping in coder.generate
1 parent af0c214 commit 39b12ed

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

tests/commands/test_compaction.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import asyncio
2+
import pytest
3+
from unittest.mock import AsyncMock, MagicMock
4+
5+
# It's better to patch the Coder class where it's used if possible,
6+
# but for this test, we will instantiate it and mock its methods.
7+
from cecli.coders.base_coder import Coder
8+
from cecli.io import InputOutput
9+
10+
11+
@pytest.fixture
12+
def mock_io():
13+
"""Fixture for a mocked InputOutput object."""
14+
return MagicMock(spec=InputOutput)
15+
16+
17+
@pytest.fixture
18+
def mock_model():
19+
"""Fixture for a mocked model object."""
20+
model = MagicMock()
21+
model.info = {"max_input_tokens": 10000}
22+
# Mock the name attribute that is used in Coder.create
23+
model.name = "mock_model"
24+
model.edit_format = "wholefile"
25+
return model
26+
27+
28+
@pytest.mark.asyncio
29+
async def test_generate_skips_compaction_for_clear_command(mock_io, mock_model):
30+
"""
31+
Verify that compact_context_if_needed is NOT called for the /clear command.
32+
"""
33+
# Arrange
34+
coder = await Coder.create(main_model=mock_model, io=mock_io, edit_format="wholefile")
35+
coder.enable_context_compaction = True
36+
coder.compact_context_if_needed = AsyncMock()
37+
coder.run_one = AsyncMock()
38+
user_message = "/clear"
39+
40+
# Act
41+
await coder.generate(user_message, preproc=True)
42+
43+
# Assert
44+
coder.compact_context_if_needed.assert_not_called()
45+
coder.run_one.assert_called_once_with(user_message, True)
46+
47+
48+
@pytest.mark.asyncio
49+
async def test_generate_skips_compaction_for_exit_command(mock_io, mock_model):
50+
"""
51+
Verify that compact_context_if_needed is NOT called for the /exit command.
52+
"""
53+
# Arrange
54+
coder = await Coder.create(main_model=mock_model, io=mock_io, edit_format="wholefile")
55+
coder.enable_context_compaction = True
56+
coder.compact_context_if_needed = AsyncMock()
57+
coder.run_one = AsyncMock()
58+
user_message = "/exit"
59+
60+
# Act
61+
await coder.generate(user_message, preproc=True)
62+
63+
# Assert
64+
coder.compact_context_if_needed.assert_not_called()
65+
coder.run_one.assert_called_once_with(user_message, True)
66+
67+
68+
@pytest.mark.asyncio
69+
async def test_generate_skips_compaction_for_quit_command(mock_io, mock_model):
70+
"""
71+
Verify that compact_context_if_needed is NOT called for the /quit command.
72+
"""
73+
# Arrange
74+
coder = await Coder.create(main_model=mock_model, io=mock_io, edit_format="wholefile")
75+
coder.enable_context_compaction = True
76+
coder.compact_context_if_needed = AsyncMock()
77+
coder.run_one = AsyncMock()
78+
user_message = "/quit"
79+
80+
# Act
81+
await coder.generate(user_message, preproc=True)
82+
83+
# Assert
84+
coder.compact_context_if_needed.assert_not_called()
85+
coder.run_one.assert_called_once_with(user_message, True)
86+
87+
88+
@pytest.mark.asyncio
89+
async def test_generate_runs_compaction_for_regular_message(mock_io, mock_model):
90+
"""
91+
Verify that compact_context_if_needed IS called for a regular message.
92+
"""
93+
# Arrange
94+
coder = await Coder.create(main_model=mock_model, io=mock_io, edit_format="wholefile")
95+
coder.enable_context_compaction = True
96+
coder.compact_context_if_needed = AsyncMock()
97+
coder.run_one = AsyncMock()
98+
user_message = "This is a regular message"
99+
100+
# Act
101+
await coder.generate(user_message, preproc=True)
102+
103+
# Assert
104+
coder.compact_context_if_needed.assert_called_once()
105+
coder.run_one.assert_called_once_with(user_message, True)

0 commit comments

Comments
 (0)