From e46b4e5ba0cf7becc2f9038dc9ba258bd8496668 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Mon, 20 Jul 2026 15:31:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=87=8D=E6=96=B0=E6=95=B4=E7=90=86?= =?UTF-8?q?=E6=97=B6=E5=BF=BD=E7=95=A5=E6=97=A7=E4=B8=8B=E8=BD=BD=E5=93=88?= =?UTF-8?q?=E5=B8=8C=20(#6154)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/endpoints/transfer.py | 3 +- tests/test_transfer_history_retransfer.py | 55 +++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/app/api/endpoints/transfer.py b/app/api/endpoints/transfer.py index d93eb3c5..996d1ecf 100644 --- a/app/api/endpoints/transfer.py +++ b/app/api/endpoints/transfer.py @@ -270,7 +270,8 @@ def manual_transfer( # 强制转移 force = True downloader = history.downloader - download_hash = history.download_hash + # 仅复用历史识别信息时才传入旧 Hash,否则由当前文件路径重新匹配下载记录。 + download_hash = history.download_hash if transer_item.from_history else None if history.status and ("move" in history.mode): # 重新整理成功的转移,则使用成功的 dest 做 in_path src_fileitems = [FileItem(**history.dest_fileitem)] diff --git a/tests/test_transfer_history_retransfer.py b/tests/test_transfer_history_retransfer.py index f7522d18..d4636b5d 100644 --- a/tests/test_transfer_history_retransfer.py +++ b/tests/test_transfer_history_retransfer.py @@ -10,6 +10,7 @@ from app.schemas import EpisodeFormatRecommendItem, ManualTransferItem, Transfer def test_manual_transfer_from_history_preserves_download_context(monkeypatch): + """复用历史识别信息时应传递原下载上下文。""" history = SimpleNamespace( status=0, mode="copy", @@ -53,6 +54,60 @@ def test_manual_transfer_from_history_preserves_download_context(monkeypatch): assert captured["season"] == 1 +def test_manual_transfer_without_history_recognition_ignores_old_hash(monkeypatch): + """从历史重新识别时应忽略旧 Hash,但保留下载器上下文。""" + history = SimpleNamespace( + status=0, + mode="copy", + src_fileitem={ + "storage": "local", + "path": "/downloads/test.mkv", + "name": "test.mkv", + "type": "file", + }, + dest_fileitem=None, + downloader="qbittorrent", + download_hash="polluted-hash", + type="电视剧", + tmdbid="100", + doubanid="200", + seasons="S01", + episodes="E01", + episode_group="WEB-DL", + ) + captured = {} + + def fake_get(_db, logid): + """返回受污染的历史记录。""" + assert logid == 1 + return history + + class FakeTransferChain: + """记录 API 传入整理链的参数。""" + + def manual_transfer(self, **kwargs): + """记录手动整理参数。""" + captured.update(kwargs) + return True, "" + + monkeypatch.setattr("app.api.endpoints.transfer.TransferHistory.get", fake_get) + monkeypatch.setattr("app.api.endpoints.transfer.TransferChain", FakeTransferChain) + + resp = manual_transfer( + transer_item=ManualTransferItem(logid=1, from_history=False), + background=True, + db=object(), + _="token", + ) + + assert resp.success is True + assert captured["downloader"] == "qbittorrent" + assert captured["download_hash"] is None + assert captured["tmdbid"] is None + assert captured["doubanid"] is None + assert captured["episode_group"] is None + + def test_manual_transfer_from_history_passes_old_dest_cleanup_to_chain(monkeypatch): history = SimpleNamespace( status=0,