diff --git a/app/chain/download/existence.py b/app/chain/download/existence.py index bffb6e5c1..adc9e0488 100644 --- a/app/chain/download/existence.py +++ b/app/chain/download/existence.py @@ -118,7 +118,8 @@ class DownloadExistenceOwner(_DownloadOwnerBase): continue total_ep = totals.get(season) or len(episodes) self._append_no_exists( - no_exists, media_source, media_id, season, [], + no_exists, media_source, media_id, season, + self._whole_season_missing(episodes, total_ep), total_ep, min(episodes) ) return False, no_exists @@ -148,7 +149,8 @@ class DownloadExistenceOwner(_DownloadOwnerBase): ) else: self._append_no_exists( - no_exists, media_source, media_id, season, [], + no_exists, media_source, media_id, season, + self._whole_season_missing(episodes, season_total), season_total, min(episodes) ) if no_exists: @@ -188,6 +190,29 @@ class DownloadExistenceOwner(_DownloadOwnerBase): "seasons": {season: sorted(episodes) for season, episodes in seasons.items()}, }) if combined else None + @staticmethod + def _whole_season_missing(episodes: list[int], total: int) -> list[int]: + """ + 计算一季完全未入库时的缺集列表。 + + 下载链把空列表当作缺整季,整季包会不选集整包提交;传入的总集数少于本季集数时 + (如订阅只要第 1 集),必须写出目标集,下载链才会只选范围内的集。 + + 部分入库分支把总集数当作从本季最小集号起数的集数,订阅缺集裁剪却把它当作末集号。 + 只有按两种口径得到的目标集相同(本季从第 1 集起且前段不断号)时才写出目标集; + 口径不一致时保留空列表,避免订阅裁剪把显式集全部裁掉、误判为已完成。 + + :param episodes: 本季全部集号 + :param total: 本季有效总集数 + :return: 范围覆盖整季或口径不一致时为空列表,否则为显式目标集 + """ + if total >= len(episodes): + return [] + target = sorted(episodes)[:total] + if target[-1] != total: + return [] + return target + @staticmethod def _append_no_exists( no_exists: Dict[str, Dict[int, NotExistMediaInfo]], diff --git a/docs/testing.md b/docs/testing.md index 6d688d781..38f5c5b38 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -246,6 +246,11 @@ with patch.object(SomeModule, "fetch", new=AsyncMock(return_value=FAKE)): 或写入洗版优先级。测试覆盖显式缺集列表、后页整包、不完整包和搜索期间进度变化,均隔离外部服务。 完整覆盖目标来自订阅起止集;整包含范围外剧集时只选目标集提交,例如只订阅第 5 集时整季包也只下载 第 5 集,种子集数与目标一致时仍整包提交。下载层用例见 `tests/test_download_batch_phases.py`。 +整包未通过优先级门槛而回退普通下载时,同样不能下载订阅范围外的剧集:本季完全未入库、订阅总集数 +少于本季集数时,缺集计算写出显式目标集,不再用空列表表示缺整季。`tests/test_subscribe_missing_scope.py` +覆盖媒体库无此剧和只有其他季两种情况,并从订阅下载入口验证只订阅第 1 集时整季包只提交第 1 集。 +季内集号不从 1 开始或前段断号时,总集数按集数和按末集号理解会得到不同目标,此时保留整季标记, +用例断言订阅不会被裁成空集而误判完成。 整包优先也约束候选分批:已收集的整包即使排序低于分集、检查点恢复后缺少临时下载地址, 也须先恢复地址并共同进入下载选择;多个整包不完整、下载失败或恢复后已不可用时才回退分集。 diff --git a/tests/test_subscribe_missing_scope.py b/tests/test_subscribe_missing_scope.py index 965c7ec64..828da3818 100644 --- a/tests/test_subscribe_missing_scope.py +++ b/tests/test_subscribe_missing_scope.py @@ -124,6 +124,55 @@ def test_missing_scope_materializes_full_season_after_start_episode(scope): assert missing["tmdb:223911"][1].start_episode == 159 +@pytest.mark.parametrize("library", [None, ExistMediaInfo(seasons={2: [1]})]) +@pytest.mark.parametrize(("total", "expected"), [(1, [1]), (5, [1, 2, 3, 4, 5]), (200, [])]) +def test_missing_scope_materializes_full_season_before_total_episode(scope, library, total, expected): + """ + 整季未入库且总集数裁掉季末时,同样转成显式集数;总集数覆盖整季才保留整季标记。 + + 媒体库完全没有该剧,或只有其他季时,两条缺集分支口径一致。 + """ + scope.download._media_exists_with_server_cache.return_value = library + subscribe = replace(scope.subscribe, start_episode=1, total_episode=total, note=[]) + + satisfied, missing = scope.chain.resolve_subscribe_missing( + subscribe=subscribe, meta=MetaInfo("Renegade Immortal 2023"), mediainfo=scope.media, + ) + + assert not satisfied + info = missing["tmdb:223911"][1] + assert info.episodes == expected + assert info.start_episode == 1 + assert info.total_episode == total + + +@pytest.mark.parametrize(("season_episodes", "start", "total", "expected"), [ + (list(range(13, 25)), 13, 6, []), + (list(range(13, 25)), None, 6, []), + (list(range(13, 25)), 13, 18, []), + (list(range(1, 11)) + [12], 1, 11, []), + (list(range(1, 11)) + [12], 1, 8, list(range(1, 9))), +]) +def test_missing_scope_keeps_whole_season_when_total_semantics_disagree( + scope, season_episodes, start, total, expected, +): + """ + 总集数按集数和按末集号理解得到不同目标集时,保留整季标记,不能裁成空集而误判订阅已完成。 + + 季内集号不从 1 开始或前段断号时两种口径不一致;从第 1 集起且前段连续时仍写出目标集。 + """ + scope.download._media_exists_with_server_cache.return_value = None + media = replace(scope.media, seasons={1: season_episodes}) + subscribe = replace(scope.subscribe, start_episode=start, total_episode=total, note=[]) + + satisfied, missing = scope.chain.resolve_subscribe_missing( + subscribe=subscribe, meta=MetaInfo("Renegade Immortal 2023"), mediainfo=media, + ) + + assert not satisfied + assert missing["tmdb:223911"][1].episodes == expected + + def test_missing_scope_finishes_only_when_subscription_range_is_satisfied(scope): """ 下载历史已覆盖目标范围时,应清空裁剪结果而不是返回更早的库内缺集。 @@ -232,3 +281,56 @@ def test_policy_recheck_keeps_scope_through_batch_file_selection(scope, monkeypa download.download_single.assert_called_once() assert download.download_single.call_args.kwargs["episodes"] == set(range(161, 166)) assert set(remaining["tmdb:223911"][1].episodes) == set(range(166, 201)) + + +@pytest.mark.parametrize("title", [ + "Renegade Immortal S01 2023 2160p WEB-DL H265 AAC-UBWEB", + "Renegade Immortal E01-E200 2023 1080p HDTV x265 AC3", +]) +@pytest.mark.parametrize("pri_order", [0, 90]) +def test_trimmed_total_subscription_only_selects_target_from_season_pack(scope, monkeypatch, pri_order, title): + """ + 只订阅第 1 集时,整季包无论走首次整包优先还是回退的普通下载,都只向下载器提交第 1 集。 + + 优先级为 0 时整包优先被拒、回退普通下载;此前缺集仍是空列表,被当成缺整季而整包提交。 + 标题无集号和标注全季集号的整包都要覆盖。 + """ + subscribe = replace(scope.subscribe, start_episode=1, total_episode=1, note=[]) + scope.chain.subscription_repository.get.return_value = subscribe + meta = MetaInfo(title) + context = Context( + meta_info=meta, + media_info=scope.media, + torrent_info=TorrentInfo( + title=meta.org_string, site=1, site_name="TestSite", pri_order=pri_order, + ), + ) + download = scope.download + download._media_exists_with_server_cache.return_value = None + download.eventmanager = SimpleNamespace(send_event=Mock(return_value=None)) + download._active_download_failure_fingerprints = Mock(return_value={}) + download.download_torrent = Mock(return_value=(b"torrent", None, list(range(1, 201)))) + download.download_single = Mock(return_value="download-id") + helper = SimpleNamespace( + sort_torrents=lambda contexts: contexts, + get_torrent_episodes=lambda files, **_kwargs: files, + ) + stop = SimpleNamespace(is_system_stopped=False) + monkeypatch.setattr(download_batch, "_new_torrent_helper", lambda: helper) + monkeypatch.setattr(download_selection, "_new_torrent_helper", lambda: helper) + monkeypatch.setattr(download_batch, "runtime_stop_state", stop) + monkeypatch.setattr(download_selection, "runtime_stop_state", stop) + satisfied, prepared_missing = scope.chain.resolve_subscribe_missing( + subscribe=subscribe, meta=build_subscribe_meta(subscribe), mediainfo=scope.media, + ) + assert not satisfied + + downloads, remaining = scope.chain._SubscribeChain__download_best_version_with_full_pack_first( + contexts=[context], no_exists=prepared_missing, + subscribe=subscribe, mediakey="tmdb:223911", + ) + + assert downloads == [context] + assert remaining == {} + download.download_single.assert_called_once() + assert download.download_single.call_args.kwargs["episodes"] == {1}