import pytest from pathlib import Path from src.tools.ast_parser import RepositoryASTParser, StructureVisitor def test_ast_parser_success(tmp_path): parser = RepositoryASTParser(repo_root=str(tmp_path)) test_file = tmp_path / "sample.py" code_content = """ import os from pathlib import Path class SampleClass: def sample_method(self, x): return x + 1 def sample_function(y): return y * 2 """ test_file.write_text(code_content, encoding="utf-8") res = parser.parse_file("sample.py") assert res["status"] == "success" assert "os" in res["imports"] assert "pathlib.Path" in res["imports"] assert len(res["classes"]) == 1 assert res["classes"][0]["name"] == "SampleClass" assert "sample_method" in res["classes"][0]["methods"] assert len(res["functions"]) == 2 func_names = [f["name"] for f in res["functions"]] assert "sample_method" in func_names assert "sample_function" in func_names def test_ast_parser_file_not_found(tmp_path): parser = RepositoryASTParser(repo_root=str(tmp_path)) res = parser.parse_file("nonexistent.py") assert res["status"] == "error" assert "not found" in res["message"] def test_ast_parser_syntax_error(tmp_path): parser = RepositoryASTParser(repo_root=str(tmp_path)) test_file = tmp_path / "broken.py" test_file.write_text("def broken_syntax( :", encoding="utf-8") res = parser.parse_file("broken.py") assert res["status"] == "error" assert "Failed to parse AST" in res["message"]