feat(import): adopt files that are already in a library

This commit is contained in:
Steppenstreuner committed 2026-10-25 19:55:05 +01:00
1 parent 46b88e2fd5
commit 46c3c91a8d
2 files changed
+117 -2

No files matched your search

+34
View File
@@ -1294,3 +1294,37 @@ def test_bulk_move_delete_and_count(client):
json={"ids": ids[1:]}).json() == {"deleted": 2}
assert client.get("/api/requests/count?status=missing").json() == {"count": 1}
assert [r["event"] for r in client.get("/api/history").json()] == ["deleted"] * 2
def test_adopt_files_already_in_the_library(client):
root = client.tmp_path / "audio"
lib = client.post("/api/libraries", json={
"name": "Audio", "media_type": "audiobook", "root_path": str(root)}).json()["id"]
hobbit = client.post("/api/requests", json={
"library_id": lib, "title": "Der Hobbit", "authors": "Tolkien"}).json()["id"]
known = client.post("/api/requests", json={
"library_id": lib, "title": "Momo", "authors": "Ende"}).json()["id"]
for folder in ("Tolkien/Der Hobbit", "Ende/Momo"):
(root / folder).mkdir(parents=True)
for i in (1, 2):
(root / folder / f"{i:02d}.mp3").write_bytes(b"")
(root / "Tolkien" / "Notizen.epub").write_bytes(b"") # other media type
# Momo already points at its folder, so only the Hobbit is offered
adopted = client.post(f"/api/import/library/{lib}", json={"items": [
{"path": str(root / "Ende/Momo"), "request_id": known}]}).json()
assert adopted == {"results": [{"path": str(root / "Ende/Momo"), "ok": True}]}
items = client.get(f"/api/import/library/{lib}").json()["items"]
assert [(i["name"], i["suggested_request_id"]) for i in items] == [("Der Hobbit", hobbit)]
resp = client.post(f"/api/import/library/{lib}", json={"items": [
{"path": items[0]["path"], "request_id": hobbit},
{"path": str(root / "Ende/Momo"), "request_id": known}, # no longer open
{"path": str(client.downloads), "request_id": hobbit},
]}).json()["results"]
assert [r["ok"] for r in resp] == [True, False, False]
reqs = {r["id"]: r for r in client.get("/api/requests?status=imported").json()}
assert reqs[hobbit]["imported_path"] == str(root / "Tolkien/Der Hobbit")
assert (root / "Tolkien/Der Hobbit/01.mp3").exists() # nothing moved
assert client.get(f"/api/import/library/{lib}").json()["items"] == []
assert [h["event"] for h in client.get("/api/history").json()] == ["adopted"] * 2