Files
MoviePilot/tests/test_torrent_cache_music.py
freeman f3ad81b39e style: apply safe ruff auto-fixes and exempt startup late imports
Run `ruff check app tests scripts --fix` with the configured rule set
(E4/E7/E9/F/I): import sorting (I001), f-strings without placeholders
(F541), lambda/variable/unused-import cleanups that ruff marks safe.
Nine files whose only ruff debt sits next to pre-existing pylint
import-resolution errors (lazy exports, sys.path script imports) or
whose "unused" imports are reached through attribute access by tests are
left untouched so the changed-file pylint gate and the benchmark tests
stay green; their diagnostics remain in the baseline.

app/startup/lifecycle/__init__.py and app/main.py import business
modules after cache composition, import-path and stdio setup on purpose
(the source comments document the contract), so declare E402
per-file-ignores for them instead of adding per-line noqa.

Ruff ratchet watermark drops from 498 to 100; sync the baseline fixture
and the optimization checklist figure.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-09 13:29:09 +08:00

121 lines
4.4 KiB
Python

"""种子缓存音乐实体识别测试。"""
import asyncio
from unittest.mock import AsyncMock, Mock, patch
from app.api.endpoints.torrent import reidentify_cache, torrents_cache
from app.domain.context import MUSIC_ENTITY_ALBUM, Context, MusicInfo, TorrentInfo
from app.domain.meta.metamusic import MetaMusic
from app.foundation.crypto import HashUtils
from app.schemas.types import MediaType
def _album_context() -> Context:
"""构造带完整专辑身份的缓存上下文。"""
album = MusicInfo(
media_source="musicbrainz",
media_id="release-group-1",
music_type=MUSIC_ENTITY_ALBUM,
title="叶惠美",
artists=["周杰伦"],
)
return Context(
meta_info=MetaMusic.from_music_info(album),
media_info=album,
torrent_info=TorrentInfo(
title="周杰伦 - 叶惠美 FLAC",
description="11 tracks",
category=MediaType.MUSIC.value,
site_name="Music Site",
),
)
def _mock_torrents_chain(context: Context) -> Mock:
"""构造可记录缓存读写的种子链替身。"""
chain = Mock()
chain.async_get_torrents = AsyncMock(return_value={"music.example": [context]})
chain.split_cache_contexts.return_value = ({}, {"music.example": [context]})
chain.cache_files.return_value = ("video-cache", "music-cache")
chain.async_save_cache = AsyncMock()
return chain
def test_torrent_cache_exposes_music_identity_namespace():
"""缓存列表应返回重识别所需的数据源、原生 ID 和实体类型。"""
context = _album_context()
torrents_chain = _mock_torrents_chain(context)
with patch(
"app.api.endpoints.torrent.TorrentsChain",
return_value=torrents_chain,
):
response = asyncio.run(torrents_cache(_=Mock()))
item = response.data["data"][0]
assert item["media_source"] == "musicbrainz"
assert item["media_id"] == "release-group-1"
assert item["music_type"] == MUSIC_ENTITY_ALBUM
def test_torrent_cache_exact_reidentify_forwards_album_namespace():
"""缓存按专辑 ID 重识别时必须在进入 MediaChain 前绑定 album。"""
context = _album_context()
torrents_chain = _mock_torrents_chain(context)
media_chain = Mock()
media_chain.async_recognize_media = AsyncMock(return_value=context.media_info)
torrent_hash = HashUtils.md5(
f"{context.torrent_info.title}{context.torrent_info.description}"
)
with patch(
"app.api.endpoints.torrent.TorrentsChain",
return_value=torrents_chain,
), patch("app.api.endpoints.torrent.MediaChain", return_value=media_chain):
response = asyncio.run(
reidentify_cache(
domain="music.example",
torrent_hash=torrent_hash,
media_source="musicbrainz",
media_id="release-group-1",
music_type="album",
_=Mock(),
)
)
assert response.success is True
recognize_kwargs = media_chain.async_recognize_media.await_args.kwargs
assert recognize_kwargs["mtype"] == MediaType.MUSIC
assert recognize_kwargs["music_type"] == MUSIC_ENTITY_ALBUM
assert recognize_kwargs["media_id"] == "release-group-1"
def test_torrent_cache_auto_reidentify_keeps_music_meta_and_entity():
"""不指定 ID 的专辑重识别应沿用 MetaMusic 和原有 album 命名空间。"""
context = _album_context()
torrents_chain = _mock_torrents_chain(context)
media_chain = Mock()
media_chain.async_recognize_by_meta = AsyncMock(return_value=context.media_info)
torrent_hash = HashUtils.md5(
f"{context.torrent_info.title}{context.torrent_info.description}"
)
with patch(
"app.api.endpoints.torrent.TorrentsChain",
return_value=torrents_chain,
), patch("app.api.endpoints.torrent.MediaChain", return_value=media_chain):
response = asyncio.run(
reidentify_cache(
domain="music.example",
torrent_hash=torrent_hash,
_=Mock(),
)
)
assert response.success is True
meta = media_chain.async_recognize_by_meta.await_args.args[0]
recognize_kwargs = media_chain.async_recognize_by_meta.await_args.kwargs
assert meta.type == MediaType.MUSIC
assert recognize_kwargs["mtype"] == MediaType.MUSIC
assert recognize_kwargs["music_type"] == MUSIC_ENTITY_ALBUM