"""Tests for kind-0 lud06/lud16 URL collection and LNURL-pay merge (mocked HTTP).""" from __future__ import annotations from typing import Any from unittest.mock import MagicMock, patch from imwald.core.profile_lnurl import ( build_merged_lnurl_pay_section, collect_unique_lnurlp_urls, lnurlp_url_from_lud16, normalize_callback, ) def test_lnurlp_url_from_lightning_address() -> None: assert lnurlp_url_from_lud16("Alice@Example.COM") == "https://example.com/.well-known/lnurlp/Alice" def test_collect_unique_order_and_dedupe() -> None: u = collect_unique_lnurlp_urls("https://domain/.well-known/lnurlp/x", "https://domain/.well-known/lnurlp/x") assert u == ["https://domain/.well-known/lnurlp/x"] u2 = collect_unique_lnurlp_urls(None, "a@b.co") assert u2 == ["https://b.co/.well-known/lnurlp/a"] def test_normalize_callback_strips_query() -> None: assert normalize_callback("HTTPS://Host/path?x=1") == "https://host/path" @patch("imwald.core.profile_lnurl.fetch_lnurlp_pay_json") def test_build_merge_dedupes_same_wallet_callback(mock_fetch: MagicMock) -> None: def side_effect(url: str) -> dict[str, Any]: _ = url return { "tag": "payRequest", "callback": "https://wallet.example/lnurlpay/cb?ok=1", "minSendable": 1000, "maxSendable": 500_000, "allowsNostr": True, "nostrPubkey": "ab" * 32, "metadata": "[]", } mock_fetch.side_effect = side_effect html = build_merged_lnurl_pay_section( [ "https://relay-a/.well-known/lnurlp/alice", "https://relay-b/.well-known/lnurlp/alice", ] ) assert mock_fetch.call_count == 2 assert html.count("wallet.example/lnurlpay/cb") >= 1 assert "Merged sources" in html assert "(2)" in html @patch("imwald.core.profile_lnurl.fetch_lnurlp_pay_json") def test_build_merge_two_distinct_callbacks(mock_fetch: MagicMock) -> None: urls = ["https://a/1", "https://b/2"] payloads: list[dict[str, Any]] = [ {"tag": "payRequest", "callback": "https://w/a", "minSendable": 1000, "maxSendable": 100_000, "metadata": "[]"}, {"tag": "payRequest", "callback": "https://w/b", "minSendable": 2000, "maxSendable": 200_000, "metadata": "[]"}, ] def side_effect(url: str) -> dict[str, Any]: return payloads[urls.index(url)] mock_fetch.side_effect = side_effect html = build_merged_lnurl_pay_section(urls) assert mock_fetch.call_count == 2 assert html.count("Merged sources") == 2 assert "https://w/a" in html and "https://w/b" in html