"""Tests that src/api_hooks.py has NO top-level heavy imports (websockets, cost_tracker, session_logger).

Per the Main Thread Purity Invariant, the 3 heavy modules are loaded lazily
via _require_warmed() at use sites. The file already has
'from __future__ import annotations' so type hints are strings.

These tests run in a fresh subprocess to ensure no warmup state leaks.
"""

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_api_hooks_does_not_import_heavy_at_module_level() -> None:
 res = _run_in_subprocess("""
  import sys
  import src.api_hooks
  for mod in ('websockets', 'src.cost_tracker', 'src.session_logger'):
   print(mod, mod in sys.modules)
 """)
 assert res.returncode == 0, f"stderr: {res.stderr}"
 for line in res.stdout.strip().splitlines():
  name, present = line.split()
  assert present == "False", f"src.api_hooks triggered {name} import: {res.stdout}"


def test_api_hooks_loads_heavy_module_only_on_require_warmed() -> None:
 res = _run_in_subprocess("""
  import sys
  import src.api_hooks
  pre = ('websockets' in sys.modules, 'src.cost_tracker' in sys.modules, 'src.session_logger' in sys.modules)
  print('PRE', pre)
  _require_warmed = src.api_hooks._require_warmed
  ct = _require_warmed('src.cost_tracker')
  post1 = ('src.cost_tracker' in sys.modules, ct is _require_warmed('src.cost_tracker'))
  print('POST1', post1)
 """)
 assert res.returncode == 0, f"stderr: {res.stderr}"
 lines = res.stdout.strip().splitlines()
 assert "PRE (False, False, False)" in lines[0], f"heavy modules leaked at import: {res.stdout}"
 assert "POST1 (True, True)" in lines[1], f"_require_warmed did not load/cache src.cost_tracker: {res.stdout}"


def test_audit_sees_no_violation_in_api_hooks() -> None:
 res = _run_in_subprocess("""
  import ast
  from pathlib import Path
  tree = ast.parse(Path('src/api_hooks.py').read_text(encoding='utf-8'))
  heavy = {'websockets', 'src.cost_tracker', 'src.session_logger'}
  for node in tree.body:
   if isinstance(node, ast.Import):
    for alias in node.names:
     top = alias.name.split('.')[0]
     if top in heavy or any(alias.name.startswith(h + '.') for h in heavy):
      print('VIOLATION:', alias.name)
   elif isinstance(node, ast.ImportFrom):
    if node.module and (node.module in heavy or any(node.module.startswith(h + '.') for h in heavy)):
     print('VIOLATION:', node.module)
  print('OK')
 """)
 assert res.returncode == 0, f"stderr: {res.stderr}"
 assert "VIOLATION" not in res.stdout
 assert "OK" in res.stdout
