import importlib.util
import os
import subprocess
import tempfile
from pathlib import Path

HERE = Path(__file__).resolve().parent
ROOT = HERE.parent
_spec = importlib.util.spec_from_file_location("guard_mod", HERE / "guard_mod.py")
guard = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(guard)


def _worktrees():
    return subprocess.run(["git", "-C", str(ROOT), "worktree", "list"], capture_output=True)


def test_lists_the_real_worktrees():
    out = subprocess.run(["git", "-C", str(ROOT), "worktree", "list"])  # cotterpin:expect
    assert out


def test_reaches_the_worktrees_through_a_helper():
    assert _worktrees()  # cotterpin:expect


def test_inspects_real_processes():
    assert subprocess.check_output(["ps", "-eo", "pid"])  # cotterpin:expect


def test_expands_the_real_home():
    assert os.path.expanduser("~")  # cotterpin:expect


def test_asks_for_the_home_directory():
    assert Path.home()  # cotterpin:expect


def test_reads_the_load_average():
    assert os.getloadavg()[0] < 4  # cotterpin:expect


def test_reads_the_real_claude_folder():
    assert Path("~/.claude/settings.json")  # cotterpin:expect


def test_calls_a_script_that_expands_home():
    assert guard.lane_advisory("git commit")  # cotterpin:expect


def test_leaves_scratch_in_tmp():
    scratch = tempfile.mkdtemp(prefix="x-", dir="/tmp")  # cotterpin:expect
    assert scratch


def _worktree_root():
    for base in ("/private/tmp", "/tmp"):
        return tempfile.mkdtemp(prefix="wt-", dir=base)  # cotterpin:expect
