fix: add transcription provider fallback

This commit is contained in:
2026-06-10 11:47:47 +00:00
parent 6670b5adec
commit 53889da8f6
4 changed files with 183 additions and 7 deletions
+17
View File
@@ -33,6 +33,23 @@ def test_summarize_error_falls_back_to_string():
assert summarize_error({"unexpected": True}, fallback="fallback") == "fallback"
def test_summarize_error_compacts_cloudflare_html():
html = """
<!DOCTYPE html>
<html><head><title>openrouter.ai | 502: Bad gateway</title></head>
<body><h1>Bad gateway</h1><span class="code-label">Error code 502</span></body></html>
"""
assert summarize_error(None, fallback=html) == "Cloudflare/OpenRouter returned HTTP 502 Bad gateway"
def test_summarize_error_truncates_long_plain_text():
message = summarize_error(None, fallback="x" * 600)
assert len(message) < 260
assert message.endswith("")
def test_build_transcript_block_labels_transcript():
block = build_transcript_block("Chris", "Discussed deadlines")
assert block == "[Chris]\nDiscussed deadlines"
+46
View File
@@ -16,6 +16,7 @@ from openrouter_client import (
_chunk_text,
_combined_summary_prompt,
_summary_prompt,
_transcribe_chunk,
summarize,
transcribe,
)
@@ -136,3 +137,48 @@ def test_combined_summary_prompt_requires_requested_sections():
assert "## Action Items" in prompt
assert "## Decisions" in prompt
assert "all dev team members' reports" in prompt
def test_transcribe_chunk_falls_back_to_direct_openai_when_openrouter_fails(tmp_path: Path, monkeypatch):
audio = tmp_path / "chunk-000.wav"
audio.write_bytes(b"audio")
calls = []
async def fake_openrouter(_client, audio_path: str, _headers: dict[str, str]) -> str:
calls.append(("openrouter", Path(audio_path).name))
raise RuntimeError("OpenRouter transcription failed across all models: Cloudflare 502")
async def fake_openai(_client, audio_path: str) -> str:
calls.append(("openai", Path(audio_path).name))
return "direct provider transcript"
monkeypatch.setattr(openrouter_client, "_transcribe_chunk_with_openrouter", fake_openrouter)
monkeypatch.setattr(openrouter_client, "_transcribe_chunk_with_openai", fake_openai)
monkeypatch.setattr(openrouter_client, "_openai_api_key", lambda: "sk-test")
result = asyncio.run(_transcribe_chunk(object(), str(audio), {"Authorization": "Bearer test"}))
assert result == "direct provider transcript"
assert calls == [("openrouter", "chunk-000.wav"), ("openai", "chunk-000.wav")]
def test_transcribe_chunk_reports_provider_fallback_when_all_providers_fail(tmp_path: Path, monkeypatch):
audio = tmp_path / "chunk-000.wav"
audio.write_bytes(b"audio")
async def fake_openrouter(_client, _audio_path: str, _headers: dict[str, str]) -> str:
raise RuntimeError("OpenRouter transcription failed across all models: Cloudflare 502")
monkeypatch.setattr(openrouter_client, "_transcribe_chunk_with_openrouter", fake_openrouter)
monkeypatch.setattr(openrouter_client, "_openai_api_key", lambda: "")
try:
asyncio.run(_transcribe_chunk(object(), str(audio), {"Authorization": "Bearer test"}))
except RuntimeError as exc:
message = str(exc)
else:
raise AssertionError("Expected transcription failure")
assert "Transcription failed across providers" in message
assert "OpenRouter transcription failed" in message
assert "direct OpenAI fallback skipped" in message