fix(import): re-offer imported requests whose files left the library

This commit is contained in:
Steppenstreuner
2026-08-29 07:39:56 +02:00
parent 7daa0d61a4
commit d432881a7a
4 changed files with 98 additions and 8 deletions
+69
View File
@@ -1,4 +1,5 @@
import importlib
import shutil
from pathlib import Path
import pytest
@@ -820,3 +821,71 @@ def test_relocate_refuses_to_overwrite(client):
assert res.status_code == 409
assert (occupied / "fremd.mp3").exists()
assert (root / "Erstes" / "Erstes - Part 01.mp3").exists()
def test_files_moved_back_are_matched_against_imported_requests(client):
"""Moving a library folder back into the download dir: the request still says
"imported", but its files are gone - the scan should suggest it anyway."""
root = client.tmp_path / "library" / "kids"
lib_id = client.post("/api/libraries", json={
"name": "kids", "media_type": "audiobook", "root_path": str(root),
"folder_template": "{Series}/{Volume} - {Title}", "file_template": "{Title}",
}).json()["id"]
req_id = client.post("/api/requests", json={
"library_id": lib_id, "title": "Die Zeitreisende",
"series": "Die drei ???", "volume": 194,
}).json()["id"]
src = client.downloads / "194 - Die Zeitreisende"
src.mkdir()
for i in (1, 2):
(src / f"{i:02d}.mp3").write_bytes(b"")
dest = Path(client.post("/api/import", json={"items": [{
"path": str(src), "is_dir": True,
"files": [str(f) for f in sorted(src.iterdir())], "request_id": req_id,
}]}).json()["results"][0]["dest"])
# while the files are in place the request is not offered again
scan = client.get("/api/import/scan").json()
assert scan["orphaned_request_ids"] == []
# now move the folder back into the download dir, as the user did
shutil.move(str(dest), client.downloads / dest.name)
scan = client.get("/api/import/scan").json()
assert scan["orphaned_request_ids"] == [req_id]
item = next(i for i in scan["items"] if i["name"] == dest.name)
assert item["suggested_request_id"] == req_id
res = client.post("/api/import", json={"items": [{
"path": item["path"], "is_dir": True, "files": item["files"],
"request_id": req_id,
}]}).json()["results"][0]
assert res["ok"], res
assert Path(res["dest"]) == root / "Die drei ???" / "194 - Die Zeitreisende"
assert sorted(p.name for p in Path(res["dest"]).iterdir()) == [
"Die Zeitreisende - Part 01.mp3", "Die Zeitreisende - Part 02.mp3",
]
def test_imported_request_with_files_in_place_is_still_refused(client):
lib_id = client.post("/api/libraries", json={
"name": "L", "media_type": "audiobook", "root_path": str(client.tmp_path / "l"),
}).json()["id"]
req_id = client.post("/api/requests",
json={"library_id": lib_id, "title": "X"}).json()["id"]
src = client.downloads / "a"
src.mkdir()
(src / "1.mp3").write_bytes(b"")
(src / "2.mp3").write_bytes(b"")
client.post("/api/import", json={"items": [{
"path": str(src), "is_dir": True,
"files": [str(src / "1.mp3"), str(src / "2.mp3")], "request_id": req_id,
}]})
again = client.downloads / "b"
again.mkdir()
(again / "1.mp3").write_bytes(b"")
res = client.post("/api/import", json={"items": [{
"path": str(again), "is_dir": False, "files": [str(again / "1.mp3")],
"request_id": req_id,
}]}).json()["results"][0]
assert not res["ok"] and "bereits importiert" in res["error"]