import unittest from importlib.machinery import SourceFileLoader from pathlib import Path from unittest.mock import patch ROOT = Path(__file__).resolve().parents[1] MODULE = SourceFileLoader("zhizhen_spider", str(ROOT / "至臻.py")).load_module() Spider = MODULE.Spider class TestZhiZhenSpider(unittest.TestCase): def setUp(self): Spider._instance = None self.spider = Spider() self.spider.init() def test_home_content_exposes_all_categories(self): content = self.spider.homeContent(False) self.assertEqual( [(item["type_id"], item["type_name"]) for item in content["class"]], [ ("1", "至臻电影"), ("2", "至臻剧集"), ("3", "至臻动漫"), ("4", "至臻综艺"), ("5", "至臻短剧"), ("24", "至臻老剧"), ("26", "至臻严选"), ], ) def test_home_video_content_returns_empty_list(self): self.assertEqual(self.spider.homeVideoContent(), {"list": []}) def test_build_url_and_detect_pan_type(self): self.assertEqual( self.spider._build_url("/index.php/vod/detail/id/1.html"), "http://www.miqk.cc/index.php/vod/detail/id/1.html", ) self.assertEqual(self.spider._detect_pan_type("https://pan.baidu.com/s/demo"), ("baidu", "百度资源")) self.assertEqual(self.spider._detect_pan_type("https://pan.quark.cn/s/demo"), ("quark", "夸克资源")) self.assertEqual(self.spider._detect_pan_type("https://example.com/video"), ("", "")) def test_parse_cards_extracts_short_path_ids(self): html = """
""" self.assertEqual( self.spider._parse_cards(html), [ { "vod_id": "/index.php/vod/detail/id/123.html", "vod_name": "示例影片", "vod_pic": "http://www.miqk.cc/poster.jpg", "vod_remarks": "HD", "vod_year": "2025", } ], ) def test_parse_cards_ignores_module_items_container_and_uses_first_card_cover(self): html = """ """ self.assertEqual( self.spider._parse_cards(html), [ { "vod_id": "/index.php/vod/detail/id/123.html", "vod_name": "示例影片A", "vod_pic": "http://www.miqk.cc/poster-a.jpg", "vod_remarks": "HD", "vod_year": "2025", }, { "vod_id": "/index.php/vod/detail/id/456.html", "vod_name": "示例影片B", "vod_pic": "http://www.miqk.cc/poster-b.jpg", "vod_remarks": "更新至10集", "vod_year": "2024", }, ], ) def test_parse_cards_normalizes_nested_absolute_cover_url(self): html = """ """ self.assertEqual( self.spider._parse_cards(html), [ { "vod_id": "/index.php/vod/detail/id/123.html", "vod_name": "嵌套封面", "vod_pic": "https://img.ffzy888.com/upload/vod/2024-06-13/17182715511.jpg", "vod_remarks": "HD", "vod_year": "2024", } ], ) @patch.object(Spider, "_request_html") def test_category_content_builds_reference_url_and_returns_page_payload(self, mock_request_html): mock_request_html.return_value = """ """ result = self.spider.categoryContent("2", "3", False, {}) self.assertEqual( mock_request_html.call_args.args[0], "http://www.miqk.cc/index.php/vod/show/id/2/page/3.html", ) self.assertEqual(result["page"], 3) self.assertEqual(result["limit"], 1) self.assertEqual(result["list"][0]["vod_name"], "分类影片") self.assertNotIn("pagecount", result) @patch.object(Spider, "_request_html") def test_search_content_builds_reference_search_url_and_parses_results(self, mock_request_html): mock_request_html.return_value = """ """ result = self.spider.searchContent("繁花", False, "2") self.assertEqual( mock_request_html.call_args.args[0], "http://www.miqk.cc/index.php/vod/search/page/2/wd/%E7%B9%81%E8%8A%B1.html", ) self.assertEqual( result["list"][0], { "vod_id": "/index.php/vod/detail/id/789.html", "vod_name": "搜索影片", "vod_pic": "http://www.miqk.cc/search.jpg", "vod_remarks": "抢先版", }, ) def test_search_content_returns_empty_list_for_blank_keyword(self): self.assertEqual(self.spider.searchContent("", False, "1"), {"page": 1, "total": 0, "list": []}) def test_build_pan_lines_deduplicates_and_sorts_supported_links(self): detail = { "pan_urls": [ "https://pan.quark.cn/s/q1", "https://pan.baidu.com/s/b1", "https://pan.baidu.com/s/b1", "https://example.com/ignored", ] } self.assertEqual( self.spider._build_pan_lines(detail), [ ("baidu#至臻", "百度资源$https://pan.baidu.com/s/b1"), ("quark#至臻", "夸克资源$https://pan.quark.cn/s/q1"), ], ) def test_parse_detail_page_extracts_meta_content_and_pan_urls(self): html = """一段剧情简介
https://pan.quark.cn/s/q1
https://pan.baidu.com/s/b1
一段剧情简介
https://pan.quark.cn/s/q1
https://pan.baidu.com/s/b1
