mirror of
https://github.com/calibrain/shelfmark.git
synced 2026-11-16 15:08:13 +00:00
Searching a translated title on Open Library or Hardcover returned the original-language work. "Der Weg der Könige" came back as "The Way of Kings" (2010), and that English title then went into the release search and the download name. Both providers keep one record per work, titled after the original, and list translations underneath it. #483 already searches releases per language with Hardcover's localized titles, which fixed the release side of #360 when the matching language is selected. This PR fixes the search result itself, so the book a user picks is the translation they searched for, and it covers Open Library, the default provider, which has no titles_by_language. - Open Library: request the matched edition (editions.*) and prefer its title, ISBNs, cover, language and publication year when its title differs from the work's. - Hardcover: use the alternative title named in the hit's highlights (alternative_titles, best index first), without the original's subtitle. A hit whose title also matched keeps the work title. - /api/releases: when the modal sends a title that differs from the book record's, it becomes search_title. Before, Hardcover's search_title from the original won, so picking "Kinder des Nebels" still searched "The Final Empire" first. - Release cache: keyed on title and language as well as the provider ID. All translations share that ID, so opening "The Way of Kings" after "Der Weg der Könige" showed the German releases until a reload. Known limitations: neither provider has descriptions per edition, so the description stays the work's (usually English). Hardcover's search hits carry no year or language for alternative titles, so those stay the work's; Open Library's do not. Google Books is unaffected, since it already returns one volume per edition. Tests: Open Library edition parsing, Hardcover alternative-title matching, the /api/releases title override, and the release cache, each written to fail first. The Python suite passes apart from the 9 test_entrypoint_permissions.py tests, which fail the same way on an unmodified main on macOS. The frontend typecheck, lint, format check and unit tests pass. Checked on a live instance with Open Library, Hardcover and Google Books. Refs #360, #483 Disclaimer: This was developed with the help of AI but reviewed and tested as well as deployed on my server by a real human. --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from shelfmark.core.cache import get_metadata_cache
|
|
from shelfmark.metadata_providers import MetadataSearchOptions
|
|
from shelfmark.metadata_providers.hardcover import HardcoverProvider
|
|
|
|
# Hardcover indexes one book per work and keeps its translations' titles in
|
|
# `alternative_titles`. The hit's `highlights` say which field - and which entry of
|
|
# an array field - matched the query. Trimmed from a live response for
|
|
# "Der Weg der Könige".
|
|
WAY_OF_KINGS = {
|
|
"id": 978066,
|
|
"title": "The Way of Kings, Part 1",
|
|
"subtitle": "The Stormlight Archive Book One",
|
|
"alternative_titles": [
|
|
"Der Weg der Könige",
|
|
"The Way of Kings Part One",
|
|
"La Voie des rois, tome 1",
|
|
],
|
|
"author_names": ["Brandon Sanderson"],
|
|
"release_year": 2010,
|
|
}
|
|
|
|
|
|
def _hit(*highlighted: tuple[str, list[int]]) -> dict[str, Any]:
|
|
return {
|
|
"document": WAY_OF_KINGS,
|
|
"highlights": [{"field": field, "indices": indices} for field, indices in highlighted],
|
|
}
|
|
|
|
|
|
def _search(monkeypatch, query: str, hit: dict[str, Any]):
|
|
provider = HardcoverProvider(api_key="test-token")
|
|
monkeypatch.setattr(
|
|
provider,
|
|
"_execute_query",
|
|
lambda _query, _variables: {"search": {"results": {"hits": [hit], "found": 1}}},
|
|
)
|
|
books = provider.search(MetadataSearchOptions(query=query, limit=5))
|
|
assert len(books) == 1
|
|
return books[0]
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _fresh_search_cache():
|
|
"""Each test stubs a different response for the same book; don't reuse results."""
|
|
get_metadata_cache().clear()
|
|
yield
|
|
get_metadata_cache().clear()
|
|
|
|
|
|
class TestHardcoverMatchedAlternativeTitle:
|
|
def test_uses_the_translated_title_that_matched(self, monkeypatch):
|
|
"""Searching a translation by name should surface that translation's title.
|
|
|
|
The work's own title is the English original. Showing it - and handing it to
|
|
the release search and the download - would put the user's German search
|
|
back into English.
|
|
"""
|
|
book = _search(monkeypatch, "Der Weg der Könige", _hit(("alternative_titles", [0])))
|
|
|
|
assert book.title == "Der Weg der Könige"
|
|
assert book.search_title == "Der Weg der Könige"
|
|
# The subtitle belongs to the English title; it is not the translation's.
|
|
assert book.subtitle is None
|
|
|
|
def test_keeps_the_work_title_when_the_title_itself_matched(self, monkeypatch):
|
|
book = _search(
|
|
monkeypatch,
|
|
"The Way of Kings Part 1",
|
|
_hit(("title", []), ("alternative_titles", [1])),
|
|
)
|
|
|
|
assert book.title == "The Way of Kings, Part 1"
|
|
assert book.subtitle == "The Stormlight Archive Book One"
|
|
|
|
def test_keeps_the_work_title_without_highlights(self, monkeypatch):
|
|
book = _search(monkeypatch, "Sanderson", {"document": WAY_OF_KINGS})
|
|
|
|
assert book.title == "The Way of Kings, Part 1"
|