ee763eea98
Replaces the broken-script-generated imports in src/ and tests/ with clean direct imports from the destination modules. Per user directive: 'we should adjust the tests instead' — no legacy __getattr__ shim is re-introduced. Key fixes: - src/mcp_client.py: remove self-import (MCPServerConfig etc. are defined locally; the script's module-top self-import caused the circular ImportError blocking all 11 test tiers) - src/gui_2.py: add missing module-top imports for FileItem, ContextFileEntry, ContextPreset, Tool, Persona, BiasProfile, parse_history_entries; remove broken-script local imports inside function bodies - src/app_controller.py: remove FileItem/FileItems from the type_aliases import block (was shadowing the direct import with the forward-reference TypeAlias string, breaking isinstance() calls); confirm isinstance() now works - src/commands.py: script correctly removed unused 'from src import models' - tests/test_models_no_top_level_tomli_w.py: import save_config_to_disk from src.project (no legacy shim back in models.py) - tests/test_rag_engine_ready_status_bug.py: import RAGConfig and VectorStoreConfig from src.mcp_client - tests/test_gui_2_result.py: patch src.gui_2.Persona/BiasProfile (gui_2 binds at module load; src.personas patch doesn't affect the gui_2 namespace) - tests/test_gui_2_result.py: patch src.gui_2.parse_diff (it lives in gui_2, not patch_modal) - tests/test_generate_type_registry.py: Metadata is now a dataclass in src_type_aliases.md (not a TypeAlias in type_aliases.md); src_models.md is no longer generated (src/models.py has no dataclasses after the de-cruft track) No local imports inside function bodies (per python.md §17.9a). All new imports are at module top with surgical edits.
74 lines
2.3 KiB
Python
74 lines
2.3 KiB
Python
"""Tests that src/models.py has no top-level tomli_w import (sub-track 2).
|
|
|
|
Per spec.md:2.2 Layer 1 (startup_speedup_20260606), the main thread's
|
|
import chain must not include heavy modules. tomli_w (~30ms cold) is
|
|
loaded on first call to save_config() instead of at module import time.
|
|
"""
|
|
|
|
import sys
|
|
import subprocess
|
|
import textwrap
|
|
import os
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).parent.parent
|
|
|
|
|
|
def _run_in_subprocess(snippet: str, timeout: int = 30) -> subprocess.CompletedProcess:
|
|
script = textwrap.dedent(snippet)
|
|
return subprocess.run(
|
|
[sys.executable, "-c", script],
|
|
capture_output=True,
|
|
text=True,
|
|
cwd=str(ROOT),
|
|
timeout=timeout,
|
|
)
|
|
|
|
|
|
def test_models_does_not_import_tomli_w_at_module_level() -> None:
|
|
"""import src.models should NOT trigger tomli_w to be in sys.modules."""
|
|
res = _run_in_subprocess("""
|
|
import sys
|
|
import src.models
|
|
print('tomli_w' in sys.modules)
|
|
""")
|
|
assert res.stdout.strip() == "False", (
|
|
f"tomli_w should not be in sys.modules after import src.models. "
|
|
f"Got: {res.stdout!r}, stderr: {res.stderr!r}"
|
|
)
|
|
|
|
|
|
def test_models_can_still_call_save_config_after_lazy_load() -> None:
|
|
"""save_config() must work even though tomli_w is lazy-loaded."""
|
|
import src.models
|
|
from src.project import save_config_to_disk
|
|
config = {
|
|
"ai": {"provider": "minimax", "model": "MiniMax-M3", "temperature": 0.0},
|
|
"theme": {"palette": "solarized_dark", "font_size": 16.0},
|
|
}
|
|
try:
|
|
save_config_to_disk(config)
|
|
except Exception as e:
|
|
pytest.fail(f"save_config raised after lazy tomli_w: {e}")
|
|
finally:
|
|
# Clean up: restore original config if we modified it
|
|
pass
|
|
|
|
|
|
def test_save_config_uses_tomli_w_on_demand() -> None:
|
|
"""First call to save_config() should trigger tomli_w import (subsequent calls hit sys.modules cache)."""
|
|
import src.models
|
|
from src.project import save_config_to_disk
|
|
# Drop tomli_w from sys.modules if it was already loaded (e.g. by other tests)
|
|
if "tomli_w" in sys.modules:
|
|
del sys.modules["tomli_w"]
|
|
assert "tomli_w" not in sys.modules
|
|
# Call save_config - this should trigger the import
|
|
try:
|
|
save_config_to_disk({"test_key": "test_value"})
|
|
except Exception:
|
|
# We don't care if the save itself fails; we just want to verify
|
|
# the import happened.
|
|
pass
|
|
assert "tomli_w" in sys.modules, "save_config() should have imported tomli_w on demand"
|