# #region Test.AsyncSupersetClient [C:3] [TYPE Module] [SEMANTICS test,superset,async,client]
# @BRIEF Tests for core/async_superset_client.py — AsyncSupersetClient.
# @RELATION BINDS_TO -> [AsyncSupersetClientModule]
# @TEST_EDGE: aclose_called -> delegates to client.aclose

from pathlib import Path
import sys

sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src"))

from unittest.mock import AsyncMock, MagicMock, patch
import pytest


class TestAsyncSupersetClient:
    """Verify AsyncSupersetClient backward-compat wrapper."""

    def test_init_delegates_to_superset_client(self):
        """__init__ calls super().__init__ with env."""
        from src.core.async_superset_client import AsyncSupersetClient
        from src.core.config_models import Environment

        env = MagicMock(spec=Environment)
        env.id = "test-env"
        env.name = "Test"
        env.url = "https://test.example.com"
        env.username = "admin"
        env.password = "pass"
        env.verify_ssl = True
        env.timeout = 30

        with patch("src.core.superset_client._base.SupersetClientBase.__init__") as mock_init:
            mock_init.return_value = None
            client = AsyncSupersetClient(env)
            mock_init.assert_called_once_with(env)

    async def test_aclose_calls_client_aclose(self):
        from src.core.async_superset_client import AsyncSupersetClient
        from src.core.config_models import Environment

        env = MagicMock(spec=Environment)
        env.id = "test-env"
        env.name = "Test"
        env.url = "https://test.example.com"
        env.username = "admin"
        env.password = "pass"
        env.verify_ssl = True
        env.timeout = 30

        client = AsyncSupersetClient.__new__(AsyncSupersetClient)
        client.client = MagicMock()
        client.client.aclose = AsyncMock()

        await client.aclose()
        client.client.aclose.assert_awaited_once()

    async def test_aclose_on_real_init(self):
        """Integration: init with env, then aclose should work."""
        from src.core.config_models import Environment

        env = Environment(
            id="test-env",
            name="Test",
            url="https://test.example.com",
            username="admin",
            password="pass",
            verify_ssl=False,
            timeout=10,
        )

        with patch("src.core.superset_client._base.SupersetClientBase.__init__") as mock_init:
            mock_init.return_value = None
            from src.core.async_superset_client import AsyncSupersetClient

            client = AsyncSupersetClient.__new__(AsyncSupersetClient)
            client.client = MagicMock()
            client.client.aclose = AsyncMock()
            await client.aclose()
            client.client.aclose.assert_awaited_once()
# #endregion Test.AsyncSupersetClient
