1- """Test suite for chat completions tool."""
1+ """Test suite for codebase consultant tool."""
22
33import pytest
44from unittest .mock import AsyncMock , MagicMock , patch
55import json
66from fastmcp import Context
7- from tools .chat import chat_completions
7+ from tools .chat import codebase_consultant
88
99
1010@pytest .mark .asyncio
1111@patch ('tools.chat.get_api_key_from_context' )
12- async def test_chat_with_simple_ids (mock_get_api_key ):
13- """Test chat completions with simple string IDs."""
12+ async def test_consultant_with_simple_ids (mock_get_api_key ):
13+ """Test codebase consultant with simple string IDs."""
1414 mock_get_api_key .return_value = "test_key"
1515
1616 ctx = MagicMock (spec = Context )
@@ -40,9 +40,9 @@ async def mock_aiter_lines():
4040 ctx .request_context .lifespan_context = mock_codealive_context
4141
4242 # Test with simple string IDs
43- result = await chat_completions (
43+ result = await codebase_consultant (
4444 ctx = ctx ,
45- messages = [{ "role" : "user" , "content" : " Test question"}] ,
45+ question = " Test question" ,
4646 data_sources = ["repo123" , "repo456" ]
4747 )
4848
@@ -61,8 +61,8 @@ async def mock_aiter_lines():
6161
6262@pytest .mark .asyncio
6363@patch ('tools.chat.get_api_key_from_context' )
64- async def test_chat_with_dict_ids (mock_get_api_key ):
65- """Test chat completions with dictionary format IDs."""
64+ async def test_consultant_preserves_string_ids (mock_get_api_key ):
65+ """Test codebase consultant preserves string IDs."""
6666 mock_get_api_key .return_value = "test_key"
6767
6868 ctx = MagicMock (spec = Context )
@@ -88,14 +88,11 @@ async def mock_aiter_lines():
8888
8989 ctx .request_context .lifespan_context = mock_codealive_context
9090
91- # Test with dict format (backward compatibility)
92- result = await chat_completions (
91+ # Test with string IDs
92+ result = await codebase_consultant (
9393 ctx = ctx ,
94- messages = [{"role" : "user" , "content" : "Test" }],
95- data_sources = [
96- {"type" : "repository" , "id" : "repo123" },
97- {"id" : "repo456" }
98- ]
94+ question = "Test" ,
95+ data_sources = ["repo123" , "repo456" ]
9996 )
10097
10198 call_args = mock_client .post .call_args
@@ -112,8 +109,8 @@ async def mock_aiter_lines():
112109
113110@pytest .mark .asyncio
114111@patch ('tools.chat.get_api_key_from_context' )
115- async def test_chat_with_conversation_id (mock_get_api_key ):
116- """Test chat completions with existing conversation ID."""
112+ async def test_consultant_with_conversation_id (mock_get_api_key ):
113+ """Test codebase consultant with existing conversation ID."""
117114 mock_get_api_key .return_value = "test_key"
118115
119116 ctx = MagicMock (spec = Context )
@@ -137,9 +134,9 @@ async def mock_aiter_lines():
137134
138135 ctx .request_context .lifespan_context = mock_codealive_context
139136
140- result = await chat_completions (
137+ result = await codebase_consultant (
141138 ctx = ctx ,
142- messages = [{ "role" : "user" , "content" : " Follow up"}] ,
139+ question = " Follow up" ,
143140 conversation_id = "conv_123"
144141 )
145142
@@ -156,51 +153,29 @@ async def mock_aiter_lines():
156153
157154@pytest .mark .asyncio
158155@patch ('tools.chat.get_api_key_from_context' )
159- async def test_chat_empty_messages_validation (mock_get_api_key ):
160- """Test validation of empty messages ."""
156+ async def test_consultant_empty_question_validation (mock_get_api_key ):
157+ """Test validation of empty question ."""
161158 mock_get_api_key .return_value = "test_key"
162159
163160 ctx = MagicMock (spec = Context )
164161 ctx .request_context .lifespan_context = MagicMock ()
165162
166- # Test with no messages
167- result = await chat_completions (ctx = ctx , messages = None )
168- assert "Error: No messages provided" in result
163+ # Test with empty question
164+ result = await codebase_consultant (ctx = ctx , question = "" )
165+ assert "Error: No question provided" in result
169166
170- # Test with empty list
171- result = await chat_completions (ctx = ctx , messages = [] )
172- assert "Error: No messages provided" in result
167+ # Test with whitespace only
168+ result = await codebase_consultant (ctx = ctx , question = " " )
169+ assert "Error: No question provided" in result
173170
174171
175- @pytest .mark .asyncio
176- @patch ('tools.chat.get_api_key_from_context' )
177- async def test_chat_invalid_message_format (mock_get_api_key ):
178- """Test validation of message format."""
179- mock_get_api_key .return_value = "test_key"
180-
181- ctx = MagicMock (spec = Context )
182- ctx .request_context .lifespan_context = MagicMock ()
183-
184- # Test with missing role
185- result = await chat_completions (
186- ctx = ctx ,
187- messages = [{"content" : "Test" }]
188- )
189- assert "Error: Each message must have 'role' and 'content'" in result
190-
191- # Test with missing content
192- result = await chat_completions (
193- ctx = ctx ,
194- messages = [{"role" : "user" }]
195- )
196- assert "Error: Each message must have 'role' and 'content'" in result
197172
198173
199174@pytest .mark .asyncio
200175@patch ('tools.chat.get_api_key_from_context' )
201176@patch ('tools.chat.handle_api_error' )
202- async def test_chat_error_handling (mock_handle_error , mock_get_api_key ):
203- """Test error handling in chat completions ."""
177+ async def test_consultant_error_handling (mock_handle_error , mock_get_api_key ):
178+ """Test error handling in codebase consultant ."""
204179 mock_get_api_key .return_value = "test_key"
205180 mock_handle_error .return_value = "Error: Authentication failed"
206181
@@ -216,9 +191,9 @@ async def test_chat_error_handling(mock_handle_error, mock_get_api_key):
216191
217192 ctx .request_context .lifespan_context = mock_codealive_context
218193
219- result = await chat_completions (
194+ result = await codebase_consultant (
220195 ctx = ctx ,
221- messages = [{ "role" : "user" , "content" : " Test"}] ,
196+ question = " Test" ,
222197 data_sources = ["repo123" ]
223198 )
224199
0 commit comments