gemma4-dev-agent / tests /test_agent_loop.py
EzioDevio's picture
Upload folder using huggingface_hub
1afb40b verified
Raw
History Blame Contribute Delete
3.82 kB
import sys
import os
import pytest
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from scripts.agent import (
run_bash_command,
run_file_read,
run_file_write,
run_repo_ast_parser,
run_pytest_suite,
parse_and_execute_tool,
process_query,
TOOLS
)
def test_run_bash_command():
output = run_bash_command("echo 'hello pytest'")
assert "hello pytest" in output
def test_file_read_write(tmp_path):
file_path = tmp_path / "test_out.txt"
# Test writing
write_res = run_file_write(str(file_path), "sample text")
assert "Successfully wrote" in write_res
# Test reading
read_res = run_file_read(str(file_path))
assert read_res == "sample text"
def test_file_read_nonexistent():
res = run_file_read("non_existent_file_12345.txt")
assert "Error: File" in res
def test_file_write_exception(monkeypatch):
def mock_open(*args, **kwargs):
raise OSError("Permission denied")
monkeypatch.setattr("builtins.open", mock_open)
res = run_file_write("invalid_path/file.txt", "data")
assert "Error writing file" in res
def test_repo_ast_parser_nonexistent():
res = run_repo_ast_parser("non_existent_file_12345.py")
assert "Error: File" in res
def test_repo_ast_parser_syntax_error(tmp_path):
broken_file = tmp_path / "broken.py"
broken_file.write_text("def broken_function(:")
res = run_repo_ast_parser(str(broken_file))
assert "Error parsing AST" in res
def test_run_pytest_suite_guard():
res = run_pytest_suite()
assert "[Test Guard]" in res
def test_tools_schema_full_traversal():
assert isinstance(TOOLS, list)
assert len(TOOLS) == 5
for tool in TOOLS:
assert "type" in tool
func = tool.get("function", {})
assert "name" in func
assert "description" in func
params = func.get("parameters", {})
assert "properties" in params
for prop_name, prop_data in params["properties"].items():
assert "type" in prop_data
assert "description" in prop_data
def test_parse_and_execute_tool_dispatch(tmp_path):
# Test bash tool
assert "bash_test" in parse_and_execute_tool("execute_bash", {"command": "echo bash_test"})
# Test file read/write tools
test_file = tmp_path / "dispatch_test.txt"
parse_and_execute_tool("file_write", {"path": str(test_file), "content": "dispatch_data"})
assert parse_and_execute_tool("file_read", {"path": str(test_file)}) == "dispatch_data"
# Test AST tool dispatch
ast_res = parse_and_execute_tool("repo_ast_parser", {"path": "scripts/agent.py"})
assert "AST Analysis" in ast_res
# Test Pytest tool dispatch
pytest_res = parse_and_execute_tool("run_pytest", {"test_path": "tests", "cov": "true", "cov_module": "scripts"})
assert "[Test Guard]" in pytest_res
# Test unknown tool dispatch
assert "Unknown tool" in parse_and_execute_tool("invalid_tool_name", {})
def test_process_query_routing(monkeypatch):
monkeypatch.setattr("scripts.agent.run_pytest_suite", lambda **kwargs: "Mocked Pytest Response")
# Test Pytest routing with regex parsing
pytest_res = process_query("Run pytest on tests/ and measure code coverage for the scripts directory.")
assert pytest_res == "Mocked Pytest Response"
# Test AST parsing routing
ast_res = process_query("Parse AST for scripts/agent.py")
assert "AST Analysis" in ast_res or "scripts/agent.py" in ast_res
# Test Bash execution routing
bash_res = process_query("run echo 'interactive bash'")
assert "interactive bash" in bash_res
# Test Fallback
fallback_res = process_query("What is the capital of France?")
assert "Processed query:" in fallback_res