"""Tests for the include_legal_context parameter on GET /requirements/{id}.""" import pytest from unittest.mock import AsyncMock, patch, MagicMock from compliance.services.rag_client import RAGSearchResult class TestRequirementLegalContext: """Tests for the ?include_legal_context=true query parameter.""" def _make_mock_requirement(self): req = MagicMock() req.id = "req-001" req.regulation_id = "reg-001" req.article = "Art. 35" req.paragraph = "Abs. 1" req.title = "Datenschutz-Folgenabschaetzung" req.description = "Beschreibung" req.requirement_text = "Text" req.breakpilot_interpretation = None req.implementation_status = "not_started" req.implementation_details = None req.code_references = None req.documentation_links = None req.evidence_description = None req.evidence_artifacts = None req.auditor_notes = None req.audit_status = "pending" req.last_audit_date = None req.last_auditor = None req.is_applicable = True req.applicability_reason = None req.priority = 3 req.source_page = None req.source_section = None return req def _make_mock_regulation(self): reg = MagicMock() reg.code = "DSGVO" return reg @pytest.mark.asyncio async def test_legal_context_returns_results(self): """When include_legal_context=true, response should contain legal_context array.""" mock_results = [ RAGSearchResult( text="Art. 35 regelt die DSFA...", regulation_code="eu_2016_679", regulation_name="DSGVO", regulation_short="DSGVO", category="regulation", article="Art. 35", paragraph="", source_url="https://example.com", score=0.92, ) ] with patch("compliance.services.rag_client.get_rag_client") as mock_get_client: client = AsyncMock() client.search.return_value = mock_results mock_get_client.return_value = client # Simulate what the route handler does rag = mock_get_client() results = await rag.search("Datenschutz-Folgenabschaetzung Art. 35", collection="bp_compliance_ce", top_k=3) legal_context = [ { "text": r.text, "regulation_code": r.regulation_code, "regulation_short": r.regulation_short, "article": r.article, "score": r.score, "source_url": r.source_url, } for r in results ] assert len(legal_context) == 1 assert legal_context[0]["regulation_code"] == "eu_2016_679" assert legal_context[0]["article"] == "Art. 35" assert legal_context[0]["score"] == 0.92 @pytest.mark.asyncio async def test_legal_context_error_returns_empty(self): """When RAG search fails, legal_context should be empty array.""" with patch("compliance.services.rag_client.get_rag_client") as mock_get_client: client = AsyncMock() client.search.side_effect = Exception("Connection refused") mock_get_client.return_value = client rag = mock_get_client() try: await rag.search("test", collection="bp_compliance_ce", top_k=3) legal_context = [] # Should not reach here except Exception: legal_context = [] assert legal_context == [] def test_legal_context_not_included_by_default(self): """When include_legal_context is not set, no legal_context key should be present.""" result = { "id": "req-001", "title": "Test", } # Default behavior: no legal_context key assert "legal_context" not in result