from unittest.mock import MagicMock

from db.url_shortener import (
    is_short_image_url,
    resolve_destination_image_url,
    resolve_destination_image_urls,
)


def test_is_short_image_url_detects_base_slug():
    base = "https://app.example/proxy/api/i"
    assert is_short_image_url(f"{base}/AbCdEfGhIjKl", base_url=base)
    assert not is_short_image_url("https://cdn.plytix.com/render.jpg", base_url=base)


def test_resolve_destination_image_url_expands_short_link():
    base = "https://app.example/proxy/api/i"
    repo = MagicMock()
    repo.get_short_url_by_slug.return_value = MagicMock(
        destination_url="https://cdn.plytix.com/long-render.jpg"
    )
    resolved = resolve_destination_image_url(
        f"{base}/AbCdEfGhIjKl",
        repo=repo,
        base_url=base,
    )
    assert resolved == "https://cdn.plytix.com/long-render.jpg"
    repo.get_short_url_by_slug.assert_called_once_with("AbCdEfGhIjKl")


def test_resolve_destination_image_urls_dedupes():
    base = "https://app.example/proxy/api/i"
    repo = MagicMock()
    repo.get_short_url_by_slug.return_value = MagicMock(
        destination_url="https://cdn.plytix.com/a.jpg"
    )
    urls = resolve_destination_image_urls(
        [f"{base}/slug1", "https://cdn.plytix.com/a.jpg", f"{base}/slug1"],
        repo=repo,
        base_url=base,
    )
    assert urls == ["https://cdn.plytix.com/a.jpg"]
