2026-09-09 21:55:37 +08:00
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from app.service.relationship_service import GraphDegradedResult, RelationshipService
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FailingGraph:
|
|
|
|
|
async def execute_query(self, query: str, **parameters: object) -> list[object]:
|
|
|
|
|
raise TimeoutError("neo4j timeout")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_neo4j_timeout_returns_explicit_degraded_result() -> None:
|
|
|
|
|
result = await RelationshipService(FailingGraph()).neighbors(1, "HOLDS")
|
|
|
|
|
assert len(result) == 1
|
|
|
|
|
assert isinstance(result[0], GraphDegradedResult)
|
|
|
|
|
assert result[0].degraded is True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_neo4j_rejects_unapproved_relationship() -> None:
|
|
|
|
|
with pytest.raises(ValueError, match="not allowed"):
|
|
|
|
|
await RelationshipService(FailingGraph()).neighbors(1, "DELETE_ALL")
|
|
|
|
|
|
|
|
|
|
|
2026-09-11 14:14:50 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_portfolio_graph_context_uses_fixed_query_and_filters_noise() -> None:
|
|
|
|
|
class Graph:
|
|
|
|
|
async def execute_query(self, query: str, **parameters: object) -> list[object]:
|
|
|
|
|
assert "HOLDS" in query and "EXPOSED_TO_INDUSTRY" in query
|
|
|
|
|
assert parameters == {"customer_id": 7, "limit": 20}
|
|
|
|
|
return [
|
|
|
|
|
{"industry_name": "科技", "product_count": 2},
|
|
|
|
|
{"industry_name": "单产品行业", "product_count": 1},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
result = await RelationshipService(Graph()).portfolio_industry_context(7)
|
|
|
|
|
assert result == {
|
|
|
|
|
"degraded": False,
|
|
|
|
|
"overlaps": [
|
|
|
|
|
{"industry_name": "科技", "product_count": 2},
|
|
|
|
|
{"industry_name": "单产品行业", "product_count": 1},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-09-09 21:55:37 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_model_router_excludes_unhealthy_endpoint() -> None:
|
|
|
|
|
from app.model.configuration import ModelEndpointConfig
|
|
|
|
|
from app.service.model_router_service import ModelRouterService
|
|
|
|
|
|
|
|
|
|
primary = ModelEndpointConfig(
|
|
|
|
|
id=1,
|
|
|
|
|
endpoint_code="primary",
|
|
|
|
|
provider="x",
|
|
|
|
|
model_name="m1",
|
|
|
|
|
base_url="http://x",
|
|
|
|
|
secret_ref="secret://one",
|
|
|
|
|
capabilities=["chat"],
|
|
|
|
|
status="active",
|
|
|
|
|
timeout_ms=1000,
|
|
|
|
|
)
|
|
|
|
|
fallback = ModelEndpointConfig(
|
|
|
|
|
id=2,
|
|
|
|
|
endpoint_code="fallback",
|
|
|
|
|
provider="x",
|
|
|
|
|
model_name="m2",
|
|
|
|
|
base_url="http://y",
|
|
|
|
|
secret_ref="secret://two",
|
|
|
|
|
capabilities=["chat"],
|
|
|
|
|
status="active",
|
|
|
|
|
timeout_ms=1000,
|
|
|
|
|
)
|
|
|
|
|
session = AsyncMock()
|
|
|
|
|
session.scalars.return_value = [primary, fallback]
|
|
|
|
|
router = ModelRouterService(session)
|
|
|
|
|
router.mark_health("primary", False)
|
|
|
|
|
selected = await router.select_with_fallback(
|
|
|
|
|
agent_type="customer_service", task_type="chat", fallback_codes=["primary", "fallback"]
|
|
|
|
|
)
|
|
|
|
|
assert [endpoint.endpoint_code for endpoint in selected] == ["fallback"]
|