"""Tests for TemporaryAuthStore."""

import time


class TestTemporaryAuthStore:
    """Tests for TemporaryAuthStore class."""

    def test_init_with_default_ttl(self):
        """TemporaryAuthStore initializes with default 10-second TTL."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore()
        assert store.ttl == 10

    def test_init_with_custom_ttl(self):
        """TemporaryAuthStore accepts custom TTL in seconds."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore(ttl_seconds=60)
        assert store.ttl == 60

    def test_store_auth_returns_token(self):
        """store_auth returns a token string."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore(ttl_seconds=60)
        token = store.store_auth("testuser", "testpass")

        assert token is not None
        assert isinstance(token, str)
        assert len(token) > 0

    def test_store_auth_token_is_url_safe(self):
        """store_auth returns URL-safe token."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore(ttl_seconds=60)
        token = store.store_auth("testuser", "testpass")

        # URL-safe tokens should not contain +, /, =
        # secrets.token_urlsafe uses - and _ instead
        assert "+" not in token
        assert "/" not in token

    def test_store_auth_tokens_are_unique(self):
        """Each store_auth call returns a unique token."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore(ttl_seconds=60)
        token1 = store.store_auth("user1", "pass1")
        token2 = store.store_auth("user2", "pass2")

        assert token1 != token2

    def test_retrieve_auth_returns_credentials(self):
        """retrieve_auth returns (username, password) tuple."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore(ttl_seconds=60)
        token = store.store_auth("myuser", "mypass")

        result = store.retrieve_auth(token)

        assert result is not None
        assert result == ("myuser", "mypass")

    def test_retrieve_auth_removes_entry(self):
        """retrieve_auth removes the entry after retrieval."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore(ttl_seconds=60)
        token = store.store_auth("myuser", "mypass")

        # First retrieval succeeds
        result1 = store.retrieve_auth(token)
        assert result1 == ("myuser", "mypass")

        # Second retrieval returns None
        result2 = store.retrieve_auth(token)
        assert result2 is None

    def test_retrieve_auth_nonexistent_returns_none(self):
        """retrieve_auth returns None for nonexistent token."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore(ttl_seconds=60)
        result = store.retrieve_auth("nonexistent-token")
        assert result is None

    def test_peek_auth_returns_credentials(self):
        """peek_auth returns (username, password) tuple."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore(ttl_seconds=60)
        token = store.store_auth("peekuser", "peekpass")

        result = store.peek_auth(token)
        assert result == ("peekuser", "peekpass")

    def test_peek_auth_does_not_remove_entry(self):
        """peek_auth does not remove the entry."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore(ttl_seconds=60)
        token = store.store_auth("peekuser", "peekpass")

        # Peek multiple times
        result1 = store.peek_auth(token)
        result2 = store.peek_auth(token)

        assert result1 == ("peekuser", "peekpass")
        assert result2 == ("peekuser", "peekpass")

    def test_peek_auth_nonexistent_returns_none(self):
        """peek_auth returns None for nonexistent token."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore(ttl_seconds=60)
        result = store.peek_auth("nonexistent-token")
        assert result is None

    def test_auth_expires_after_ttl(self):
        """Auth expires and returns None after TTL."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore(ttl_seconds=60)
        token = store.store_auth("expuser", "exppass")

        # Manually set expiration to past
        store._store[token]["expires_at"] = time.time() - 1

        # Should return None
        result = store.retrieve_auth(token)
        assert result is None

    def test_expired_peek_returns_none(self):
        """peek_auth returns None for expired entry."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore(ttl_seconds=60)
        token = store.store_auth("expuser", "exppass")

        # Manually set expiration to past
        store._store[token]["expires_at"] = time.time() - 1

        # Should return None
        result = store.peek_auth(token)
        assert result is None

    def test_store_alias_method(self):
        """store() is an alias for store_auth()."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore(ttl_seconds=60)
        token = store.store("aliasuser", "aliaspass")

        result = store.retrieve_auth(token)
        assert result == ("aliasuser", "aliaspass")

    def test_retrieve_alias_method(self):
        """retrieve() is an alias for retrieve_auth()."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore(ttl_seconds=60)
        token = store.store_auth("aliasuser", "aliaspass")

        result = store.retrieve(token)
        assert result == ("aliasuser", "aliaspass")

    def test_multiple_users(self):
        """Can store credentials for multiple users."""
        from local_deep_research.database.temp_auth import TemporaryAuthStore

        store = TemporaryAuthStore(ttl_seconds=60)
        token1 = store.store_auth("user1", "pass1")
        token2 = store.store_auth("user2", "pass2")
        token3 = store.store_auth("user3", "pass3")

        assert store.peek_auth(token1) == ("user1", "pass1")
        assert store.peek_auth(token2) == ("user2", "pass2")
        assert store.peek_auth(token3) == ("user3", "pass3")


class TestTemporaryAuthStoreGlobalInstance:
    """Tests for the global temp_auth_store instance."""

    def test_global_instance_exists(self):
        """Global temp_auth_store instance exists."""
        from local_deep_research.database.temp_auth import temp_auth_store

        assert temp_auth_store is not None

    def test_global_instance_is_temporary_auth_store(self):
        """Global instance is TemporaryAuthStore type."""
        from local_deep_research.database.temp_auth import (
            temp_auth_store,
            TemporaryAuthStore,
        )

        assert isinstance(temp_auth_store, TemporaryAuthStore)
