fixed the RAg in test pipeline issue

This commit is contained in:
belviskhoremk
2026-04-26 18:51:48 +00:00
parent 205d9d7901
commit 97a501097d
14 changed files with 249 additions and 57 deletions

View File

@@ -206,9 +206,11 @@ class TestChatHandoff:
assert resp.status_code == 200
assert resp.json()["handoff"] is True
def test_handoff_not_triggered_without_keyword_match(self, client):
def test_handoff_not_triggered_when_high_confidence_no_keyword(self, client):
# High confidence + no keyword match → no handoff
chatbot = _make_chatbot(handoff_enabled=True, handoff_keywords=["human"])
rag_result = {"response": "Sure!", "sources": [], "model": "m", "tokens_used": 5}
rag_result = {"response": "We open at 9am.", "sources": [], "confidence_score": 0.85,
"model": "m", "tokens_used": 5}
with patch("app.routers.chat.get_supabase") as mock_sb, \
patch("app.routers.chat.rag_engine") as mock_rag:
@@ -219,6 +221,22 @@ class TestChatHandoff:
assert resp.status_code == 200
assert resp.json()["handoff"] is False
def test_handoff_triggered_by_low_confidence(self, client):
# Low confidence (no sources) triggers handoff even without a keyword
chatbot = _make_chatbot(handoff_enabled=True, handoff_keywords=["human"])
rag_result = {"response": "I'm not sure.", "sources": [], "confidence_score": 0.1,
"model": "m", "tokens_used": 5}
with patch("app.routers.chat.get_supabase") as mock_sb, \
patch("app.routers.chat.rag_engine") as mock_rag:
mock_rag.process_query = AsyncMock(return_value=rag_result)
mock_sb.return_value = _make_chat_sb(chatbot=chatbot)
resp = client.post("/api/v1/chat/cb-1", json={"message": "Tell me about quantum physics", "language": "en"})
assert resp.status_code == 200
assert resp.json()["handoff"] is True
assert resp.json()["low_confidence"] is True
class TestChatHistory:
def test_history_returns_empty_for_unknown_session(self, client):

View File

@@ -216,8 +216,10 @@ class TestDocumentDelete:
"file_url": None,
}
with patch("app.routers.documents.get_supabase") as mock_sb, \
patch("app.routers.documents.vector_store") as mock_vs:
patch("app.routers.documents.vector_store") as mock_vs, \
patch("app.routers.documents.response_cache") as mock_cache:
mock_vs.delete_by_document_id = MagicMock()
mock_cache.invalidate = MagicMock()
mock_sb.return_value = _make_doc_sb(doc=doc)
resp = client.delete("/api/v1/chatbots/cb-1/documents/doc-1", headers=AUTH)
assert resp.status_code == 200

View File

@@ -185,7 +185,7 @@ class TestMarketplaceRating:
assert resp.status_code == 404
def test_rate_chatbot_success(self, client):
bot = {"id": "bot-1", "average_rating": 4.0}
bot = {"id": "bot-1", "average_rating": 4.0, "rating_count": 1}
with patch("app.routers.marketplace.get_supabase") as mock_sb:
mock_sb.return_value = _make_marketplace_sb(chatbot_data=[bot])
resp = client.post(
@@ -195,12 +195,12 @@ class TestMarketplaceRating:
)
assert resp.status_code == 200
body = resp.json()
assert "new_average" in body
assert body["new_average"] == 4.5 # (4.0 + 5) / 2
assert "average_rating" in body
assert body["average_rating"] == 4.5 # (4.0 * 1 + 5) / 2
def test_rate_chatbot_first_rating(self, client):
"""When average_rating is None, should use the submitted rating as both sides."""
bot = {"id": "bot-1", "average_rating": None}
"""When average_rating is None, should use the submitted rating as the new average."""
bot = {"id": "bot-1", "average_rating": None, "rating_count": 0}
with patch("app.routers.marketplace.get_supabase") as mock_sb:
mock_sb.return_value = _make_marketplace_sb(chatbot_data=[bot])
resp = client.post(
@@ -209,4 +209,4 @@ class TestMarketplaceRating:
headers=AUTH,
)
assert resp.status_code == 200
assert resp.json()["new_average"] == 5.0
assert resp.json()["average_rating"] == 5.0