# Copyright 2025 Softwell S.r.l.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for Avatar — user identity with Bag data."""

from genro_bag import Bag

from genro_asgi.session import Session
from genro_asgi.session.avatar import Avatar


class TestAvatar:
    """Tests for Avatar class."""

    def test_avatar_creation(self) -> None:
        """Avatar created with identity and tags."""
        avatar = Avatar(identity="admin", tags=["admin", "write"])

        assert avatar.identity == "admin"
        assert avatar.tags == ["admin", "write"]

    def test_avatar_bag_data(self) -> None:
        """Avatar data is a Bag instance."""
        avatar = Avatar(identity="user1", tags=["read"])

        assert isinstance(avatar.data, Bag)

    def test_avatar_from_auth(self) -> None:
        """Avatar(auth=...) creates avatar from auth dict."""
        auth = {"identity": "mrossi", "tags": ["read", "write"], "backend": "basic"}
        avatar = Avatar(auth=auth)

        assert avatar.identity == "mrossi"
        assert avatar.tags == ["read", "write"]

    def test_avatar_from_auth_none(self) -> None:
        """Avatar(auth=None) returns anonymous avatar."""
        avatar = Avatar(auth=None)

        assert avatar.identity == "anonymous"
        assert avatar.tags == []

    def test_avatar_tags_property(self) -> None:
        """Tags are accessible as list."""
        avatar = Avatar(identity="user", tags=["read", "admin"])

        assert "read" in avatar.tags
        assert "admin" in avatar.tags

    def test_avatar_in_session(self) -> None:
        """Session.avatar returns Avatar from auth snapshot."""
        auth = {"identity": "user1", "tags": ["read"], "backend": "bearer"}
        session = Session(session_id="test_id", auth=auth, ttl=3600)

        assert session.avatar is not None
        assert session.avatar.identity == "user1"
        assert session.avatar.tags == ["read"]

    def test_avatar_in_session_no_auth(self) -> None:
        """Session without auth has anonymous avatar."""
        session = Session(session_id="test_id", auth=None, ttl=3600)

        assert session.avatar.identity == "anonymous"
