from pathlib import Path

from app.main import app

REPO_ROOT = Path(__file__).resolve().parent.parent


def test_openapi_has_description_and_tag_metadata():
    spec = app.openapi()
    assert spec["info"]["description"]
    assert spec["info"]["version"]
    tag_names = {t["name"] for t in spec["tags"]}
    # Spot-check a few — every router's tag should have a description,
    # not just exist.
    assert "auth" in tag_names
    assert "orders" in tag_names
    for tag in spec["tags"]:
        assert tag["description"]


def test_key_request_schemas_have_examples():
    """The highest-traffic flows for a client integrating against this
    API — signup, login, checkout — should always ship a usable
    example, not just a bare field list."""
    schemas = app.openapi()["components"]["schemas"]
    for name in ("CustomerSignupRequest", "LoginRequest", "CheckoutRequest"):
        examples = schemas[name].get("examples") or schemas[name].get("example")
        assert examples, f"{name} has no example in its OpenAPI schema"


def test_api_errors_reference_doc_exists():
    content = (REPO_ROOT / "docs" / "API_ERRORS.md").read_text()
    # Spot-check a few domains are actually documented, not just that
    # the file exists.
    assert "## auth.py" in content
    assert "## orders.py" in content
    assert "429" in content
