@@ -106,6 +106,39 @@ async def test_ai_command_success(
106106 assert embed .author .name == "TestUser"
107107 assert embed .author .icon_url == "https://example.com/avatar.png"
108108
109+ @pytest .mark .asyncio
110+ @patch ("src.bot.cogs.open_ai.get_bot_settings" )
111+ @patch ("src.bot.cogs.open_ai.bot_utils.send_embed" )
112+ async def test_aiweb_command_success (
113+ self , mock_send_embed , mock_get_settings , openai_cog , mock_ctx , mock_bot_settings
114+ ):
115+ """`aiweb` runs the shared flow with use_web=True (web search enabled)."""
116+ mock_get_settings .return_value = mock_bot_settings
117+
118+ with patch .object (openai_cog , "_get_ai_response" , return_value = "web answer" ) as mock_get_response :
119+ await openai_cog .aiweb .callback (openai_cog , mock_ctx , msg_text = "Latest news" )
120+
121+ mock_get_response .assert_awaited_once_with ("Latest news" , use_web = True )
122+ mock_ctx .send .assert_called_once () # progress message
123+ mock_send_embed .assert_called_once ()
124+ embed = mock_send_embed .call_args [0 ][1 ]
125+ assert embed .description == "web answer"
126+ assert embed .color == discord .Color .green ()
127+
128+ @pytest .mark .asyncio
129+ @patch ("src.bot.cogs.open_ai.get_bot_settings" )
130+ @patch ("src.bot.cogs.open_ai.bot_utils.send_embed" )
131+ async def test_ai_command_uses_plain_path (
132+ self , mock_send_embed , mock_get_settings , openai_cog , mock_ctx , mock_bot_settings
133+ ):
134+ """`ai` routes through _get_ai_response with use_web=False."""
135+ mock_get_settings .return_value = mock_bot_settings
136+
137+ with patch .object (openai_cog , "_get_ai_response" , return_value = "plain answer" ) as mock_get_response :
138+ await openai_cog .ai .callback (openai_cog , mock_ctx , msg_text = "What is Python?" )
139+
140+ mock_get_response .assert_awaited_once_with ("What is Python?" , use_web = False )
141+
109142 @pytest .mark .asyncio
110143 @patch ("src.bot.cogs.open_ai.get_bot_settings" )
111144 @patch ("src.bot.cogs.open_ai.bot_utils.send_embed" )
@@ -141,7 +174,7 @@ async def test_get_ai_response_success(
141174 mock_client .responses .create = AsyncMock (return_value = mock_openai_response )
142175 openai_cog ._openai_client = mock_client
143176
144- result = await openai_cog ._get_ai_response ("What is Python?" )
177+ result = await openai_cog ._get_ai_response ("What is Python?" , use_web = False )
145178
146179 assert result == "This is a mock AI response from OpenAI."
147180
@@ -151,11 +184,31 @@ async def test_get_ai_response_success(
151184
152185 assert call_args [1 ]["model" ] == "gpt-3.5-turbo"
153186 assert call_args [1 ]["max_output_tokens" ] is None
187+ # Plain (no web search) uses the plain instructions and no tools
154188 assert call_args [1 ]["instructions" ] == openai_cog ._instructions
155189 assert call_args [1 ]["input" ] == "What is Python?"
156- # Reasoning effort and web search are enabled
157190 assert call_args [1 ]["reasoning" ]["effort" ] == "xhigh"
191+ assert call_args [1 ]["tools" ] == []
192+
193+ @pytest .mark .asyncio
194+ @patch ("src.bot.cogs.open_ai.get_bot_settings" )
195+ async def test_get_ai_response_web_success (
196+ self , mock_get_settings , openai_cog , mock_bot_settings , mock_openai_response
197+ ):
198+ """Web variant: uses web-grounded instructions and the web_search tool."""
199+ mock_get_settings .return_value = mock_bot_settings
200+
201+ mock_client = MagicMock ()
202+ mock_client .responses .create = AsyncMock (return_value = mock_openai_response )
203+ openai_cog ._openai_client = mock_client
204+
205+ result = await openai_cog ._get_ai_response ("What is Python?" , use_web = True )
206+
207+ assert result == "This is a mock AI response from OpenAI."
208+ call_args = mock_client .responses .create .call_args
209+ assert call_args [1 ]["instructions" ] == openai_cog ._instructions_web
158210 assert call_args [1 ]["tools" ][0 ]["type" ] == "web_search"
211+ assert call_args [1 ]["reasoning" ]["effort" ] == "xhigh"
159212
160213 @pytest .mark .asyncio
161214 @patch ("src.bot.cogs.open_ai.get_bot_settings" )
@@ -171,7 +224,7 @@ async def test_get_ai_response_with_leading_trailing_spaces(
171224 mock_client .responses .create = AsyncMock (return_value = mock_openai_response )
172225 openai_cog ._openai_client = mock_client
173226
174- result = await openai_cog ._get_ai_response ("Test message" )
227+ result = await openai_cog ._get_ai_response ("Test message" , use_web = False )
175228
176229 assert result == "Response with spaces"
177230
@@ -301,7 +354,7 @@ async def test_get_ai_response_system_message_content(
301354 mock_client .responses .create = AsyncMock (return_value = mock_openai_response )
302355 openai_cog ._openai_client = mock_client
303356
304- await openai_cog ._get_ai_response ("Test message" )
357+ await openai_cog ._get_ai_response ("Test message" , use_web = False )
305358
306359 call_args = mock_client .responses .create .call_args [1 ]
307360 assert call_args ["instructions" ] == openai_cog ._instructions
@@ -320,12 +373,12 @@ async def test_get_ai_response_api_parameters(
320373 mock_client .responses .create = AsyncMock (return_value = mock_openai_response )
321374 openai_cog ._openai_client = mock_client
322375
323- await openai_cog ._get_ai_response ("Test message" )
376+ await openai_cog ._get_ai_response ("Test message" , use_web = False )
324377
325378 call_args = mock_client .responses .create .call_args [1 ]
326379 assert call_args ["max_output_tokens" ] is None
327380 assert call_args ["reasoning" ]["effort" ] == "xhigh"
328- assert call_args ["tools" ][ 0 ][ "type" ] == "web_search"
381+ assert call_args ["tools" ] == [] # plain path has no tools
329382 assert "temperature" not in call_args
330383 assert call_args ["model" ] == "gpt-3.5-turbo"
331384
@@ -422,7 +475,7 @@ async def test_get_ai_response_empty_response(self, mock_get_settings, openai_co
422475 mock_client .responses .create = AsyncMock (return_value = mock_response )
423476 openai_cog ._openai_client = mock_client
424477
425- result = await openai_cog ._get_ai_response ("Test message" )
478+ result = await openai_cog ._get_ai_response ("Test message" , use_web = False )
426479
427480 assert result == "" # Should strip to empty string
428481
0 commit comments