"""Opt-in full-pipeline smoke test.

Runs:
1. setup_tier2_clone.ps1 -WhatIf (verify the bootstrap script is well-formed)
2. run_track.py with the smoke track (verify the CLI runs the protocol)

This test does NOT require a real Tier 2 clone (it uses a temp workspace).
"""
import os
import subprocess
from pathlib import Path

import pytest

pytestmark = [
 pytest.mark.skipif(
  not os.environ.get("TIER2_SANDBOX_TESTS"),
  reason="opt-in: smoke test off by default; set TIER2_SANDBOX_TESTS=1",
 ),
 pytest.mark.skipif(
  not os.environ.get("TIER2_SMOKE"),
  reason="opt-in: full e2e smoke test off; set TIER2_SANDBOX_TESTS=1 TIER2_SMOKE=1",
 ),
 pytest.mark.tier2_smoke,
]


def test_run_track_initializes_branch(tmp_path: Path) -> None:
 """run_track.py creates the feature branch via git switch -c."""
 main_repo = tmp_path / "main"
 main_repo.mkdir()
 subprocess.run(["git", "init", "--bare"], cwd=str(main_repo), check=True, capture_output=True)
 clone = tmp_path / "clone"
 subprocess.run(
  ["git", "clone", str(main_repo), str(clone)],
  check=True, capture_output=True,
 )
 subprocess.run(["git", "config", "user.email", "test@test"], cwd=str(clone), check=True)
 subprocess.run(["git", "config", "user.name", "Test"], cwd=str(clone), check=True)
 (clone / "README.md").write_text("test\n")
 subprocess.run(["git", "add", "README.md"], cwd=str(clone), check=True)
 subprocess.run(["git", "commit", "-m", "init"], cwd=str(clone), check=True)
 subprocess.run(["git", "push", "origin", "HEAD:main"], cwd=str(clone), check=True)
 result = subprocess.run(
  [
   "uv", "run", "python", "-m", "scripts.tier2.run_track",
   "smoke_track", "--repo-path", str(clone),
  ],
  capture_output=True, text=True, timeout=60,
 )
 assert result.returncode == 0, f"run_track failed: rc={result.returncode}\nstdout={result.stdout}\nstderr={result.stderr}"
 branch_result = subprocess.run(
  ["git", "branch"],
  cwd=str(clone), capture_output=True, text=True,
 )
 assert "tier2/smoke_track" in branch_result.stdout
