|
| 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 queue |
| 16 | +import json |
| 17 | +import base64 |
| 18 | +import requests |
| 19 | +from unittest import TestCase |
| 20 | +from unittest.mock import patch, MagicMock |
| 21 | +from google.adk.tools import ToolContext |
| 22 | +from veadk.tools.builtin_tools.tts import ( |
| 23 | + text_to_speech, |
| 24 | + handle_server_response, |
| 25 | + save_output_to_file, |
| 26 | + _audio_player_thread, |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +class TestTTS(TestCase): |
| 31 | + def setUp(self): |
| 32 | + self.mock_tool_context = MagicMock(spec=ToolContext) |
| 33 | + self.mock_tool_context._invocation_context = MagicMock() |
| 34 | + self.mock_tool_context._invocation_context.user_id = "test_user" |
| 35 | + |
| 36 | + # Mock environment variables |
| 37 | + self.patcher_env = patch.dict( |
| 38 | + "os.environ", |
| 39 | + { |
| 40 | + "TOOL_VESPEECH_APP_ID": "test_app_id", |
| 41 | + "TOOL_VESPEECH_API_KEY": "test_api_key", |
| 42 | + "TOOL_VESPEECH_SPEAKER": "test_speaker", |
| 43 | + }, |
| 44 | + ) |
| 45 | + self.patcher_env.start() |
| 46 | + |
| 47 | + def tearDown(self): |
| 48 | + self.patcher_env.stop() |
| 49 | + |
| 50 | + @patch("requests.Session") |
| 51 | + def test_tts_success(self, mock_session): |
| 52 | + """Test successful TTS request""" |
| 53 | + # Setup mock response |
| 54 | + mock_response = MagicMock() |
| 55 | + mock_response.headers = {"X-Tt-Logid": "test_log_id"} |
| 56 | + mock_response.iter_lines.return_value = [ |
| 57 | + json.dumps({"code": 0, "data": base64.b64encode(b"audio_chunk").decode()}), |
| 58 | + json.dumps({"code": 20000000}), |
| 59 | + ] |
| 60 | + mock_session.return_value.post.return_value = mock_response |
| 61 | + |
| 62 | + # Call function |
| 63 | + result = text_to_speech("test text", self.mock_tool_context) |
| 64 | + |
| 65 | + # Assertions |
| 66 | + self.assertIsInstance(result, dict) |
| 67 | + self.assertIn("saved_audio_path", result) |
| 68 | + mock_session.return_value.post.assert_called_once() |
| 69 | + mock_response.close.assert_called_once() |
| 70 | + |
| 71 | + @patch("requests.Session") |
| 72 | + def test_tts_failure(self, mock_session): |
| 73 | + """Test TTS request failure""" |
| 74 | + # Setup mock to raise exception |
| 75 | + mock_session.return_value.post.side_effect = ( |
| 76 | + requests.exceptions.RequestException("Test error") |
| 77 | + ) |
| 78 | + |
| 79 | + # Call function |
| 80 | + result = text_to_speech("test text", self.mock_tool_context) |
| 81 | + |
| 82 | + # Assertions |
| 83 | + self.assertIsInstance(result, dict) |
| 84 | + self.assertIn("error", result) |
| 85 | + self.assertIn("Test error", result["error"]) |
| 86 | + mock_session.return_value.post.assert_called_once() |
| 87 | + |
| 88 | + @patch("builtins.open") |
| 89 | + def test_handle_server_response_success(self, mock_open): |
| 90 | + """Test successful response handling""" |
| 91 | + # Setup mock response |
| 92 | + mock_response = MagicMock() |
| 93 | + mock_response.iter_lines.return_value = [ |
| 94 | + json.dumps({"code": 0, "data": base64.b64encode(b"audio_chunk").decode()}), |
| 95 | + json.dumps({"code": 20000000}), |
| 96 | + ] |
| 97 | + |
| 98 | + # Call function |
| 99 | + handle_server_response(mock_response, "test.pcm") |
| 100 | + |
| 101 | + # Assertions |
| 102 | + mock_open.assert_called_once_with("test.pcm", "wb") |
| 103 | + |
| 104 | + @patch("builtins.open") |
| 105 | + def test_save_output_to_file_success(self, mock_open): |
| 106 | + """Test successful audio file save""" |
| 107 | + # Setup mock file handler |
| 108 | + mock_file = MagicMock() |
| 109 | + mock_open.return_value.__enter__.return_value = mock_file |
| 110 | + |
| 111 | + # Call function |
| 112 | + save_output_to_file(b"audio_data", "test.pcm") |
| 113 | + |
| 114 | + # Assertions |
| 115 | + mock_open.assert_called_once_with("test.pcm", "wb") |
| 116 | + mock_file.write.assert_called_once_with(b"audio_data") |
| 117 | + |
| 118 | + @patch("time.sleep") |
| 119 | + def test_audio_player_thread(self, mock_sleep): |
| 120 | + """Test audio player thread""" |
| 121 | + # Setup test data |
| 122 | + mock_queue = MagicMock() |
| 123 | + mock_queue.get.side_effect = [b"audio_data", queue.Empty] |
| 124 | + mock_stream = MagicMock() |
| 125 | + stop_event = MagicMock() |
| 126 | + stop_event.is_set.side_effect = [False, True] |
| 127 | + |
| 128 | + # Call function |
| 129 | + _audio_player_thread(mock_queue, mock_stream, stop_event) |
| 130 | + |
| 131 | + # Assertions |
| 132 | + mock_stream.write.assert_called_once_with(b"audio_data") |
| 133 | + mock_queue.task_done.assert_called_once() |
0 commit comments