"""FRREP-LD010 — PWA shell.

Asserts that the manifest, service worker, and offline route are reachable
at site-root scope with the correct headers.
"""
from __future__ import annotations

import json

import pytest
from django.urls import reverse


@pytest.fixture(autouse=True)
def _disable_axes(settings):
    settings.AXES_ENABLED = False
    yield


def test_manifest_served_at_root_with_correct_content_type(client):
    response = client.get("/manifest.webmanifest")
    assert response.status_code == 200
    assert response["Content-Type"].startswith("application/manifest+json")
    body = b"".join(response.streaming_content).decode() if response.streaming else response.content.decode()
    data = json.loads(body)
    assert data["name"] == "IILMP — RUFORUM"
    assert data["start_url"] == "/"
    assert data["scope"] == "/"
    assert data["display"] == "standalone"
    assert data["theme_color"] == "#0F4C2A"
    assert isinstance(data["icons"], list) and data["icons"]


def test_service_worker_served_at_root_with_scope_header(client):
    response = client.get("/service-worker.js")
    assert response.status_code == 200
    assert response["Content-Type"].startswith("application/javascript")
    assert response["Service-Worker-Allowed"] == "/"


def test_offline_page_renders(client):
    response = client.get("/offline/")
    assert response.status_code == 200
    body = response.content.decode()
    assert "You're offline" in body
    # Must be reachable as a no-auth fallback.
    assert "Try again" in body
