"""APP_URL / APP_URL_WHITELIST resolution."""

from src.core.app_url import (
    _effective_client_whitelist,
    _normalize_github_mirror_redirect_override,
    _url_origin_in_whitelist,
)


def test_redirect_base_matches_app_url_when_whitelist_typo():
    """APP_URL origin is allowed even if missing from APP_URL_WHITELIST."""
    wl = _effective_client_whitelist(
        "https://app.p2hs.com",  # typo: dashboard missing
        "https://dashboard.p2hs.com",
    )
    path = "/settings/github-user-callback"
    url = _normalize_github_mirror_redirect_override(
        "https://dashboard.p2hs.com/projects",
        path,
    )
    assert _url_origin_in_whitelist(url, wl)


def test_empty_whitelist_stays_permissive():
    assert _effective_client_whitelist("", "https://dashboard.p2hs.com") == []


def test_app_url_merged_without_duplicating_whitelist_entry():
    wl = _effective_client_whitelist(
        "https://dashboard.p2hs.com",
        "https://dashboard.p2hs.com",
    )
    origins = [o for o, _ in wl]
    assert origins.count("https://dashboard.p2hs.com") == 1
