mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 16:59:33 +08:00
The cached product catalog lists models the product knows about, not the ones provisioned for the signed-in account. Probe each candidate once and keep only the ones that answer, while preserving the catalog whenever the probe is inconclusive (offline, expired token, 5xx).
80 lines
2.9 KiB
Python
80 lines
2.9 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from deeptutor.services.llm.provider_core.codebuddy_model_probe import (
|
|
AVAILABLE,
|
|
INCONCLUSIVE,
|
|
MISSING,
|
|
NEEDS_REASONING,
|
|
TRANSIENT,
|
|
Verdict,
|
|
business_code,
|
|
classify,
|
|
select_available,
|
|
)
|
|
|
|
|
|
def test_business_code_from_json_and_sse():
|
|
assert business_code('{"code": 11102, "message": "service info not found"}') == "11102"
|
|
assert business_code('data: {"businessCode":"11150"}\n\ndata: [DONE]') == "11150"
|
|
assert business_code('data: {"choices":[{"delta":{"content":"hi"}}]}') is None
|
|
assert business_code("") is None
|
|
|
|
|
|
def test_classify_provisioning_errors():
|
|
assert classify(200, 'data: {"code":"11102"}')[0] == MISSING
|
|
assert classify(400, '{"error":{"code":"11102"}}')[0] == MISSING
|
|
assert classify(400, '{"error":{"code":"11150"}}')[0] == NEEDS_REASONING
|
|
assert classify(200, 'data: {"choices":[]}')[0] == AVAILABLE
|
|
|
|
|
|
def test_auth_and_network_noise_is_inconclusive():
|
|
assert classify(None)[0] == INCONCLUSIVE
|
|
assert classify(401)[0] == INCONCLUSIVE
|
|
assert classify(429)[0] == INCONCLUSIVE
|
|
assert classify(503)[0] == INCONCLUSIVE
|
|
assert classify(400, "unreadable body")[0] == INCONCLUSIVE
|
|
assert classify(400, '{"code":"11134"}')[0] == TRANSIENT
|
|
|
|
|
|
def test_select_available_drops_only_missing_models():
|
|
candidates = ["hunyuan-chat", "not-provisioned", "reasoning", "offline"]
|
|
verdicts = {
|
|
"hunyuan-chat": Verdict(AVAILABLE, at=0),
|
|
"not-provisioned": Verdict(MISSING, "11102", at=0),
|
|
"reasoning": Verdict(NEEDS_REASONING, "11150", at=0),
|
|
"offline": Verdict(INCONCLUSIVE, at=0),
|
|
}
|
|
assert select_available(candidates, verdicts, previous=candidates) == [
|
|
"hunyuan-chat",
|
|
"reasoning",
|
|
"offline",
|
|
]
|
|
assert select_available(candidates, verdicts) == ["hunyuan-chat"]
|
|
assert select_available(candidates, {}, previous=candidates) == candidates
|
|
|
|
|
|
def test_verdict_ttl_and_expiry():
|
|
assert Verdict(AVAILABLE, at=100).ttl == 6 * 3600
|
|
assert Verdict(MISSING, "11102", at=100).ttl == 7 * 24 * 3600
|
|
assert Verdict(INCONCLUSIVE, at=100).ttl == 3600
|
|
assert Verdict(AVAILABLE, at=100).expired(now=100 + 6 * 3600)
|
|
assert not Verdict(AVAILABLE, at=100).expired(now=101)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_available_codebuddy_models_without_login_keeps_catalog(monkeypatch):
|
|
from deeptutor.services.llm.provider_core import codebuddy_model_probe
|
|
|
|
monkeypatch.setattr(codebuddy_model_probe, "load_credentials", lambda: None)
|
|
assert await codebuddy_model_probe.available_codebuddy_models(candidates=["a"]) is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_available_codebuddy_models_can_be_switched_off(monkeypatch):
|
|
from deeptutor.services.llm.provider_core import codebuddy_model_probe
|
|
|
|
monkeypatch.setenv("DEEPTUTOR_CODEBUDDY_PROBE_MODELS", "0")
|
|
assert await codebuddy_model_probe.available_codebuddy_models(candidates=["a"]) is None
|