Files
DeepTutor/tests/services/config/test_context_window_detection.py
jax-novita 160f12ab24 feat(providers): add Novita AI as an LLM gateway (#746)
Registers Novita AI as an OpenAI-compatible gateway (https://api.novita.ai/openai, NOVITA_API_KEY) with base-URL detection and brand aliases.

Also teaches context-window detection about Novita's `context_size` metadata key — without it every Novita model, including the 1M-token ones, planned against the 16K-64K fallback.
2026-08-03 20:38:05 +08:00

149 lines
4.4 KiB
Python

from __future__ import annotations
import asyncio
from deeptutor.services.config import context_window_detection as detection_module
from deeptutor.services.config.context_window_detection import (
detect_context_window,
)
from deeptutor.services.llm.config import LLMConfig
def _config(**overrides):
defaults = {
"model": "gpt-4o-mini",
"api_key": "sk-test",
"base_url": "https://api.example.com/v1",
"effective_url": "https://api.example.com/v1",
"binding": "openai",
"provider_name": "openai",
"provider_mode": "standard",
"api_version": None,
"extra_headers": {},
"reasoning_effort": None,
"max_tokens": 4096,
}
defaults.update(overrides)
return LLMConfig(**defaults)
async def _metadata_128k(*_args, **_kwargs):
return 128000
async def _metadata_none(*_args, **_kwargs):
return None
def test_detect_context_window_prefers_provider_metadata(monkeypatch) -> None:
monkeypatch.setattr(
"deeptutor.services.config.context_window_detection._detect_from_models_endpoint",
_metadata_128k,
)
result = asyncio.run(detect_context_window(_config(model="kimi-k2.6")))
assert result.context_window == 128000
assert result.source == "metadata"
def test_detect_context_window_uses_runtime_default_when_metadata_missing(
monkeypatch,
) -> None:
monkeypatch.setattr(
"deeptutor.services.config.context_window_detection._detect_from_models_endpoint",
_metadata_none,
)
result = asyncio.run(detect_context_window(_config(model="unknown-model", max_tokens=5000)))
assert result.context_window == 20000
assert result.source == "default"
def test_detect_context_window_uses_known_model_metadata_when_provider_omits_window(
monkeypatch,
) -> None:
monkeypatch.setattr(
"deeptutor.services.config.context_window_detection._detect_from_models_endpoint",
_metadata_none,
)
result = asyncio.run(detect_context_window(_config(model="deepseek-v4-flash")))
assert result.context_window == 1_000_000
assert result.source == "known_model"
def test_detect_context_window_uses_known_minimax_m3_window(
monkeypatch,
) -> None:
monkeypatch.setattr(
"deeptutor.services.config.context_window_detection._detect_from_models_endpoint",
_metadata_none,
)
result = asyncio.run(detect_context_window(_config(model="MiniMax-M3")))
assert result.context_window == 1_000_000
assert result.source == "known_model"
def test_extract_context_window_reads_novita_context_size_key() -> None:
"""Novita's /openai/models advertises the window as ``context_size``."""
payload = {
"data": [
{"id": "deepseek/deepseek-v3.2", "context_size": 1_000_000},
]
}
assert (
detection_module._extract_context_window_from_payload(payload, "deepseek/deepseek-v3.2")
== 1_000_000
)
def test_models_endpoint_probe_honors_disable_ssl_verify(monkeypatch) -> None:
captured: dict[str, object] = {}
class FakeConnector:
pass
class FakeResponse:
status = 200
async def __aenter__(self):
return self
async def __aexit__(self, *_args):
return None
async def json(self):
return {"data": [{"id": "gpt-4o-mini", "context_window": 123456}]}
class FakeSession:
def __init__(self, **kwargs):
captured["session_kwargs"] = kwargs
async def __aenter__(self):
return self
async def __aexit__(self, *_args):
return None
def get(self, url, headers):
captured["url"] = url
captured["headers"] = headers
return FakeResponse()
def fake_connector(**kwargs):
captured["connector_kwargs"] = kwargs
return FakeConnector()
monkeypatch.setattr(detection_module, "disable_ssl_verify_enabled", lambda: True)
monkeypatch.setattr(detection_module.aiohttp, "TCPConnector", fake_connector)
monkeypatch.setattr(detection_module.aiohttp, "ClientSession", FakeSession)
result = asyncio.run(detection_module._detect_from_models_endpoint(_config()))
assert result == 123456
assert captured["url"] == "https://api.example.com/v1/models"
assert captured["connector_kwargs"] == {"ssl": False}
assert isinstance(captured["session_kwargs"]["connector"], FakeConnector)