fix(acoustid): 测试函数独立检测本地 fpcalc 依赖而非仅校验网络

原先 test() 依赖 init_module() 一次性缓存的 self._fpcalc_path,初始化之后再安装或移除 fpcalc 都不会反映在测试结果中,导致 UI 测试可能错误显示为通过。

- 抽出静态方法 _resolve_fpcalc() 基于 shutil.which 定位可执行 fpcalc,init_module() 复用
- test() 每次自行重新定位 fpcalc,缺失则直接失败且不发起网络请求,并写回 self._fpcalc_path
- 新增 3 个回归用例覆盖本地依赖检测与独立于 init 缓存的行为
This commit is contained in:
jxxghp
2026-08-17 11:07:13 +08:00
parent 77b7960b7d
commit 23ca70fa37
2 changed files with 68 additions and 3 deletions
+50
View File
@@ -129,6 +129,56 @@ def test_identify_music_by_fingerprint_skips_missing_fpcalc(tmp_path, monkeypatc
post_res.assert_not_called()
def test_test_fails_without_local_fpcalc_and_skips_network(monkeypatch):
"""缺少本地 fpcalc 时,测试应直接失败且不发起任何网络请求。"""
monkeypatch.setattr("app.modules.acoustid.shutil.which", lambda _: None)
monkeypatch.setattr("app.modules.acoustid.settings.ACOUSTID_API_KEY", "client-key")
get_res = Mock()
monkeypatch.setattr(RequestUtils, "get_res", get_res)
module = AcoustIdModule()
ok, message = module.test()
assert ok is False
assert "fpcalc" in message
get_res.assert_not_called()
def test_test_passes_with_local_fpcalc_and_network(monkeypatch):
"""fpcalc 存在且网络可达时,测试应成功并刷新本地依赖快照。"""
monkeypatch.setattr(
"app.modules.acoustid.shutil.which",
lambda _: "/usr/bin/fpcalc",
)
monkeypatch.setattr("app.modules.acoustid.settings.ACOUSTID_API_KEY", "client-key")
monkeypatch.setattr(RequestUtils, "get_res", Mock(return_value=FakeResponse({})))
module = AcoustIdModule()
ok, message = module.test()
assert ok is True
assert message == ""
assert module._fpcalc_path == "/usr/bin/fpcalc"
def test_test_resolves_fpcalc_independently_of_init(monkeypatch):
"""即便 init_module 早期未定位到 fpcalc,测试也应重新检测本地依赖。"""
monkeypatch.setattr(
"app.modules.acoustid.shutil.which",
lambda _: "/usr/bin/fpcalc",
)
monkeypatch.setattr("app.modules.acoustid.settings.ACOUSTID_API_KEY", "client-key")
monkeypatch.setattr(RequestUtils, "get_res", Mock(return_value=FakeResponse({})))
module = AcoustIdModule()
module._fpcalc_path = None
ok, _ = module.test()
assert ok is True
assert module._fpcalc_path == "/usr/bin/fpcalc"
def test_async_identify_music_by_fingerprint_uses_async_process_and_http(
tmp_path,
monkeypatch,