from __future__ import annotations

import pytest

from src.core.appwrite_session import normalize_appwrite_session_cookie


@pytest.mark.parametrize(
    "project_id, raw, expected",
    [
        ("proj123", None, None),
        ("proj123", "", None),
        ("proj123", "   \t", None),
        (None, "token", "token"),
        ("proj123", "token", "a_session_proj123=token"),
        ("proj123", "token; Path=/; HttpOnly", "a_session_proj123=token; Path=/; HttpOnly"),
        (
            "proj123",
            "session=value",
            "a_session_proj123=value",
        ),
        (
            "proj123",
            "a_session_proj123=value; Path=/; HttpOnly",
            "a_session_proj123=value; Path=/; HttpOnly",
        ),
        (
            "proj123",
            "locale=pt-BR; a_session_proj123=value",
            "locale=pt-BR; a_session_proj123=value",
        ),
    ],
)
def test_normalize_appwrite_session_cookie(project_id: str | None, raw: str | None, expected: str | None) -> None:
    assert normalize_appwrite_session_cookie(project_id, raw) == expected


def test_normalize_preserves_additional_cookies() -> None:
    raw = "token; Path=/; HttpOnly; locale=pt-BR"
    normalized = normalize_appwrite_session_cookie("proj123", raw)
    assert normalized == "a_session_proj123=token; Path=/; HttpOnly; locale=pt-BR"

