Private
Public Access
0
0
Files
manual_slop/tests/test_rag_integration.py
T
ed ada9617308 test(ai_client): rename send_result to send in 22 remaining test files
Batch rename of 22 test files. 62 references renamed total.

The full test suite is now GREEN again, matching the pre-rename baseline
from Task 1.1. Pure mechanical rename. No behavior change.

Files affected: test_ai_cache_tracking, test_ai_client_cli,
test_ai_client_result, test_api_events, test_context_pruner,
test_deepseek_provider, test_gemini_cli_* (3 files), test_gui2_mcp,
test_headless_* (2 files), test_live_gui_integration_v2,
test_orchestration_logic, test_phase6_engine, test_rag_integration,
test_run_worker_lifecycle_abort, test_spawn_interception_v2,
test_symbol_parsing, test_tier4_interceptor, test_tiered_aggregation,
test_token_usage.

Note: spec estimated 24 files; actual is 22 (test_deprecation_warnings
no longer exists, and 1 fewer file than spec's list).

Refs: conductor/tracks/send_result_to_send_20260616/
2026-06-17 00:38:29 -04:00

115 lines
4.2 KiB
Python

import os
import shutil
import tempfile
from unittest.mock import MagicMock, patch
import pytest
from src.app_controller import AppController
from src import ai_client
from src import events
from src import models
from src.result_types import Result
@pytest.fixture
def mock_project():
# Use a temporary directory for the mock project
temp_dir = tempfile.mkdtemp()
try:
# Create a minimal manual_slop.toml
with open(os.path.join(temp_dir, "manual_slop.toml"), "w") as f:
f.write('discussion_history = []\n')
yield temp_dir
finally:
# Clean up after test
shutil.rmtree(temp_dir)
def test_rag_integration(mock_project):
"""
Integration test verifying the flow from AppController through RAGEngine to ai_client.
"""
# 1. Initializes a mock project and AppController.
# We patch several components to avoid side effects during initialization.
with patch('src.app_controller.AppController._fetch_models'), \
patch('src.app_controller.AppController.load_config', return_value={}), \
patch('src.paths.get_full_path_info', return_value={'logs_dir': {'path': mock_project}, 'scripts_dir': {'path': mock_project}}), \
patch('src.theme_2.load_from_config'):
app = AppController()
# Minimal state setup for _handle_request_event
app.ui_global_system_prompt = ""
app.ui_project_system_prompt = ""
app.ui_base_system_prompt = ""
app.ui_use_default_base_prompt = True
app.ui_project_context_marker = ""
app.temperature = 0.0
app.max_tokens = 100
app.history_trunc_limit = 1000
app.top_p = 1.0
app.ui_agent_tools = {}
app.ui_gemini_cli_path = "gemini"
app.current_model = "gemini-1.5-flash"
app.active_project_path = os.path.join(mock_project, "manual_slop.toml")
# Ensure the provider is set to 'gemini' for our test
ai_client.set_provider("gemini", "gemini-1.5-flash")
# 2. Configures a mock RAG setup (enabled=True, provider='mock').
rag_config = models.RAGConfig(
enabled=True,
vector_store=models.VectorStoreConfig(provider='mock')
)
app.rag_config = rag_config
# 3. Mocks rag_engine.search to return a known chunk.
mock_rag_engine = MagicMock()
mock_rag_engine.config = rag_config
mock_rag_engine.search.return_value = [
{"document": "This is a retrieved chunk from RAG.", "metadata": {"path": "test_file.py"}}
]
app.rag_engine = mock_rag_engine
# 4. Mocks ai_client.send to verify that the retrieved chunk appears in the
# message sent to the provider. We use 'wraps' to let the real logic run
# while still having a mock we can inspect. We also mock the internal
# _send_gemini which is what actually "sends to the provider".
with patch('src.ai_client.send', wraps=ai_client.send) as mock_send:
with patch('src.ai_client._send_gemini') as mock_provider:
mock_provider.return_value = Result(data="Mock AI Response")
# Create a UserRequestEvent as if the user clicked "Gen + Send"
event = events.UserRequestEvent(
prompt="Tell me about the code.",
stable_md="Context",
file_items=[],
disc_text="History",
base_dir=mock_project
)
# Trigger the request event processing logic in AppController
app._handle_request_event(event)
# 5. This verifies the wiring from AppController through RAGEngine to ai_client.
# Verify that ai_client.send was called by AppController
assert mock_send.called
_, kwargs = mock_send.call_args
# rag_engine is now handled inside _handle_request_event, so send() gets None
assert kwargs['rag_engine'] is None
# Verify that the internal provider call was made
assert mock_provider.called
# Extract the user_message passed to the provider call
args, _ = mock_provider.call_args
# _send_gemini(md_content, user_message, ...) -> user_message is index 1
sent_user_message = args[1]
# Verify that the RAG chunk was prepended to the original prompt
assert "This is a retrieved chunk from RAG." in sent_user_message
assert "Tell me about the code." in sent_user_message
assert "## Retrieved Context" in sent_user_message
assert "Source: test_file.py" in sent_user_message
# Verify that rag_engine.search was called with the original prompt
mock_rag_engine.search.assert_called_once_with("Tell me about the code.")