max / test_dev_workbench.py
manjunathan123's picture
Upload folder using huggingface_hub
071cd07 verified
Raw History Blame Contribute Delete
5.21 kB
import json
import tempfile
import unittest
from pathlib import Path
from dev_workbench import DevelopmentWorkbench, DevWorkbenchError
class FakeModel:
def __init__(self, responses):
self.responses = list(responses)
self.calls = []
def __call__(self, system, prompt, max_tokens=1000):
self.calls.append((system, prompt, max_tokens))
if not self.responses:
return ""
return self.responses.pop(0)
class DevelopmentWorkbenchTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.root = Path(self.tmp.name) / "workspace"
def make_wb(self, responses):
fake = FakeModel(responses)
return DevelopmentWorkbench(self.root, llm_call=fake), fake
def test_create_drafts_proposal_without_writing_project_files(self):
response = json.dumps({"summary":"small calculator", "files":[
{"path":"main.py", "content":"def add(a, b):\n return a + b\n"},
{"path":"test_main.py", "content":"import unittest\nfrom main import add\nclass T(unittest.TestCase):\n def test_add(self): self.assertEqual(add(2, 3), 5)\n"}
], "verification_plan":["python syntax", "unittest"], "risks":[]})
wb, _ = self.make_wb([response])
result = wb.create("calc-demo", "Make a tiny calculator with tests")
self.assertEqual(result["status"], "pending_operator_apply")
self.assertTrue(result["proposal_id"].startswith("P-"))
self.assertFalse((self.root / "projects" / "calc-demo").exists())
def test_apply_writes_only_to_workspace_and_backups_modified_files(self):
response1 = json.dumps({"summary":"initial", "files":[{"path":"main.py", "content":"VALUE = 1\n"}]})
response2 = json.dumps({"summary":"change", "files":[{"path":"main.py", "content":"VALUE = 2\n"}]})
wb, _ = self.make_wb([response1, response2])
first = wb.create("demo", "make file")
wb.apply(first["proposal_id"])
second = wb.patch("demo", "main.py", "Change value to 2")
self.assertEqual((self.root / "projects" / "demo" / "main.py").read_text(), "VALUE = 1\n")
applied = wb.apply(second["proposal_id"])
self.assertEqual((self.root / "projects" / "demo" / "main.py").read_text(), "VALUE = 2\n")
self.assertTrue(applied["backup_dir"])
self.assertTrue((Path(applied["backup_dir"]) / "main.py").is_file())
def test_path_traversal_rejected(self):
wb, _ = self.make_wb([])
with self.assertRaises(DevWorkbenchError):
wb.create("demo", "spec") if False else wb._relative_path("../outside.py")
def test_python_syntax_failure_is_reported(self):
wb, _ = self.make_wb([json.dumps({"files":[{"path":"broken.py", "content":"def x(:\n pass\n"}]})])
with self.assertRaises(DevWorkbenchError):
wb.create("badproj", "Create code with syntax error")
self.assertFalse((self.root / "projects" / "badproj").exists())
def test_compile_and_run_trusted_unittest(self):
wb, _ = self.make_wb([json.dumps({"files":[
{"path":"mathutil.py", "content":"def mul(a, b):\n return a * b\n"},
{"path":"test_mathutil.py", "content":"import unittest\nfrom mathutil import mul\nclass T(unittest.TestCase):\n def test_mul(self): self.assertEqual(mul(3, 4), 12)\n"}
]})])
prop = wb.create("testable", "Create a utility with unit test")
wb.apply(prop["proposal_id"])
result = wb.test("testable")
self.assertEqual(result["status"], "passed", result)
self.assertEqual(result["tests"]["status"], "passed", result)
def test_compile_only_does_not_run_tests(self):
wb, _ = self.make_wb([json.dumps({"files":[{"path":"main.py", "content":"print('hello')\n"}]})])
prop = wb.create("compile-only", "Small program")
wb.apply(prop["proposal_id"])
result = wb.test("compile-only", compile_only=True)
self.assertEqual(result["status"], "passed")
self.assertEqual(result["tests"]["status"], "not_run")
def test_review_and_explain_use_model_not_execution(self):
wb, fake = self.make_wb(["This function adds two integers.", "No issues found; add a regression test."])
project = self.root / "projects" / "reviewable"
project.mkdir(parents=True)
(project / "main.py").write_text("def add(a,b): return a+b\n")
explanation = wb.explain("reviewable", "main.py")
review = wb.review("reviewable", "main.py")
self.assertIn("adds two integers", explanation)
self.assertIn("No issues", review)
self.assertEqual(len(fake.calls), 2)
def test_status_marks_model_drafts_as_pending(self):
response = json.dumps({"files":[{"path":"a.py", "content":"x = 1\n"}]})
wb, _ = self.make_wb([response])
prop = wb.create("status-proj", "make script")
state = wb.status()
self.assertTrue(state["model_backend_configured"])
self.assertEqual(state["pending_proposals"][0]["proposal_id"], prop["proposal_id"])
if __name__ == "__main__":
unittest.main(verbosity=2)