Abmelde-Agent: Fälle, Zeitlauf und Agent-Dienst (Vikunja 97) (#3)

This commit was merged in pull request #3.
This commit is contained in:
Rsclub2_2 committed 2026-11-16 07:41:08 +01:00
1 parent a9ba08bfe0
commit e0e8cad4de
36 files changed
+4385 -8

No files matched your search

+70
View File
@@ -0,0 +1,70 @@
"""Model access through an OpenAI-compatible chat endpoint (Open WebUI)."""
import httpx
import pytest
from llm import LLM, LLMUnavailable
def client(handler):
return httpx.Client(transport=httpx.MockTransport(handler))
def test_ask_sends_model_messages_and_key_and_returns_text():
seen = {}
def handler(request):
seen["url"] = str(request.url)
seen["auth"] = request.headers["authorization"]
seen["body"] = __import__("json").loads(request.content)
return httpx.Response(200, json={"choices": [{"message": {"content": '{"action":"done"}'}}]})
llm = LLM("https://ai.example/", "deepseek-chat", "sk-test", client=client(handler))
assert llm.ask("system text", "user text") == '{"action":"done"}'
assert seen["url"] == "https://ai.example/api/chat/completions"
assert seen["auth"] == "Bearer sk-test"
assert seen["body"]["model"] == "deepseek-chat" and seen["body"]["stream"] is False
assert seen["body"]["messages"] == [{"role": "system", "content": "system text"}, {"role": "user", "content": "user text"}]
@pytest.mark.parametrize("response", [
httpx.Response(401, json={"detail": "bad key"}), httpx.Response(502, text="bad gateway"),
httpx.Response(200, text="<html>login</html>"), httpx.Response(200, json={"choices": []}),
httpx.Response(200, json={"choices": [{"message": {"content": None}}]})])
def test_bad_answers_mean_unavailable(response):
with pytest.raises(LLMUnavailable):
LLM("https://ai.example", "m", "k", client=client(lambda request: response)).ask("s", "u")
def test_network_error_means_unavailable():
def handler(request):
raise httpx.ConnectError("no route")
with pytest.raises(LLMUnavailable):
LLM("https://ai.example", "m", "k", client=client(handler)).ask("s", "u")
def test_error_text_never_contains_the_key():
llm = LLM("https://ai.example", "m", "sk-secret", client=client(lambda r: httpx.Response(401, text="sk-secret is wrong")))
with pytest.raises(LLMUnavailable) as e:
llm.ask("s", "u")
assert "sk-secret" not in str(e.value)
@pytest.mark.parametrize("base_url", ["https://ai.example:abc", "https://ai.ex\x00ample", "http://", "ai.example"])
def test_invalid_endpoint_url_means_unavailable(base_url):
with pytest.raises(LLMUnavailable):
LLM(base_url, "m", "k", client=client(lambda r: httpx.Response(200, json={}))).ask("s", "u")
@pytest.mark.parametrize("given,expected", [(None, 60), (5.5, 5.5), (120, 60)])
def test_timeout_can_be_capped_per_call_but_never_raised(given, expected):
seen = {}
def handler(request):
seen["timeout"] = request.extensions["timeout"]
return httpx.Response(200, json={"choices": [{"message": {"content": "x"}}]})
llm = LLM("https://ai.example", "m", "k", timeout=60, client=client(handler))
llm.ask("s", "u") if given is None else llm.ask("s", "u", timeout=given)
assert seen["timeout"] == {"connect": expected, "read": expected, "write": expected, "pool": expected}