|
7 | 7 | from corecoder import Agent, LLM, Config, ALL_TOOLS, __version__ |
8 | 8 | from corecoder.context import ContextManager, estimate_tokens |
9 | 9 | from corecoder.session import save_session, load_session, list_sessions |
| 10 | +from corecoder.tools import get_tool |
10 | 11 |
|
11 | 12 |
|
12 | 13 | def test_version(): |
@@ -96,3 +97,50 @@ def test_session_not_found(): |
96 | 97 | def test_list_sessions(): |
97 | 98 | sessions = list_sessions() |
98 | 99 | 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