|
1 | 1 | """Tests for astr_main_agent module.""" |
2 | 2 |
|
| 3 | +import asyncio |
3 | 4 | import datetime |
4 | 5 | import os |
5 | 6 | from contextlib import nullcontext |
|
20 | 21 | from astrbot.core.config.agent_runner import resolve_context_compression_config |
21 | 22 | from astrbot.core.conversation_mgr import Conversation |
22 | 23 | from astrbot.core.cron.manager import CronJobManager |
23 | | -from astrbot.core.message.components import File, Image, Plain, Reply, Video |
| 24 | +from astrbot.core.message.components import File, Image, Plain, Record, Reply, Video |
24 | 25 | from astrbot.core.platform.astr_message_event import AstrMessageEvent |
25 | 26 | from astrbot.core.platform.platform_metadata import PlatformMetadata |
26 | 27 | from astrbot.core.provider import Provider |
@@ -2439,6 +2440,164 @@ async def _raise_video_conversion_error(self): |
2439 | 2440 | for call in mock_logger.error.call_args_list |
2440 | 2441 | ) |
2441 | 2442 |
|
| 2443 | + @pytest.mark.asyncio |
| 2444 | + async def test_build_main_agent_with_voice_attachments( |
| 2445 | + self, mock_event, mock_context, mock_provider |
| 2446 | + ): |
| 2447 | + """Test that resolvable voice attachments keep their local paths.""" |
| 2448 | + module = ama |
| 2449 | + direct_path = str(Path("/path/to/voice.wav")) |
| 2450 | + quoted_path = str(Path("/path/to/quoted.wav")) |
| 2451 | + mock_record = Record(file="direct.amr") |
| 2452 | + mock_quoted_record = Record(file="quoted.amr") |
| 2453 | + mock_reply = Reply( |
| 2454 | + id="reply-1", |
| 2455 | + chain=[mock_quoted_record], |
| 2456 | + sender_nickname="", |
| 2457 | + message_str="quoted message", |
| 2458 | + ) |
| 2459 | + mock_event.message_obj.message = [mock_record, mock_reply] |
| 2460 | + |
| 2461 | + mock_context.get_provider_by_id.return_value = None |
| 2462 | + mock_context.get_using_provider.return_value = mock_provider |
| 2463 | + mock_context.get_config.return_value = {} |
| 2464 | + |
| 2465 | + conv_mgr = mock_context.conversation_manager |
| 2466 | + _setup_conversation_for_build(conv_mgr) |
| 2467 | + |
| 2468 | + async def _resolve_voice(self): |
| 2469 | + return quoted_path if self.file == "quoted.amr" else direct_path |
| 2470 | + |
| 2471 | + with ( |
| 2472 | + patch("astrbot.core.astr_main_agent.AgentRunner") as mock_runner_cls, |
| 2473 | + patch("astrbot.core.astr_main_agent.AstrAgentContext"), |
| 2474 | + patch.object( |
| 2475 | + Record, |
| 2476 | + "convert_to_file_path", |
| 2477 | + _resolve_voice, |
| 2478 | + ), |
| 2479 | + ): |
| 2480 | + mock_runner = MagicMock() |
| 2481 | + mock_runner.reset = AsyncMock() |
| 2482 | + mock_runner_cls.return_value = mock_runner |
| 2483 | + |
| 2484 | + result = await module.build_main_agent( |
| 2485 | + event=mock_event, |
| 2486 | + plugin_context=mock_context, |
| 2487 | + config=module.MainAgentBuildConfig(tool_call_timeout=60), |
| 2488 | + ) |
| 2489 | + |
| 2490 | + assert result is not None |
| 2491 | + assert result.provider_request.audio_urls == [direct_path, quoted_path] |
| 2492 | + extra_texts = [ |
| 2493 | + part.text for part in result.provider_request.extra_user_content_parts |
| 2494 | + ] |
| 2495 | + assert f"[Audio Attachment: path {direct_path}]" in extra_texts |
| 2496 | + assert ( |
| 2497 | + f"[Audio Attachment in quoted message: path {quoted_path}]" in extra_texts |
| 2498 | + ) |
| 2499 | + assert "[Voice unavailable]" not in extra_texts |
| 2500 | + |
| 2501 | + @pytest.mark.asyncio |
| 2502 | + async def test_build_main_agent_skips_unavailable_voice_attachments( |
| 2503 | + self, mock_event, mock_context, mock_provider |
| 2504 | + ): |
| 2505 | + """Unresolvable voice attachments degrade instead of failing the turn. |
| 2506 | +
|
| 2507 | + Both the current message and the quoted chain raise a bare ``Exception``, |
| 2508 | + which ``is_recoverable_image_error`` rejects, so the branches must degrade |
| 2509 | + unconditionally rather than re-raise. |
| 2510 | + """ |
| 2511 | + module = ama |
| 2512 | + mock_record = Record(file="") |
| 2513 | + mock_quoted_record = Record(file="") |
| 2514 | + mock_reply = Reply( |
| 2515 | + id="reply-1", |
| 2516 | + chain=[Plain(text="quoted text"), mock_quoted_record], |
| 2517 | + sender_nickname="", |
| 2518 | + message_str="quoted text", |
| 2519 | + ) |
| 2520 | + mock_event.message_obj.message = [Plain(text="Hello"), mock_reply, mock_record] |
| 2521 | + |
| 2522 | + mock_context.get_provider_by_id.return_value = None |
| 2523 | + mock_context.get_using_provider.return_value = mock_provider |
| 2524 | + mock_context.get_config.return_value = {} |
| 2525 | + |
| 2526 | + conv_mgr = mock_context.conversation_manager |
| 2527 | + _setup_conversation_for_build(conv_mgr) |
| 2528 | + |
| 2529 | + async def _raise_unavailable_voice(self): |
| 2530 | + raise Exception(f"not a valid file: {self.file}") |
| 2531 | + |
| 2532 | + with ( |
| 2533 | + patch("astrbot.core.astr_main_agent.AgentRunner") as mock_runner_cls, |
| 2534 | + patch("astrbot.core.astr_main_agent.AstrAgentContext"), |
| 2535 | + patch.object( |
| 2536 | + Record, |
| 2537 | + "convert_to_file_path", |
| 2538 | + _raise_unavailable_voice, |
| 2539 | + ), |
| 2540 | + ): |
| 2541 | + mock_runner = MagicMock() |
| 2542 | + mock_runner.reset = AsyncMock() |
| 2543 | + mock_runner_cls.return_value = mock_runner |
| 2544 | + |
| 2545 | + result = await module.build_main_agent( |
| 2546 | + event=mock_event, |
| 2547 | + plugin_context=mock_context, |
| 2548 | + config=module.MainAgentBuildConfig(tool_call_timeout=60), |
| 2549 | + ) |
| 2550 | + |
| 2551 | + assert result is not None |
| 2552 | + assert result.provider_request.audio_urls == [] |
| 2553 | + extra_texts = [ |
| 2554 | + part.text for part in result.provider_request.extra_user_content_parts |
| 2555 | + ] |
| 2556 | + assert extra_texts.count("[Voice unavailable]") == 2 |
| 2557 | + assert not any("Audio Attachment" in part for part in extra_texts) |
| 2558 | + |
| 2559 | + @pytest.mark.asyncio |
| 2560 | + async def test_build_main_agent_propagates_voice_cancellation( |
| 2561 | + self, mock_event, mock_context, mock_provider |
| 2562 | + ): |
| 2563 | + """Cancellation must not be swallowed by the voice degrade path.""" |
| 2564 | + module = ama |
| 2565 | + mock_quoted_record = Record(file="quoted.amr") |
| 2566 | + mock_reply = Reply( |
| 2567 | + id="reply-1", |
| 2568 | + chain=[mock_quoted_record], |
| 2569 | + sender_nickname="", |
| 2570 | + message_str="quoted message", |
| 2571 | + ) |
| 2572 | + mock_event.message_obj.message = [Plain(text="Hello"), mock_reply] |
| 2573 | + |
| 2574 | + mock_context.get_provider_by_id.return_value = None |
| 2575 | + mock_context.get_using_provider.return_value = mock_provider |
| 2576 | + mock_context.get_config.return_value = {} |
| 2577 | + |
| 2578 | + conv_mgr = mock_context.conversation_manager |
| 2579 | + _setup_conversation_for_build(conv_mgr) |
| 2580 | + |
| 2581 | + with ( |
| 2582 | + patch("astrbot.core.astr_main_agent.AgentRunner") as mock_runner_cls, |
| 2583 | + patch("astrbot.core.astr_main_agent.AstrAgentContext"), |
| 2584 | + patch.object( |
| 2585 | + Record, |
| 2586 | + "convert_to_file_path", |
| 2587 | + AsyncMock(side_effect=asyncio.CancelledError), |
| 2588 | + ), |
| 2589 | + ): |
| 2590 | + mock_runner = MagicMock() |
| 2591 | + mock_runner.reset = AsyncMock() |
| 2592 | + mock_runner_cls.return_value = mock_runner |
| 2593 | + |
| 2594 | + with pytest.raises(asyncio.CancelledError): |
| 2595 | + await module.build_main_agent( |
| 2596 | + event=mock_event, |
| 2597 | + plugin_context=mock_context, |
| 2598 | + config=module.MainAgentBuildConfig(tool_call_timeout=60), |
| 2599 | + ) |
| 2600 | + |
2442 | 2601 | @pytest.mark.asyncio |
2443 | 2602 | async def test_build_main_agent_no_prompt_no_images( |
2444 | 2603 | self, mock_event, mock_context, mock_provider |
|
0 commit comments