from pathlib import Path
import argparse, subprocess, sys

ROOT = Path(__file__).resolve().parent
parser = argparse.ArgumentParser(description="Run the supplied local behavior check against a transfer-case repository state.")
parser.add_argument("--repo", default="workspace", help="Repository path, relative to this directory unless absolute. Default: workspace")
args = parser.parse_args()
repo = Path(args.repo)
if not repo.is_absolute():
    repo = (ROOT / repo).resolve()
if not (repo / ".git").exists():
    raise SystemExit("Repository fixture is missing. Run setup_transfer_fixture.py first or pass --repo to a Git worktree.")
check = repo / "tests/check_rounding.py"
if not check.is_file():
    raise SystemExit("Behavior check is missing from the supplied repository state.")
p = subprocess.run([sys.executable, str(check)], cwd=repo, text=True, capture_output=True)
print("BEHAVIOR_CHECK_REPO", repo)
print("BEHAVIOR_CHECK_EXIT", p.returncode)
if p.stdout: print(p.stdout, end="")
if p.stderr: print(p.stderr, end="", file=sys.stderr)
raise SystemExit(p.returncode)
