legco_ai_assistant/backend/app/test/test_phase4_config.py

141 lines
4.7 KiB
Python

"""Phase 4 config tests: system audio and mic capture feature toggles."""
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
@pytest.fixture
def phase4_ws_app(monkeypatch):
monkeypatch.setenv("DASHSCOPE_API_KEY", "sk-test-key")
monkeypatch.setenv("SYSTEM_AUDIO_ENABLED", "true")
monkeypatch.setenv("MIC_ENABLED", "true")
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
class TestWSSourceToggle:
def test_system_audio_source_connects(self, phase4_ws_app):
client = TestClient(phase4_ws_app)
with client.websocket_connect("/ws/asr/test-uuid?source=system-audio") as ws:
pass
def test_mic_source_connects(self, phase4_ws_app):
client = TestClient(phase4_ws_app)
with client.websocket_connect("/ws/asr/test-uuid?source=mic") as ws:
pass
def test_default_source_is_upload(self, phase4_ws_app):
client = TestClient(phase4_ws_app)
with client.websocket_connect("/ws/asr/test-uuid") as ws:
pass
def test_system_audio_disabled_rejects(self, monkeypatch):
monkeypatch.setenv("DASHSCOPE_API_KEY", "sk-test-key")
monkeypatch.setenv("SYSTEM_AUDIO_ENABLED", "false")
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-uuid?source=system-audio") as ws:
data = ws.receive_json()
assert "disabled" in data.get("error", "").lower()
def test_mic_disabled_rejects(self, monkeypatch):
monkeypatch.setenv("DASHSCOPE_API_KEY", "sk-test-key")
monkeypatch.setenv("MIC_ENABLED", "false")
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-uuid?source=mic") as ws:
data = ws.receive_json()
assert "disabled" in data.get("error", "").lower()
def test_config_system_audio_defaults(monkeypatch, tmp_path):
monkeypatch.delenv("SYSTEM_AUDIO_ENABLED", raising=False)
monkeypatch.setenv("LLM_API_KEY", "sk-test")
monkeypatch.setenv("DP_API_KEY", "sk-test")
monkeypatch.setenv("EMBEDDING_API_KEY", "sk-test")
env_file = tmp_path / ".env"
env_file.write_text("")
monkeypatch.chdir(tmp_path)
from app.core.config import Settings, get_settings
get_settings.cache_clear()
settings = Settings(_env_file=())
assert settings.system_audio_enabled is True
def test_config_mic_defaults(monkeypatch, tmp_path):
monkeypatch.delenv("MIC_ENABLED", raising=False)
monkeypatch.setenv("LLM_API_KEY", "sk-test")
monkeypatch.setenv("DP_API_KEY", "sk-test")
monkeypatch.setenv("EMBEDDING_API_KEY", "sk-test")
env_file = tmp_path / ".env"
env_file.write_text("")
monkeypatch.chdir(tmp_path)
from app.core.config import Settings, get_settings
get_settings.cache_clear()
settings = Settings(_env_file=())
assert settings.mic_enabled is True
def test_config_system_audio_disabled(tmp_path, monkeypatch):
env_file = tmp_path / ".env"
env_file.write_text(
"SYSTEM_AUDIO_ENABLED=false\n"
"LLM_API_KEY=sk-test\n"
"DP_API_KEY=sk-test\n"
"EMBEDDING_API_KEY=sk-test\n"
)
monkeypatch.chdir(tmp_path)
from app.core.config import Settings, get_settings
get_settings.cache_clear()
settings = Settings()
assert settings.system_audio_enabled is False
def test_config_mic_disabled(tmp_path, monkeypatch):
env_file = tmp_path / ".env"
env_file.write_text(
"MIC_ENABLED=false\n"
"LLM_API_KEY=sk-test\n"
"DP_API_KEY=sk-test\n"
"EMBEDDING_API_KEY=sk-test\n"
)
monkeypatch.chdir(tmp_path)
from app.core.config import Settings, get_settings
get_settings.cache_clear()
settings = Settings()
assert settings.mic_enabled is False
def test_config_loads_both_toggles_from_env(tmp_path, monkeypatch):
env_file = tmp_path / ".env"
env_file.write_text(
"SYSTEM_AUDIO_ENABLED=true\n"
"MIC_ENABLED=true\n"
"LLM_API_KEY=sk-test\n"
"DP_API_KEY=sk-test\n"
"EMBEDDING_API_KEY=sk-test\n"
)
monkeypatch.chdir(tmp_path)
from app.core.config import Settings, get_settings
get_settings.cache_clear()
settings = Settings()
assert settings.system_audio_enabled is True
assert settings.mic_enabled is True