CoolFace
Apppublic

mrme77/dfars-assistant

sourceHugging Faceupdated 1mo agoView on Hugging Face
0likes
test_answer.py60 linesDownload Raw Back to tests
1"""Tests for OpenRouter answer configuration."""2 3import sys4from types import SimpleNamespace5 6import pytest7 8from src.generation.answer import (9    DEFAULT_OPENROUTER_MODEL,10    _openrouter_headers,11    _load_openrouter_api_key,12    answer_with_openrouter,13)14 15 16def test_default_openrouter_model_is_flash_lite_preview() -> None:17    """It uses the requested Gemini Flash Lite preview model by default."""18    assert DEFAULT_OPENROUTER_MODEL == "google/gemini-2.5-flash-lite-preview-09-2025"19 20 21def test_answer_requires_openrouter_key(monkeypatch: pytest.MonkeyPatch) -> None:22    """It fails loudly when the OpenRouter API key is missing."""23    monkeypatch.delenv("OPENROUTER_API_KEY", raising=False)24 25    with pytest.raises(RuntimeError, match="OPENROUTER_API_KEY is required"):26        answer_with_openrouter("context")27 28 29def test_load_openrouter_api_key_prefers_environment(30    monkeypatch: pytest.MonkeyPatch,31) -> None:32    """It reads the OpenRouter key from the environment first."""33    monkeypatch.setenv("OPENROUTER_API_KEY", "env-key")34 35    assert _load_openrouter_api_key() == "env-key"36 37 38def test_load_openrouter_api_key_falls_back_to_keychain(39    monkeypatch: pytest.MonkeyPatch,40) -> None:41    """It reads the OpenRouter key from keychain when env is empty."""42    fake_keyring = SimpleNamespace(43        get_password=lambda service, account: f"{service}:{account}:key"44    )45    monkeypatch.delenv("OPENROUTER_API_KEY", raising=False)46    monkeypatch.setenv("OPENROUTER_KEYCHAIN_SERVICE", "openrouter")47    monkeypatch.setenv("OPENROUTER_KEYCHAIN_ACCOUNT", "OPENROUTER_API_KEY")48    monkeypatch.setitem(sys.modules, "keyring", fake_keyring)49 50    assert _load_openrouter_api_key() == "openrouter:OPENROUTER_API_KEY:key"51 52 53def test_openrouter_headers_include_app_title(monkeypatch: pytest.MonkeyPatch) -> None:54    """It sends OpenRouter attribution headers for tracking."""55    headers = _openrouter_headers("test-key")56 57    assert headers["X-Title"] == "DFARS App"58    assert headers["HTTP-Referer"] == "http://127.0.0.1:8501"59    assert headers["Authorization"] == "Bearer test-key"60