From f135e5e8fb8fb1a8bb07b1c8d11f68323cdae9ea Mon Sep 17 00:00:00 2001 From: Roni Saguey Date: Sat, 14 Nov 2026 23:15:59 -0500 Subject: [PATCH] fix: keep the calibre title search an exact match Co-Authored-By: Shengcun --- skills/shelfmark/download_books.py | 2 +- tests/skills/test_check_calibre.py | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/skills/shelfmark/download_books.py b/skills/shelfmark/download_books.py index 11b9c9b0..f9930855 100755 --- a/skills/shelfmark/download_books.py +++ b/skills/shelfmark/download_books.py @@ -286,7 +286,7 @@ def calibre_title_search(title: str) -> str: `"` ends the quoted term and the rest is read as more query syntax. """ escaped = title.replace("\\", "\\\\").replace('"', '\\"') - return f'title:"{escaped}"' + return f'title:"={escaped}"' def check_calibre(title: str) -> bool: diff --git a/tests/skills/test_check_calibre.py b/tests/skills/test_check_calibre.py index 005b822a..8ac6b86c 100644 --- a/tests/skills/test_check_calibre.py +++ b/tests/skills/test_check_calibre.py @@ -58,8 +58,10 @@ def test_title_is_one_argument_and_one_query_term(title): assert not kwargs.get("shell"), "a shell must not re-parse the title" term = command[3] - assert term.startswith('title:"') and term.endswith('"'), term - inner = term[len('title:"'):-1] + # `=` keeps it an exact-title search; without it calibre does "contains", + # so "Dune" would match "Dune Messiah" and the book would be skipped. + assert term.startswith('title:"=') and term.endswith('"'), term + inner = term[len('title:"='):-1] # Nothing inside the term may end it early: every quote and every # backslash is escaped, and unescaping gives the title back. assert _unescape(inner) == title @@ -94,4 +96,13 @@ def _unescape(inner: str) -> str: def test_plain_title_is_unchanged_by_escaping(): module = _load_module() - assert module.calibre_title_search("Mistborn") == 'title:"Mistborn"' + assert module.calibre_title_search("Mistborn") == 'title:"=Mistborn"' + + +def test_search_stays_an_exact_title_match(): + """A contains-search would treat "Dune" as present when only + "Dune Messiah" is in the library, and the download would be skipped.""" + module = _load_module() + term = module.calibre_title_search("Dune") + assert term == 'title:"=Dune"', term + assert not term.startswith('title:"D'), "lost the exact-match marker"