soulsync/tests/downloads/test_worker_quality_ordering.py
dev ab05508acc feat(quality): rank-based candidate ordering toggle for priority mode
Adds an opt-in `rank_candidates_by_quality` profile flag. When on, the
priority-mode download walk orders candidates by the ranked-target quality
(confidence/speed only break ties) instead of confidence-first. Default off
keeps the byte-for-byte old behaviour, so existing installs are unaffected.

Best-quality search mode is always quality-first regardless of the flag; the
toggle only affects priority mode. Search-time source selection is unchanged —
nothing is skipped, so a track can never go missing, only the order in which
copies are tried changes.

The version-mismatch force-import follows automatically: it accepts the
first-tried (= best-ordered) quarantined candidate, which is the highest-quality
one once the walk is quality-first. No change to its selection logic needed.

- core/quality/selection.py: load_rank_candidates_by_quality() (fail-closed).
- core/downloads/task_worker.py: _best_quality_ordering -> _candidate_ordering;
  quality-first when best_quality mode OR the toggle is on.
- database/music_database.py: default profile carries the flag (False).
- web_server.py: flag is preserved globally across preset apply/reset, like
  search_mode.
- core/imports/version_mismatch_fallback.py: comment clarified (no behaviour
  change).

Tests (TDD): load_rank_candidates_by_quality default/enabled/disabled/error;
_candidate_ordering across all mode+toggle combinations + fail-closed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-25 22:00:57 +02:00

47 lines
1.7 KiB
Python

"""core.downloads.task_worker._candidate_ordering — decides whether the
download walk is ordered quality-first or confidence-first.
Quality-first applies when:
- best_quality search mode is active (always), OR
- priority mode AND the rank_candidates_by_quality toggle is on.
Default (priority mode, toggle off) → confidence-first, the byte-for-byte old
behaviour. Any error fails closed to confidence-first so a DB hiccup never
blocks a download.
"""
import core.quality.selection as selection
import core.downloads.task_worker as task_worker
_TARGETS = ["t1", "t2"]
def _patch(monkeypatch, *, mode, rank_toggle):
monkeypatch.setattr(selection, "load_search_mode", lambda: mode)
monkeypatch.setattr(
selection, "load_rank_candidates_by_quality", lambda: rank_toggle
)
monkeypatch.setattr(selection, "load_profile_targets", lambda: (_TARGETS, True))
def test_priority_mode_toggle_off_is_confidence_first(monkeypatch):
_patch(monkeypatch, mode="priority", rank_toggle=False)
assert task_worker._candidate_ordering() == (False, None)
def test_priority_mode_toggle_on_is_quality_first(monkeypatch):
_patch(monkeypatch, mode="priority", rank_toggle=True)
assert task_worker._candidate_ordering() == (True, _TARGETS)
def test_best_quality_mode_is_quality_first_regardless_of_toggle(monkeypatch):
_patch(monkeypatch, mode="best_quality", rank_toggle=False)
assert task_worker._candidate_ordering() == (True, _TARGETS)
def test_fails_closed_to_confidence_first_on_error(monkeypatch):
def _boom():
raise RuntimeError("db down")
monkeypatch.setattr(selection, "load_search_mode", _boom)
assert task_worker._candidate_ordering() == (False, None)