2026-09-04 10:20:40 +08:00

39 lines
1.4 KiB
Python

import pytest
from app.core.config import get_settings
from app.core.exceptions import AppException
from app.services.llm_client import LLMClient
def test_llm_endpoint_supports_openai_v1_base_url(monkeypatch: pytest.MonkeyPatch) -> None:
get_settings.cache_clear()
monkeypatch.setenv("RISK_LLM_API_KEY", "test-key")
monkeypatch.setenv("RISK_LLM_BASE_URL", "https://example.test/v1")
client = LLMClient()
assert client.configured
assert client.endpoint == "https://example.test/v1/chat/completions"
get_settings.cache_clear()
def test_llm_client_accepts_reference_project_environment(monkeypatch: pytest.MonkeyPatch) -> None:
get_settings.cache_clear()
monkeypatch.delenv("RISK_LLM_API_KEY", raising=False)
monkeypatch.setenv("OPENAI_API_KEY", "reference-key")
monkeypatch.setenv("LLM_BASE_URL", "https://reference.test/v1")
monkeypatch.setenv("LLM_MODEL", "reference-model")
client = LLMClient()
assert client.configured
assert client.endpoint == "https://reference.test/v1/chat/completions"
assert client.model == "reference-model"
get_settings.cache_clear()
def test_llm_client_parses_json_and_rejects_non_object() -> None:
parsed = LLMClient._parse_json('```json\n{"intent":"assessment","confidence":0.9}\n```')
assert parsed["intent"] == "assessment"
assert parsed["confidence"] == 0.9
with pytest.raises(AppException):
LLMClient._parse_json("[]")