File size: 1,743 Bytes
4298e57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Tests RAG context formatting and source citation consistency

Use case:
    Verifies that retrieved document chunks are assigned stable citation
    markers, such as [S1] and [S2], along with relevant page references

Stable citations:
    Stable citations are predictable source labels that consistently map each
    retrieved chunk to a reference in the generated context

Why:
    They make generated answers traceable to their source material and help
    users verify where supporting information came from
"""

from document_loader import Chunk
from rag_engine import SearchHit, build_context
def test_context_has_stable_source_markers():
    """verifying source markers and page references are preserved in RAG context"""

    # creating retrieval results with source metadata and relevance scores
    '''
    Unique chunk ID = 1 (chunk id)
    Alpha = Sample text stored in the first chunk
    Policy A,B = Name of the source document
    page = Page associated with the chunk
    0.9,0.8 = Retrieval relevance score for this chunk
    '''
    
    hits = [
        SearchHit(
            Chunk("1", "Alpha", "Policy A", "TXT", page=2), #Alpha / Beta → dummy document content used only for testing
            0.9,
        ),
        SearchHit(
            Chunk("2", "Beta", "Policy B", "PDF", page=4),
            0.8,
        ),
    ]

    # building the formatted context passed to the RAG model
    context = build_context(hits)
    # making sure the first retrieved source receives the [S1] marker
    assert "[S1]" in context
    # making sure the second retrieved source receives the [S2] marker
    assert "[S2]" in context
    # making sure page metadata is included for source traceability
    assert "p. 2" in context