test: update bit-depth guard tests to the unified quality-guard behavior

check_flac_bit_depth now delegates to check_quality_target, which probes the
real file and treats bit depth as a MINIMUM (24-bit satisfies a 16-bit
target) — the old context-string parsing, per-quality bit_depth_fallback, and
'reject higher bit depth' semantics are gone. Rewrite the wrapper tests to
the probe-based model and update the rejection-reason assertion to the
unified 'quality filter' wording.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
dev 2026-06-14 12:52:14 +02:00
parent 95a7b51966
commit d717f06afe
2 changed files with 42 additions and 32 deletions

View file

@ -1,47 +1,55 @@
"""``check_flac_bit_depth`` is a thin legacy wrapper that delegates to the
unified ``check_quality_target`` guard, which probes the REAL file (mutagen)
and ranks it against the v3 ranked-targets list.
The old per-quality ``bit_depth_fallback`` config and the "reject a higher bit
depth" semantics are gone by design: bit depth is now a MINIMUM, so a 24-bit
file satisfies a 16-bit target. These tests pin the wrapper's current
behaviour (deeper coverage of ``check_quality_target`` lives in
``tests/imports/test_quality_guard.py``).
"""
from types import SimpleNamespace
from core.imports import guards
import core.imports.guards as guards
import core.imports.file_ops as file_ops
from core.quality.model import AudioQuality
class _FakeDB:
def __init__(self, quality_profile):
self._quality_profile = quality_profile
def __init__(self, profile):
self._profile = profile
def get_quality_profile(self):
return self._quality_profile
return self._profile
def test_check_flac_bit_depth_rejects_strict_mismatch(monkeypatch):
_WANT_FLAC24 = {
"fallback_enabled": False,
"ranked_targets": [
{"label": "FLAC 24-bit/96kHz", "format": "flac", "bit_depth": 24, "min_sample_rate": 96000},
],
}
def _patch(monkeypatch, aq, profile):
monkeypatch.setattr(file_ops, "probe_audio_quality", lambda fp: aq)
monkeypatch.setattr(guards, "MusicDatabase", lambda: _FakeDB(profile))
monkeypatch.setattr(
guards,
"MusicDatabase",
lambda: _FakeDB({"qualities": {"flac": {"bit_depth": "16", "bit_depth_fallback": False}}}),
)
monkeypatch.setattr(
guards,
"_get_config_manager",
guards, "_get_config_manager",
lambda: SimpleNamespace(get=lambda _key, default=None: False),
)
context = {"_audio_quality": "FLAC 24bit", "track_info": {"name": "Song One"}}
assert guards.check_flac_bit_depth("/tmp/Song One.flac", context) == (
"FLAC bit depth mismatch: file is 24-bit, preference is 16-bit"
)
def test_check_flac_bit_depth_rejects_below_target(monkeypatch):
# 16-bit file, target wants 24-bit, fallback off → rejected.
_patch(monkeypatch, AudioQuality("flac", sample_rate=44100, bit_depth=16), _WANT_FLAC24)
reason = guards.check_flac_bit_depth("/tmp/Song One.flac", {})
assert reason is not None
assert "FLAC 24-bit/96kHz" in reason
def test_check_flac_bit_depth_allows_fallback_when_enabled(monkeypatch):
monkeypatch.setattr(
guards,
"MusicDatabase",
lambda: _FakeDB({"qualities": {"flac": {"bit_depth": "16", "bit_depth_fallback": True}}}),
)
monkeypatch.setattr(
guards,
"_get_config_manager",
lambda: SimpleNamespace(get=lambda _key, default=None: False),
)
context = {"_audio_quality": "FLAC 24bit", "track_info": {"name": "Song One"}}
assert guards.check_flac_bit_depth("/tmp/Song One.flac", context) is None
def test_check_flac_bit_depth_accepts_when_meeting_target(monkeypatch):
# 24-bit/96k file meets the 24-bit target → accepted.
_patch(monkeypatch, AudioQuality("flac", sample_rate=96000, bit_depth=24), _WANT_FLAC24)
assert guards.check_flac_bit_depth("/tmp/Song One.flac", {}) is None

View file

@ -48,9 +48,11 @@ def test_acoustid_quarantine_without_message_still_flags():
def test_bitdepth_rejection_detected():
# The _bitdepth_rejected flag now signals any quality-target rejection
# (bit depth, sample rate, format, bitrate) — the unified quality guard.
reason = import_rejection_reason({'_bitdepth_rejected': True})
assert reason is not None
assert 'bit-depth' in reason.lower()
assert 'quality filter' in reason.lower()
def test_race_guard_failure_detected():