"""Cluster 7 (audiobook/ABB) parse-contract guards. ABB forces ``https://`` for search and detail fetches, so it can't be exercised hermetically in the HTTP e2e docker platform. Its recurring bugs are instead in *parsing*: magnet/info-hash extraction ("Fix ABB magnet parsing", and the qbittorrent hash-length issue #386) and DOM/layout drift. These contract tests feed golden HTML through the real scraper — the same fail-on-drift philosophy as the AA layout-drift guard — and run in normal CI. They deliberately cover cases the existing ``test_scraper.py`` does not: info-hash *normalization* (whitespace/case), the in-page magnet *fallback*, and a layout drift that must degrade to an empty result rather than crash. """ from __future__ import annotations import re from unittest.mock import patch from shelfmark.release_sources.audiobookbay import scraper # Detail page where the Info Hash is lowercase and split by whitespace/newlines — # the exact shape that produced malformed magnets / wrong hash lengths (#386). DETAIL_HTML_MESSY_HASH = """
| Info Hash | abc123def456789012345678 901234567890abcd |
| Tracker 1 | udp://tracker.openbittorrent.com:80 |
| Info Hash | n/a |
Mirror: magnet:?xt=urn:btih:1111111111111111111111111111111111111111&dn=x
""" # DOM drift: results are present but the .post / .postTitle structure changed. SEARCH_HTML_LAYOUT_DRIFT = """nothing here
"): assert ( scraper.extract_magnet_link("https://audiobookbay.lu/abss/z/", "audiobookbay.lu") is None ) def test_search_layout_drift_degrades_to_empty() -> None: """A changed results DOM yields zero parsed results without raising — the ABB analogue of the AA layout-drift guard.""" with ( patch( "shelfmark.release_sources.audiobookbay.scraper.downloader.html_get_page", return_value=(SEARCH_HTML_LAYOUT_DRIFT, "https://audiobookbay.lu/?s=test"), ), patch( "shelfmark.release_sources.audiobookbay.scraper.config.get", return_value=0.0, ), ): results = scraper.search_audiobookbay("test", max_pages=1, hostname="audiobookbay.lu") assert results == [], f"drifted DOM should parse to no results, got {results!r}"