# #region TestSupersetClientAsync [C:3] [TYPE Module] [SEMANTICS test, superset, async, concurrency]
# @BRIEF Concurrency and rejected-path tests for the async SupersetClient.
# @RELATION BINDS_TO -> [SupersetClient]
# @TEST_INVARIANT: Three concurrent calls run in parallel — VERIFIED_BY: test_concurrent_superset_calls
# @TEST_EDGE: sync_in_async_context — VERIFIED_BY: test_sync_superset_client_raises_in_async_context
import asyncio
import pytest
from unittest.mock import AsyncMock, MagicMock, patch

from src.core.config_models import Environment
from src.core.superset_client import SupersetClient


# #region test_concurrent_superset_calls [C:2] [TYPE Function]
# @BRIEF Three concurrent Superset API calls execute in parallel, total time ≈ max(t) not sum(t).
@pytest.mark.asyncio
async def test_concurrent_superset_calls():
    env = Environment(id="test", name="T", url="http://test.local", username="u", password="p")
    client = SupersetClient(env)
    client.client = MagicMock()
    client.client.request = AsyncMock()
    client.client.request.return_value = {"result": [{"id": 1}]}
    client.client.fetch_paginated_data = AsyncMock()
    client.client.fetch_paginated_data.return_value = [{"id": 1}, {"id": 2}]

    async def call_one():
        await asyncio.sleep(0.05)
        return client.client.request(method="GET", endpoint="/dashboard/1")

    t0 = asyncio.get_event_loop().time()
    await asyncio.gather(call_one(), call_one(), call_one())
    elapsed = asyncio.get_event_loop().time() - t0
    # Three 50ms calls in parallel ≈ 50ms, not 150ms
    assert elapsed < 0.12, f"Not concurrent: {elapsed:.3f}s (expected < 0.12s)"
# #endregion test_concurrent_superset_calls


# #endregion TestSupersetClientAsync
