"""Tests for duty_teller.handlers.common (is_admin_async)."""

from unittest.mock import MagicMock, patch

import pytest

from duty_teller.handlers.common import is_admin_async


@pytest.mark.asyncio
async def test_is_admin_async_true_when_repository_returns_true():
    """is_admin_async returns True when is_admin_for_telegram_user returns True."""
    mock_session = MagicMock()
    mock_scope = MagicMock()
    mock_scope.return_value.__enter__.return_value = mock_session
    mock_scope.return_value.__exit__.return_value = None

    with patch(
        "duty_teller.handlers.common.session_scope",
        mock_scope,
    ):
        with patch(
            "duty_teller.handlers.common.is_admin_for_telegram_user",
            return_value=True,
        ) as mock_is_admin:
            result = await is_admin_async(12345)
    assert result is True
    mock_scope.assert_called_once()
    mock_scope.return_value.__enter__.assert_called_once()
    mock_is_admin.assert_called_once_with(mock_session, 12345)


@pytest.mark.asyncio
async def test_is_admin_async_false_when_repository_returns_false():
    """is_admin_async returns False when is_admin_for_telegram_user returns False."""
    mock_session = MagicMock()
    mock_scope = MagicMock()
    mock_scope.return_value.__enter__.return_value = mock_session
    mock_scope.return_value.__exit__.return_value = None

    with patch(
        "duty_teller.handlers.common.session_scope",
        mock_scope,
    ):
        with patch(
            "duty_teller.handlers.common.is_admin_for_telegram_user",
            return_value=False,
        ) as mock_is_admin:
            result = await is_admin_async(99999)
    assert result is False
    mock_scope.assert_called_once()
    mock_is_admin.assert_called_once_with(mock_session, 99999)
