"""Testy kontekstu regulaminowego w generatorze (external_context → snapshot boost).""" from unittest.mock import MagicMock, patch from agents.generator_agent import DocumentGeneratorAgent def _state(**overrides): base = { "project_id": "proj-1", "namespace": "tenant_test", "document_type": "Eurogranty PARP", "project_description": "Projekt B+R", "external_context": { "regulation_snapshot_id": "snap-abc", "regulation_urls": ["https://example.com/reg.pdf"], "required_sections": ["Opis projektu", "Budżet"], "grant_name": "Eurogranty", "program_type": "PARP", }, } base.update(overrides) return base @patch("core.search.regulation_snapshot.regulation_snapshot_store") def test_build_regulation_boost_uses_snapshot_id(mock_store): snap = MagicMock() snap.key_rules = ["Reguła 1", "Reguła 2"] snap.version_hash = "hash123" mock_store.get_by_id.return_value = snap agent = DocumentGeneratorAgent() boost = agent._build_regulation_boost(_state()) assert "[REGULATION SNAPSHOT v5.0" in boost assert "Reguła 1" in boost mock_store.get_by_id.assert_called_once_with("snap-abc") def test_regulation_context_present_from_external_context(): agent = DocumentGeneratorAgent() assert agent._regulation_context_present(_state(), context="Brak RAG") is True def test_regulation_context_present_from_formatted_boost(): agent = DocumentGeneratorAgent() ctx = "Kontekst regulaminowy (wiele dokumentów):\n- [regulamin] Test" assert agent._regulation_context_present({}, context=ctx) is True @patch("agents.generator_agent.DocumentGeneratorAgent._build_regulation_boost") @patch("agents.generator_agent.get_parent_document_retriever") def test_fetch_context_includes_regulation_boost(mock_retriever, mock_boost): mock_retriever.return_value.invoke.return_value = [] mock_boost.return_value = "\n\n[REGULATION SNAPSHOT v5.0 - TEST]:\n- reguła\n" agent = DocumentGeneratorAgent() out = agent.fetch_context( { **_state(), "sections_plan": [{"title": "Wstęp", "type": "intro"}], "current_section_idx": 0, "graph_step": 0, "traceability_data": {}, } ) assert "[REGULATION SNAPSHOT v5.0" in out["context"] mock_boost.assert_called_once() def test_build_generator_external_context_extracts_regulation_fields(): from endpoints.generator import _build_generator_external_context project = MagicMock() project.external_context = { "regulation_snapshot_id": "snap-1", "regulation_urls": ["https://a.pdf", "https://b.pdf"], "regulation_url": "https://a.pdf", "required_sections": ["Sekcja A"], "grant_name": "Program X", } project.program_name = "Program X" project.program_type = "PARP" project.grant_id = "grant-1" ctx = _build_generator_external_context(project) assert ctx["regulation_snapshot_id"] == "snap-1" assert ctx["required_sections"] == ["Sekcja A"] assert "https://a.pdf" in ctx["regulation_urls"] assert ctx["grant_name"] == "Program X"