"""Tests for the GET /v1/models endpoint."""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

if TYPE_CHECKING:
    from httpx import AsyncClient


@pytest.mark.unit
async def test_models_returns_200(client: AsyncClient) -> None:
    resp = await client.get("/v1/models")
    assert resp.status_code == 200


@pytest.mark.unit
async def test_models_openai_format(client: AsyncClient) -> None:
    """Response must follow OpenAI's model list schema."""
    data = (await client.get("/v1/models")).json()
    assert data["object"] == "list"
    assert isinstance(data["data"], list)


@pytest.mark.unit
async def test_models_each_entry_has_required_fields(client: AsyncClient) -> None:
    """Every model entry should contain id, object, created, owned_by."""
    data = (await client.get("/v1/models")).json()
    for entry in data["data"]:
        assert "id" in entry
        assert entry["object"] == "model"
        assert isinstance(entry["created"], int)
        assert "owned_by" in entry


@pytest.mark.unit
async def test_models_empty_when_no_backends(client: AsyncClient) -> None:
    """Without Ollama running, models list should be empty."""
    data = (await client.get("/v1/models")).json()
    # No Ollama in test env, so data should be empty
    assert data["data"] == [] or isinstance(data["data"], list)
