# #region Test.AuthOauth [C:3] [TYPE Module] [SEMANTICS test,auth,oauth,adfs,oidc]
# @BRIEF Tests for core/auth/oauth.py — register_adfs, is_adfs_configured.
# @RELATION BINDS_TO -> [AuthOauthModule]

from pathlib import Path
import sys

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

from unittest.mock import MagicMock, patch
import pytest


# #region test_register_adfs [C:2] [TYPE Function]
# @BRIEF Test register_adfs and is_adfs_configured.
class TestRegisterAdfs:
    def test_register_adfs_when_configured(self):
        """ADFS registration happens when client_id is set."""
        import src.core.auth.oauth as oauth_module
        with patch.object(oauth_module.auth_config, "ADFS_CLIENT_ID", "test-client-id"), \
             patch.object(oauth_module.auth_config, "ADFS_CLIENT_SECRET", "test-secret"), \
             patch.object(oauth_module.auth_config, "ADFS_METADATA_URL", "https://adfs/metadata"), \
             patch.object(oauth_module.oauth, "register") as mock_register:
            oauth_module.register_adfs()
            mock_register.assert_called_once_with(
                name="adfs",
                client_id="test-client-id",
                client_secret="test-secret",
                server_metadata_url="https://adfs/metadata",
                client_kwargs={"scope": "openid email profile groups"},
            )

    def test_register_adfs_when_not_configured(self):
        """ADFS registration is skipped when client_id is empty."""
        import src.core.auth.oauth as oauth_module
        with patch.object(oauth_module.auth_config, "ADFS_CLIENT_ID", ""), \
             patch.object(oauth_module.oauth, "register") as mock_register:
            oauth_module.register_adfs()
            mock_register.assert_not_called()

    def test_is_adfs_configured_when_registered(self):
        """is_adfs_configured returns True when adfs is in registry."""
        import src.core.auth.oauth as oauth_module
        with patch.object(oauth_module.oauth, "_registry", {"adfs": "something"}):
            assert oauth_module.is_adfs_configured() is True

    def test_is_adfs_configured_when_not_registered(self):
        """is_adfs_configured returns False when adfs is not in registry."""
        import src.core.auth.oauth as oauth_module
        with patch.object(oauth_module.oauth, "_registry", {}):
            assert oauth_module.is_adfs_configured() is False
# #endregion test_register_adfs


# #region test_oauth_module_init [C:2] [TYPE Function]
# @BRIEF Test module-level initialization calls register_adfs.
class TestOauthModuleInit:
    def test_oauth_instance_exists(self):
        import src.core.auth.oauth as oauth_module
        assert oauth_module.oauth is not None
# #endregion test_oauth_module_init
# #endregion Test.AuthOauth
