def test_projects_filter_by_service(api, content):
    slugs = {p["slug"] for p in api.get("/api/v1/projects/?service=creation-de-site-web").json()}
    assert slugs == {"restaurant-sobbq", "neo-industry-s-a"}


def test_project_list_includes_the_detail_text(api, content):
    """L'accueil affiche le même texte que la fiche de la réalisation."""
    projects = {p["slug"]: p for p in api.get("/api/v1/projects/").json()}
    detail = api.get("/api/v1/projects/restaurant-sobbq/").json()
    assert projects["restaurant-sobbq"]["solution"] == detail["solution"]
    assert "spécialisé dans les grillades" in detail["solution"]


def test_project_detail(api, content):
    data = api.get("/api/v1/projects/rapid-alert-com/").json()
    assert data["live_url"] == "https://rapid-alert.com"
    assert data["gallery"] == []


def test_testimonials_and_clients(api, content):
    assert len(api.get("/api/v1/testimonials/").json()) == 4
    first = api.get("/api/v1/clients/").json()[0]
    assert first["name"] == "Afrikpay"
    assert first["logo"].endswith(".webp")


def test_logo_is_served_as_webp(api, content, settings, tmp_path):
    from io import BytesIO

    from django.core.files.uploadedfile import SimpleUploadedFile
    from PIL import Image

    from apps.portfolio.models import Client

    settings.MEDIA_ROOT = tmp_path
    buffer = BytesIO()
    Image.new("RGB", (800, 400), "navy").save(buffer, "PNG")
    client = Client.objects.get(name="SABC")
    client.logo = None
    client.logo = SimpleUploadedFile("sabc.png", buffer.getvalue(), content_type="image/png")
    client.save()
    logo = next(c["logo"] for c in api.get("/api/v1/clients/").json() if c["name"] == "SABC")
    assert logo.startswith("/media/CACHE/") and logo.endswith(".webp")
