"""Route smoke tests — every public route should respond without a 5xx,
both anonymous and (where relevant) logged in. This is the cheap net that
catches a template-rename, a bad import, or an unguarded None before it
reaches prod. It hits the dev mirror, so it's marked db/integration and
skips when the mirror is down.
"""
import pytest

pytestmark = [pytest.mark.integration, pytest.mark.db]


# (path, set-of-acceptable-status-codes). We assert "not a server error"
# broadly, but pin the expected redirect/404 cases so a route silently
# flipping behavior is caught.
PUBLIC_GET_ROUTES = [
    ("/", {200}),
    ("/browse", {200}),
    ("/instock/pricedrops", {200}),
    ("/faq", {200, 404}),            # tolerate route renames across envs
    ("/terms", {200, 404}),
    ("/login", {200}),
    ("/build", {200}),
    ("/changelog", {200, 404}),
    ("/sitemap.xml", {200}),
    ("/robots.txt", {200, 404}),
    ("/my-builds", {302}),           # auth-gated -> redirect to /login
]


@pytest.mark.parametrize("path,ok", PUBLIC_GET_ROUTES)
def test_get_route_no_server_error(client, db_conn, path, ok):
    resp = client.get(path)
    assert resp.status_code < 500, f"{path} -> {resp.status_code}"
    assert resp.status_code in ok, \
        f"{path} -> {resp.status_code}, expected one of {ok}"


def test_unknown_build_code_404(client, db_conn):
    resp = client.get("/b/this-code-does-not-exist")
    assert resp.status_code == 404


def test_browse_type_filter_renders(client, db_conn):
    # A type-filtered browse page is its own SEO landing page; it must
    # render even when the type has zero in-stock parts.
    resp = client.get("/browse?type=trigger")
    assert resp.status_code == 200
    assert b"<html" in resp.data.lower()


def test_my_builds_redirects_to_login_when_anonymous(client, db_conn):
    resp = client.get("/my-builds")
    assert resp.status_code == 302
    assert "/login" in resp.headers.get("Location", "")
