feat(bandcamp): simulate typed autocomplete searches

This commit is contained in:
beefcarver committed 2026-11-01 08:58:43 -05:00
1 parent 48e480d58c
commit 3ae4dc3dab
2 files changed
+185 -41

No files matched your search

+73 -17
View File
@@ -151,26 +151,60 @@ def test_pick_track_and_album_ignore_mismatches() -> None:
assert bandcamp.pick_album(results, "Someone Else", "Gravity") is None
def test_build_query_normalises_for_autocomplete() -> None:
assert bandcamp.build_query(
" Diskette Park ", "Dreams — [Extended Mix] (Live)!"
) == "diskette park dreams extended mix live!"
assert bandcamp.build_query("Don't Stop", "AC/DC") == "dont stop acdc"
def test_build_prefixes_grows_in_small_typing_groups() -> None:
assert bandcamp.build_prefixes("diskette park dreams") == [
"dis",
"disket",
"diskette ",
"diskette par",
"diskette park d",
"diskette park dreams",
]
assert bandcamp.build_prefixes("abcde") == ["abcde"]
assert bandcamp.build_prefixes("") == []
def test_matches_is_case_and_whitespace_insensitive() -> None:
assert bandcamp._matches("Jens Buchert", "jens buchert")
assert bandcamp._matches("Rauschebart", "RAUSCHEBART (Live)")
assert not bandcamp._matches("Dreams", "Something Else")
@pytest.mark.asyncio
async def test_find_track_uses_track_search_first() -> None:
requests: list[httpx.Request] = []
match_text = "jens buchert ra"
def handler(request: httpx.Request) -> httpx.Response:
requests.append(request)
assert request.method == "POST"
assert json.loads(request.content) == {
"search_text": "Jens Buchert Rauschebart",
"search_filter": "t",
"full_page": True,
"fan_id": None,
body = json.loads(request.content)
assert body["search_filter"] == "t"
assert body["full_page"] is True
assert body["fan_id"] is None
payload = TRACK_PAYLOAD if body["search_text"] == match_text else {
"auto": {"results": []}
}
return httpx.Response(200, json=TRACK_PAYLOAD)
return httpx.Response(200, json=payload)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
url = await bandcamp.find_track(client, "Jens Buchert", "Gravity", "Rauschebart")
assert url == "https://jensbuchert.bandcamp.com/track/rauschebart"
assert len(requests) == 1
assert [json.loads(request.content)["search_text"] for request in requests] == [
"jen",
"jens b",
"jens buch",
"jens buchert",
"jens buchert ra",
]
@pytest.mark.asyncio
@@ -184,7 +218,12 @@ async def test_find_track_falls_back_to_album_then_track_on_page() -> None:
if body["search_filter"] == "t":
return httpx.Response(200, json={"auto": {"results": []}})
if body["search_filter"] == "a":
return httpx.Response(200, json=ALBUM_PAYLOAD)
payload = (
ALBUM_PAYLOAD
if body["search_text"] == "jens buchert"
else {"auto": {"results": []}}
)
return httpx.Response(200, json=payload)
if request.url.path == "/album/gravity":
return httpx.Response(200, text=ALBUM_PAGE_HTML)
return httpx.Response(404)
@@ -195,15 +234,30 @@ async def test_find_track_falls_back_to_album_then_track_on_page() -> None:
)
assert url == "https://jensbuchert.bandcamp.com/track/rauschebart"
assert len(requests) == 3
search_bodies = [json.loads(request.content) for request in requests[:2]]
assert [body["search_text"] for body in search_bodies] == [
"Jens Buchert Rauschebart",
"Jens Buchert Gravity",
search_bodies = [
json.loads(request.content)
for request in requests
if request.method == "POST"
]
assert [body["search_filter"] for body in search_bodies] == ["t", "a"]
assert requests[2].method == "GET"
assert requests[2].url.path == "/album/gravity"
assert [body["search_text"] for body in search_bodies] == [
"jen",
"jens b",
"jens buch",
"jens buchert",
"jens buchert ra",
"jens buchert rausc",
"jens buchert rauscheb",
"jens buchert rauschebart",
"jen",
"jens b",
"jens buch",
"jens buchert",
]
assert [body["search_filter"] for body in search_bodies] == ["t"] * 8 + [
"a"
] * 4
assert requests[-1].method == "GET"
assert requests[-1].url.path == "/album/gravity"
@pytest.mark.asyncio
@@ -216,4 +270,6 @@ async def test_find_track_returns_none_for_challenge_or_no_results() -> None:
async with httpx.AsyncClient(transport=httpx.MockTransport(challenge)) as client:
assert await bandcamp.find_track(client, "A", "B", "C") is None
assert len(requests) == 2
assert len(requests) == len(bandcamp.build_prefixes("a c")) + len(
bandcamp.build_prefixes("a b")
)