from __future__ import annotations

"""SDK client tests — mocked HTTP."""

import os
import sys
from unittest.mock import patch

import pytest

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

from nexure import Nexure, NexureError


class TestClientInit:
    """Client initialization."""

    def test_init_with_api_key(self):
        """Client should accept api_key parameter."""
        nx = Nexure(api_key="test_key", base_url="http://localhost:8000/v1")
        assert nx.api_key == "test_key"
        assert nx.base_url == "http://localhost:8000/v1"
        nx.close()

    def test_init_without_key_raises(self):
        """Client should raise NexureError if no key provided."""
        with patch.dict(os.environ, {}, clear=True):
            # Ensure NEXURE_API_KEY is not set
            os.environ.pop("NEXURE_API_KEY", None)
            with pytest.raises(NexureError, match="API key is required"):
                Nexure()

    def test_init_from_env_var(self):
        """Client should read key from NEXURE_API_KEY env var."""
        with patch.dict(os.environ, {"NEXURE_API_KEY": "env_key"}):
            nx = Nexure(base_url="http://localhost:8000/v1")
            assert nx.api_key == "env_key"
            nx.close()

    def test_context_manager(self):
        """Client should work as context manager."""
        with Nexure(api_key="test", base_url="http://localhost:8000/v1") as nx:
            assert nx.api_key == "test"

    def test_resource_namespaces(self):
        """Client should expose all resource namespaces."""
        nx = Nexure(api_key="test", base_url="http://localhost:8000/v1")
        assert hasattr(nx, "decisions")
        assert hasattr(nx, "packages")
        assert hasattr(nx, "ledger")
        assert hasattr(nx, "briefs")
        assert hasattr(nx, "analytics")
        assert hasattr(nx, "templates")
        nx.close()
