Private
Public Access
0
0
Files
manual_slop/tests/test_rag_integration.py
T
ed 7bcb5a8c07 refactor(config): Route all config I/O through AppController
Eliminates 22 call sites that bypassed the AppController state owner
and read/wrote config.toml directly. AppController is now the single
source of truth for self.config; gui_2.py, commands.py, etc. go
through controller.save_config() / controller.load_config().

Production changes:
- src/models.py: rename load_config -> _load_config_from_disk,
  save_config -> _save_config_to_disk (private I/O primitives)
- src/app_controller.py: add public load_config()/save_config() methods
  that own the state. Update 3 internal call sites and 3 ConductorEngine
  call sites to pass max_workers from self.config
- src/multi_agent_conductor.py: ConductorEngine.__init__ now takes
  max_workers as a parameter (caller responsibility, not I/O primitive)
- src/external_editor.py: get_default_launcher() takes config as a
  parameter; gui_2.py:1311,4776 pass app.config
- src/gui_2.py: 17 sites of models.save_config(X.config) replaced with
  X.save_config() (delegates via __getattr__ to controller)
- src/commands.py: save_all() uses app.save_config()

Test changes (route through controller, not I/O primitive):
- tests/conftest.py: mock_app and app_instance fixtures now patch
  AppController.load_config/save_config instead of models I/O primitives
- 18 other test files: patches renamed from models._save_config_to_disk
  to AppController.save_config (and same for load_config)
- tests/test_app_controller_mcp.py: use SLOP_CONFIG env var instead of
  patching removed CONFIG_PATH module constant
- tests/test_parallel_execution.py: pass max_workers=2 explicitly to
  ConductorEngine (caller no longer reads config)
- tests/test_gui_paths.py: add save_config=MagicMock() to MockApp;
  assert on controller method, not I/O primitive
- tests/test_models_no_top_level_tomli_w.py: still calls private
  _save_config_to_disk directly (the only allowed exception; tests
  the lazy-load behavior of the primitive itself)

New files:
- scripts/audit_no_models_config_io.py: enforces the rule (--strict,
  --json modes; AST-based docstring detection to avoid false positives)
- conductor/code_styleguides/config_state_owner.md: documents the rule

Verification:
- 67 targeted tests pass
- scripts/audit_no_models_config_io.py --strict returns 0

This is the architectural cleanup that surfaced during the
audit_architectural_cheats_20260607 review. Closes the smoke-gun
CONFIG_PATH module constant (already done in 0c7ebf22) AND the
free-function models.load_config/save_config smell.

[conductor(checkpoint): config-iO-refactor-20260607]
2026-06-07 19:54:17 -04:00

114 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
@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 = "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.")