File size: 3,687 Bytes
60e356c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294a450
 
60e356c
 
7e1939b
60e356c
 
 
7e1939b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60e356c
 
 
 
 
 
294a450
 
60e356c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294a450
60e356c
294a450
 
60e356c
7e1939b
 
 
 
 
 
 
 
 
60e356c
 
7e1939b
 
 
60e356c
 
 
 
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
122
"""
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()