diff --git a/app/modules/qbittorrent/__init__.py b/app/modules/qbittorrent/__init__.py index 6da0da0c6..b1441376f 100644 --- a/app/modules/qbittorrent/__init__.py +++ b/app/modules/qbittorrent/__init__.py @@ -218,6 +218,7 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]): if settings.TORRENT_TAG and settings.TORRENT_TAG not in torrent_tags: logger.info(f"给种子 {torrent_hash} 打上标签:{settings.TORRENT_TAG}") server.set_torrents_tag(ids=torrent_hash, tags=[settings.TORRENT_TAG]) + server.delete_torrents_tag(torrent_hash, tag) return downloader or self.get_default_config_name(), torrent_hash, torrent_layout, f"下载任务已存在" finally: torrents.clear() diff --git a/tests/test_qbittorrent_compat.py b/tests/test_qbittorrent_compat.py index 88c7112d1..eb13980fd 100644 --- a/tests/test_qbittorrent_compat.py +++ b/tests/test_qbittorrent_compat.py @@ -541,6 +541,30 @@ def test_download_falls_back_to_tag_lookup_when_added_ids_missing(): fake_server.get_torrent_id_by_tag.assert_called_once_with(tags="tmp-tag-01") +def test_download_removes_temporary_tag_from_existing_torrent(): + """重复添加任务时应从已存在的种子中删除本次临时标签。""" + fake_server = MagicMock() + fake_server.add_torrent.return_value = (False, []) + fake_server.get_content_layout.return_value = "Original" + fake_server.get_torrents.return_value = ([{ + "name": "test", + "total_size": len(b"torrent-content"), + "hash": "existing123", + "tags": "tmp-tag-01,moviepilot-tag", + }], None) + + module = _build_module(fake_server) + result = module.download( + content=b"torrent-content", + download_dir=Path("/downloads"), + cookie="", + downloader="qb", + ) + + assert result == ("qb", "existing123", "Original", "下载任务已存在") + fake_server.delete_torrents_tag.assert_called_once_with("existing123", "tmp-tag-01") + + def test_get_files_retries_until_qbittorrent_files_available(): """qBittorrent 添加任务后文件列表短暂未就绪时应重试。""" torrent_files = [{"id": 12, "name": "Show.S01E12.mkv"}]