ada-assistant / tests /unit /agents /test_code_analyzer_agent.py
syum-af's picture
added playwright-based integration tests
6a19500
Raw
History Blame Contribute Delete
3.69 kB
"""
Unit tests for CodeAnalyzerAgent
"""
import pytest
from pathlib import Path
from unittest.mock import Mock, MagicMock
from src.agents.code_analyzer import CodeAnalyzerAgent
class TestCodeAnalyzerAgent:
"""Test suite for CodeAnalyzerAgent"""
@pytest.fixture
def mock_model(self):
"""Mock model for testing"""
return Mock()
@pytest.fixture
def analyzer_agent(self, mock_model):
"""Create CodeAnalyzerAgent instance with mocked model"""
return CodeAnalyzerAgent(mock_model)
def test_initialization_with_model(self, mock_model):
"""Test agent initialization with model parameter"""
agent = CodeAnalyzerAgent(mock_model)
assert agent.model == mock_model
assert agent.supported_extensions == ['.ads', '.adb', '.ada']
assert agent.analysis_results == {}
assert hasattr(agent, 'code_agent')
def test_extract_business_logic_with_ada_code(self, mock_model):
"""Test extract_business_logic with Ada code string and mocked LLM response"""
from unittest.mock import MagicMock
# Mock the CodeAgent run method to return markdown business logic analysis
mock_result = MagicMock()
mock_result.messages = []
mock_message = MagicMock()
mock_message.content = '''# Business Logic Analysis
## Core Algorithms
- TicTacToe game logic
- Board state evaluation
- Win condition checking
## Data Structures & Types
- Board array structure
- Position type system
- Slot enumeration
## Business Rules & Constraints
- SPARK verification contracts
- Pre/post conditions
- Game state constraints
## Domain Concepts
- Game board management
- Player moves
- Win detection
## Conversion Complexity
**Level**: Medium
**Reasoning**: Moderate complexity due to formal verification contracts
## Recommended Approach
Translate to Python classes with similar structure
'''
mock_result.messages.append(mock_message)
analyzer_agent = CodeAnalyzerAgent(mock_model)
analyzer_agent.code_agent.run = MagicMock(return_value=mock_result)
# Test Ada code string
ada_code = """
package Tictactoe
with SPARK_Mode => On
is
type Slot is (Empty, Player, Computer);
type Pos is new Integer range 1 .. 3;
type Column is array (Pos) of Slot;
type Board is array (Pos) of Column;
My_Board : Board := (others => (others => Empty));
procedure Initialize
with Post => Num_Free_Slots = 9;
procedure Player_Play (S : String)
with Pre => not Is_Full and Won = Empty,
Post => Num_Free_Slots = Num_Free_Slots'Old - 1;
function Won return Slot;
function Is_Full return Boolean is (Num_Free_Slots = 0);
end Tictactoe;
""".strip()
# Test - now passing Ada code string directly
result = analyzer_agent.extract_business_logic(ada_code)
# Verify result is markdown string
assert isinstance(result, str)
assert "# Business Logic Analysis" in result
assert "## Core Algorithms" in result
assert "## Data Structures & Types" in result
assert "## Business Rules & Constraints" in result
assert "## Domain Concepts" in result
assert "## Conversion Complexity" in result
assert "## Recommended Approach" in result
# Verify LLM found business logic concepts
assert "TicTacToe game logic" in result
assert "Board array structure" in result
assert "Medium" in result
# Verify LLM analysis was called
analyzer_agent.code_agent.run.assert_called_once()