"""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 config = { "ai": {"provider": "minimax", "model": "MiniMax-M3", "temperature": 0.0}, "theme": {"palette": "solarized_dark", "font_size": 16.0}, } try: src.models.save_config(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 # 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: src.models.save_config({"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"