"""Unit tests for the pure helpers added to downloads.py.""" from types import SimpleNamespace from app.tasks.downloads import ( _effective_patreon_url, _extract_vanity_from_url, _looks_like_campaign_id_failure, ) # --- _effective_patreon_url ------------------------------------------------- def test_effective_url_patreon_with_cached_id_uses_id_url(): assert ( _effective_patreon_url("patreon", "https://www.patreon.com/mstsu", {"patreon_campaign_id": "12345"}) == "https://www.patreon.com/id:12345" ) def test_effective_url_patreon_without_cached_id_uses_original(): assert ( _effective_patreon_url("patreon", "https://www.patreon.com/mstsu", {}) == "https://www.patreon.com/mstsu" ) def test_effective_url_patreon_none_metadata_uses_original(): assert ( _effective_patreon_url("patreon", "https://www.patreon.com/mstsu", None) == "https://www.patreon.com/mstsu" ) def test_effective_url_non_patreon_ignores_cached_id(): # Even if campaign_id somehow got set on a non-patreon source, don't rewrite. assert ( _effective_patreon_url("subscribestar", "https://subscribestar.adult/foo", {"patreon_campaign_id": "12345"}) == "https://subscribestar.adult/foo" ) # --- _looks_like_campaign_id_failure --------------------------------------- def _result(stdout="", stderr=""): return SimpleNamespace(stdout=stdout, stderr=stderr) def test_failure_pattern_in_stderr(): assert _looks_like_campaign_id_failure( _result(stderr="[patreon][error] Failed to extract campaign ID") ) is True def test_failure_pattern_in_stdout(): assert _looks_like_campaign_id_failure( _result(stdout="something Failed to extract campaign ID something") ) is True def test_failure_pattern_case_insensitive(): assert _looks_like_campaign_id_failure( _result(stderr="FAILED TO EXTRACT CAMPAIGN ID") ) is True def test_failure_pattern_absent(): assert _looks_like_campaign_id_failure( _result(stderr="HTTP 401 Unauthorized") ) is False def test_failure_pattern_empty(): assert _looks_like_campaign_id_failure(_result()) is False def test_failure_pattern_none_streams(): assert _looks_like_campaign_id_failure( SimpleNamespace(stdout=None, stderr=None) ) is False # --- _extract_vanity_from_url ---------------------------------------------- def test_vanity_plain_patreon_url(): assert _extract_vanity_from_url("https://www.patreon.com/mstsu") == "mstsu" def test_vanity_with_c_prefix(): assert _extract_vanity_from_url("https://www.patreon.com/c/mstsu/posts") == "mstsu" def test_vanity_with_c_prefix_no_trailing_path(): assert _extract_vanity_from_url("https://www.patreon.com/c/mstsu") == "mstsu" def test_vanity_without_www(): assert _extract_vanity_from_url("https://patreon.com/mstsu") == "mstsu" def test_vanity_http_scheme(): assert _extract_vanity_from_url("http://www.patreon.com/mstsu") == "mstsu" def test_vanity_id_url_returns_none(): assert _extract_vanity_from_url("https://www.patreon.com/id:12345") is None def test_vanity_bare_domain_returns_none(): assert _extract_vanity_from_url("https://www.patreon.com/") is None def test_vanity_unrelated_url_returns_none(): assert _extract_vanity_from_url("https://subscribestar.adult/mstsu") is None def test_vanity_ignores_trailing_query_and_hash(): assert _extract_vanity_from_url("https://www.patreon.com/mstsu?x=1") == "mstsu" assert _extract_vanity_from_url("https://www.patreon.com/mstsu#anchor") == "mstsu"