feat: optional Hardcover wishlist sync with automated downloading (1 of 5) (#1516)

This replaces #1047, which @InfiniteAvenger opened and calibrain offered
to re-review. It is Caleb's feature commit with his authorship kept,
rebased onto current main, with small fixes, tests and a docs page on
top. Everything is opt-in and off by default, so nothing changes for
anyone who doesn't turn it on.

What it adds is a **Settings, Hardcover Sync** tab with three parts that
can be switched on separately:

- **Shelf sync**: a Hardcover shelf (Want to Read by default) is pulled
into requests on a schedule, or with a Sync now button. Requests carry
the Hardcover metadata and cover.
- **Automatic downloads**: synced requests are approved and a release is
picked from a source priority list. A strict title, author and format
match (plus a seeder minimum for torrents) decides, so anything without
a confident match stays pending for manual review.
- **Skip books you already own**: sync and automatic downloads ask
whichever libraries are enabled under Library Check (Calibre today) and
skip a book that is already there.

On top of Caleb's commit I made the request owner a lookup instead of
assuming user id 1, sent synced cover URLs through the proxy when the
request is created, got ruff and basedpyright clean, and added tests and
`docs/hardcover-sync.md`. His commit also had its own Audiobookshelf
settings and a Test library connection button, written before the
library check was split into providers. The button would have raised a
TypeError against the current `test_connection`, so those settings are
gone from this tab and an Audiobookshelf provider comes as a separate
PR, which is the part of #1047 that is missing here (for now
anyways...).

I've run this daily for about a couple of weeks. Ruff, basedpyright and
the backend suite pass (4426 passed).

This is 1 of 5. As calibrain suggested, the rest are stacked on this
one, each saying which number it is.

Closes
https://github.com/calibrain/shelfmark/pull/1047#issuecomment-6302071588

---------

Co-authored-by: InfiniteAvenger <calebewest02@gmail.com>
Co-authored-by: CaliBrain <calibrain@l4n.xyz>
This commit is contained in:
authored and GitHub committed 2026-10-22 20:27:47 -04:00
1 parent 72b5d04939
commit 48ae3dbef4
14 files changed
+2039

No files matched your search

@@ -0,0 +1,71 @@
"""Tests for the Hardcover Sync settings tab registration and action buttons."""
import shelfmark.config.hardcover_sync_settings # noqa: F401 - registers the tab
from shelfmark.core import settings_registry
def _field_map():
tab = settings_registry.get_settings_tab("hardcover_sync")
assert tab is not None
return {field.key: field for field in tab.fields if hasattr(field, "key")}
def test_tab_is_registered():
tab = settings_registry.get_settings_tab("hardcover_sync")
assert tab is not None
assert tab.display_name == "Hardcover Sync"
def test_everything_is_off_by_default():
fields = _field_map()
assert fields["HARDCOVER_SYNC_ENABLED"].default is False
assert fields["AUTO_DOWNLOAD_ENABLED"].default is False
def test_schedule_and_content_type_defaults():
fields = _field_map()
assert fields["HARDCOVER_SYNC_INTERVAL"].default == 6
assert fields["HARDCOVER_SYNC_INTERVAL"].min_value == 1
assert fields["HARDCOVER_SYNC_INTERVAL_UNIT"].default == "hours"
assert fields["HARDCOVER_SYNC_CONTENT_TYPE"].default == "audiobook"
assert fields["HARDCOVER_SYNC_STATUSES"].default == "1"
assert fields["AUTO_DOWNLOAD_SOURCE_PRIORITY"].default == []
def test_shelf_options_come_from_hardcover_statuses():
options = _field_map()["HARDCOVER_SYNC_STATUSES"].options()
assert {"value": "1", "label": "Want to Read"} in options
def test_action_buttons_are_registered():
fields = _field_map()
assert fields["sync_now"].callback is not None
def test_sync_now_reports_busy_when_a_run_is_active(monkeypatch):
monkeypatch.setattr("shelfmark.core.hardcover_scheduler.trigger_async", lambda **_: False)
result = settings_registry.execute_action("hardcover_sync", "sync_now")
assert result["success"] is False
assert "already running" in result["message"]
def test_sync_now_starts_a_forced_run(monkeypatch):
seen = []
def _trigger(**kwargs):
seen.append(kwargs)
return True
monkeypatch.setattr("shelfmark.core.hardcover_scheduler.trigger_async", _trigger)
result = settings_registry.execute_action("hardcover_sync", "sync_now")
assert result["success"] is True
assert seen == [{"force": True}]
+401
View File
@@ -0,0 +1,401 @@
"""Tests for strict release matching and auto-download of pending requests."""
import pytest
from shelfmark.core import auto_download
from shelfmark.core.user_db import UserDB
from shelfmark.metadata_providers import BookMetadata
from shelfmark.release_sources import Release, ReleaseProtocol
AUDIOBOOK_FORMATS = {"m4b", "mp3"}
class _Config:
def __init__(self, **values):
self.values = values
def get(self, key, default=None, user_id=None):
return self.values.get(key, default)
def _book(**overrides):
fields = {
"provider": "hardcover",
"provider_id": "42",
"title": "Dungeon Crawler Carl",
"authors": ["Matt Dinniman"],
}
fields.update(overrides)
return BookMetadata(**fields)
def _release(**overrides):
fields = {
"source": "prowlarr",
"source_id": "rel-1",
"title": "Dungeon Crawler Carl - Matt Dinniman [M4B]",
"format": "m4b",
"protocol": ReleaseProtocol.TORRENT,
"seeders": 10,
}
fields.update(overrides)
return Release(**fields)
def _strict(release, **kwargs):
kwargs.setdefault("audiobook_formats", AUDIOBOOK_FORMATS)
return auto_download.strict_match(release, _book(), **kwargs)
@pytest.fixture
def user_db(tmp_path):
db = UserDB(str(tmp_path / "users.db"))
db.initialize()
return db
def _pending_request(user_db, user_id, **book_data_overrides):
book_data = {
"title": "Dungeon Crawler Carl",
"author": "Matt Dinniman",
"provider": "hardcover",
"provider_id": "42",
"content_type": "audiobook",
}
book_data.update(book_data_overrides)
return user_db.create_request(
user_id=user_id,
content_type="audiobook",
request_level="book",
policy_mode="request_book",
book_data=book_data,
)
def _stub_provider(monkeypatch, book):
class _Provider:
def get_book(self, book_id):
assert book_id == "42"
return book
monkeypatch.setattr(
"shelfmark.metadata_providers.is_provider_registered", lambda name: name == "hardcover"
)
monkeypatch.setattr("shelfmark.metadata_providers.get_provider_kwargs", lambda _name: {})
monkeypatch.setattr(
"shelfmark.metadata_providers.get_provider", lambda _name, **_kwargs: _Provider()
)
class TestStrictMatch:
def test_matching_release_passes(self):
assert _strict(_release()) is True
def test_title_mismatch_rejected(self):
assert _strict(_release(title="Some Other Book - Matt Dinniman [M4B]")) is False
def test_missing_author_surname_rejected(self):
assert _strict(_release(title="Dungeon Crawler Carl [M4B]", indexer=None)) is False
def test_author_found_in_extra_metadata(self):
release = _release(title="Dungeon Crawler Carl [M4B]", extra={"author": "Matt Dinniman"})
assert _strict(release) is True
def test_ebook_only_release_rejected(self):
release = _release(title="Dungeon Crawler Carl - Matt Dinniman", format="epub")
assert _strict(release) is False
def test_audiobook_signal_in_title_accepted_without_format(self):
release = _release(title="Dungeon Crawler Carl by Matt Dinniman (Unabridged)", format=None)
assert _strict(release) is True
@pytest.mark.parametrize("seeders", [0, None])
def test_torrent_below_minimum_seeders_rejected(self, seeders):
assert _strict(_release(seeders=seeders), min_seeders=1) is False
def test_torrent_without_seeder_count_allowed_when_minimum_is_zero(self):
assert _strict(_release(seeders=None), min_seeders=0) is True
def test_non_torrent_ignores_seeders(self):
release = _release(protocol=ReleaseProtocol.NZB, seeders=None)
assert _strict(release, min_seeders=5) is True
class TestPickBestRelease:
def test_empty_returns_none(self):
assert auto_download.pick_best_release([]) is None
def test_m4b_outranks_mp3_regardless_of_seeders(self):
mp3 = _release(source_id="mp3", format="mp3", seeders=500)
m4b = _release(source_id="m4b", format="m4b", seeders=1)
assert auto_download.pick_best_release([mp3, m4b]) is m4b
def test_m4b_in_title_ranks_as_m4b_when_format_missing(self):
mp3 = _release(source_id="mp3", format="mp3", seeders=500)
untagged = _release(source_id="untagged", format=None, seeders=1)
assert auto_download.pick_best_release([mp3, untagged]) is untagged
def test_seeders_break_format_ties(self):
few = _release(source_id="few", seeders=2, size_bytes=10)
many = _release(source_id="many", seeders=20, size_bytes=1)
assert auto_download.pick_best_release([few, many]) is many
def test_size_breaks_seeder_ties(self):
small = _release(source_id="small", seeders=5, size_bytes=100)
large = _release(source_id="large", seeders=5, size_bytes=200)
assert auto_download.pick_best_release([small, large]) is large
class TestBuildReleaseData:
def test_drops_none_values(self):
release = _release(size=None, size_bytes=None, language=None, info_url=None)
data = auto_download.build_release_data(release, _book(), "audiobook")
assert data["source"] == "prowlarr"
assert data["source_id"] == "rel-1"
assert data["protocol"] == "torrent"
assert data["author"] == "Matt Dinniman"
assert data["content_type"] == "audiobook"
assert not {"size", "size_bytes", "language", "info_url", "preview", "year"} & data.keys()
def test_carries_cover_series_and_book_metadata(self):
book = _book(
cover_url="https://img.example/cover.jpg",
publish_year=2020,
subtitle="Book One",
series_name="Dungeon Crawler Carl",
series_position=1.0,
)
release = _release(content_type=None, extra={"preview": "https://img.example/rel.jpg"})
data = auto_download.build_release_data(release, book, "audiobook")
assert data["preview"] == "https://img.example/cover.jpg"
assert data["year"] == 2020
assert data["subtitle"] == "Book One"
assert data["series_name"] == "Dungeon Crawler Carl"
assert data["series_position"] == 1.0
assert data["content_type"] == "audiobook"
assert data["extra"] == {"preview": "https://img.example/rel.jpg"}
def test_falls_back_to_release_preview_without_book_cover(self):
release = _release(extra={"preview": "https://img.example/rel.jpg"})
data = auto_download.build_release_data(release, _book(), "audiobook")
assert data["preview"] == "https://img.example/rel.jpg"
class TestConfiguredSourcePriority:
@pytest.fixture(autouse=True)
def _available_sources(self, monkeypatch):
monkeypatch.setattr(
"shelfmark.release_sources.list_available_sources",
lambda: [
{"name": "first", "enabled": True},
{"name": "second", "enabled": True},
{"name": "unavailable", "enabled": False},
],
)
def _configure(self, monkeypatch, priority):
monkeypatch.setattr(
auto_download, "app_config", _Config(AUTO_DOWNLOAD_SOURCE_PRIORITY=priority)
)
def test_respects_configured_order_and_skips_unusable_entries(self, monkeypatch):
self._configure(
monkeypatch,
[
{"id": "second"},
{"id": "first", "enabled": False},
{"id": "unavailable"},
{"id": "unknown"},
"not-a-dict",
],
)
assert auto_download._configured_source_priority() == ["second"]
@pytest.mark.parametrize("priority", [[], "garbage", ["not-a-dict"]])
def test_falls_back_to_all_enabled_sources_when_nothing_is_configured(
self, monkeypatch, priority
):
self._configure(monkeypatch, priority)
assert auto_download._configured_source_priority() == ["first", "second"]
@pytest.mark.parametrize(
"priority",
[
[{"id": "first", "enabled": False}, {"id": "second", "enabled": False}],
[{"id": "unavailable"}],
[{"id": "unknown"}],
],
)
def test_a_configured_list_with_nothing_usable_searches_nothing(self, monkeypatch, priority):
self._configure(monkeypatch, priority)
assert auto_download._configured_source_priority() == []
class TestAutoDownloadPending:
def test_disabled_returns_zeros_without_touching_the_db(self, monkeypatch):
monkeypatch.setattr(auto_download, "app_config", _Config(AUTO_DOWNLOAD_ENABLED=False))
class _UntouchableDb:
def list_requests(self, **_kwargs):
raise AssertionError("pending requests must not be queried")
summary = auto_download.auto_download_pending(
_UntouchableDb(), queue_release=lambda *_args, **_kwargs: (True, None)
)
assert summary == {"queued": 0, "no_match": 0, "in_library": 0, "skipped": 0, "error": 0}
class TestAutoDownloadRequest:
@pytest.fixture(autouse=True)
def _library_check_off(self, monkeypatch):
monkeypatch.setattr(auto_download, "app_config", _Config())
monkeypatch.setattr(
"shelfmark.core.library_index.is_in_library", lambda *_args, **_kwargs: False
)
def _run(self, user_db, row, admin, *, queue_release=None, sources=("first", "second")):
return auto_download.auto_download_request(
user_db,
row,
sources=list(sources),
content_type="audiobook",
min_seeders=1,
queue_release=queue_release or (lambda *_args, **_kwargs: (True, None)),
admin_user_id=admin["id"],
)
def test_queues_first_strict_match_via_fulfil_path(self, user_db, monkeypatch):
admin = user_db.create_user(username="admin", role="admin")
reader = user_db.create_user(username="reader", role="user")
row = _pending_request(user_db, reader["id"])
_stub_provider(monkeypatch, _book(cover_url="https://img.example/cover.jpg"))
chosen = _release(source="second", source_id="good", seeders=5)
searched = []
def _search(_book_arg, *, sources, **_kwargs):
searched.extend(sources)
if sources == ["first"]:
epub = _release(source="first", title="Dungeon Crawler Carl.epub", format="epub")
return [epub], {"first": [epub]}, []
return [chosen], {"second": [chosen]}, []
monkeypatch.setattr(auto_download, "search_book_releases", _search)
queued = []
def _queue(release_data, priority, *, user_id, username):
queued.append((release_data, priority, user_id, username))
return True, None
outcome = self._run(user_db, row, admin, queue_release=_queue)
assert outcome.status == "queued"
assert outcome.source == "second"
assert searched == ["first", "second"]
stored = user_db.get_request(row["id"])
assert stored["status"] == "fulfilled"
assert stored["delivery_state"] == "queued"
assert stored["reviewed_by"] == admin["id"]
assert stored["release_data"]["source_id"] == "good"
release_data, priority, user_id, username = queued[0]
assert release_data["_request_id"] == row["id"]
assert release_data["preview"] == "https://img.example/cover.jpg"
assert priority == 0
assert user_id == reader["id"]
assert username == "reader"
def test_queue_failure_rolls_request_back_to_pending(self, user_db, monkeypatch):
admin = user_db.create_user(username="admin", role="admin")
reader = user_db.create_user(username="reader", role="user")
row = _pending_request(user_db, reader["id"])
_stub_provider(monkeypatch, _book())
chosen = _release(source="first")
monkeypatch.setattr(
auto_download,
"search_book_releases",
lambda *_args, **_kwargs: ([chosen], {"first": [chosen]}, []),
)
outcome = self._run(
user_db, row, admin, queue_release=lambda *_args, **_kwargs: (False, "client down")
)
assert outcome.status == "error"
assert outcome.detail == "client down"
stored = user_db.get_request(row["id"])
assert stored["status"] == "pending"
assert stored["release_data"] is None
def test_no_strict_match_leaves_request_pending(self, user_db, monkeypatch):
admin = user_db.create_user(username="admin", role="admin")
reader = user_db.create_user(username="reader", role="user")
row = _pending_request(user_db, reader["id"])
_stub_provider(monkeypatch, _book())
wrong = _release(title="A Different Audiobook [M4B]")
monkeypatch.setattr(
auto_download,
"search_book_releases",
lambda *_args, **_kwargs: ([wrong], {"first": [wrong], "second": [wrong]}, []),
)
def _never_queue(*_args, **_kwargs):
raise AssertionError("nothing should be queued")
outcome = self._run(user_db, row, admin, queue_release=_never_queue)
assert outcome.status == "no_match"
assert user_db.get_request(row["id"])["status"] == "pending"
def test_book_already_in_library_is_skipped_before_searching(self, user_db, monkeypatch):
admin = user_db.create_user(username="admin", role="admin")
reader = user_db.create_user(username="reader", role="user")
row = _pending_request(user_db, reader["id"])
_stub_provider(monkeypatch, _book())
monkeypatch.setattr(auto_download, "app_config", _Config())
monkeypatch.setattr(
"shelfmark.core.library_index.is_in_library", lambda *_args, **_kwargs: True
)
def _never_search(*_args, **_kwargs):
raise AssertionError("release search must not run")
monkeypatch.setattr(auto_download, "search_book_releases", _never_search)
outcome = self._run(user_db, row, admin)
assert outcome.status == "in_library"
assert user_db.get_request(row["id"])["status"] == "pending"
def test_unregistered_provider_is_skipped(self, user_db, monkeypatch):
admin = user_db.create_user(username="admin", role="admin")
reader = user_db.create_user(username="reader", role="user")
row = _pending_request(user_db, reader["id"], provider="nope")
monkeypatch.setattr("shelfmark.metadata_providers.is_provider_registered", lambda _n: False)
outcome = self._run(user_db, row, admin)
assert outcome.status == "skipped"
assert outcome.detail == "no usable provider/id"
def test_pending_pass_without_an_admin_queues_nothing(user_db, monkeypatch):
monkeypatch.setattr(auto_download, "app_config", _Config(AUTO_DOWNLOAD_ENABLED=True))
user_db.create_user(username="reader", role="user")
summary = auto_download.auto_download_pending(
user_db, queue_release=lambda *_args, **_kwargs: (True, None)
)
assert summary == {"queued": 0, "no_match": 0, "in_library": 0, "skipped": 0, "error": 0}
+158
View File
@@ -0,0 +1,158 @@
"""Tests for the Hardcover sync scheduler's interval parsing and run gating."""
import threading
import pytest
from shelfmark.core import hardcover_scheduler as scheduler
class _Config:
def __init__(self, **values):
self.values = values
def get(self, key, default=None, user_id=None):
return self.values.get(key, default)
@pytest.fixture(autouse=True)
def _isolated_scheduler(monkeypatch):
monkeypatch.setattr(scheduler, "_ctx", {})
monkeypatch.setattr(scheduler, "_run_lock", threading.Lock())
monkeypatch.setattr(scheduler, "app_config", _Config())
def _configure(monkeypatch, **values):
monkeypatch.setattr(scheduler, "app_config", _Config(**values))
@pytest.mark.parametrize(
("interval", "unit", "expected"),
[
(30, "minutes", 1800),
(2, "hours", 7200),
("1.5", "hours", 5400),
(0.5, "minutes", 60),
(6, "fortnights", 6 * 3600),
("garbage", "hours", 6 * 3600),
([], "hours", 6 * 3600),
],
)
def test_interval_seconds(monkeypatch, interval, unit, expected):
_configure(monkeypatch, HARDCOVER_SYNC_INTERVAL=interval, HARDCOVER_SYNC_INTERVAL_UNIT=unit)
assert scheduler._interval_seconds() == expected
def test_interval_seconds_defaults_to_six_hours(monkeypatch):
_configure(monkeypatch)
assert scheduler._interval_seconds() == 6 * 3600
@pytest.mark.parametrize(
("values", "expected"),
[
({}, False),
({"HARDCOVER_SYNC_ENABLED": True}, True),
({"AUTO_DOWNLOAD_ENABLED": True}, True),
({"HARDCOVER_SYNC_ENABLED": False, "AUTO_DOWNLOAD_ENABLED": False}, False),
],
)
def test_enabled(monkeypatch, values, expected):
_configure(monkeypatch, **values)
assert scheduler._enabled() is expected
def test_run_once_before_configure_is_unconfigured():
assert scheduler.run_once() == {"status": "unconfigured"}
assert scheduler.run_once(force=True) == {"status": "unconfigured"}
def test_run_once_reports_busy_while_another_run_holds_the_lock():
with scheduler._run_lock:
assert scheduler.run_once(force=True) == {"status": "busy"}
assert scheduler.run_once() == {"status": "unconfigured"}
class TestRunOnce:
@pytest.fixture
def calls(self, monkeypatch):
recorded = {"sync": [], "auto": []}
def _sync(user_db, **kwargs):
recorded["sync"].append((user_db, kwargs))
return {"added": 1}
def _auto(user_db, **kwargs):
recorded["auto"].append((user_db, kwargs))
return {"queued": 1}
monkeypatch.setattr("shelfmark.core.hardcover_sync.sync_wishlist", _sync)
monkeypatch.setattr("shelfmark.core.auto_download.auto_download_pending", _auto)
return recorded
def test_scheduled_run_skips_sync_when_disabled(self, calls):
user_db, queue_release = object(), object()
scheduler.configure(user_db, queue_release, "/data/users.db")
result = scheduler.run_once()
assert result == {"status": "ok", "sync": None, "auto_download": {"queued": 1}}
assert calls["sync"] == []
assert calls["auto"] == [(user_db, {"queue_release": queue_release})]
def test_forced_run_syncs_regardless_of_toggle(self, calls):
user_db = object()
scheduler.configure(user_db, object(), "/data/users.db")
result = scheduler.run_once(force=True)
assert result == {"status": "ok", "sync": {"added": 1}, "auto_download": {"queued": 1}}
assert calls["sync"] == [(user_db, {"db_path": "/data/users.db"})]
def test_scheduled_run_syncs_when_enabled(self, monkeypatch, calls):
_configure(monkeypatch, HARDCOVER_SYNC_ENABLED=True)
user_db = object()
scheduler.configure(user_db, object(), None)
result = scheduler.run_once()
assert result["sync"] == {"added": 1}
assert calls["sync"] == [(user_db, {"db_path": None})]
def test_lock_is_released_after_a_run(self, calls):
scheduler.configure(object(), object(), None)
scheduler.run_once()
assert scheduler._run_lock.locked() is False
class TestTriggerAsync:
def test_returns_false_while_a_run_is_active(self, monkeypatch):
def _unexpected(**_kwargs):
raise AssertionError("run_once must not be scheduled")
monkeypatch.setattr(scheduler, "run_once", _unexpected)
with scheduler._run_lock:
assert scheduler.trigger_async() is False
@pytest.mark.parametrize("force", [True, False])
def test_runs_once_in_the_background_with_force_flag(self, monkeypatch, force):
finished = threading.Event()
seen = []
def _run_once(**kwargs):
seen.append(kwargs)
finished.set()
return {"status": "ok"}
monkeypatch.setattr(scheduler, "run_once", _run_once)
assert scheduler.trigger_async(force=force) is True
assert finished.wait(timeout=5)
assert seen == [{"force": force}]
+295
View File
@@ -0,0 +1,295 @@
"""Tests for syncing Hardcover status shelves into pending requests."""
import sqlite3
from types import SimpleNamespace
import pytest
from shelfmark.core import hardcover_sync
from shelfmark.core.user_db import UserDB
from shelfmark.core.utils import transform_cover_url
from shelfmark.metadata_providers import BookMetadata
class _Config:
def __init__(self, **values):
self.values = values
def get(self, key, default=None, user_id=None):
return self.values.get(key, default)
class _Provider:
"""Stub exposing the private paging method ``sync_wishlist`` relies on."""
def __init__(self, pages_by_status):
self.pages_by_status = pages_by_status
self.calls = []
def _fetch_current_user_books_by_status(self, status_id, page, limit):
self.calls.append((status_id, page, limit))
pages = self.pages_by_status.get(status_id, [])
books = pages[page - 1] if page <= len(pages) else []
return SimpleNamespace(books=books, has_more=page < len(pages))
def _book(provider_id, title, author="Matt Dinniman", **overrides):
fields = {
"provider": "hardcover",
"provider_id": provider_id,
"title": title,
"authors": [author],
}
fields.update(overrides)
return BookMetadata(**fields)
def _insert_history(db_path, *, title, author):
conn = sqlite3.connect(db_path)
try:
conn.execute(
"INSERT INTO download_history (task_id, source, title, author, final_status) "
"VALUES (?, ?, ?, ?, ?)",
(f"task-{title}", "prowlarr", title, author, "complete"),
)
conn.commit()
finally:
conn.close()
@pytest.fixture
def db_path(tmp_path):
return str(tmp_path / "users.db")
@pytest.fixture
def user_db(db_path):
db = UserDB(db_path)
db.initialize()
return db
def _configure(monkeypatch, **values):
monkeypatch.setattr(hardcover_sync, "app_config", _Config(**values))
@pytest.mark.parametrize(
("values", "expected"),
[
({"HARDCOVER_SYNC_TOKEN": " sync-token ", "HARDCOVER_API_KEY": "api"}, "sync-token"),
({"HARDCOVER_SYNC_TOKEN": "", "HARDCOVER_API_KEY": "api-key"}, "api-key"),
({}, ""),
],
)
def test_configured_token_falls_back_to_provider_api_key(monkeypatch, values, expected):
_configure(monkeypatch, **values)
assert hardcover_sync._configured_token() == expected
@pytest.mark.parametrize(
("raw", "expected"),
[
("3", [3]),
(" 2 ", [2]),
(["1", "3"], [1, 3]),
((1, "x", None), [1]),
("garbage", [1]),
([], [1]),
(42, [1]),
],
)
def test_configured_statuses_parsing(monkeypatch, raw, expected):
_configure(monkeypatch, HARDCOVER_SYNC_STATUSES=raw)
assert hardcover_sync._configured_statuses() == expected
def test_configured_statuses_default(monkeypatch):
_configure(monkeypatch)
assert hardcover_sync._configured_statuses() == [1]
def test_book_to_book_data_includes_optional_fields_only_when_present():
full = _book(
7,
"Dungeon Crawler Carl",
cover_url="https://img.example/c.jpg",
publish_year=2020,
subtitle="Book One",
search_author="Dinniman",
)
sparse = BookMetadata(provider="hardcover", provider_id="8", title="Untitled")
assert hardcover_sync._book_to_book_data(full, "audiobook") == {
"title": "Dungeon Crawler Carl",
"author": "Dinniman",
"provider": "hardcover",
"provider_id": "7",
"content_type": "audiobook",
"preview": transform_cover_url("https://img.example/c.jpg", "hardcover_7"),
"year": 2020,
"subtitle": "Book One",
}
assert hardcover_sync._book_to_book_data(sparse, "ebook") == {
"title": "Untitled",
"author": "Unknown",
"provider": "hardcover",
"provider_id": "8",
"content_type": "ebook",
}
def test_existing_provider_ids_reads_provider_id_or_id(user_db):
user = user_db.create_user(username="reader", role="user")
for book_data in (
{"title": "A", "author": "X", "provider": "hardcover", "provider_id": 11},
{"title": "B", "author": "X", "provider": "hardcover", "id": "22"},
{"title": "C", "author": "X", "provider": "openlibrary", "id": "OL22W"},
{"title": "D", "author": "X", "provider": "googlebooks", "provider_id": 33},
{"title": "E", "author": "X", "provider": "manual"},
):
user_db.create_request(
user_id=user["id"],
content_type="audiobook",
request_level="book",
policy_mode="request_book",
book_data=book_data,
)
assert hardcover_sync._existing_provider_ids(user_db) == {"11", "22"}
def test_already_downloaded_matches_history_case_insensitively(user_db, db_path):
_insert_history(db_path, title="Dungeon Crawler Carl", author="Matt Dinniman")
assert hardcover_sync._already_downloaded(db_path, "dungeon crawler CARL", "MATT dinniman")
assert not hardcover_sync._already_downloaded(db_path, "Dungeon Crawler Carl", "Someone")
assert not hardcover_sync._already_downloaded(None, "Dungeon Crawler Carl", "Matt Dinniman")
def test_already_downloaded_fails_open_without_history_table(tmp_path):
missing = str(tmp_path / "empty.db")
assert hardcover_sync._already_downloaded(missing, "Anything", "Anyone") is False
class TestSyncWishlist:
@pytest.fixture(autouse=True)
def _defaults(self, monkeypatch):
_configure(monkeypatch, HARDCOVER_SYNC_STATUSES="1")
monkeypatch.setattr(
"shelfmark.core.library_index.is_in_library", lambda *_args, **_kwargs: False
)
def _use_provider(self, monkeypatch, provider):
monkeypatch.setattr(hardcover_sync, "_build_provider", lambda: provider)
def test_adds_new_requests_across_pages(self, user_db, db_path, monkeypatch):
user = user_db.create_user(username="reader", role="user")
provider = _Provider(
{
1: [
[_book(1, "Dungeon Crawler Carl", cover_url="https://img.example/1.jpg")],
[_book(2, "Carl's Doomsday Scenario", publish_year=2021)],
]
}
)
self._use_provider(monkeypatch, provider)
summary = hardcover_sync.sync_wishlist(user_db, db_path=db_path, user_id=user["id"])
assert summary == {"added": 2, "skipped": 0, "in_library": 0, "errors": 0}
assert provider.calls == [(1, 1, 25), (1, 2, 25)]
rows = {row["book_data"]["provider_id"]: row for row in user_db.list_requests()}
assert set(rows) == {"1", "2"}
assert rows["1"]["status"] == "pending"
assert rows["1"]["content_type"] == "audiobook"
assert rows["1"]["book_data"]["preview"] == transform_cover_url(
"https://img.example/1.jpg", "hardcover_1"
)
assert rows["2"]["book_data"]["year"] == 2021
assert "preview" not in rows["2"]["book_data"]
def test_skips_known_downloaded_owned_and_duplicate_books(self, user_db, db_path, monkeypatch):
user = user_db.create_user(username="reader", role="user")
user_db.create_request(
user_id=user["id"],
content_type="audiobook",
request_level="book",
policy_mode="request_book",
book_data={
"title": "Already Requested",
"author": "Matt Dinniman",
"provider": "hardcover",
"provider_id": "10",
},
)
_insert_history(db_path, title="Already Downloaded", author="Matt Dinniman")
_configure(monkeypatch, HARDCOVER_SYNC_STATUSES="1")
monkeypatch.setattr(
"shelfmark.core.library_index.is_in_library",
lambda book, *_args, **_kwargs: book.title == "Already Owned",
)
provider = _Provider(
{
1: [
[
_book(10, "Already Requested"),
_book(11, "Already Downloaded"),
_book(12, "Already Owned"),
_book(13, "Brand New"),
_book(14, "Brand New"),
]
]
}
)
self._use_provider(monkeypatch, provider)
summary = hardcover_sync.sync_wishlist(user_db, db_path=db_path, user_id=user["id"])
assert summary == {"added": 1, "skipped": 3, "in_library": 1, "errors": 0}
titles = sorted(row["book_data"]["title"] for row in user_db.list_requests())
assert titles == ["Already Requested", "Brand New"]
def test_unavailable_provider_counts_as_error(self, user_db, db_path, monkeypatch):
self._use_provider(monkeypatch, None)
summary = hardcover_sync.sync_wishlist(user_db, db_path=db_path)
assert summary == {"added": 0, "skipped": 0, "in_library": 0, "errors": 1}
assert user_db.list_requests() == []
class TestResolveRequestOwner:
def test_returns_lowest_id_admin(self, user_db):
user_db.create_user(username="reader", role="user")
first_admin = user_db.create_user(username="ops", role="admin")
user_db.create_user(username="second", role="admin")
assert hardcover_sync.resolve_request_owner(user_db) == first_admin["id"]
def test_returns_none_without_admins(self, user_db):
user_db.create_user(username="reader", role="user")
assert hardcover_sync.resolve_request_owner(user_db) is None
def test_sync_without_admin_counts_an_error_and_creates_nothing(self, user_db, db_path):
user_db.create_user(username="reader", role="user")
summary = hardcover_sync.sync_wishlist(user_db, db_path=db_path)
assert summary == {"added": 0, "skipped": 0, "in_library": 0, "errors": 1}
assert user_db.list_requests() == []
def test_sync_defaults_to_the_lowest_id_admin(self, user_db, db_path, monkeypatch):
user_db.create_user(username="reader", role="user")
admin = user_db.create_user(username="ops", role="admin")
provider = _Provider({1: [[_book(1, "Dungeon Crawler Carl")]]})
monkeypatch.setattr(hardcover_sync, "_build_provider", lambda: provider)
summary = hardcover_sync.sync_wishlist(user_db, db_path=db_path)
assert summary["added"] == 1
assert user_db.list_requests()[0]["user_id"] == admin["id"]
+55
View File
@@ -117,3 +117,58 @@ def test_plan_receives_filters_and_user_id(monkeypatch: pytest.MonkeyPatch) -> N
"source_filters": None,
"user_id": 42,
}
def test_search_book_releases_preserves_source_order_and_aggregates(
monkeypatch: pytest.MonkeyPatch,
) -> None:
sources = {"a": _Source(result=["a1"]), "b": _Source(result=["b1", "b2"])}
monkeypatch.setattr("shelfmark.release_sources.get_source", lambda name: sources[name])
all_releases, by_source, errors = release_search.search_book_releases(
_book(), sources=["b", "a"], content_type="audiobook"
)
assert list(by_source) == ["b", "a"]
assert by_source == {"b": ["b1", "b2"], "a": ["a1"]}
assert all_releases == ["b1", "b2", "a1"]
assert errors == []
_book_arg, _plan, expand_search, content_type = sources["a"].calls[0]
assert expand_search is True
assert content_type == "audiobook"
def test_search_book_releases_collects_errors_and_keeps_healthy_results(
monkeypatch: pytest.MonkeyPatch,
) -> None:
sources = {
"down": _Source(error=SourceUnavailableError("offline")),
"ok": _Source(result=["r1"]),
}
def _get(name: str) -> Any:
if name not in sources:
msg = f"Unknown release source: {name}"
raise ValueError(msg)
return sources[name]
monkeypatch.setattr("shelfmark.release_sources.get_source", _get)
all_releases, by_source, errors = release_search.search_book_releases(
_book(), sources=["down", "ok", "nope"]
)
assert all_releases == ["r1"]
assert by_source == {"ok": ["r1"]}
assert errors == ["down: offline", "Unknown source: nope"]
def test_search_book_releases_with_no_sources_returns_empty(
monkeypatch: pytest.MonkeyPatch,
) -> None:
def _unexpected(_name: str) -> Any:
raise AssertionError("no source should be resolved")
monkeypatch.setattr("shelfmark.release_sources.get_source", _unexpected)
assert release_search.search_book_releases(_book(), sources=[]) == ([], {}, [])