"""IT-FILE-001 — GET /api/files/browse returns directory JSON."""

from __future__ import annotations

import json
from unittest.mock import AsyncMock, patch

import pytest

from server.api.files import browse_directory_get

pytestmark = pytest.mark.integration


@pytest.mark.asyncio
async def test_browse_directory_get_returns_items(
    db_session,
    test_admin,
    test_server,
    mock_request,
):
    mock_request.state.db = db_session
    payload = {
        "path": "/www/wwwroot",
        "items": [
            {
                "name": "index.html",
                "is_dir": False,
                "size": 1024,
                "mtime": "2026-06-08 12:00",
            }
        ],
    }

    with patch(
        "server.api.files.list_remote_directory",
        new_callable=AsyncMock,
        return_value=payload,
    ):
        response = await browse_directory_get(
            mock_request,
            server_id=test_server.id,
            path="/www/wwwroot",
            admin=test_admin["admin"],
            if_none_match=None,
        )

    assert response.status_code == 200
    body = json.loads(response.body)
    assert body["path"] == "/www/wwwroot"
    assert len(body["items"]) == 1
    assert body["items"][0]["name"] == "index.html"
    assert "ETag" in response.headers
