"""Tests for #657 brand-by-account — UserScope.brand resolution.

Pure-function tests against app.core.auth: brand slug привязывается к аккаунту
(praktika/kopylov → "praktika"), остальные юзеры → None. get_user_scope
прокидывает brand в scope, который /me отдаёт фронту.

Без БД и сети: читается реальный auth/roles.yaml (ancestor-walk внутри репо).
"""

from __future__ import annotations

import os
import sys
from unittest.mock import MagicMock

# DATABASE_URL required by config before any app import (см. test_dadata.py).
os.environ.setdefault("DATABASE_URL", "postgresql+psycopg://test:test@localhost:5432/test")

# WeasyPrint stub — not installed без GTK (consistent с прочими тестами).
_wp_mock = MagicMock()
sys.modules.setdefault("weasyprint", _wp_mock)

import pytest  # noqa: E402

from app.core.auth import get_brand_for_user, get_user_scope  # noqa: E402


@pytest.mark.parametrize(
    ("username", "expected"),
    [
        ("praktika", "praktika"),
        ("kopylov", "praktika"),
        ("admin", None),
        ("user1", None),
        ("unknown_user", None),
    ],
)
def test_get_brand_for_user(username: str, expected: str | None) -> None:
    assert get_brand_for_user(username) == expected


def test_user_scope_carries_brand_for_praktika() -> None:
    scope = get_user_scope("praktika")
    assert scope["brand"] == "praktika"
    assert scope["username"] == "praktika"


def test_user_scope_brand_none_for_generic_user() -> None:
    scope = get_user_scope("user1")
    assert scope["brand"] is None
