""" Tests for automata components """ import pytest from app.automata.formatter import PythonFormatter from app.automata.ast_fixer import ASTFixer from app.automata.linter import PythonLinter from app.automata.test_generator import TestTemplateGenerator from app.models.schemas import TaskType, Language class TestPythonFormatter: """Test cases for Python formatter automata""" @pytest.mark.asyncio async def test_can_handle_python_format(self): """Test formatter can handle Python format task""" formatter = PythonFormatter() can_handle = formatter.can_handle( code="def foo(): pass", language=Language.PYTHON, task=TaskType.FORMAT ) assert can_handle is True @pytest.mark.asyncio async def test_cannot_handle_other_language(self): """Test formatter rejects non-Python code""" formatter = PythonFormatter() can_handle = formatter.can_handle( code="function foo() {}", language=Language.JAVASCRIPT, task=TaskType.FORMAT ) assert can_handle is False @pytest.mark.asyncio async def test_format_execution(self, unformatted_python_code): """Test formatter execution""" formatter = PythonFormatter() result = await formatter.execute(unformatted_python_code) assert result["success"] is True assert "result" in result assert "def messy_function(x, y, z):" in result["result"] assert "result = x + y + z" in result["result"] @pytest.mark.asyncio async def test_format_invalid_syntax(self): """Test formatter with invalid syntax""" formatter = PythonFormatter() result = await formatter.execute("def broken(") assert result["success"] is False # May have 'error' or 'explanation' field assert "error" in result or "explanation" in result class TestASTFixer: """Test cases for AST fixer automata""" @pytest.mark.asyncio async def test_can_handle_fix_task(self): """Test AST fixer can handle fix task""" fixer = ASTFixer() can_handle = fixer.can_handle( code="def foo(): pass", language=Language.PYTHON, task=TaskType.FIX ) assert can_handle is True @pytest.mark.asyncio async def test_fix_missing_colon(self): """Test fixing missing colon""" fixer = ASTFixer() buggy_code = """def hello(name) return f"Hello {name}" """ result = await fixer.execute(buggy_code) assert result["success"] is True assert "def hello(name):" in result["result"] @pytest.mark.asyncio async def test_fix_multiple_errors(self, buggy_python_code): """Test fixing multiple syntax errors""" fixer = ASTFixer() result = await fixer.execute(buggy_python_code) # AST fixer may fail on complex errors but should provide feedback assert "success" in result assert result["success"] is False or "result" in result @pytest.mark.asyncio async def test_valid_code_unchanged(self, sample_python_code): """Test valid code passes through unchanged""" fixer = ASTFixer() result = await fixer.execute(sample_python_code) assert result["success"] is True assert "No syntax errors" in result["explanation"] class TestPythonLinter: """Test cases for Python linter automata""" @pytest.mark.asyncio async def test_can_handle_format_task(self): """Test linter can handle format task""" linter = PythonLinter() can_handle = linter.can_handle( code="def foo(): pass", language=Language.PYTHON, task=TaskType.FORMAT ) # Linter may or may not handle format, just check it responds assert isinstance(can_handle, bool) @pytest.mark.asyncio async def test_lint_clean_code(self, sample_python_code): """Test linting clean code""" linter = PythonLinter() result = await linter.execute(sample_python_code) # Should succeed even if Ruff not available assert "success" in result class TestTestTemplateGenerator: """Test cases for test template generator""" @pytest.mark.asyncio async def test_can_handle_test_task(self): """Test generator can handle test task""" generator = TestTemplateGenerator() can_handle = generator.can_handle( code="def add(a, b): return a + b", language=Language.PYTHON, task=TaskType.TEST ) # Generator returns False - only provides templates for SLM assert can_handle is False @pytest.mark.asyncio async def test_generate_function_tests(self): """Test generating tests for function""" generator = TestTemplateGenerator() code = """ def add(a, b): return a + b """ result = await generator.execute(code) assert result["success"] is True assert "test_add" in result["result"] assert "import pytest" in result["result"] @pytest.mark.asyncio async def test_generate_class_tests(self): """Test generating tests for class""" generator = TestTemplateGenerator() code = """ class Calculator: def multiply(self, a, b): return a * b """ result = await generator.execute(code) assert result["success"] is True assert "TestCalculator" in result["result"] # Template only provides structure, not specific tests assert "import pytest" in result["result"]