Private
Public Access
0
0
Files
manual_slop/tests/test_audit_main_thread_imports.py
ed 6f9a3af201 feat(audit): add main-thread import graph audit + baseline measurements
Phase 1, Tasks T1.2 + T1.4 of the startup_speedup_20260606 track.

NEW: scripts/audit_main_thread_imports.py
  Static CI gate that AST-walks the import graph reachable from
  sloppy.py and fails (exit 1) if any heavy module is imported at the
  top of a main-thread-reachable file. Walks into if/elif/else and
  try/except branches (which run at import time) but skips function
  bodies (which only run when called). Allowlist: stdlib + the lean
  gui_2 skeleton (imgui_bundle, defer, src.imgui_scopes, src.theme_2,
  src.theme_models, src.paths, src.models, src.events).

NEW: scripts/audit_gui2_imports.py
  Read-only analysis tool that lists every top-level and function-level
  import in src/gui_2.py, classified by location. Used in Phase 5D to
  identify which imports to remove.

NEW: tests/test_audit_main_thread_imports.py
  9 tests covering: --help exits 0, clean stdlib-only passes, heavy
  third-party fails, google.genai fails, transitive walks, function-
  body imports ignored, if-branch imports flagged, try-block imports
  flagged, file:line reported. All 9 pass.

NEW: docs/reports/startup_baseline_20260606.txt
  3-run median cold-start benchmark. Worst offenders: src.gui_2
  (1770ms), simulation.user_agent (1517ms), google.genai (1001ms),
  openai (482ms), anthropic (441ms), imgui_bundle (255ms),
  src.theme_nerv* (485ms combined), src.markdown_table (243ms),
  src.command_palette (242ms).

NEW: docs/reports/startup_audit_20260606.txt
  Audit output on the CURRENT codebase. Reports 67 violations across
  the main-thread import graph (incl. numpy in src/gui_2.py:9,
  tomli_w in src/gui_2.py:18, fastapi + requests in src/app_controller,
  tree_sitter_* in src/file_cache, pydantic in src/models, plus all
  the src.* subsystem imports that drag in heavy transitive deps).
  Phase 3-5 of the track will resolve these one by one.

After Phase 3-5, this audit must exit 0 (no violations).

Co-located reports in docs/reports/ per project convention; the other
agent finished their work in docs/superpowers/ and is unrelated.
2026-06-06 14:22:18 -04:00

144 lines
3.3 KiB
Python

"""Tests for scripts/audit_main_thread_imports.py.
TDD Red phase: tests the audit logic in isolation. The full CLI is also
exercised via subprocess in test_audit_cli_subprocess (kept simple so it
doesn't depend on the actual codebase).
"""
import subprocess
import sys
import textwrap
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
SCRIPT = ROOT / "scripts" / "audit_main_thread_imports.py"
assert SCRIPT.exists(), f"audit script missing: {SCRIPT}"
def _run_audit_on_fixture(tmp_path: Path, source: str) -> subprocess.CompletedProcess:
entry = tmp_path / "entry.py"
entry.write_text(textwrap.dedent(source), encoding="utf-8")
return subprocess.run(
[sys.executable, str(SCRIPT), "--root", str(tmp_path), "--entry", str(entry)],
capture_output=True,
text=True,
timeout=30,
)
def test_audit_cli_help_exits_zero() -> None:
res = subprocess.run(
[sys.executable, str(SCRIPT), "--help"],
capture_output=True,
text=True,
timeout=10,
)
assert res.returncode == 0
assert "audit" in res.stdout.lower() or "usage" in res.stdout.lower()
def test_audit_passes_on_clean_stdlib_only(tmp_path: Path) -> None:
res = _run_audit_on_fixture(
tmp_path,
"""\
import os
import sys
import json
from pathlib import Path
""",
)
assert res.returncode == 0, f"unexpected failure: {res.stdout}\n{res.stderr}"
def test_audit_fails_on_heavy_third_party_top_level(tmp_path: Path) -> None:
res = _run_audit_on_fixture(
tmp_path,
"""\
import os
import anthropic
""",
)
assert res.returncode != 0, f"should have failed but exited 0: {res.stdout}"
assert "anthropic" in res.stdout
def test_audit_fails_on_google_genai_top_level(tmp_path: Path) -> None:
res = _run_audit_on_fixture(
tmp_path,
"""\
from google import genai
""",
)
assert res.returncode != 0
assert "google" in res.stdout
def test_audit_walks_transitive_imports(tmp_path: Path) -> None:
(tmp_path / "leaky.py").write_text("import requests\n", encoding="utf-8")
res = _run_audit_on_fixture(
tmp_path,
"""\
import os
from pathlib import Path
import leaky
""",
)
assert res.returncode != 0
assert "requests" in res.stdout
assert "leaky.py" in res.stdout
def test_audit_passes_on_nested_function_level_imports(tmp_path: Path) -> None:
res = _run_audit_on_fixture(
tmp_path,
"""\
import os
def lazy():
import anthropic
return anthropic
""",
)
assert res.returncode == 0, f"function-level imports should be ignored: {res.stdout}\n{res.stderr}"
def test_audit_fails_on_import_inside_if_branch(tmp_path: Path) -> None:
res = _run_audit_on_fixture(
tmp_path,
"""\
import os
if True:
from google import genai
""",
)
assert res.returncode != 0, f"if-branch imports should be flagged: {res.stdout}\n{res.stderr}"
assert "google" in res.stdout
def test_audit_fails_on_import_inside_try_block(tmp_path: Path) -> None:
res = _run_audit_on_fixture(
tmp_path,
"""\
import os
try:
import requests
except ImportError:
pass
""",
)
assert res.returncode != 0
assert "requests" in res.stdout
def test_audit_reports_file_line_for_violation(tmp_path: Path) -> None:
res = _run_audit_on_fixture(
tmp_path,
"""\
import os
import fastapi
""",
)
assert res.returncode != 0
assert "entry.py" in res.stdout
assert "L" in res.stdout