test_retrieval_logging.py 2.6 KB
from types import SimpleNamespace

from src.rag.retrieval import Candidate, _candidate_log_items, _chunk_log_items


def test_candidate_log_items_include_source_score_and_metadata():
    candidate = Candidate(
        child_chunk_id="child-1",
        parent_chunk_id="parent-1",
        file_id="file-1",
        retrieval_type="direct",
        hybrid_score=0.87654,
        payload={
            "source_name": "source.pdf",
            "chunk_index": 12,
            "page_start": 3,
            "page_end": 4,
            "section_title": "features",
            "brand": "brand-a",
            "business_category": "vehicle",
            "content_kind": "text",
        },
    )

    items = _candidate_log_items([candidate], limit=5)

    assert items == [
        {
            "rank": 1,
            "type": "direct",
            "score": 0.8765,
            "child": "child-1",
            "parent": "parent-1",
            "file_id": "file-1",
            "source": "source.pdf",
            "chunk_index": 12,
            "page": "3-4",
            "section": "features",
            "kind": "text",
            "brand": "brand-a",
            "category": "vehicle",
        }
    ]


def test_chunk_log_items_include_scores_location_and_preview():
    chunk = SimpleNamespace(
        chunk_id="parent-1",
        child_chunk_id="child-1",
        file_id="file-1",
        source_name="source.pdf",
        chunk_index=12,
        page_start=3,
        page_end=3,
        row_start=None,
        row_end=None,
        title_path="features > cockpit",
        section_title="features",
        content_kind="text",
        brand_series="brand-a",
        business_category="vehicle",
        retrieval_type="direct",
        vector_score=0.45678,
        final_score=0.45678,
        rerank_score=0.81234,
        is_neighbor=False,
        metadata_json={},
        content="line one\nline two",
    )

    items = _chunk_log_items([chunk], limit=5, include_preview=True)

    assert items == [
        {
            "rank": 1,
            "chunk_id": "parent-1",
            "child": "child-1",
            "file_id": "file-1",
            "source": "source.pdf",
            "chunk_index": 12,
            "page": "3",
            "title": "features > cockpit",
            "section": "features",
            "kind": "text",
            "brand": "brand-a",
            "category": "vehicle",
            "type": "direct",
            "vector_score": 0.4568,
            "final_score": 0.4568,
            "rerank_score": 0.8123,
            "neighbor": False,
            "chars": 17,
            "preview": "line one line two",
        }
    ]