"""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
