| import os |
|
|
| import pytest |
| from google import genai |
| from google.genai import types |
|
|
| from src.server.config.model_ssot import SYSTEM_MODELS |
| from src.server.services.credential_service import credential_service |
|
|
|
|
| @pytest.mark.integration |
| @pytest.mark.asyncio |
| async def test_audio_semantic_loop_live(client): |
| """ |
| Live Integration Test for TTS Audio Generation and Semantic Validation. |
| """ |
| from tests.conftest import _global_patches |
| genai_patch = next((p for p in _global_patches if hasattr(p, "target") and getattr(p.target, "__name__", "") == "google.genai"), None) |
|
|
| if genai_patch: |
| genai_patch.stop() |
|
|
| try: |
| import respx |
| |
| respx.route(host__regex=r".*generativelanguage\.googleapis\.com").pass_through() |
|
|
| |
| api_key = os.getenv("GEMINI_API_KEY") |
| if not api_key: |
| api_key = await credential_service.get_credential("GEMINI_API_KEY") |
|
|
| if not api_key or api_key == "fake-api-key": |
| pytest.skip("Skipping live integration test: GEMINI_API_KEY not found in environment or DB") |
|
|
| |
| input_text = "The operational parameters of the Archon defense system are currently optimal." |
|
|
| |
| try: |
| response = client.post("/api/audio/generate", json={ |
| "text": input_text, |
| "scene": "commander_briefing", |
| "voice": "Charon" |
| }) |
|
|
| |
| assert response.status_code == 200 |
| assert response.headers.get("content-type") == "audio/wav" |
|
|
| audio_bytes = response.content |
| assert len(audio_bytes) > 100 |
| assert audio_bytes.startswith(b"RIFF") |
| assert b"WAVEfmt" in audio_bytes |
|
|
| |
| genai_client = genai.Client(api_key=api_key) |
|
|
| |
| audio_part = types.Part.from_bytes( |
| data=audio_bytes, |
| mime_type="audio/wav" |
| ) |
|
|
| model_name = SYSTEM_MODELS["DEFAULT_TEXT"].split("/")[-1] |
|
|
| |
| res = genai_client.models.generate_content( |
| model=model_name, |
| contents=[ |
| audio_part, |
| "Transcribe this audio precisely. Return ONLY the transcribed text. Do not add comments or explanations." |
| ] |
| ) |
| except Exception as e: |
| err_msg = str(e) |
| if any(msg in err_msg for msg in ["leaked", "PERMISSION_DENIED", "API key was reported as leaked", "API_KEY_INVALID", "500 INTERNAL"]): |
| pytest.skip(f"Skipping live integration test: API key/Upstream issue ({e})") |
| raise |
|
|
| transcription = res.text |
| assert transcription is not None |
| transcription_lower = transcription.lower() |
|
|
| |
| assert any(word in transcription_lower for word in ["operational", "parameters", "archon", "defense", "system", "optimal"]) |
| finally: |
| if genai_patch: |
| genai_patch.start() |
|
|
|
|