Files
MoviePilot/tests/test_plex_image_lookup.py
DDSRem 2de976f6b3 Merge pull request #7050 from DDSRem-Dev/codex-comment-migration
docs: 统一全项目中文注释和结构化契约说明
2026-10-17 19:26:29 +08:00

66 lines
1.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
验证 Plex 子条目图片发现与本地图片回退。
"""
from unittest.mock import Mock
from app.modules.plex.plex import Plex
def _build_plex():
"""
构造绕过真实连接的Plex实例,便于单测直接注入plexapi mock。
"""
plex = Plex.__new__(Plex)
plex._host = "http://192.168.8.254:32400/"
plex._playhost = None
plex._token = "plex-token"
plex._plex = Mock()
return plex
def test_get_remote_image_by_id_uses_item_arts_for_children_key() -> None:
"""
子条目路径通过条目对象的图片接口取得远端背景图。
"""
plex = _build_plex()
plex._plex.fetchItems.side_effect = AssertionError("should not use raw fetchItems with /children key")
item = Mock()
item.TYPE = "show"
item.art = "/library/metadata/29242/art/1"
item.parentKey = None
item.arts.return_value = [Mock(key="https://image.tmdb.org/t/p/original/test.jpg")]
plex._plex.fetchItem.return_value = item
image_url = plex.get_remote_image_by_id(
item_id="/library/metadata/29242/children",
image_type="Backdrop",
plex_url=False,
)
assert image_url == "https://image.tmdb.org/t/p/original/test.jpg"
item.arts.assert_called_once_with()
plex._plex.fetchItem.assert_called_once_with(ekey="/library/metadata/29242/children")
def test_get_remote_image_by_id_falls_back_to_local_art_url() -> None:
"""
没有远端图片时回退到带访问令牌的本地背景图 URL。
"""
plex = _build_plex()
item = Mock()
item.TYPE = "show"
item.art = "/library/metadata/29242/art/1"
item.parentKey = None
item.arts.return_value = []
plex._plex.fetchItem.return_value = item
image_url = plex.get_remote_image_by_id(
item_id="/library/metadata/29242/children",
image_type="Backdrop",
)
assert image_url == "http://192.168.8.254:32400/library/metadata/29242/art/1?X-Plex-Token=plex-token"