|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import pytest |
| 16 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 17 | +from google.genai import types |
| 18 | +from veadk.realtime.doubao_realtime_voice_llm import DoubaoRealtimeVoice |
| 19 | +from google.adk.models.llm_request import LlmRequest |
| 20 | +from google.adk.models.base_llm_connection import BaseLlmConnection |
| 21 | +from google.genai.types import GenerateContentConfig |
| 22 | +import os |
| 23 | +from veadk.realtime.client import DoubaoClient |
| 24 | +from veadk.realtime.doubao_realtime_voice_llm import ( |
| 25 | + _AGENT_ENGINE_TELEMETRY_TAG, |
| 26 | + _AGENT_ENGINE_TELEMETRY_ENV_VARIABLE_NAME, |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +class TestDoubaoRealtimeVoice: |
| 31 | + @pytest.fixture |
| 32 | + def mock_llm_request(self): |
| 33 | + request = MagicMock(spec=LlmRequest) |
| 34 | + request.model = "doubao_realtime_voice" |
| 35 | + request.config = GenerateContentConfig() |
| 36 | + request.config.system_instruction = "Test instruction" |
| 37 | + request.config.tools = [] |
| 38 | + request.live_connect_config = types.LiveConnectConfig( |
| 39 | + http_options=types.HttpOptions() |
| 40 | + ) |
| 41 | + return request |
| 42 | + |
| 43 | + def test_supported_models(self): |
| 44 | + """Test supported_models returns correct model patterns""" |
| 45 | + models = DoubaoRealtimeVoice.supported_models() |
| 46 | + assert isinstance(models, list) |
| 47 | + assert len(models) == 2 |
| 48 | + assert r"doubao_realtime_voice.*" in models |
| 49 | + assert r"Doubao_scene_SLM_Doubao_realtime_voice_model.*" in models |
| 50 | + |
| 51 | + def test_api_client_property(self): |
| 52 | + """Test api_client property returns DoubaoClient with correct options""" |
| 53 | + model = DoubaoRealtimeVoice() |
| 54 | + client = model.api_client |
| 55 | + assert isinstance(client, DoubaoClient) |
| 56 | + assert client._api_client._http_options.retry_options == model.retry_options |
| 57 | + |
| 58 | + def test_live_api_client_property(self): |
| 59 | + """Test _live_api_client property returns DoubaoClient with correct version""" |
| 60 | + model = DoubaoRealtimeVoice() |
| 61 | + client = model._live_api_client |
| 62 | + assert isinstance(client, DoubaoClient) |
| 63 | + assert client._api_client._http_options.api_version == model._live_api_version |
| 64 | + |
| 65 | + def test_tracking_headers_without_env(self): |
| 66 | + """Test _tracking_headers without environment variable""" |
| 67 | + model = DoubaoRealtimeVoice() |
| 68 | + headers = model._tracking_headers |
| 69 | + assert "x-volcengine-api-client" in headers |
| 70 | + assert "user-agent" in headers |
| 71 | + assert _AGENT_ENGINE_TELEMETRY_TAG not in headers["x-volcengine-api-client"] |
| 72 | + |
| 73 | + @patch.dict(os.environ, {_AGENT_ENGINE_TELEMETRY_ENV_VARIABLE_NAME: "test_id"}) |
| 74 | + def test_tracking_headers_with_env(self): |
| 75 | + """Test _tracking_headers with environment variable set""" |
| 76 | + model = DoubaoRealtimeVoice() |
| 77 | + headers = model._tracking_headers |
| 78 | + assert _AGENT_ENGINE_TELEMETRY_TAG in headers["x-volcengine-api-client"] |
| 79 | + |
| 80 | + @pytest.mark.asyncio |
| 81 | + async def test_connect_with_speech_config(self, mock_llm_request): |
| 82 | + """Test connect method with speech config""" |
| 83 | + speech_config = types.SpeechConfig() |
| 84 | + model = DoubaoRealtimeVoice(speech_config=speech_config) |
| 85 | + |
| 86 | + # 修正异步上下文管理器的 mock 设置 |
| 87 | + with patch.object(model._live_api_client.aio.live, "connect") as mock_connect: |
| 88 | + # 创建模拟的异步上下文管理器 |
| 89 | + mock_session = AsyncMock() |
| 90 | + mock_connect.return_value.__aenter__.return_value = mock_session |
| 91 | + |
| 92 | + async with model.connect(mock_llm_request) as connection: |
| 93 | + assert isinstance(connection, BaseLlmConnection) |
| 94 | + assert ( |
| 95 | + mock_llm_request.live_connect_config.speech_config == speech_config |
| 96 | + ) |
| 97 | + mock_connect.assert_called_once_with( |
| 98 | + model=mock_llm_request.model, |
| 99 | + config=mock_llm_request.live_connect_config, |
| 100 | + ) |
| 101 | + |
| 102 | + @pytest.mark.asyncio |
| 103 | + async def test_connect_without_speech_config(self, mock_llm_request): |
| 104 | + """Test connect method without speech config""" |
| 105 | + model = DoubaoRealtimeVoice() |
| 106 | + |
| 107 | + with patch.object(model._live_api_client.aio.live, "connect") as mock_connect: |
| 108 | + # 使用AsyncMock模拟会话对象,更贴近真实场景 |
| 109 | + mock_session = AsyncMock() |
| 110 | + mock_connect.return_value.__aenter__.return_value = mock_session |
| 111 | + |
| 112 | + async with model.connect(mock_llm_request) as connection: |
| 113 | + assert isinstance(connection, BaseLlmConnection) |
| 114 | + # 验证speech_config为None而非检查属性是否存在 |
| 115 | + assert mock_llm_request.live_connect_config.speech_config is None |
| 116 | + mock_connect.assert_called_once_with( |
| 117 | + model=mock_llm_request.model, |
| 118 | + config=mock_llm_request.live_connect_config, |
| 119 | + ) |
0 commit comments