"""Tests for SessionPasswordStore."""

import time


class TestSessionPasswordStore:
    """Tests for SessionPasswordStore class."""

    def test_init_with_default_ttl(self):
        """SessionPasswordStore initializes with default 24-hour TTL."""
        from local_deep_research.database.session_passwords import (
            SessionPasswordStore,
        )

        store = SessionPasswordStore()
        # TTL should be 24 hours in seconds
        assert store.ttl == 24 * 3600

    def test_init_with_custom_ttl(self):
        """SessionPasswordStore accepts custom TTL in hours."""
        from local_deep_research.database.session_passwords import (
            SessionPasswordStore,
        )

        store = SessionPasswordStore(ttl_hours=12)
        assert store.ttl == 12 * 3600

    def test_store_session_password_stores_correctly(self):
        """store_session_password stores password correctly."""
        from local_deep_research.database.session_passwords import (
            SessionPasswordStore,
        )

        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("testuser", "session123", "mypassword")

        # Verify it was stored
        result = store.get_session_password("testuser", "session123")
        assert result == "mypassword"

    def test_get_session_password_returns_password(self):
        """get_session_password returns the stored password."""
        from local_deep_research.database.session_passwords import (
            SessionPasswordStore,
        )

        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("user1", "sess1", "pass123")

        result = store.get_session_password("user1", "sess1")
        assert result == "pass123"

    def test_get_session_password_nonexistent_returns_none(self):
        """get_session_password returns None for nonexistent entries."""
        from local_deep_research.database.session_passwords import (
            SessionPasswordStore,
        )

        store = SessionPasswordStore(ttl_hours=1)
        result = store.get_session_password("nonexistent", "nosession")
        assert result is None

    def test_clear_session_clears_entry(self):
        """clear_session removes the stored password."""
        from local_deep_research.database.session_passwords import (
            SessionPasswordStore,
        )

        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("user1", "sess1", "pass123")

        # Verify stored
        assert store.get_session_password("user1", "sess1") == "pass123"

        # Clear
        store.clear_session("user1", "sess1")

        # Verify cleared
        assert store.get_session_password("user1", "sess1") is None

    def test_clear_session_nonexistent_entry_no_error(self):
        """clear_session does not raise error for nonexistent entry."""
        from local_deep_research.database.session_passwords import (
            SessionPasswordStore,
        )

        store = SessionPasswordStore(ttl_hours=1)
        # Should not raise
        store.clear_session("nonexistent", "nosession")

    def test_session_key_format_is_username_session_id(self):
        """Session key format is (username, session_id) tuple."""
        from local_deep_research.database.session_passwords import (
            SessionPasswordStore,
        )

        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("myuser", "mysession", "pass")

        # Check the internal key format
        expected_key = ("myuser", "mysession")
        assert expected_key in store._store

    def test_password_expires_after_ttl(self):
        """Password expires and returns None after TTL."""
        from local_deep_research.database.session_passwords import (
            SessionPasswordStore,
        )

        # Use a very short TTL (1 second converted from hours)
        # But we can manipulate the store directly for testing
        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("user1", "sess1", "pass123")

        # Manually set expiration to past
        key = ("user1", "sess1")
        store._store[key]["expires_at"] = time.time() - 1

        # Should return None
        result = store.get_session_password("user1", "sess1")
        assert result is None

    def test_store_alias_method(self):
        """store() is an alias for store_session_password()."""
        from local_deep_research.database.session_passwords import (
            SessionPasswordStore,
        )

        store = SessionPasswordStore(ttl_hours=1)
        store.store("alias_user", "alias_session", "alias_pass")

        result = store.get_session_password("alias_user", "alias_session")
        assert result == "alias_pass"

    def test_retrieve_alias_method(self):
        """retrieve() is an alias for get_session_password()."""
        from local_deep_research.database.session_passwords import (
            SessionPasswordStore,
        )

        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("user1", "sess1", "pass123")

        result = store.retrieve("user1", "sess1")
        assert result == "pass123"

    def test_multiple_sessions_same_user(self):
        """Can store multiple sessions for same user."""
        from local_deep_research.database.session_passwords import (
            SessionPasswordStore,
        )

        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("user1", "session_a", "pass_a")
        store.store_session_password("user1", "session_b", "pass_b")

        assert store.get_session_password("user1", "session_a") == "pass_a"
        assert store.get_session_password("user1", "session_b") == "pass_b"

    def test_overwrite_session_password(self):
        """Storing same session again overwrites password."""
        from local_deep_research.database.session_passwords import (
            SessionPasswordStore,
        )

        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("user1", "sess1", "original")
        store.store_session_password("user1", "sess1", "updated")

        result = store.get_session_password("user1", "sess1")
        assert result == "updated"


class TestSessionPasswordStoreGlobalInstance:
    """Tests for the global session_password_store instance."""

    def test_global_instance_is_session_password_store(self):
        """Global instance is SessionPasswordStore type."""
        from local_deep_research.database.session_passwords import (
            session_password_store,
            SessionPasswordStore,
        )

        assert isinstance(session_password_store, SessionPasswordStore)
