"""App-level tests: SPA fallback, health, error shapes."""
from __future__ import annotations

from flask.testing import FlaskClient


def test_health_endpoint(client: FlaskClient) -> None:
    resp = client.get("/api/health")
    assert resp.status_code == 200
    assert resp.get_json() == {"status": "ok"}


def test_unknown_api_path_returns_json_404(client: FlaskClient) -> None:
    """SPA fallback must not shadow unknown /api/* routes with index.html."""
    resp = client.get("/api/nonexistent")
    assert resp.status_code == 404
    assert resp.is_json, f"expected JSON, got Content-Type={resp.content_type}"
    assert resp.get_json() == {"error": "Not found"}


def test_unknown_nested_api_path_returns_json_404(client: FlaskClient) -> None:
    resp = client.get("/api/foo/bar/baz")
    assert resp.status_code == 404
    assert resp.is_json
    assert resp.get_json() == {"error": "Not found"}


def test_wrong_method_on_api_returns_json(client: FlaskClient) -> None:
    # PUT is not defined on /api/auth/login — should stay JSON, not HTML.
    resp = client.put("/api/auth/login")
    assert resp.status_code in (404, 405)
    assert resp.is_json


def test_index_without_built_frontend_returns_json(client: FlaskClient) -> None:
    resp = client.get("/")
    assert resp.status_code == 200
    # Tests run with no built frontend → factory falls back to a JSON status payload.
    assert resp.is_json
    body = resp.get_json()
    assert body["status"] == "ok"
