"""Sprint 0 Scaffolding E2E Test.

Design Doc: docs/design/sprint-0-scaffolding.md
Validates the complete developer onboarding journey:
clone -> pip install -> pytest -> import smoke check.
"""

import subprocess
import sys
from pathlib import Path

import pytest


class TestFullInstallTestImportCycle:
    """E2E-1: AC-7 — Full install-test-import cycle."""

    def test_ac7_full_install_pytest_import_cycle(self) -> None:
        project_root = Path(__file__).resolve().parent.parent

        # Step 1: pip install -e ".[dev]" succeeds
        result = subprocess.run(
            [sys.executable, "-m", "pip", "install", "-e", f"{project_root}[dev]"],
            capture_output=True,
            text=True,
            timeout=120,
        )
        assert result.returncode == 0, (
            f"pip install failed:\n{result.stderr}"
        )

        # Step 2: pytest succeeds with 6+ tests, 0 failures
        result = subprocess.run(
            [sys.executable, "-m", "pytest", str(project_root / "tests"), "-v",
             "--ignore", str(project_root / "tests" / "test_scaffolding_e2e.py")],
            capture_output=True,
            text=True,
            timeout=120,
        )
        assert result.returncode == 0, (
            f"pytest failed:\n{result.stdout}\n{result.stderr}"
        )
        assert "passed" in result.stdout
        assert "failed" not in result.stdout.lower().split("passed")[0]

        # Step 3: import smoke check
        result = subprocess.run(
            [sys.executable, "-c",
             "from godotiq.server import mcp; print('OK')"],
            capture_output=True,
            text=True,
            timeout=30,
        )
        assert result.returncode == 0, (
            f"Import check failed:\n{result.stderr}"
        )
        assert "OK" in result.stdout
