File size: 3,487 Bytes
5959a78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

from unittest.mock import Mock, patch

from backend.services.rag_engine import RAGEngine
from backend.services.rag_graph import DocuMindRAGGraph


# Confidence scoring tests

def test_confidence_is_low_when_no_chunks():
    assert RAGEngine._assess_confidence([]) == "LOW"


def test_confidence_is_high_for_strong_similarity():
    chunks = [
        {"similarity": 0.90},
        {"similarity": 0.85},
    ]
    assert RAGEngine._assess_confidence(chunks) == "HIGH"


def test_confidence_is_medium_for_moderate_similarity():
    chunks = [
        {"similarity": 0.55},
        {"similarity": 0.50},
    ]
    assert RAGEngine._assess_confidence(chunks) == "MEDIUM"


def test_confidence_is_low_for_weak_similarity():
    chunks = [
        {"similarity": 0.20},
        {"similarity": 0.15},
    ]
    assert RAGEngine._assess_confidence(chunks) == "LOW"


# RAG engine adapter tests

def test_query_adapts_graph_result():
    fake_graph = Mock()
    fake_graph.invoke.return_value = {
        "answer": "Python and SQL are listed.",
        "chunks": [
            {
                "doc_name": "Resume.pdf",
                "page_number": 1,
                "text": "Technical skills: Python and SQL.",
                "similarity": 0.85,
            }
        ],
        "is_grounded": True,
        "error": None,
    }

    with patch(
        "backend.services.rag_engine.get_rag_graph",
        return_value=fake_graph,
    ):
        engine = RAGEngine()
        result = engine.query("Which skills are listed?")

    assert result.answer == "Python and SQL are listed."
    assert result.query == "Which skills are listed?"
    assert result.chunks_retrieved == 1
    assert result.sources[0].doc_name == "Resume.pdf"
    assert result.sources[0].page_number == 1
    assert result.is_grounded is True
    assert result.confidence == "HIGH"
    fake_graph.invoke.assert_called_once()


def test_query_handles_empty_graph_result():
    fake_graph = Mock()
    fake_graph.invoke.return_value = {
        "answer": "",
        "chunks": [],
        "is_grounded": False,
        "error": None,
    }

    with patch(
        "backend.services.rag_engine.get_rag_graph",
        return_value=fake_graph,
    ):
        engine = RAGEngine()
        result = engine.query("What is in this document?")

    assert result.chunks_retrieved == 0
    assert result.is_grounded is False
    assert result.confidence == "LOW"
    assert "couldn't find relevant information" in result.answer


# LangGraph retrieval-routing tests

def test_retrieval_gate_finishes_when_no_chunks():
    assert DocuMindRAGGraph.route_after_retrieval(
        {"chunks": []}
    ) == "finish"


def test_retrieval_gate_finishes_for_weak_evidence():
    state = {
        "chunks": [
            {"similarity": 0.10},
            {"similarity": 0.15},
        ]
    }

    assert DocuMindRAGGraph.route_after_retrieval(state) == "finish"


def test_retrieval_gate_generates_for_sufficient_similarity():
    state = {
        "chunks": [
            {"similarity": 0.35},
            {"similarity": 0.25},
        ]
    }

    assert DocuMindRAGGraph.route_after_retrieval(state) == "generate"


def test_refusal_is_never_grounded():
    graph = object.__new__(DocuMindRAGGraph)
    result = graph.check_evidence({
        "answer": "The document does not contain that information.",
        "chunks": [{"text": "The document contains many important facts."}],
    })
    assert result["is_grounded"] is False