codeplain 0.4.3.dev61__py3-none-any.whl → 0.4.3.dev62__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tests/test_agent_tools.py CHANGED
@@ -13,6 +13,11 @@ from render_machine.render_types import UnitTestsRunningContext
13
13
  class FakeRenderContext(SimpleNamespace):
14
14
  unit_tests_agent_session = RenderContext.unit_tests_agent_session
15
15
 
16
+ @property
17
+ def plain_module(self):
18
+ # The rendered modules folder is the build folder's parent, as in plain_modules/<module>.
19
+ return SimpleNamespace(build_folder=os.path.dirname(self.build_folder), all_required_modules=[])
20
+
16
21
 
17
22
  @pytest.fixture
18
23
  def project(tmp_path, monkeypatch):
@@ -133,6 +138,8 @@ def test_file_change_invalidates_verified_pass_and_read_cache(project):
133
138
  repeat = agent_tools.execute_calls([{"id": "2", "name": "read_file", "args": {"file_path": "app.py"}}], project.rc)
134
139
  assert first[0]["output"].startswith("1: def add")
135
140
  assert repeat[0]["output"].startswith("Same call as an earlier one")
141
+ # the earlier result is repeated in full: compaction may have removed it from the conversation
142
+ assert repeat[0]["output"] == agent_tools.REPEATED_CALL_NOTE + first[0]["output"]
136
143
 
137
144
  agent_tools.write_file({"file_path": "other.py", "content": "y = 1\n"}, project.rc)
138
145
  assert context.verified_passing is False
@@ -164,3 +171,176 @@ def test_bound_truncates_long_lines_and_large_output():
164
171
  bounded = agent_tools._bound("a" * (agent_tools.MAX_LINE_CHARS + 5) + "\n" + "b\n" * 40_000)
165
172
  assert "[line truncated]" in bounded and "[truncated" in bounded
166
173
  assert len(bounded) < agent_tools.MAX_OUTPUT_CHARS + 200
174
+
175
+
176
+ def test_grep_takes_python_regex_syntax(project):
177
+ log = project.build / "conformance.script_output"
178
+ log.write_text("Master password: ***\nName: alice\nnoise\nFAILED test_add\nTests run: 12\n")
179
+ out = agent_tools.grep(
180
+ {"pattern": r"Master password|Name:|FAILED|run: \d+", "file_path": str(log)}, project.rc
181
+ ).splitlines()
182
+ assert out == [
183
+ "conformance.script_output:1:Master password: ***",
184
+ "conformance.script_output:2:Name: alice",
185
+ "conformance.script_output:4:FAILED test_add",
186
+ "conformance.script_output:5:Tests run: 12",
187
+ ]
188
+ assert agent_tools.grep({"pattern": "(?i)failed", "file_path": str(log)}, project.rc).endswith("FAILED test_add")
189
+ assert agent_tools.grep({"pattern": "foo(", "file_path": str(log)}, project.rc).startswith(
190
+ "Error: invalid regular expression"
191
+ )
192
+
193
+
194
+ def test_grep_separates_context_groups_and_skips_binary_and_excluded_files(project):
195
+ (project.build / "long.py").write_text("\n".join(["hit"] + ["x"] * 10 + ["hit"]) + "\n")
196
+ (project.build / "blob.bin").write_bytes(b"hit\0\0")
197
+ (project.build / "node_modules").mkdir()
198
+ (project.build / "node_modules" / "dep.py").write_text("hit\n")
199
+ out = agent_tools.grep({"pattern": "^hit$", "context_lines": 1}, project.rc).splitlines()
200
+ assert out == ["long.py:1:hit", "long.py-2-x", "--", "long.py-11-x", "long.py:12:hit"]
201
+
202
+
203
+ def test_edit_names_the_lines_it_removed(project):
204
+ pom = project.build / "pom.xml"
205
+ pom.write_text("<project>\n <dependencies/>\n</project>\n")
206
+ out = agent_tools.edit_file(
207
+ {
208
+ "file_path": "pom.xml",
209
+ "search": " <dependencies/>\n</project>\n",
210
+ "replace": " <dependencies/>\n <build/>\n",
211
+ },
212
+ project.rc,
213
+ )
214
+ assert "This edit removed these lines: `</project>`" in out
215
+ kept = agent_tools.edit_file(
216
+ {"file_path": "pom.xml", "search": " <build/>\n", "replace": " <build/>\n</project>\n"}, project.rc
217
+ )
218
+ assert "removed these lines" not in kept
219
+
220
+
221
+ def test_unit_tests_agent_reads_and_greps_other_modules_but_writes_only_its_build_folder(project):
222
+ other = project.build.parent / "base" / "code" / "lib.py"
223
+ other.parent.mkdir(parents=True)
224
+ other.write_text("def helper():\n return 1\n")
225
+ assert agent_tools.read_file({"file_path": str(other)}, project.rc).startswith("1: def helper")
226
+ hits = agent_tools.grep({"pattern": "def (helper|sub)", "file_path": str(project.build.parent)}, project.rc)
227
+ assert "base/code/lib.py:1:def helper():" in hits and "def sub(a, b):" in hits
228
+ denied = agent_tools.write_file({"file_path": str(other), "content": "x\n"}, project.rc)
229
+ assert denied.startswith("Error: write access denied") and other.read_text().startswith("def helper")
230
+
231
+
232
+ def test_a_pattern_that_finds_nothing_says_where_to_look(project):
233
+ result = agent_tools.ls_files({"pattern": "**/tests/**/pom.xml"}, project.rc)
234
+ assert result.startswith("No files match")
235
+ assert "Relative paths resolve against" in result and "`tests`" in result
236
+
237
+
238
+ def test_a_search_text_that_is_not_found_gets_the_closest_lines(project):
239
+ test_file = project.build / "ReaderTest.java"
240
+ test_file.write_text('class ReaderTest {\n Path file = writeCsv("id\\\\n7\\\\n");\n}\n')
241
+ out = agent_tools.edit_file(
242
+ {"file_path": "ReaderTest.java", "search": 'Path file = writeCsv("id\\n7\\n");', "replace": "x"}, project.rc
243
+ )
244
+ assert "found 0 times" in out
245
+ assert '2: Path file = writeCsv("id\\\\n7\\\\n");' in out
246
+ assert "class ReaderTest" not in out
247
+ unrelated = agent_tools.edit_file(
248
+ {"file_path": "ReaderTest.java", "search": "completely different text here", "replace": "x"}, project.rc
249
+ )
250
+ assert "No line of the file resembles it" in unrelated
251
+
252
+
253
+ def _script(path, body):
254
+ path.write_text("#!/bin/bash\n" + body)
255
+ path.chmod(0o755)
256
+ return str(path)
257
+
258
+
259
+ def test_the_conformance_fixer_runs_the_tests_and_prepares_only_after_a_change(project, tmp_path):
260
+ prepared = tmp_path / "prepared.txt"
261
+ tests_folder = tmp_path / "conformance" / "add"
262
+ tests_folder.mkdir(parents=True)
263
+ rc = SimpleNamespace(
264
+ build_folder=str(project.build),
265
+ prepare_environment_script=_script(tmp_path / "prepare.sh", f'echo x >> "{prepared}"\n'),
266
+ conformance_tests_script=_script(
267
+ tmp_path / "run.sh", 'grep -q "a + b" "$1/app.py" || { echo FAIL: test_add; exit 1; }\n'
268
+ ),
269
+ test_script_timeout=None,
270
+ stop_event=None,
271
+ )
272
+ workspace = agent_tools.AgentWorkspace(
273
+ build_folder=str(project.build),
274
+ writable_folders=[str(project.build)],
275
+ render_context=rc,
276
+ conformance_tests_folder=str(tests_folder),
277
+ )
278
+
279
+ failed = agent_tools.run_conformance_tests({}, workspace)
280
+ assert failed["output"].startswith("The conformance tests failed") and "FAIL: test_add" in failed["test_output"]
281
+ repeated = agent_tools.run_conformance_tests({}, workspace)
282
+ assert repeated["output"].startswith("Nothing changed") and "FAIL: test_add" in repeated["test_output"]
283
+ (tests_folder / "debug.txt").write_text("diagnostics\n")
284
+ agent_tools.run_conformance_tests({}, workspace)
285
+ assert prepared.read_text().count("x") == 1 # only the tests changed, so no second preparation
286
+
287
+ agent_tools.edit_file(
288
+ {
289
+ "file_path": "app.py",
290
+ "search": "def add(a, b):\n return a - b",
291
+ "replace": "def add(a, b):\n return a + b",
292
+ },
293
+ workspace,
294
+ )
295
+ assert agent_tools.run_conformance_tests({}, workspace) == {"output": "The conformance tests passed."}
296
+ assert prepared.read_text().count("x") == 2
297
+
298
+
299
+ def test_files_the_scripts_write_themselves_are_no_change(project, tmp_path):
300
+ # Preparation scripts write build output into the build folder, and test runs write caches (__pycache__) into
301
+ # both folders; none of that is a change that needs another preparation or another run.
302
+ prepared = tmp_path / "prepared.txt"
303
+ tests_folder = tmp_path / "conformance" / "add"
304
+ tests_folder.mkdir(parents=True)
305
+ rc = SimpleNamespace(
306
+ build_folder=str(project.build),
307
+ prepare_environment_script=_script(
308
+ tmp_path / "prepare.sh", f'echo x >> "{prepared}"\nmkdir -p "$1/target"\necho built > "$1/target/app"\n'
309
+ ),
310
+ conformance_tests_script=_script(
311
+ tmp_path / "run.sh",
312
+ 'mkdir -p "$2/__pycache__"\necho pyc > "$2/__pycache__/test_add.pyc"\necho log > "$1/.test-cache"\n'
313
+ "echo FAIL: test_add\nexit 1\n",
314
+ ),
315
+ test_script_timeout=None,
316
+ stop_event=None,
317
+ )
318
+ workspace = agent_tools.AgentWorkspace(
319
+ build_folder=str(project.build),
320
+ writable_folders=[str(project.build)],
321
+ render_context=rc,
322
+ conformance_tests_folder=str(tests_folder),
323
+ )
324
+
325
+ assert agent_tools.run_conformance_tests({}, workspace)["output"].startswith("The conformance tests failed")
326
+ assert agent_tools.run_conformance_tests({}, workspace)["output"].startswith("Nothing changed")
327
+ (tests_folder / "debug.txt").write_text("diagnostics\n")
328
+ assert agent_tools.run_conformance_tests({}, workspace)["output"].startswith("The conformance tests failed")
329
+ assert prepared.read_text().count("x") == 1
330
+
331
+
332
+ def test_writing_under_a_spelling_that_differs_only_in_case_renames_the_existing_folder(project):
333
+ old_folder = project.build / "com" / "incodeSmile"
334
+ old_folder.mkdir(parents=True)
335
+ (old_folder / "Other.java").write_text("class Other {}\n")
336
+ case_insensitive = os.path.exists(str(project.build / "com" / "INCODESMILE"))
337
+
338
+ out = agent_tools.write_file({"file_path": "com/incodesmile/Dto.java", "content": "class Dto {}\n"}, project.rc)
339
+
340
+ entries = os.listdir(project.build / "com")
341
+ if case_insensitive:
342
+ assert entries == ["incodesmile"] and "was renamed to" in out
343
+ assert sorted(os.listdir(project.build / "com" / "incodesmile")) == ["Dto.java", "Other.java"]
344
+ else:
345
+ # On a case-sensitive file system the two spellings are two folders, and nothing is renamed.
346
+ assert sorted(entries) == ["incodeSmile", "incodesmile"] and "renamed" not in out
@@ -0,0 +1,52 @@
1
+ """Which failed requests the API client retries."""
2
+
3
+ from types import SimpleNamespace
4
+
5
+ import pytest
6
+ import requests
7
+
8
+ import codeplain_REST_api
9
+ from codeplain_REST_api import CodeplainAPI
10
+
11
+
12
+ class FakeResponse:
13
+ def __init__(self, status_code):
14
+ self.status_code = status_code
15
+ self.text = "{}"
16
+
17
+ def json(self):
18
+ return {}
19
+
20
+ def raise_for_status(self):
21
+ if self.status_code >= 400:
22
+ raise requests.exceptions.HTTPError(f"{self.status_code} error", response=self)
23
+
24
+
25
+ def make_api(monkeypatch, statuses):
26
+ api = CodeplainAPI("key", SimpleNamespace(debug=lambda *a, **k: None, error=lambda *a, **k: None))
27
+ calls = []
28
+
29
+ def post(url, headers=None, json=None, timeout=None):
30
+ calls.append(url)
31
+ return FakeResponse(statuses[min(len(calls), len(statuses)) - 1])
32
+
33
+ api.session = SimpleNamespace(post=post)
34
+ monkeypatch.setattr(codeplain_REST_api.time, "sleep", lambda seconds: None)
35
+ return api, calls
36
+
37
+
38
+ def test_a_not_found_answer_is_not_retried_and_raised_as_it_is(monkeypatch):
39
+ api, calls = make_api(monkeypatch, [404])
40
+
41
+ with pytest.raises(requests.exceptions.HTTPError) as raised:
42
+ api.post_request("http://api/agent/continue", {}, {}, None, num_retries=8)
43
+
44
+ assert raised.value.response.status_code == 404 and len(calls) == 1
45
+
46
+
47
+ @pytest.mark.parametrize("status", [429, 500, 503])
48
+ def test_rate_limits_and_server_errors_are_retried(monkeypatch, status):
49
+ api, calls = make_api(monkeypatch, [status, 200])
50
+
51
+ assert api.post_request("http://api/x", {}, {}, None, num_retries=2) == {}
52
+ assert len(calls) == 2
@@ -0,0 +1,73 @@
1
+ import os
2
+ import threading
3
+ from types import SimpleNamespace
4
+ from unittest import mock
5
+
6
+ from render_machine.actions import prepare_testing_environment as pte
7
+
8
+
9
+ def _context(tmp_path, should_prepare=True):
10
+ build = tmp_path / "code"
11
+ build.mkdir()
12
+ (build / "a.txt").write_text("a")
13
+ return SimpleNamespace(
14
+ prepare_environment_script="prepare.sh",
15
+ build_folder=str(build),
16
+ test_script_timeout=10,
17
+ stop_event=threading.Event(),
18
+ conformance_tests_running_context=SimpleNamespace(should_prepare_testing_environment=should_prepare),
19
+ script_execution_history=SimpleNamespace(),
20
+ background_preparation=None,
21
+ )
22
+
23
+
24
+ def test_a_preparation_started_ahead_is_used_once_the_build_folder_is_unchanged(tmp_path):
25
+ ctx = _context(tmp_path)
26
+ with mock.patch.object(pte.render_utils, "execute_script", return_value=(0, "", "out")) as run:
27
+ pte.start_in_background(ctx)
28
+ outcome, _ = pte.PrepareTestingEnvironment().execute(ctx, None)
29
+ assert outcome == pte.PrepareTestingEnvironment.SUCCESSFUL_OUTCOME
30
+ assert run.call_count == 1 and ctx.background_preparation is None
31
+ assert ctx.conformance_tests_running_context.should_prepare_testing_environment is False
32
+
33
+
34
+ def test_a_preparation_started_ahead_is_redone_when_the_build_folder_changed(tmp_path):
35
+ ctx = _context(tmp_path)
36
+ with mock.patch.object(pte.render_utils, "execute_script", return_value=(0, "", "out")) as run:
37
+ pte.start_in_background(ctx)
38
+ ctx.background_preparation.done.wait(5)
39
+ with open(os.path.join(ctx.build_folder, "b.txt"), "w") as f:
40
+ f.write("b")
41
+ pte.PrepareTestingEnvironment().execute(ctx, None)
42
+ assert run.call_count == 2
43
+
44
+
45
+ def test_what_the_preparation_script_writes_into_the_build_folder_is_no_change(tmp_path):
46
+ # e.g. npm install writing node_modules, mvn compile writing target/
47
+ ctx = _context(tmp_path)
48
+
49
+ def build(*_args, **_kwargs):
50
+ os.makedirs(os.path.join(ctx.build_folder, "target"))
51
+ with open(os.path.join(ctx.build_folder, "target", "app.jar"), "w") as f:
52
+ f.write("built")
53
+ return (0, "", "out")
54
+
55
+ with mock.patch.object(pte.render_utils, "execute_script", side_effect=build) as run:
56
+ pte.start_in_background(ctx)
57
+ outcome, _ = pte.PrepareTestingEnvironment().execute(ctx, None)
58
+ assert outcome == pte.PrepareTestingEnvironment.SUCCESSFUL_OUTCOME and run.call_count == 1
59
+
60
+
61
+ def test_nothing_is_started_when_no_preparation_is_due(tmp_path):
62
+ ctx = _context(tmp_path, should_prepare=False)
63
+ with mock.patch.object(pte.render_utils, "execute_script") as run:
64
+ pte.start_in_background(ctx)
65
+ assert ctx.background_preparation is None and run.call_count == 0
66
+
67
+
68
+ def test_a_failed_preparation_started_ahead_fails_the_action(tmp_path):
69
+ ctx = _context(tmp_path)
70
+ with mock.patch.object(pte.render_utils, "execute_script", return_value=(3, "", "out")):
71
+ pte.start_in_background(ctx)
72
+ outcome, _ = pte.PrepareTestingEnvironment().execute(ctx, None)
73
+ assert outcome == pte.PrepareTestingEnvironment.FAILED_OUTCOME