"""Tests for RAG upload sanitisation (regex path; no live OpenAI).""" from __future__ import annotations import pytest from app.services.document_sanitiser import ( regex_sanitise_text, sanitise_text_for_rag_sync, should_sanitise_for_rag, ) def test_should_skip_kb_tenant(monkeypatch: pytest.MonkeyPatch) -> None: from app.config import settings monkeypatch.setattr(settings, "enable_rag_upload_sanitisation", True) monkeypatch.setattr(settings, "rag_sanitisation_skip_kb_tenant", True) monkeypatch.setattr(settings, "knowledge_base_tenant_id", "__rics_kb__") assert should_sanitise_for_rag("tenant-a") is True assert should_sanitise_for_rag("__rics_kb__") is False def test_regex_removes_postcode_and_email() -> None: raw = ( "The roof covering is slate. Client: jane.doe@example.com. " "Property at 12 High Street, London SW1A 1AA. Price £450,000." ) out = regex_sanitise_text(raw) assert "jane.doe@example.com" not in out assert "SW1A" not in out assert "450,000" not in out assert "slate" in out.lower() def test_sanitise_sync_regex_only_without_llm_flag(monkeypatch: pytest.MonkeyPatch) -> None: from app.config import settings monkeypatch.setattr(settings, "enable_rag_upload_sanitisation", True) monkeypatch.setattr(settings, "rag_sanitisation_use_llm", False) monkeypatch.setattr(settings, "openai_api_key", "sk-test") monkeypatch.setattr(settings, "rag_sanitisation_fail_closed", False) text = "Defect noted at 10 Church Road, Bristol BS1 4ST." out = sanitise_text_for_rag_sync(text, tenant_id="firm-1") assert "BS1" not in out assert "defect" in out.lower() def test_sanitise_sync_without_openai_key(monkeypatch: pytest.MonkeyPatch) -> None: from app.config import settings monkeypatch.setattr(settings, "enable_rag_upload_sanitisation", True) monkeypatch.setattr(settings, "rag_sanitisation_use_llm", False) monkeypatch.setattr(settings, "openai_api_key", "") monkeypatch.setattr(settings, "rag_sanitisation_fail_closed", False) text = "Defect noted at 10 Church Road, Bristol BS1 4ST." out = sanitise_text_for_rag_sync(text, tenant_id="firm-1") assert "BS1" not in out assert "defect" in out.lower()