legco_ai_assistant/backend/app/test/test_phase2_ws_asr.py

74 lines
2.1 KiB
Python

"""Phase 2 tests: WebSocket ASR endpoint.
Covers:
- WebSocket connection accepted
- Language parameter defaults and customization
- Client disconnect handled cleanly
- Missing API key returns error
"""
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
@pytest.fixture
def ws_app(monkeypatch):
monkeypatch.setenv("DASHSCOPE_API_KEY", "sk-test-key")
from app.core.config import get_settings
from app.routers.ws_asr import router
get_settings.cache_clear()
app = FastAPI()
app.include_router(router)
return app
@pytest.fixture
def ws_client(ws_app):
return TestClient(ws_app)
class TestWSEndpointAcceptsConnection:
def test_connect_success(self, ws_client):
with ws_client.websocket_connect("/ws/asr/test-video-123") as ws:
pass
def test_connect_with_video_id(self, ws_client):
with ws_client.websocket_connect("/ws/asr/my-video-id") as ws:
pass
class TestWSEndpointLanguageParam:
def test_default_language_is_yue(self, ws_client):
with ws_client.websocket_connect("/ws/asr/test-vid") as ws:
pass
def test_custom_language_param(self, ws_client):
with ws_client.websocket_connect("/ws/asr/test-vid?language=zh") as ws:
pass
def test_english_language_param(self, ws_client):
with ws_client.websocket_connect("/ws/asr/test-vid?language=en") as ws:
pass
class TestWSEndpointHandlesDisconnect:
def test_clean_disconnect(self, ws_client):
with ws_client.websocket_connect("/ws/asr/test-vid") as ws:
pass
class TestWSEndpointMissingApiKey:
def test_missing_api_key_sends_error(self, monkeypatch):
monkeypatch.setenv("DASHSCOPE_API_KEY", "")
from app.core.config import get_settings
from app.routers.ws_asr import router
get_settings.cache_clear()
app = FastAPI()
app.include_router(router)
client = TestClient(app)
with client.websocket_connect("/ws/asr/test-vid") as ws:
msg = ws.receive_json()
assert "error" in msg or "detail" in msg