tuts-rag-service / tests /test_citations.py
Gil Almeida
fix: anexar fontes deterministicas no RAG
9f31c97
Raw
History Blame Contribute Delete
2.37 kB
from types import SimpleNamespace
from core.citations import (
build_missing_citations_block,
extract_citations_from_context,
is_smalltalk_message,
resposta_indica_sem_informacao,
)
def test_extracts_pdf_citations_from_context_headers_and_deduplicates():
chunks = [
"[CABE\u00c7ALHO FONTE: redes.pdf:2]\nConteudo A",
"[CABE\u00c7ALHO FONTE: redes.pdf:2]\nConteudo repetido",
"[CABE\u00c7ALHO FONTE: storage.pdf:7]\nConteudo B",
]
citations = extract_citations_from_context(chunks)
assert [citation.token for citation in citations] == [
"[redes.pdf:2]",
"[storage.pdf:7]",
]
def test_extracts_pdf_citations_from_document_metadata():
doc = SimpleNamespace(
page_content="Sem cabecalho",
metadata={"source": "/tmp/material.pdf", "page_number": 3, "chunk_id": "abc"},
)
citations = extract_citations_from_context([doc])
assert len(citations) == 1
assert citations[0].filename == "material.pdf"
assert citations[0].page == 3
assert citations[0].chunk_id == "abc"
def test_build_missing_citations_block_ignores_already_present_tokens():
citations = extract_citations_from_context([
"[CABE\u00c7ALHO FONTE: redes.pdf:2]\nConteudo A",
"[CABE\u00c7ALHO FONTE: storage.pdf:7]\nConteudo B",
])
block = build_missing_citations_block("Resposta com [redes.pdf:2]", citations)
assert block == "\n\nFontes:\n[storage.pdf:7]"
def test_smalltalk_is_detected_without_matching_real_questions():
assert is_smalltalk_message("Ol\u00e1!")
assert is_smalltalk_message("tudo bem?")
assert is_smalltalk_message("obrigado")
assert not is_smalltalk_message("Ola, quais sao as quotas no storage?")
assert not is_smalltalk_message("Explica o plano de storage do TUTS")
def test_detects_lack_of_information():
assert resposta_indica_sem_informacao("Não há informações disponíveis nos materiais sobre X")
assert resposta_indica_sem_informacao("Não encontrei nos materiais")
assert resposta_indica_sem_informacao("Não foi encontrado")
assert resposta_indica_sem_informacao("Não existe informação suficiente")
assert resposta_indica_sem_informacao("Os materiais não indicam isso.")
assert not resposta_indica_sem_informacao("O plano de storage do TUTS oferece 10GB de cota.")