Update to add streaming tests (#600)

This commit is contained in:
cybermaggedon 2026-01-06 21:48:05 +00:00 committed by GitHub
parent f0c95a4c5e
commit f79d0603f7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 1062 additions and 57 deletions

View file

@ -480,11 +480,15 @@ def streaming_chunk_collector():
class ChunkCollector:
def __init__(self):
self.chunks = []
self.end_of_stream_flags = []
self.complete = False
async def collect(self, chunk):
"""Async callback to collect chunks"""
async def collect(self, chunk, end_of_stream=False):
"""Async callback to collect chunks with end_of_stream flag"""
self.chunks.append(chunk)
self.end_of_stream_flags.append(end_of_stream)
if end_of_stream:
self.complete = True
def get_full_text(self):
"""Concatenate all chunk content"""
@ -496,6 +500,14 @@ def streaming_chunk_collector():
return [c.get("chunk_type") for c in self.chunks]
return []
def verify_streaming_protocol(self):
"""Verify that streaming protocol is correct"""
assert len(self.chunks) > 0, "Should have received at least one chunk"
assert len(self.chunks) == len(self.end_of_stream_flags), "Each chunk should have an end_of_stream flag"
assert self.end_of_stream_flags.count(True) == 1, "Exactly one chunk should have end_of_stream=True"
assert self.end_of_stream_flags[-1] is True, "Last chunk should have end_of_stream=True"
assert self.complete is True, "Should be marked complete after final chunk"
return ChunkCollector

View file

@ -46,9 +46,16 @@ class TestDocumentRagStreaming:
full_text = "Machine learning is a subset of artificial intelligence that focuses on algorithms that learn from data."
if streaming and chunk_callback:
# Simulate streaming chunks
# Simulate streaming chunks with end_of_stream flags
chunks = []
async for chunk in mock_streaming_llm_response():
await chunk_callback(chunk)
chunks.append(chunk)
# Send all chunks with end_of_stream=False except the last
for i, chunk in enumerate(chunks):
is_final = (i == len(chunks) - 1)
await chunk_callback(chunk, is_final)
return full_text
else:
# Non-streaming response - same text
@ -89,6 +96,9 @@ class TestDocumentRagStreaming:
assert_streaming_chunks_valid(collector.chunks, min_chunks=1)
assert_callback_invoked(AsyncMock(call_count=len(collector.chunks)), min_calls=1)
# Verify streaming protocol compliance
collector.verify_streaming_protocol()
# Verify full response matches concatenated chunks
full_from_chunks = collector.get_full_text()
assert result == full_from_chunks
@ -117,7 +127,7 @@ class TestDocumentRagStreaming:
# Act - Streaming
streaming_chunks = []
async def collect(chunk):
async def collect(chunk, end_of_stream):
streaming_chunks.append(chunk)
streaming_result = await document_rag_streaming.query(

View file

@ -59,9 +59,16 @@ class TestGraphRagStreaming:
full_text = "Machine learning is a subset of artificial intelligence that focuses on algorithms that learn from data."
if streaming and chunk_callback:
# Simulate streaming chunks
# Simulate streaming chunks with end_of_stream flags
chunks = []
async for chunk in mock_streaming_llm_response():
await chunk_callback(chunk)
chunks.append(chunk)
# Send all chunks with end_of_stream=False except the last
for i, chunk in enumerate(chunks):
is_final = (i == len(chunks) - 1)
await chunk_callback(chunk, is_final)
return full_text
else:
# Non-streaming response - same text
@ -102,6 +109,9 @@ class TestGraphRagStreaming:
assert_streaming_chunks_valid(collector.chunks, min_chunks=1)
assert_callback_invoked(AsyncMock(call_count=len(collector.chunks)), min_calls=1)
# Verify streaming protocol compliance
collector.verify_streaming_protocol()
# Verify full response matches concatenated chunks
full_from_chunks = collector.get_full_text()
assert result == full_from_chunks
@ -128,7 +138,7 @@ class TestGraphRagStreaming:
# Act - Streaming
streaming_chunks = []
async def collect(chunk):
async def collect(chunk, end_of_stream):
streaming_chunks.append(chunk)
streaming_result = await graph_rag_streaming.query(

View file

@ -0,0 +1,351 @@
"""
Integration tests for RAG service streaming protocol compliance.
These tests verify that RAG services correctly forward end_of_stream flags
and don't duplicate final chunks, ensuring proper streaming semantics.
"""
import pytest
from unittest.mock import AsyncMock, MagicMock, call
from trustgraph.retrieval.graph_rag.graph_rag import GraphRag
from trustgraph.retrieval.document_rag.document_rag import DocumentRag
class TestGraphRagStreamingProtocol:
"""Integration tests for GraphRAG streaming protocol"""
@pytest.fixture
def mock_embeddings_client(self):
"""Mock embeddings client"""
client = AsyncMock()
client.embed.return_value = [[0.1, 0.2, 0.3]]
return client
@pytest.fixture
def mock_graph_embeddings_client(self):
"""Mock graph embeddings client"""
client = AsyncMock()
client.query.return_value = ["entity1", "entity2"]
return client
@pytest.fixture
def mock_triples_client(self):
"""Mock triples client"""
client = AsyncMock()
client.query.return_value = []
return client
@pytest.fixture
def mock_streaming_prompt_client(self):
"""Mock prompt client that simulates realistic streaming with end_of_stream flags"""
client = AsyncMock()
async def kg_prompt_side_effect(query, kg, timeout=600, streaming=False, chunk_callback=None):
if streaming and chunk_callback:
# Simulate realistic streaming: chunks with end_of_stream=False, then final with end_of_stream=True
await chunk_callback("The", False)
await chunk_callback(" answer", False)
await chunk_callback(" is here.", False)
await chunk_callback("", True) # Empty final chunk with end_of_stream=True
return "" # Return value not used since callback handles everything
else:
return "The answer is here."
client.kg_prompt.side_effect = kg_prompt_side_effect
return client
@pytest.fixture
def graph_rag(self, mock_embeddings_client, mock_graph_embeddings_client,
mock_triples_client, mock_streaming_prompt_client):
"""Create GraphRag instance with mocked dependencies"""
return GraphRag(
embeddings_client=mock_embeddings_client,
graph_embeddings_client=mock_graph_embeddings_client,
triples_client=mock_triples_client,
prompt_client=mock_streaming_prompt_client,
verbose=False
)
@pytest.mark.asyncio
async def test_callback_receives_end_of_stream_parameter(self, graph_rag):
"""Test that callback receives end_of_stream parameter"""
# Arrange
callback = AsyncMock()
# Act
await graph_rag.query(
query="test query",
user="test_user",
collection="test_collection",
streaming=True,
chunk_callback=callback
)
# Assert - callback should receive (chunk, end_of_stream) signature
assert callback.call_count == 4
# All calls should have 2 arguments
for call_args in callback.call_args_list:
assert len(call_args.args) == 2, "Callback should receive (chunk, end_of_stream)"
@pytest.mark.asyncio
async def test_end_of_stream_flag_forwarded_correctly(self, graph_rag):
"""Test that end_of_stream flags are forwarded correctly"""
# Arrange
chunks_with_flags = []
async def collect(chunk, end_of_stream):
chunks_with_flags.append((chunk, end_of_stream))
# Act
await graph_rag.query(
query="test query",
user="test_user",
collection="test_collection",
streaming=True,
chunk_callback=collect
)
# Assert
assert len(chunks_with_flags) == 4
# First three chunks should have end_of_stream=False
assert chunks_with_flags[0] == ("The", False)
assert chunks_with_flags[1] == (" answer", False)
assert chunks_with_flags[2] == (" is here.", False)
# Final chunk should have end_of_stream=True
assert chunks_with_flags[3] == ("", True)
@pytest.mark.asyncio
async def test_no_duplicate_final_chunk(self, graph_rag):
"""Test that final chunk is not duplicated"""
# Arrange
chunks = []
async def collect(chunk, end_of_stream):
chunks.append(chunk)
# Act
await graph_rag.query(
query="test query",
user="test_user",
collection="test_collection",
streaming=True,
chunk_callback=collect
)
# Assert - should have exactly 4 chunks, no duplicates
assert len(chunks) == 4
assert chunks == ["The", " answer", " is here.", ""]
# The last chunk appears exactly once
assert chunks.count("") == 1
@pytest.mark.asyncio
async def test_exactly_one_end_of_stream_true(self, graph_rag):
"""Test that exactly one message has end_of_stream=True"""
# Arrange
end_of_stream_flags = []
async def collect(chunk, end_of_stream):
end_of_stream_flags.append(end_of_stream)
# Act
await graph_rag.query(
query="test query",
user="test_user",
collection="test_collection",
streaming=True,
chunk_callback=collect
)
# Assert - exactly one True
assert end_of_stream_flags.count(True) == 1
assert end_of_stream_flags.count(False) == 3
@pytest.mark.asyncio
async def test_empty_final_chunk_preserved(self, graph_rag):
"""Test that empty final chunks are preserved and forwarded"""
# Arrange
final_chunk = None
final_flag = None
async def collect(chunk, end_of_stream):
nonlocal final_chunk, final_flag
if end_of_stream:
final_chunk = chunk
final_flag = end_of_stream
# Act
await graph_rag.query(
query="test query",
user="test_user",
collection="test_collection",
streaming=True,
chunk_callback=collect
)
# Assert
assert final_flag is True
assert final_chunk == "", "Empty final chunk should be preserved"
class TestDocumentRagStreamingProtocol:
"""Integration tests for DocumentRAG streaming protocol"""
@pytest.fixture
def mock_embeddings_client(self):
"""Mock embeddings client"""
client = AsyncMock()
client.embed.return_value = [[0.1, 0.2, 0.3]]
return client
@pytest.fixture
def mock_doc_embeddings_client(self):
"""Mock document embeddings client"""
client = AsyncMock()
client.query.return_value = ["doc1", "doc2"]
return client
@pytest.fixture
def mock_streaming_prompt_client(self):
"""Mock prompt client with streaming support"""
client = AsyncMock()
async def document_prompt_side_effect(query, documents, timeout=600, streaming=False, chunk_callback=None):
if streaming and chunk_callback:
# Simulate streaming with non-empty final chunk (some LLMs do this)
await chunk_callback("Document", False)
await chunk_callback(" summary", False)
await chunk_callback(".", True) # Non-empty final chunk
return ""
else:
return "Document summary."
client.document_prompt.side_effect = document_prompt_side_effect
return client
@pytest.fixture
def document_rag(self, mock_embeddings_client, mock_doc_embeddings_client,
mock_streaming_prompt_client):
"""Create DocumentRag instance with mocked dependencies"""
return DocumentRag(
embeddings_client=mock_embeddings_client,
doc_embeddings_client=mock_doc_embeddings_client,
prompt_client=mock_streaming_prompt_client,
verbose=False
)
@pytest.mark.asyncio
async def test_callback_receives_end_of_stream_parameter(self, document_rag):
"""Test that callback receives end_of_stream parameter"""
# Arrange
callback = AsyncMock()
# Act
await document_rag.query(
query="test query",
user="test_user",
collection="test_collection",
streaming=True,
chunk_callback=callback
)
# Assert
assert callback.call_count == 3
for call_args in callback.call_args_list:
assert len(call_args.args) == 2
@pytest.mark.asyncio
async def test_non_empty_final_chunk_preserved(self, document_rag):
"""Test that non-empty final chunks are preserved with correct flag"""
# Arrange
chunks_with_flags = []
async def collect(chunk, end_of_stream):
chunks_with_flags.append((chunk, end_of_stream))
# Act
await document_rag.query(
query="test query",
user="test_user",
collection="test_collection",
streaming=True,
chunk_callback=collect
)
# Assert
assert len(chunks_with_flags) == 3
assert chunks_with_flags[0] == ("Document", False)
assert chunks_with_flags[1] == (" summary", False)
assert chunks_with_flags[2] == (".", True) # Non-empty final chunk
@pytest.mark.asyncio
async def test_no_duplicate_final_chunk(self, document_rag):
"""Test that final chunk is not duplicated"""
# Arrange
chunks = []
async def collect(chunk, end_of_stream):
chunks.append(chunk)
# Act
await document_rag.query(
query="test query",
user="test_user",
collection="test_collection",
streaming=True,
chunk_callback=collect
)
# Assert - final "." appears exactly once
assert chunks.count(".") == 1
assert chunks == ["Document", " summary", "."]
class TestStreamingProtocolEdgeCases:
"""Test edge cases in streaming protocol"""
@pytest.mark.asyncio
async def test_multiple_empty_chunks_before_final(self):
"""Test handling of multiple empty chunks (edge case)"""
# Arrange
client = AsyncMock()
async def kg_prompt_with_empties(query, kg, timeout=600, streaming=False, chunk_callback=None):
if streaming and chunk_callback:
await chunk_callback("text", False)
await chunk_callback("", False) # Empty but not final
await chunk_callback("more", False)
await chunk_callback("", True) # Empty and final
return ""
else:
return "textmore"
client.kg_prompt.side_effect = kg_prompt_with_empties
rag = GraphRag(
embeddings_client=AsyncMock(embed=AsyncMock(return_value=[[0.1]])),
graph_embeddings_client=AsyncMock(query=AsyncMock(return_value=[])),
triples_client=AsyncMock(query=AsyncMock(return_value=[])),
prompt_client=client,
verbose=False
)
chunks_with_flags = []
async def collect(chunk, end_of_stream):
chunks_with_flags.append((chunk, end_of_stream))
# Act
await rag.query(
query="test",
streaming=True,
chunk_callback=collect
)
# Assert
assert len(chunks_with_flags) == 4
assert chunks_with_flags[-1] == ("", True) # Final empty chunk
end_of_stream_flags = [f for c, f in chunks_with_flags]
assert end_of_stream_flags.count(True) == 1