File size: 3,823 Bytes
1afb40b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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