"""Verify /api/media/{project}/{path} returns 206 Partial Content on Range requests."""
from __future__ import annotations

import pytest
from httpx import ASGITransport, AsyncClient

from recoil.api.main import app
from recoil.core.paths import projects_root


@pytest.mark.asyncio
async def test_media_returns_206_on_range_request(tmp_path, monkeypatch):
    config_path = projects_root() / "tartarus" / "project_config.json"
    if not config_path.exists():
        pytest.skip(f"tartarus/project_config.json not on disk: {config_path}")

    rel = "project_config.json"
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        # Full GET first to confirm route works at all.
        r = await client.get(f"/api/media/tartarus/{rel}")
        assert r.status_code == 200, r.text
        full_len = len(r.content)
        assert full_len > 0

        # Range request: first 32 bytes.
        r = await client.get(
            f"/api/media/tartarus/{rel}",
            headers={"Range": "bytes=0-31"},
        )
        assert r.status_code == 206, (
            f"Expected 206 Partial Content, got {r.status_code}: {r.text}"
        )
        assert "content-range" in {k.lower() for k in r.headers}
        assert len(r.content) == 32
