69d098baaa
Phase 5B of startup_speedup_20260606 track.
src/theme_2.py had 3 top-level NERV imports:
from src import theme_nerv
from src.theme_nerv import DATA_GREEN
from src.theme_nerv_fx import CRTFilter, AlertPulsing, StatusFlicker
And 3 module-level FX object instantiations:
_crt_filter = CRTFilter()
_alert_pulsing = AlertPulsing()
_status_flicker = StatusFlicker()
ALL removed. The 3 use sites now lookup via _require_warmed:
- apply() NERV branch: theme_nerv = _require_warmed('src.theme_nerv')
- ai_text_color(): theme_nerv = _require_warmed('src.theme_nerv')
(then uses theme_nerv.DATA_GREEN)
- render_post_fx(): theme_nerv_fx = _require_warmed('src.theme_nerv_fx')
(then creates FX objects locally per-call)
The _status_flicker was instantiated but never used (dead code path;
the StatusFlicker class is still importable via theme_nerv_fx but not
auto-constructed in theme_2.py).
TESTS:
- tests/test_theme_2_no_top_level_nerv.py: 4/4 PASS (all RED -> GREEN)
- tests/test_theme.py, test_theme_nerv.py, test_theme_nerv_fx.py,
test_theme_models.py: 21/21 PASS (no breakage)
EFFECTIVENESS: import src.theme_2 no longer triggers src.theme_nerv or
src.theme_nerv_fx (~485ms combined). For users on default theme, these
are NEVER loaded. For NERV users, the warmup pre-loads on _io_pool and
the lookup is O(1).
NEXT: Phase 5C (markdown table) follows same TDD pattern.
108 lines
3.8 KiB
Python
108 lines
3.8 KiB
Python
"""Tests that src/theme_2.py has NO top-level NERV theme imports.
|
|
|
|
Per spec.md:2.2 Layer 1, the main thread's import chain must not include
|
|
heavy feature-gated modules. The NERV theme modules (src.theme_nerv,
|
|
src.theme_nerv_fx) are warmed on AppController's _io_pool and accessed
|
|
via _require_warmed at use sites. NERV is only active when the user
|
|
explicitly chooses it; the default theme path should be lean.
|
|
|
|
src/theme_2.py uses these NERV symbols at three sites:
|
|
- ai_text_color() uses DATA_GREEN (only when is_nerv_active() is True)
|
|
- apply() uses theme_nerv.apply_nerv() (only when palette == 'NERV')
|
|
- render_post_fx() creates CRTFilter/AlertPulsing/StatusFlicker instances
|
|
|
|
All three sites are now lazy: the modules are looked up via _require_warmed
|
|
inside the function body, and the FX objects are created locally per-call.
|
|
|
|
These tests run in a fresh subprocess to ensure no warmup state leaks
|
|
from the test runner.
|
|
"""
|
|
|
|
import subprocess
|
|
import sys
|
|
import textwrap
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
|
|
|
|
def _run_in_subprocess(snippet: str) -> subprocess.CompletedProcess:
|
|
script = textwrap.dedent(snippet)
|
|
return subprocess.run(
|
|
[sys.executable, "-c", script],
|
|
capture_output=True,
|
|
text=True,
|
|
cwd=str(ROOT),
|
|
timeout=30,
|
|
)
|
|
|
|
|
|
def test_theme_2_does_not_import_theme_nerv_at_module_level() -> None:
|
|
res = _run_in_subprocess("""
|
|
import sys
|
|
import src.theme_2
|
|
print('src.theme_nerv' in sys.modules)
|
|
""")
|
|
assert res.returncode == 0, f"stderr: {res.stderr}"
|
|
assert res.stdout.strip() == "False", f"theme_2 triggered src.theme_nerv import: {res.stdout}"
|
|
|
|
|
|
def test_theme_2_does_not_import_theme_nerv_fx_at_module_level() -> None:
|
|
res = _run_in_subprocess("""
|
|
import sys
|
|
import src.theme_2
|
|
print('src.theme_nerv_fx' in sys.modules)
|
|
""")
|
|
assert res.returncode == 0, f"stderr: {res.stderr}"
|
|
assert res.stdout.strip() == "False", f"theme_2 triggered src.theme_nerv_fx import: {res.stdout}"
|
|
|
|
|
|
def test_theme_2_ai_text_color_source_does_not_import_theme_nerv() -> None:
|
|
"""The ai_text_color function's body uses _require_warmed; verify the
|
|
file no longer has DATA_GREEN as a top-level import. (Cannot call the
|
|
function outside a real imgui context, but AST inspection is sufficient.)"""
|
|
res = _run_in_subprocess("""
|
|
import ast
|
|
from pathlib import Path
|
|
root = Path('.').resolve()
|
|
theme_path = root / 'src' / 'theme_2.py'
|
|
tree = ast.parse(theme_path.read_text(encoding='utf-8'))
|
|
# Look for any top-level ImportFrom that imports DATA_GREEN directly
|
|
has_top_level_data_green = False
|
|
for node in tree.body:
|
|
if isinstance(node, ast.ImportFrom):
|
|
for alias in node.names:
|
|
if alias.name == 'DATA_GREEN':
|
|
has_top_level_data_green = True
|
|
print('HAS_TOP_LEVEL_DATA_GREEN:', has_top_level_data_green)
|
|
""")
|
|
assert res.returncode == 0, f"stderr: {res.stderr}"
|
|
assert "HAS_TOP_LEVEL_DATA_GREEN: False" in res.stdout
|
|
|
|
|
|
def test_audit_main_thread_imports_sees_no_new_violation_from_theme_2() -> None:
|
|
"""Run the static audit and check that src/theme_2.py contributes no new NERV violations."""
|
|
res = _run_in_subprocess("""
|
|
import ast
|
|
from pathlib import Path
|
|
root = Path('.').resolve()
|
|
theme_path = root / 'src' / 'theme_2.py'
|
|
tree = ast.parse(theme_path.read_text(encoding='utf-8'))
|
|
heavy = ['src.theme_nerv', 'src.theme_nerv_fx', 'theme_nerv', 'theme_nerv_fx']
|
|
for node in tree.body:
|
|
if isinstance(node, ast.Import):
|
|
for alias in node.names:
|
|
for h in heavy:
|
|
if alias.name == h or alias.name.startswith(h + '.'):
|
|
print('VIOLATION:', alias.name)
|
|
elif isinstance(node, ast.ImportFrom):
|
|
if node.module:
|
|
for h in heavy:
|
|
if node.module == h or node.module.startswith(h + '.'):
|
|
print('VIOLATION:', node.module)
|
|
print('OK')
|
|
""")
|
|
assert res.returncode == 0, f"stderr: {res.stderr}"
|
|
assert "OK" in res.stdout
|
|
assert "VIOLATION" not in res.stdout
|