7bcb5a8c07
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]
109 lines
4.1 KiB
Python
109 lines
4.1 KiB
Python
import pytest
|
|
from unittest.mock import MagicMock, patch, mock_open
|
|
from src.gui_2 import App
|
|
from src import models
|
|
|
|
@pytest.fixture
|
|
def mock_app():
|
|
with (
|
|
patch('src.app_controller.AppController.load_config', return_value={
|
|
"ai": {"provider": "gemini", "model": "model-1"},
|
|
"projects": {"paths": [], "active": ""},
|
|
"gui": {"show_windows": {}}
|
|
}),
|
|
patch('src.project_manager.load_project', return_value={}),
|
|
patch('src.project_manager.migrate_from_legacy_config', return_value={}),
|
|
patch('src.project_manager.save_project'),
|
|
patch('src.session_logger.open_session'),
|
|
patch('src.session_logger.reset_session'),
|
|
patch('src.app_controller.AppController._init_ai_and_hooks'),
|
|
patch('src.app_controller.AppController._fetch_models'),
|
|
patch('src.gui_2.App._load_fonts'),
|
|
patch('src.gui_2.App._post_init')
|
|
):
|
|
app = App()
|
|
app.files = []
|
|
app.context_files = []
|
|
return app
|
|
|
|
def test_populate_auto_slices_basic(mock_app: App) -> None:
|
|
f_item = models.FileItem(path="test.py")
|
|
mock_outline = "[Class] MyClass (Lines 1-10)\n[Method] my_method (Lines 2-5)\n[Func] top_func (Lines 12-15)"
|
|
|
|
with (
|
|
patch('src.mcp_client.configure') as mock_conf,
|
|
patch('src.mcp_client.py_get_code_outline', return_value=mock_outline) as mock_outline_tool,
|
|
patch('builtins.open', mock_open(read_data="dummy content"))
|
|
):
|
|
mock_app._populate_auto_slices(f_item)
|
|
|
|
assert len(f_item.custom_slices) == 3
|
|
s0 = f_item.custom_slices[0]
|
|
assert s0["start_line"] == 1
|
|
assert s0["end_line"] == 10
|
|
assert s0["tag"] == 'auto-ast'
|
|
assert s0["comment"] == "MyClass"
|
|
|
|
def test_add_selected_triggers_auto_slices(mock_app):
|
|
f_mock = MagicMock()
|
|
f_mock.path = "test.py"
|
|
mock_app.files = [f_mock]
|
|
mock_app._ui_picker_selected = {"test.py"}
|
|
|
|
with patch('src.gui_2.imgui') as mock_imgui, \
|
|
patch('src.gui_2.imscope') as mock_imscope:
|
|
mock_imgui.begin_popup_modal.return_value = (True, True)
|
|
mock_imgui.button.side_effect = lambda label, size=None: label == "Add Selected"
|
|
mock_imgui.checkbox.return_value = (False, False)
|
|
mock_imgui.begin_child.return_value = True
|
|
mock_imgui.ImVec2 = MagicMock()
|
|
mock_imgui.WindowFlags_ = MagicMock()
|
|
|
|
# Setup imscope mocks to act as context managers
|
|
mock_imscope.table.return_value.__enter__.return_value = True
|
|
mock_imscope.tree_node_ex.return_value.__enter__.return_value = True
|
|
mock_imscope.child.return_value.__enter__.return_value = True
|
|
mock_imscope.window.return_value.__enter__.return_value = (True, True)
|
|
|
|
with patch.object(mock_app, '_populate_auto_slices') as mock_populate:
|
|
import src.gui_2
|
|
src.gui_2.render_add_context_files_modal(mock_app)
|
|
|
|
assert mock_populate.called
|
|
assert len(mock_app.context_files) == 1
|
|
assert mock_app.context_files[0].path == "test.py"
|
|
|
|
def test_add_all_triggers_auto_slices(mock_app):
|
|
f_mock = MagicMock()
|
|
f_mock.path = "test_all.py"
|
|
mock_app.files = [f_mock]
|
|
mock_app.context_files = []
|
|
|
|
with patch('src.gui_2.imgui') as mock_imgui, \
|
|
patch('src.gui_2.imscope') as mock_imscope:
|
|
mock_imgui.button.side_effect = lambda label, size=None: label == "Add All##addall"
|
|
mock_imgui.collapsing_header.return_value = True
|
|
mock_imgui.begin_child.return_value = True
|
|
mock_imgui.begin_table.return_value = True
|
|
mock_imgui.tree_node_ex.return_value = False
|
|
mock_imgui.checkbox.return_value = (False, False)
|
|
mock_imgui.combo.return_value = (False, 0)
|
|
mock_imgui.input_text.return_value = (False, "")
|
|
mock_imgui.TableFlags_ = MagicMock()
|
|
mock_imgui.TableColumnFlags_ = MagicMock()
|
|
mock_imgui.TreeNodeFlags_ = MagicMock()
|
|
|
|
# Setup imscope mocks to act as context managers
|
|
mock_imscope.table.return_value.__enter__.return_value = True
|
|
mock_imscope.tree_node_ex.return_value.__enter__.return_value = True
|
|
mock_imscope.child.return_value.__enter__.return_value = True
|
|
mock_imscope.window.return_value.__enter__.return_value = (True, True)
|
|
|
|
with patch.object(mock_app, '_populate_auto_slices') as mock_populate:
|
|
import src.gui_2
|
|
src.gui_2.render_context_composition_panel(mock_app)
|
|
|
|
assert mock_populate.called
|
|
assert len(mock_app.context_files) == 1
|
|
assert mock_app.context_files[0].path == "test_all.py"
|