import pytest from backend.app.services.cost_optimization_service import CostOptimizationService from backend.app.services.model_capability_service import ModelCapabilityService class _FakeTruthNode: def __init__(self, node_id: int, tenant_id: int, project_id: int): self.id = node_id self.tenant_id = tenant_id self.project_id = project_id self.title = "t" self.content = {} class _FakeResult: def __init__(self, value): self._value = value def scalar_one_or_none(self): return self._value class _FakeDB: def __init__(self, truth_node): self.truth_node = truth_node async def execute(self, query): _ = query return _FakeResult(self.truth_node) @pytest.mark.asyncio async def test_validate_truth_node_in_project_rejects_cross_project(): truth_node = _FakeTruthNode(node_id=1, tenant_id=1, project_id=2) service = ModelCapabilityService(db=_FakeDB(truth_node), ai_service=None) # type: ignore[arg-type] with pytest.raises(ValueError, match="不属于项目"): await service.validate_truth_node_in_project( truth_node_id=1, tenant_id=1, project_id=1, ) @pytest.mark.asyncio async def test_cost_opt_truth_node_validation_rejects_cross_project(): truth_node = _FakeTruthNode(node_id=1, tenant_id=1, project_id=3) service = CostOptimizationService(db=_FakeDB(truth_node)) # type: ignore[arg-type] with pytest.raises(ValueError, match="不属于项目"): await service.validate_truth_node_in_project( truth_node_id=1, tenant_id=1, project_id=1, )