mirror of
https://github.com/calibrain/shelfmark.git
synced 2026-11-25 22:41:15 +00:00
Searches and downloads share one IRC connection and run side by side, which broke in three ways: - wait_for_dcc took the first search-results file to arrive, so a late file for an earlier query was cached under the current one. It now skips a results file whose name is for a different query. - A download failing on the shared socket closed it under a search in flight. The search then cached an empty answer, hiding the book for a day. Searches and downloads now hold one lock for the whole exchange. - A search that lost its connection returned [] and counted as a no-match pass, pushing the request into backoff. It now raises SourceUnavailableError, which auto-download already treats as an error, and caches nothing. This was written with AI assistance. I reviewed the changes and ran the tests above and verified that this fixed my issues with searching and downloading using IRC. *Generated with Claude Sonnet 5.5* <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Search results are matched against the requested title, ignoring punctuation and accents while rejecting partial-title matches. Filenames without the usual suffix are also recognized. * Results and “no results” notices from earlier searches are skipped, and denied searches report an error. * Connection drops and DCC read errors are reported as failures rather than empty results or timeouts. * Repeated identical searches are rate-limited before sending, and search time limits apply while waiting for results or downloads. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com> Co-authored-by: CaliBrain <calibrain@l4n.xyz>
91 lines
4.1 KiB
Python
91 lines
4.1 KiB
Python
"""What the IRC download handler joins and where it sends the request line."""
|
|
|
|
from threading import Event
|
|
from unittest.mock import MagicMock
|
|
|
|
from shelfmark.core.models import DownloadTask
|
|
from shelfmark.release_sources.irc import handler as irc_handler
|
|
|
|
|
|
def test_download_with_hash_prefixed_channel_setting_uses_the_bare_channel(monkeypatch):
|
|
"""A stored "#ebooks" sent the download request to "##ebooks", where no bot answers.
|
|
|
|
The handler reads IRC_CHANNEL itself, so it needs the same normalisation as search.
|
|
"""
|
|
client = MagicMock()
|
|
client.wait_for_dcc.return_value = None
|
|
manager = MagicMock()
|
|
manager.get_connection.return_value = client
|
|
monkeypatch.setattr(irc_handler, "connection_manager", manager)
|
|
values = {"IRC_SERVER": "irc.example.test", "IRC_CHANNEL": "#ebooks", "IRC_NICK": "reader"}
|
|
monkeypatch.setattr(irc_handler.config, "get", lambda key, default="": values.get(key, default))
|
|
task = DownloadTask(task_id="!bot Author - Title.epub", source="irc", title="t")
|
|
|
|
irc_handler.IRCDownloadHandler().download(task, Event(), lambda _p: None, lambda _s, _m: None)
|
|
|
|
assert manager.get_connection.call_args.kwargs["channel"] == "ebooks"
|
|
client.send_message.assert_called_once_with("#ebooks", "!bot Author - Title.epub")
|
|
|
|
|
|
def test_download_holds_the_connection_lock_while_requesting_the_file(monkeypatch):
|
|
"""A search and a download on one socket steal each other's replies, so they take turns."""
|
|
from shelfmark.release_sources.irc.connection_manager import connection_manager
|
|
|
|
held: list[bool] = []
|
|
client = MagicMock()
|
|
client.wait_for_dcc.return_value = None
|
|
|
|
def get_connection(**_kwargs):
|
|
held.append(connection_manager.io_lock.locked())
|
|
return client
|
|
|
|
monkeypatch.setattr(connection_manager, "get_connection", get_connection)
|
|
monkeypatch.setattr(connection_manager, "release_connection", lambda _client: None)
|
|
values = {"IRC_SERVER": "irc.example.test", "IRC_CHANNEL": "ebooks", "IRC_NICK": "reader"}
|
|
monkeypatch.setattr(irc_handler.config, "get", lambda key, default="": values.get(key, default))
|
|
task = DownloadTask(task_id="!bot Author - Title.epub", source="irc", title="t")
|
|
|
|
irc_handler.IRCDownloadHandler().download(task, Event(), lambda _p: None, lambda _s, _m: None)
|
|
|
|
assert held == [True]
|
|
assert not connection_manager.io_lock.locked()
|
|
|
|
|
|
def test_download_releases_the_connection_lock_when_it_fails(monkeypatch):
|
|
from shelfmark.release_sources.irc.connection_manager import connection_manager
|
|
|
|
def get_connection(**_kwargs):
|
|
msg = "connection refused"
|
|
raise OSError(msg)
|
|
|
|
monkeypatch.setattr(connection_manager, "get_connection", get_connection)
|
|
values = {"IRC_SERVER": "irc.example.test", "IRC_CHANNEL": "ebooks", "IRC_NICK": "reader"}
|
|
monkeypatch.setattr(irc_handler.config, "get", lambda key, default="": values.get(key, default))
|
|
task = DownloadTask(task_id="!bot Author - Title.epub", source="irc", title="t")
|
|
|
|
irc_handler.IRCDownloadHandler().download(task, Event(), lambda _p: None, lambda _s, _m: None)
|
|
|
|
assert not connection_manager.io_lock.locked()
|
|
|
|
|
|
def test_download_frees_the_connection_lock_before_the_transfer(monkeypatch):
|
|
from shelfmark.release_sources.irc.connection_manager import connection_manager
|
|
|
|
held: list[bool] = []
|
|
client = MagicMock()
|
|
offer = MagicMock(filename="Book.epub")
|
|
client.wait_for_dcc.return_value = offer
|
|
monkeypatch.setattr(connection_manager, "get_connection", lambda **_: client)
|
|
monkeypatch.setattr(connection_manager, "release_connection", lambda _c: None)
|
|
monkeypatch.setattr(
|
|
irc_handler, "download_dcc", lambda **_: held.append(connection_manager.io_lock.locked())
|
|
)
|
|
values = {"IRC_SERVER": "irc.example.test", "IRC_CHANNEL": "ebooks", "IRC_NICK": "reader"}
|
|
monkeypatch.setattr(irc_handler.config, "get", lambda key, default="": values.get(key, default))
|
|
task = DownloadTask(task_id="!bot Author - Title.epub", source="irc", title="t")
|
|
|
|
irc_handler.IRCDownloadHandler().download(task, Event(), lambda _p: None, lambda _s, _m: None)
|
|
|
|
assert held == [False]
|
|
assert not connection_manager.io_lock.locked()
|