"""Testy API regulation_context_provider — per-grant formatting i snapshoty.""" from core.match.matcher_helpers import ( build_nabory_context_for_matcher, format_grant_for_matcher_prompt, ) from core.search.regulation_context_provider import ( format_regulation_context_for_prompt, get_regulation_context_for_candidates, ) from core.search.regulation_snapshot import regulation_snapshot_store def test_get_regulation_context_returns_dict_keyed_by_grant_id(): candidates = [ {"id": "g1", "name": "Program A", "description": "Opis A"}, {"id": "g2", "program": "PARP", "description": "Opis B"}, ] ctx = get_regulation_context_for_candidates(candidates) assert isinstance(ctx, dict) assert "g1" in ctx assert "g2" in ctx assert ctx["g1"][0]["program"] == "Program A" def test_get_regulation_context_uses_snapshots_when_available(): snap = regulation_snapshot_store.create_snapshot( program="PARP|SMART TEST", call_name="SMART TEST", source_url="https://example.com/regulamin.pdf", raw_text="§ 1 Beneficjent musi spełniać warunki kwalifikowalności kosztów kwalifikowalnych.", module="regulamin", ) candidates = [{ "id": "grant-smart", "program": "PARP", "name": "SMART TEST", "regulation_urls": [snap.source_url], }] ctx = get_regulation_context_for_candidates(candidates, k_per_grant=3) assert ctx["grant-smart"] assert ctx["grant-smart"][0]["source"] == "snapshot" assert "kwalifikowalności" in ctx["grant-smart"][0]["excerpt"].lower() def test_format_regulation_context_for_prompt_accepts_grant_id(): ctx = { "g1": [{"program": "Prog1", "doc_role": "regulamin", "excerpt": "Fragment regulaminu 1"}], "g2": [{"program": "Prog2", "doc_role": "ogloszenie", "excerpt": "Fragment regulaminu 2"}], } out = format_regulation_context_for_prompt(ctx, "g1") assert "Prog1" in out assert "[regulamin]" in out assert "Fragment regulaminu 1" in out assert "Prog2" not in out def test_format_regulation_context_for_prompt_without_grant_id_flattens_dict(): ctx = { "g1": [{"program": "Prog1", "doc_role": "rwp", "excerpt": "A"}], "g2": [{"program": "Prog2", "doc_role": "ogloszenie", "excerpt": "B"}], } out = format_regulation_context_for_prompt(ctx) assert "Prog1" in out assert "Prog2" in out assert "[rwp]" in out def test_format_grant_for_matcher_prompt_with_two_arg_formatter(): grant = {"id": "g1", "program": "PARP", "name": "SMART", "description": "Test"} ctx = get_regulation_context_for_candidates([grant]) block = format_grant_for_matcher_prompt( grant, ctx, format_regulation_context_for_prompt, ) assert "g1" in block assert "SMART" in block assert "Test" in block def test_build_nabory_context_for_matcher_no_signature_error(): nabory = [ {"id": "g1", "program": "PARP", "name": "A", "description": "d1"}, {"id": "g2", "program": "FENG", "name": "B", "description": "d2"}, ] ctx = get_regulation_context_for_candidates(nabory) result = build_nabory_context_for_matcher( nabory, ctx, format_regulation_context_for_prompt, ) assert "g1" in result assert "g2" in result