Skip to content

Commit a3186ee

Browse files
thewrzclaude
andcommitted
fix(matching): preserve remix intent in collect preview + strengthen tie test (#551)
CodeRabbit review on #553: - collect enrich-preview called find_best_match with the default prefer_original= True, so a named-remix query (e.g. "Surrender (Hardstyle Remix)") would favour the original version's BPM/key. Derive prefer_original from the title via is_remix_title, matching the enrichment pipeline and recommendation paths — the consistency #551 is about. Pinned with a regression test that asserts the computed prefer_original for a remix vs a plain query. - Strengthen the disabled-preference tie test to assert the deterministic winner (track_id "1") instead of just non-None. Closes #551 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 735a905 commit a3186ee

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

server/app/api/collect.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
from app.services.system_settings import get_system_settings
5858
from app.services.tidal import sync_collection_requests_batch
5959
from app.services.track_match import find_best_match
60-
from app.services.track_normalizer import normalize_isrc
60+
from app.services.track_normalizer import is_remix_title, normalize_isrc
6161
from app.services.vote import add_vote
6262

6363
logger = logging.getLogger(__name__)
@@ -563,7 +563,14 @@ def enrich_preview(
563563
try:
564564
matches = search_beatport_tracks(db, user, f"{item.artist} {item.title}", limit=5)
565565
if matches:
566-
best = find_best_match(matches, item.title, item.artist)
566+
# Preserve remix intent like the enrichment/recommendation paths:
567+
# only prefer the original version when the query isn't a named remix.
568+
best = find_best_match(
569+
matches,
570+
item.title,
571+
item.artist,
572+
prefer_original=not is_remix_title(item.title),
573+
)
567574
if best:
568575
bpm = int(best.bpm) if best.bpm is not None else None
569576
key = best.key or None

server/tests/test_collect_public.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,40 @@ def test_enrich_preview_returns_bpm_from_beatport(client, db, test_event: Event)
10141014
assert results[0]["genre"] == "Progressive House"
10151015

10161016

1017+
def test_enrich_preview_preserves_remix_intent(client, db, test_event: Event):
1018+
"""The preview must derive prefer_original from the title (#551/#553): a named-remix
1019+
query must NOT be matched with prefer_original=True (which would favour the original
1020+
version's BPM/key), matching the enrichment + recommendation paths."""
1021+
from unittest.mock import MagicMock, patch
1022+
1023+
_enable_collection(db, test_event)
1024+
dj = test_event.created_by
1025+
dj.beatport_access_token = "fake_token" # nosec B106
1026+
db.commit()
1027+
1028+
match = MagicMock(
1029+
title="Surrender (Hardstyle Remix)", artist="Darude", bpm=165, key=None, genre=None
1030+
)
1031+
with (
1032+
patch("app.api.collect.search_beatport_tracks", return_value=[match]),
1033+
patch("app.api.collect.find_best_match", return_value=match) as spy,
1034+
):
1035+
# Remix query → prefer_original must be False.
1036+
client.post(
1037+
f"/api/public/collect/{test_event.code}/enrich-preview",
1038+
json={"items": [{"title": "Surrender (Hardstyle Remix)", "artist": "Darude"}]},
1039+
)
1040+
assert spy.call_args.kwargs["prefer_original"] is False
1041+
1042+
# Plain query → prefer_original stays True.
1043+
spy.reset_mock()
1044+
client.post(
1045+
f"/api/public/collect/{test_event.code}/enrich-preview",
1046+
json={"items": [{"title": "Surrender", "artist": "Darude"}]},
1047+
)
1048+
assert spy.call_args.kwargs["prefer_original"] is True
1049+
1050+
10171051
def test_enrich_preview_caps_at_10_items(client, db, test_event: Event):
10181052
"""Requests with >10 items are silently capped — only first 10 processed."""
10191053
_enable_collection(db, test_event)

server/tests/test_track_match.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ def test_remix_preferred_when_disabled(self):
9292
track_id="2", title="Surrender", artist="Darude", mix_name="Original Mix", bpm=132
9393
),
9494
]
95-
# No version bonus/penalty → first encountered wins on an otherwise-exact tie.
95+
# With the original-mix bonus disabled the remix (first result) is no longer
96+
# demoted, so it wins the otherwise-exact tie instead of the Original Mix.
9697
best = find_best_match(results, "Surrender", "Darude", prefer_original=False)
97-
assert best is not None
98+
assert best.track_id == "1"
9899

99100
def test_tidal_remix_title_penalized(self):
100101
results = [

0 commit comments

Comments
 (0)