Files
ewoooc/tests/test_elephant_service.py
OoO 91ad98e621
All checks were successful
CD Pipeline / deploy (push) Successful in 1m48s
feat(ai): 強化 ElephantAlpha NIM fallback
2026-04-30 09:33:39 +08:00

67 lines
2.0 KiB
Python

import requests
class FakeResponse:
def __init__(self, status_code, payload):
self.status_code = status_code
self._payload = payload
def raise_for_status(self):
if self.status_code >= 400:
error = requests.HTTPError(f"{self.status_code} error")
error.response = self
raise error
def json(self):
return self._payload
def test_elephant_service_falls_back_when_primary_model_is_unavailable(monkeypatch):
from services import elephant_service as module
calls = []
def fake_post(_url, json, headers, timeout):
calls.append(json["model"])
if json["model"] == "nvidia/unavailable":
return FakeResponse(404, {"detail": "function not found"})
return FakeResponse(
200,
{
"choices": [{"message": {"content": "OK"}}],
"usage": {"prompt_tokens": 3, "completion_tokens": 2},
},
)
monkeypatch.setattr(module, "ELEPHANT_FALLBACK_MODELS", ["nvidia/available"])
monkeypatch.setattr(module.requests, "post", fake_post)
service = module.ElephantService(api_key="test-key", model="nvidia/unavailable")
result = service.generate("hello")
assert result.success is True
assert result.model == "nvidia/available"
assert result.content == "OK"
assert calls == ["nvidia/unavailable", "nvidia/available"]
def test_elephant_service_uses_reasoning_content_when_content_is_empty(monkeypatch):
from services import elephant_service as module
def fake_post(_url, json, headers, timeout):
return FakeResponse(
200,
{
"choices": [{"message": {"content": None, "reasoning_content": "thinking"}}],
"usage": {},
},
)
monkeypatch.setattr(module.requests, "post", fake_post)
service = module.ElephantService(api_key="test-key", model="nvidia/available")
result = service.generate("hello")
assert result.success is True
assert result.content == "thinking"