Private
Public Access
0
0
Files
manual_slop/tests/test_rag_integration.py
T
ed 9e07fac1db refactor(consumers): replace 'models.<moved_class>' with direct imports
Per post_module_taxonomy_de_cruft_20260627 Phase 2 (FR7 continued).
The previous migration commit (8f11340b) handled the
'from src.models import X' pattern (85 sites). This commit handles
the 'models.<moved_class>' attribute access pattern (44 sites in 20
files), which the __getattr__ shim previously supported.

The migration was performed by the one-time script
scripts/tier2/artifacts/post_module_taxonomy_de_cruft_20260627/migrate_models_attr.py
which:
 1. For each 'models.<moved_class>' reference, replaces it with the
    bare class name (e.g., 'models.MCPConfiguration' -> 'MCPConfiguration')
 2. Adds the import 'from src.<destination> import <moved_class>' at
    the top of the file (deduplicated if the import already exists)
 3. Skips moved classes that the file already imports directly

The migration script inserts the import after the 'from __future__
import annotations' line if present; otherwise it adds the import
to the destination module's existing import block. Two files
required manual fixes because the script's regex didn't handle them:
 - src/rag_engine.py: uses 'from src import models' (not 'from
                            src.models import X'); the class is accessed
                            via 'models.RAGConfig'. Replaced with a
                            direct 'from src.mcp_client import RAGConfig'
                            import and removed the 'from src import models'.
 - tests/test_project_context_20260627.py: uses the parens-style
                            multi-line 'from src.models import (X, Y, Z)'.
                            Replaced with the parens-style direct import.

After this commit:
 - 'models.MCPConfiguration', 'models.FileItem', 'models.Ticket', etc.
   no longer work in src/ and tests/ (the AttributeError raises
   because models.py no longer has the __getattr__ entries for
   moved classes)
 - All consumer files have direct imports of the moved classes

Total: 44 'models.<moved_class>' references rewritten across 20 files.
2026-06-26 14:06:03 -04:00

115 lines
4.1 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 = RAGConfig(
enabled=True,
vector_store=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.")