Skip to content

Commit e1200ea

Browse files
committed
test: add tests for cost estimation and file change tracking
1 parent 20eda97 commit e1200ea

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

tests/test_core.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from corecoder import Agent, LLM, Config, ALL_TOOLS, __version__
88
from corecoder.context import ContextManager, estimate_tokens
99
from corecoder.session import save_session, load_session, list_sessions
10+
from corecoder.tools import get_tool
1011

1112

1213
def test_version():
@@ -96,3 +97,50 @@ def test_session_not_found():
9697
def test_list_sessions():
9798
sessions = list_sessions()
9899
assert isinstance(sessions, list)
100+
101+
102+
# --- Cost estimation ---
103+
104+
def test_cost_estimation_known_model():
105+
from corecoder.llm import LLM
106+
llm = LLM.__new__(LLM)
107+
llm.model = "gpt-4o"
108+
llm.total_prompt_tokens = 1_000_000
109+
llm.total_completion_tokens = 500_000
110+
cost = llm.estimated_cost
111+
assert cost is not None
112+
assert cost == 2.5 + 5.0 # $2.5/M in + $10/M out * 0.5M
113+
114+
def test_cost_estimation_unknown_model():
115+
from corecoder.llm import LLM
116+
llm = LLM.__new__(LLM)
117+
llm.model = "some-custom-model"
118+
llm.total_prompt_tokens = 1000
119+
llm.total_completion_tokens = 500
120+
assert llm.estimated_cost is None
121+
122+
123+
# --- Changed files tracking ---
124+
125+
def test_edit_tracks_changed_files():
126+
from corecoder.tools.edit import _changed_files
127+
_changed_files.clear()
128+
edit = get_tool("edit_file")
129+
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
130+
f.write("aaa\nbbb\n")
131+
f.flush()
132+
edit.execute(file_path=f.name, old_string="aaa", new_string="zzz")
133+
assert any(f.name in p for p in _changed_files)
134+
os.unlink(f.name)
135+
_changed_files.clear()
136+
137+
138+
def test_write_tracks_changed_files():
139+
from corecoder.tools.edit import _changed_files
140+
_changed_files.clear()
141+
write = get_tool("write_file")
142+
path = tempfile.mktemp(suffix=".txt")
143+
write.execute(file_path=path, content="tracked\n")
144+
assert any("tracked" not in p and path.split("/")[-1] in p for p in _changed_files) or len(_changed_files) > 0
145+
os.unlink(path)
146+
_changed_files.clear()

0 commit comments

Comments
 (0)