refac: modularize apis VII

This commit is contained in:
Alpha Nerd 2026-05-19 14:57:39 +02:00
parent e74f5d1ba6
commit 4b5a70e787
Signed by: alpha-nerd
SSH key fingerprint: SHA256:QkkAgVoYi9TQ0UKPkiKSfnerZy2h4qhi3SVPXJmBN+M
7 changed files with 2244 additions and 2108 deletions

View file

@ -10,6 +10,7 @@ import pytest
from fastapi import HTTPException
import router
from api import openai as api_openai
_BYPASS = HTTPException(status_code=599, detail="bypassed")
@ -47,8 +48,8 @@ class TestOpenAIChatCompletionsCacheHit:
# Patch the route's references to both helpers — they're imported by name
# into router's namespace at module load time.
with (
patch.object(router, "get_llm_cache", return_value=fake),
patch.object(router, "choose_endpoint",
patch.object(api_openai, "get_llm_cache", return_value=fake),
patch.object(api_openai, "choose_endpoint",
AsyncMock(side_effect=AssertionError("backend must not be reached"))),
):
resp = await client.post(
@ -70,8 +71,8 @@ class TestOpenAIChatCompletionsCacheHit:
async def test_stream_cache_hit_returns_sse(self, client, cache_hit_payload):
fake = _FakeCache(cache_hit_payload)
with (
patch.object(router, "get_llm_cache", return_value=fake),
patch.object(router, "choose_endpoint",
patch.object(api_openai, "get_llm_cache", return_value=fake),
patch.object(api_openai, "choose_endpoint",
AsyncMock(side_effect=AssertionError("backend must not be reached"))),
):
resp = await client.post(
@ -98,8 +99,8 @@ class TestOpenAIChatCompletionsCacheHit:
"""When nomyo.cache=False, get_chat is never called even if a cache exists."""
fake = _FakeCache(b"") # has a response, but should never be consulted
with (
patch.object(router, "get_llm_cache", return_value=fake),
patch.object(router, "choose_endpoint",
patch.object(api_openai, "get_llm_cache", return_value=fake),
patch.object(api_openai, "choose_endpoint",
AsyncMock(side_effect=_BYPASS)),
):
resp = await client.post(
@ -117,8 +118,8 @@ class TestOpenAIChatCompletionsCacheHit:
async def test_no_cache_configured_bypasses_cache_check(self, client):
"""get_llm_cache() returning None should not break the route."""
with (
patch.object(router, "get_llm_cache", return_value=None),
patch.object(router, "choose_endpoint",
patch.object(api_openai, "get_llm_cache", return_value=None),
patch.object(api_openai, "choose_endpoint",
AsyncMock(side_effect=_BYPASS)),
):
resp = await client.post(
@ -140,8 +141,8 @@ class TestOpenAICompletionsCacheHit:
async def test_nonstream_cache_hit(self, client, cache_hit_payload):
fake = _FakeCache(cache_hit_payload)
with (
patch.object(router, "get_llm_cache", return_value=fake),
patch.object(router, "choose_endpoint",
patch.object(api_openai, "get_llm_cache", return_value=fake),
patch.object(api_openai, "choose_endpoint",
AsyncMock(side_effect=AssertionError("backend must not be reached"))),
):
resp = await client.post(
@ -163,8 +164,8 @@ class TestOpenAICompletionsCacheHit:
async def test_stream_cache_hit(self, client, cache_hit_payload):
fake = _FakeCache(cache_hit_payload)
with (
patch.object(router, "get_llm_cache", return_value=fake),
patch.object(router, "choose_endpoint",
patch.object(api_openai, "get_llm_cache", return_value=fake),
patch.object(api_openai, "choose_endpoint",
AsyncMock(side_effect=AssertionError("backend must not be reached"))),
):
resp = await client.post(