"""Extra coverage tests for session_passwords.py — clear_all_for_user and aliases."""

from local_deep_research.database.session_passwords import SessionPasswordStore


class TestClearAllForUser:
    def test_clears_all_sessions_for_user(self):
        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("alice", "s1", "pw1")
        store.store_session_password("alice", "s2", "pw2")
        store.store_session_password("bob", "s3", "pw3")

        store.clear_all_for_user("alice")

        assert store.get_session_password("alice", "s1") is None
        assert store.get_session_password("alice", "s2") is None
        assert store.get_session_password("bob", "s3") == "pw3"

    def test_clears_nothing_when_user_not_found(self):
        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("alice", "s1", "pw1")

        store.clear_all_for_user("nonexistent")

        assert store.get_session_password("alice", "s1") == "pw1"

    def test_clears_nothing_on_empty_store(self):
        store = SessionPasswordStore(ttl_hours=1)
        store.clear_all_for_user("alice")  # should not raise

    def test_does_not_clear_similar_prefix(self):
        """'alice' should not clear 'alice2:...' entries."""
        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("alice", "s1", "pw1")
        store.store_session_password("alice2", "s1", "pw2")

        store.clear_all_for_user("alice")

        assert store.get_session_password("alice", "s1") is None
        assert store.get_session_password("alice2", "s1") == "pw2"

    def test_clears_single_session(self):
        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("alice", "only-one", "pw")

        store.clear_all_for_user("alice")

        assert store.get_session_password("alice", "only-one") is None


class TestAliases:
    def test_store_alias(self):
        store = SessionPasswordStore(ttl_hours=1)
        store.store("user", "sid", "pass")
        assert store.get_session_password("user", "sid") == "pass"

    def test_retrieve_alias(self):
        store = SessionPasswordStore(ttl_hours=1)
        store.store_session_password("user", "sid", "pass")
        assert store.retrieve("user", "sid") == "pass"

    def test_retrieve_returns_none_when_missing(self):
        store = SessionPasswordStore(ttl_hours=1)
        assert store.retrieve("user", "nosid") is None
