Spaces:
Sleeping
Sleeping
Create src/macg/agents/tester.py
Browse files- src/macg/agents/tester.py +34 -0
src/macg/agents/tester.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
from macg.llm import LLMClient
|
| 3 |
+
from macg.protocol import Artifact
|
| 4 |
+
from macg.tools.python_runner import run_pytest
|
| 5 |
+
|
| 6 |
+
TESTER_SYSTEM = """You are TesterAgent.
|
| 7 |
+
Write pytest tests that validate correctness and edge cases.
|
| 8 |
+
Rules:
|
| 9 |
+
- Output ONLY python test code.
|
| 10 |
+
- Use pytest.
|
| 11 |
+
- Do not import external libs besides pytest.
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
class TesterAgent:
|
| 15 |
+
def __init__(self, llm: LLMClient) -> None:
|
| 16 |
+
self.llm = llm
|
| 17 |
+
|
| 18 |
+
def run(self, art: Artifact) -> Artifact:
|
| 19 |
+
test_prompt = f"""
|
| 20 |
+
Task:
|
| 21 |
+
{art.task}
|
| 22 |
+
|
| 23 |
+
Code under test (module {art.module_name}.py):
|
| 24 |
+
{art.code}
|
| 25 |
+
|
| 26 |
+
Write pytest tests for the expected behavior.
|
| 27 |
+
""".strip()
|
| 28 |
+
|
| 29 |
+
art.tests = self.llm.complete(TESTER_SYSTEM, test_prompt).strip()
|
| 30 |
+
|
| 31 |
+
result = run_pytest(module_name=art.module_name, code=art.code, tests=art.tests, timeout_s=15)
|
| 32 |
+
art.passed = result.ok
|
| 33 |
+
art.test_report = f"returncode={result.returncode}\nSTDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}"
|
| 34 |
+
return art
|