from pathlib import Path
import subprocess, sys

ROOT = Path(__file__).resolve().parent
WS = ROOT / "workspace"

# Ordinary validation is non-destructive. Initialize only when no workspace exists.
if not WS.exists():
    setup = subprocess.run([sys.executable, "setup_fixture.py"], cwd=ROOT, text=True, capture_output=True)
    if setup.returncode:
        print(setup.stdout); print(setup.stderr, file=sys.stderr); raise SystemExit(setup.returncode)
elif not (WS / ".git").exists():
    raise SystemExit("workspace/ exists but is not the expected Git fixture; ordinary run_lab will not replace it.")

def ok(args):
    p = subprocess.run(args, cwd=WS, text=True, capture_output=True)
    return p.returncode == 0

checks = [
    ("GIT_WORKTREE_READABLE", ok(["git", "rev-parse", "--is-inside-work-tree"])),
    ("STATUS_READABLE", ok(["git", "status", "--porcelain=v1"])),
    ("LOCAL_BEHAVIOR_CHECK_PRESENT", (WS / "tests/check_rounding.py").is_file()),
    ("TASK_BRIEF_PRESENT", (ROOT / "task_brief.json").is_file()),
    ("NO_NETWORK_REQUIRED", True),
]
for name, value in checks:
    print(name, "PASS" if value else "FAIL")
raise SystemExit(0 if all(v for _, v in checks) else 1)
