File size: 6,962 Bytes
891fe52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""Tests for IntentParser."""

import pytest
from unittest.mock import AsyncMock, Mock

from polyhedra.agent.intent_parser import IntentParser
from polyhedra.agent.models import Intent, IntentType
from polyhedra.services.llm_service import LLMService


class TestIntentParser:
    """Test suite for IntentParser."""
    
    def test_initialization_without_llm(self):
        """Test parser initialization without LLM service."""
        parser = IntentParser()
        assert parser.llm_service is None
        assert parser.patterns is not None
    
    def test_initialization_with_llm(self):
        """Test parser initialization with LLM service."""
        llm = Mock(spec=LLMService)
        parser = IntentParser(llm_service=llm)
        assert parser.llm_service is llm
    
    @pytest.mark.asyncio
    async def test_parse_research_survey_simple(self):
        """Test parsing simple research survey command."""
        parser = IntentParser()
        intent = await parser.parse("research transformers")
        
        assert intent.type == IntentType.RESEARCH_SURVEY
        assert intent.topic == "transformers"
        assert intent.confidence == 0.9
        assert intent.raw_command == "research transformers"
    
    @pytest.mark.asyncio
    async def test_parse_research_survey_with_papers_on(self):
        """Test parsing research command with 'papers on'."""
        parser = IntentParser()
        intent = await parser.parse("research papers on attention mechanisms")
        
        assert intent.type == IntentType.RESEARCH_SURVEY
        assert intent.topic == "attention mechanisms"
    
    @pytest.mark.asyncio
    async def test_parse_find_papers(self):
        """Test parsing 'find papers' command."""
        parser = IntentParser()
        intent = await parser.parse("find papers on vision transformers")
        
        assert intent.type == IntentType.RESEARCH_SURVEY
        assert intent.topic == "vision transformers"
    
    @pytest.mark.asyncio
    async def test_parse_paper_comparison(self):
        """Test parsing paper comparison command."""
        parser = IntentParser()
        intent = await parser.parse("compare BERT vs GPT")
        
        assert intent.type == IntentType.PAPER_COMPARISON
        assert "bert" in intent.topic or "gpt" in intent.topic
    
    @pytest.mark.asyncio
    async def test_parse_gap_analysis(self):
        """Test parsing gap analysis command."""
        parser = IntentParser()
        intent = await parser.parse("find research gaps in multimodal learning")
        
        assert intent.type == IntentType.GAP_ANALYSIS
        assert intent.topic == "multimodal learning"
    
    @pytest.mark.asyncio
    async def test_parse_citation_finding(self):
        """Test parsing citation finding command."""
        parser = IntentParser()
        intent = await parser.parse("find citations for attention is all you need")
        
        assert intent.type == IntentType.CITATION_FINDING
        assert "attention" in intent.topic
    
    @pytest.mark.asyncio
    async def test_parse_extracts_paper_limit(self):
        """Test extracting paper limit from command."""
        parser = IntentParser()
        intent = await parser.parse("find 50 papers on transformers")
        
        assert intent.parameters.get('limit') == 50
    
    @pytest.mark.asyncio
    async def test_parse_extracts_depth_brief(self):
        """Test extracting brief depth parameter."""
        parser = IntentParser()
        intent = await parser.parse("research transformers brief review")
        
        assert intent.parameters.get('depth') == 'brief'
    
    @pytest.mark.asyncio
    async def test_parse_extracts_depth_comprehensive(self):
        """Test extracting comprehensive depth parameter."""
        parser = IntentParser()
        intent = await parser.parse("detailed research on transformers")
        
        assert intent.parameters.get('depth') == 'comprehensive'
    
    @pytest.mark.asyncio
    async def test_parse_extracts_structure_chronological(self):
        """Test extracting chronological structure parameter."""
        parser = IntentParser()
        intent = await parser.parse("research transformers chronological timeline")
        
        assert intent.parameters.get('structure') == 'chronological'
    
    @pytest.mark.asyncio
    async def test_parse_extracts_year_range(self):
        """Test extracting year range parameters."""
        parser = IntentParser()
        intent = await parser.parse("research transformers from 2020 to 2023")
        
        assert intent.parameters.get('year_from') == 2020
        assert intent.parameters.get('year_to') == 2023
    
    @pytest.mark.asyncio
    async def test_parse_unknown_command(self):
        """Test parsing unknown command without LLM."""
        parser = IntentParser()
        intent = await parser.parse("xyz abc def nonsense")
        
        assert intent.type == IntentType.UNKNOWN
        assert intent.confidence == 0.0
    
    @pytest.mark.asyncio
    async def test_parse_with_llm_fallback(self):
        """Test LLM fallback for unknown commands."""
        llm = Mock(spec=LLMService)
        llm.complete = AsyncMock(return_value=(
            '{"type": "research_survey", "topic": "efficient transformers", "parameters": {"depth": "standard"}}',
            100,
            50
        ))
        
        parser = IntentParser(llm_service=llm)
        intent = await parser.parse("some complex command about efficient transformers")
        
        assert intent.type == IntentType.RESEARCH_SURVEY
        assert intent.topic == "efficient transformers"
        assert intent.parameters.get('depth') == 'standard'
        assert intent.confidence == 0.8
    
    @pytest.mark.asyncio
    async def test_parse_llm_failure_returns_unknown(self):
        """Test LLM parsing failure returns unknown intent."""
        llm = Mock(spec=LLMService)
        llm.complete = AsyncMock(side_effect=Exception("API Error"))
        
        parser = IntentParser(llm_service=llm)
        intent = await parser.parse("unknown complex command")
        
        assert intent.type == IntentType.UNKNOWN
    
    @pytest.mark.asyncio
    async def test_parse_llm_invalid_json_returns_unknown(self):
        """Test LLM returning invalid JSON returns unknown."""
        llm = Mock(spec=LLMService)
        llm.complete = AsyncMock(return_value=("invalid json response", 100, 50))
        
        parser = IntentParser(llm_service=llm)
        intent = await parser.parse("unknown command")
        
        assert intent.type == IntentType.UNKNOWN
    
    def test_extract_parameters_default_values(self):
        """Test default parameter extraction."""
        parser = IntentParser()
        params = parser._extract_parameters("research transformers")
        
        assert params.get('depth') == 'standard'
        assert params.get('structure') == 'thematic'