mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
234 lines
No EOL
8.2 KiB
Python
234 lines
No EOL
8.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script for Google Cloud Speech-to-Text v2 ASR Provider
|
|
This script validates the provider implementation without requiring actual credentials
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
import asyncio
|
|
from unittest.mock import Mock, patch, MagicMock
|
|
|
|
# Add the project root to Python path
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
# Mock the Google Cloud Speech v2 imports before importing our provider
|
|
sys.modules['google.cloud.speech_v2'] = Mock()
|
|
sys.modules['google.api_core.exceptions'] = Mock()
|
|
|
|
from core.providers.asr.google_speech_v2 import ASRProvider
|
|
|
|
def test_provider_initialization():
|
|
"""Test that the provider initializes correctly with valid configuration"""
|
|
config = {
|
|
"project_id": "test-project-123",
|
|
"location": "global",
|
|
"credentials_path": "/path/to/credentials.json",
|
|
"model": "chirp_2",
|
|
"language_codes": ["en-US"],
|
|
"sample_rate_hertz": 16000,
|
|
"encoding": "LINEAR16",
|
|
"enable_automatic_punctuation": True,
|
|
"enable_word_time_offsets": False,
|
|
"enable_word_confidence": False,
|
|
"output_dir": "./tmp/"
|
|
}
|
|
|
|
print("Testing provider initialization...")
|
|
|
|
# Mock the Google Cloud client
|
|
with patch('core.providers.asr.google_speech_v2.speech_v2.SpeechClient') as mock_client:
|
|
mock_instance = Mock()
|
|
mock_client.return_value = mock_instance
|
|
|
|
try:
|
|
provider = ASRProvider(config, delete_audio_file=True)
|
|
print("[PASS] Provider initialized successfully")
|
|
|
|
# Verify configuration
|
|
assert provider.project_id == "test-project-123"
|
|
assert provider.model == "chirp_2"
|
|
assert provider.language_codes == ["en-US"]
|
|
assert provider.sample_rate_hertz == 16000
|
|
print("[PASS] Configuration validated")
|
|
|
|
# Verify recognizer name format
|
|
expected_recognizer = f"projects/{config['project_id']}/locations/{config['location']}/recognizers/_"
|
|
assert provider.recognizer_name == expected_recognizer
|
|
print("[PASS] Recognizer name format correct")
|
|
|
|
except Exception as e:
|
|
print(f"[FAIL] Provider initialization failed: {e}")
|
|
return False
|
|
|
|
return True
|
|
|
|
def test_audio_encoding_mapping():
|
|
"""Test the audio encoding mapping function"""
|
|
config = {
|
|
"project_id": "test-project",
|
|
"encoding": "LINEAR16"
|
|
}
|
|
|
|
print("Testing audio encoding mapping...")
|
|
|
|
with patch('core.providers.asr.google_speech_v2.speech_v2.SpeechClient'):
|
|
provider = ASRProvider(config, delete_audio_file=True)
|
|
|
|
# Test various encoding mappings
|
|
test_cases = [
|
|
("LINEAR16", "LINEAR16"),
|
|
("FLAC", "FLAC"),
|
|
("MP3", "MP3"),
|
|
("OGG_OPUS", "OGG_OPUS"),
|
|
]
|
|
|
|
for input_encoding, expected in test_cases:
|
|
provider.encoding = input_encoding
|
|
result = provider._get_audio_encoding()
|
|
print(f"[PASS] {input_encoding} -> {result}")
|
|
|
|
return True
|
|
|
|
async def test_speech_to_text_mock():
|
|
"""Test the speech_to_text method with mocked Google Cloud API response"""
|
|
config = {
|
|
"project_id": "test-project",
|
|
"location": "global",
|
|
"model": "chirp_2",
|
|
"language_codes": ["en-US"],
|
|
"sample_rate_hertz": 16000,
|
|
"encoding": "LINEAR16",
|
|
"output_dir": "./tmp/"
|
|
}
|
|
|
|
print("Testing speech_to_text method...")
|
|
|
|
# Create mock audio data (simulating Opus packets)
|
|
mock_opus_data = [b"fake_opus_packet_1", b"fake_opus_packet_2"]
|
|
|
|
# Mock the Google Cloud response
|
|
mock_alternative = Mock()
|
|
mock_alternative.transcript = "Hello, this is a test transcript"
|
|
mock_alternative.confidence = 0.95
|
|
|
|
mock_result = Mock()
|
|
mock_result.alternatives = [mock_alternative]
|
|
|
|
mock_channel = Mock()
|
|
mock_channel.alternatives = [mock_alternative]
|
|
|
|
mock_response = Mock()
|
|
mock_response.results = [mock_result]
|
|
|
|
with patch('core.providers.asr.google_speech_v2.speech_v2.SpeechClient') as mock_client_class:
|
|
mock_client = Mock()
|
|
mock_client_class.return_value = mock_client
|
|
mock_client.recognize.return_value = mock_response
|
|
|
|
# Mock other dependencies
|
|
with patch('core.providers.asr.google_speech_v2.ASRProviderBase.decode_opus') as mock_decode, \
|
|
patch('core.providers.asr.google_speech_v2.ASRProviderBase.save_audio_to_file') as mock_save, \
|
|
patch('core.providers.asr.google_speech_v2.ASRProviderBase.log_audio_transcript') as mock_log, \
|
|
patch('os.makedirs'):
|
|
|
|
# Configure mocks
|
|
mock_decode.return_value = [b"pcm_data_1", b"pcm_data_2"]
|
|
mock_save.return_value = "/tmp/test_audio.wav"
|
|
|
|
provider = ASRProvider(config, delete_audio_file=False)
|
|
|
|
# Test the speech_to_text method
|
|
try:
|
|
result_text, file_path = await provider.speech_to_text(
|
|
mock_opus_data,
|
|
session_id="test_session_123",
|
|
audio_format="opus"
|
|
)
|
|
|
|
print(f"[PASS] Speech-to-text completed")
|
|
print(f" Result: '{result_text}'")
|
|
print(f" File path: {file_path}")
|
|
|
|
# Verify the result
|
|
assert result_text == "Hello, this is a test transcript"
|
|
assert file_path == "/tmp/test_audio.wav"
|
|
|
|
# Verify Google Cloud API was called correctly
|
|
mock_client.recognize.assert_called_once()
|
|
print("[PASS] Google Cloud API called with correct parameters")
|
|
|
|
except Exception as e:
|
|
print(f"[FAIL] Speech-to-text test failed: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
return True
|
|
|
|
def test_configuration_validation():
|
|
"""Test configuration validation and edge cases"""
|
|
print("Testing configuration validation...")
|
|
|
|
# Test minimal configuration
|
|
minimal_config = {
|
|
"project_id": "test-project"
|
|
}
|
|
|
|
with patch('core.providers.asr.google_speech_v2.speech_v2.SpeechClient'), \
|
|
patch('os.makedirs'):
|
|
provider = ASRProvider(minimal_config, delete_audio_file=True)
|
|
|
|
# Verify defaults
|
|
assert provider.location == "global"
|
|
assert provider.model == "chirp_2"
|
|
assert provider.language_codes == ["en-US"]
|
|
assert provider.sample_rate_hertz == 16000
|
|
assert provider.encoding == "LINEAR16"
|
|
|
|
print("[PASS] Default values applied correctly")
|
|
|
|
return True
|
|
|
|
def main():
|
|
"""Run all tests"""
|
|
print("Testing Google Cloud Speech-to-Text v2 ASR Provider")
|
|
print("=" * 60)
|
|
|
|
tests = [
|
|
("Provider Initialization", test_provider_initialization),
|
|
("Audio Encoding Mapping", test_audio_encoding_mapping),
|
|
("Configuration Validation", test_configuration_validation),
|
|
("Speech-to-Text (Mocked)", lambda: asyncio.run(test_speech_to_text_mock())),
|
|
]
|
|
|
|
passed = 0
|
|
total = len(tests)
|
|
|
|
for test_name, test_func in tests:
|
|
print(f"\nRunning: {test_name}")
|
|
try:
|
|
if test_func():
|
|
print(f"[PASS] {test_name}: PASSED")
|
|
passed += 1
|
|
else:
|
|
print(f"[FAIL] {test_name}: FAILED")
|
|
except Exception as e:
|
|
print(f"[ERROR] {test_name}: ERROR - {e}")
|
|
|
|
print("\n" + "=" * 60)
|
|
print(f"Test Results: {passed}/{total} tests passed")
|
|
|
|
if passed == total:
|
|
print("All tests passed! Google Speech v2 provider is ready to use.")
|
|
print("\nNext steps:")
|
|
print("1. Install dependencies: pip install google-cloud-speech>=2.26.0")
|
|
print("2. Set up Google Cloud credentials")
|
|
print("3. Update selected_module.ASR in config.yaml to: GoogleSpeechV2")
|
|
print("4. Configure your Google Cloud project settings")
|
|
else:
|
|
print("Some tests failed. Please check the implementation.")
|
|
sys.exit(1)
|
|
|
|
if __name__ == "__main__":
|
|
main() |