agent-harness / tests /test_repair_experiment.py
cuber12's picture
Publish agent harness research code and paper artifacts
d61821a verified
Raw
History Blame Contribute Delete
2 kB
import unittest
from pathlib import Path
import tempfile
from agent_harness.repair_experiment import (
RepairExperimentError,
extract_unified_diff,
oracle_hunk_ranges,
validate_patch_scope,
run_test_command,
)
class RepairExperimentTests(unittest.TestCase):
def test_oracle_hunk_ranges_read_only_old_file_locations(self) -> None:
patch = (
"diff --git a/x.go b/x.go\n--- a/x.go\n+++ b/x.go\n"
"@@ -55,6 +55,20 @@ type X struct {\n context\n"
)
self.assertEqual(oracle_hunk_ranges(patch, "x.go"), ((55, 6),))
def test_extracts_fenced_diff_without_rewriting_it(self) -> None:
response = {
"choices": [
{
"message": {
"content": "```diff\ndiff --git a/x.go b/x.go\n--- a/x.go\n+++ b/x.go\n@@ -1 +1 @@\n-a\n+b\n```"
}
}
]
}
patch = extract_unified_diff(response)
self.assertTrue(patch.startswith("diff --git a/x.go b/x.go"))
self.assertTrue(patch.endswith("+b\n"))
def test_rejects_modification_outside_evidence(self) -> None:
patch = "diff --git a/x.go b/x.go\n--- a/x.go\n+++ b/x.go\n@@ -1 +1 @@\n-a\n+b\n"
with self.assertRaises(RepairExperimentError):
validate_patch_scope(patch, ["y.go"])
def test_accepts_modification_inside_evidence(self) -> None:
patch = "diff --git a/x.go b/x.go\n--- a/x.go\n+++ b/x.go\n@@ -1 +1 @@\n-a\n+b\n"
self.assertEqual(validate_patch_scope(patch, ["x.go"]), ("x.go",))
def test_python_pytest_command_uses_frozen_interpreter(self) -> None:
with tempfile.TemporaryDirectory() as temporary:
tree = Path(temporary)
(tree / "test_smoke.py").write_text("def test_ok():\n assert True\n")
result = run_test_command(tree, "python -m pytest -q test_smoke.py")
self.assertEqual(result["returncode"], 0, result["stderr"])