Files
speaking-practice/tests/ai_config_test.py
T

170 lines
5.2 KiB
Python

import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from backend import ai_config
from backend.ai_config import (
get_ai_status,
load_deepseek_config,
load_local_whisper_config,
load_text_ai_config,
)
def test_ai_status_reports_missing_key(monkeypatch):
monkeypatch.delenv("DEEPSEEK_API_KEY", raising=False)
status = get_ai_status()
assert status["configured"] is False
assert "AI_API_KEY" in status["message"]
def test_load_deepseek_config_uses_defaults(monkeypatch):
monkeypatch.setenv("DEEPSEEK_API_KEY", "test-key")
monkeypatch.delenv("DEEPSEEK_BASE_URL", raising=False)
monkeypatch.delenv("DEEPSEEK_MODEL", raising=False)
config = load_deepseek_config()
assert config.api_key == "test-key"
assert config.base_url == "https://api.deepseek.com"
assert config.model == "deepseek-chat"
def test_load_deepseek_config_accepts_generic_ai_names(monkeypatch):
monkeypatch.setenv("AI_API_KEY", "generic-key")
monkeypatch.setenv("AI_BASE_URL", "https://coding.simgreek.com/v1")
monkeypatch.setenv("AI_TEXT_MODEL", "qwen-plus")
monkeypatch.setenv("AI_AUDIO_MODEL", "qwen-omni-turbo")
monkeypatch.setenv("AI_API_PROTOCOL", "anthropic")
monkeypatch.setenv("AI_TRANSLATION_BATCH_SIZE", "15")
monkeypatch.setenv("AI_TIMEOUT_SECONDS", "180")
config = load_deepseek_config()
assert config.api_key == "generic-key"
assert config.base_url == "https://coding.simgreek.com/v1"
assert config.model == "qwen-plus"
assert config.audio_model == "qwen-omni-turbo"
assert config.protocol == "anthropic"
assert config.translation_batch_size == 15
assert config.timeout_seconds == 180
def test_load_split_text_config(monkeypatch):
monkeypatch.setenv("TEXT_AI_API_KEY", "text-key")
monkeypatch.setenv("TEXT_AI_BASE_URL", "https://api.deepseek.com")
monkeypatch.setenv("TEXT_AI_MODEL", "deepseek-chat")
monkeypatch.setenv("TEXT_AI_PROTOCOL", "openai")
text_config = load_text_ai_config()
assert text_config.api_key == "text-key"
assert text_config.base_url == "https://api.deepseek.com"
assert text_config.model == "deepseek-chat"
assert text_config.protocol == "openai"
def test_text_ai_batch_size_allows_zero(monkeypatch):
monkeypatch.setenv("TEXT_AI_API_KEY", "text-key")
monkeypatch.setenv("TEXT_AI_BATCH_SIZE", "0")
config = load_text_ai_config()
assert config.translation_batch_size == 0
def test_load_local_whisper_config(monkeypatch):
monkeypatch.setenv("LOCAL_WHISPER_ENABLED", "true")
monkeypatch.setenv("LOCAL_WHISPER_MODEL", "small")
monkeypatch.setenv("LOCAL_WHISPER_DEVICE", "cpu")
monkeypatch.setenv("LOCAL_WHISPER_COMPUTE_TYPE", "int8")
config = load_local_whisper_config()
assert config.enabled is True
assert config.model == "small"
assert config.device == "cpu"
assert config.compute_type == "int8"
def test_ai_status_uses_configured_provider_name(monkeypatch):
monkeypatch.setenv("AI_API_KEY", "generic-key")
monkeypatch.setenv("AI_PROVIDER", "qwen")
monkeypatch.setenv("AI_TEXT_MODEL", "qwen-plus")
status = get_ai_status()
assert status["configured"] is True
assert status["provider"] == "qwen"
assert status["model"] == "qwen-plus"
assert "AI API" in status["message"]
def test_read_env_file_accepts_utf8_bom():
import tempfile
with tempfile.TemporaryDirectory() as temp_dir:
env_path = Path(temp_dir) / ".env"
env_path.write_bytes(b"\xef\xbb\xbfAI_API_KEY=test-key\nAI_API_PROTOCOL=anthropic\n")
values = ai_config.read_env_file(env_path)
assert values["AI_API_KEY"] == "test-key"
assert values["AI_API_PROTOCOL"] == "anthropic"
class EnvPatch:
def __init__(self):
self.originals = {}
def setenv(self, key, value):
import os
self.originals.setdefault(key, os.environ.get(key))
os.environ[key] = value
def delenv(self, key, raising=True):
import os
self.originals.setdefault(key, os.environ.get(key))
if key in os.environ:
del os.environ[key]
elif raising:
raise KeyError(key)
def restore(self):
import os
for key, value in self.originals.items():
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
if __name__ == "__main__":
test_read_env_file_accepts_utf8_bom()
patch = EnvPatch()
original_read_env_file = ai_config.read_env_file
ai_config.read_env_file = lambda path=ai_config.ENV_PATH: {}
try:
test_ai_status_reports_missing_key(patch)
patch.restore()
test_load_deepseek_config_uses_defaults(patch)
patch.restore()
test_load_deepseek_config_accepts_generic_ai_names(patch)
patch.restore()
test_load_split_text_config(patch)
patch.restore()
test_text_ai_batch_size_allows_zero(patch)
patch.restore()
test_load_local_whisper_config(patch)
patch.restore()
test_ai_status_uses_configured_provider_name(patch)
finally:
patch.restore()
ai_config.read_env_file = original_read_env_file