"""Tests for shared sx helper functions (call_url, get_asset_url, etc.)."""
from __future__ import annotations

from shared.sx.helpers import call_url, get_asset_url


# ---------------------------------------------------------------------------
# call_url
# ---------------------------------------------------------------------------

class TestCallUrl:
    def test_callable_url_fn(self):
        ctx = {"blog_url": lambda path: f"https://blog.example.com{path}"}
        assert call_url(ctx, "blog_url", "/posts/") == "https://blog.example.com/posts/"

    def test_callable_default_path(self):
        ctx = {"blog_url": lambda path: f"https://blog.example.com{path}"}
        assert call_url(ctx, "blog_url") == "https://blog.example.com/"

    def test_string_url(self):
        ctx = {"blog_url": "https://blog.example.com"}
        assert call_url(ctx, "blog_url", "/posts/") == "https://blog.example.com/posts/"

    def test_string_url_default_path(self):
        ctx = {"blog_url": "https://blog.example.com"}
        assert call_url(ctx, "blog_url") == "https://blog.example.com/"

    def test_missing_key(self):
        ctx = {}
        assert call_url(ctx, "blog_url", "/x") == "/x"

    def test_none_value(self):
        ctx = {"blog_url": None}
        assert call_url(ctx, "blog_url", "/x") == "/x"

    def test_callable_with_empty_path(self):
        ctx = {"cart_url": lambda path: f"https://cart.example.com{path}"}
        assert call_url(ctx, "cart_url", "") == "https://cart.example.com"


# ---------------------------------------------------------------------------
# get_asset_url
# ---------------------------------------------------------------------------

class TestGetAssetUrl:
    def test_callable_asset_url(self):
        ctx = {"asset_url": lambda path: f"https://cdn.example.com/static{path}"}
        result = get_asset_url(ctx)
        # Should strip the trailing path component
        assert "cdn.example.com" in result

    def test_string_asset_url(self):
        ctx = {"asset_url": "https://cdn.example.com/static"}
        assert get_asset_url(ctx) == "https://cdn.example.com/static"

    def test_missing_asset_url(self):
        ctx = {}
        assert get_asset_url(ctx) == ""

    def test_none_asset_url(self):
        ctx = {"asset_url": None}
        assert get_asset_url(ctx) == ""

    def test_callable_returns_path_only(self):
        # au("") → "/static", rsplit("/",1)[0] → "" (splits on leading /)
        ctx = {"asset_url": lambda path: f"/static{path}"}
        result = get_asset_url(ctx)
        assert result == ""

    def test_callable_with_nested_path(self):
        # au("") → "/assets/static", rsplit("/",1)[0] → "/assets"
        ctx = {"asset_url": lambda path: f"/assets/static{path}"}
        result = get_asset_url(ctx)
        assert result == "/assets"
