Files
shelfmark/tests/irc/test_handler.py
T
David Lacroix 5472c36c56 fix(irc): accept a channel setting written with its # prefix (#1722)
The IRC channel setting asks for the name without the `#`, and the
client adds it when it joins. Mine was stored as `#ebooks`, so Shelfmark
joined `##ebooks`, a different and nearly empty channel. The log said
`Joined ##ebooks - 1 servers online`, every search came back empty, and
nothing in the UI said why. With `ebooks` the same search joined
`#ebooks` with 26 servers and returned 9 results.

This strips whitespace and leading `#` where the two channel settings
are read: the release source (availability, search channel, audiobook
channel) and the download handler, which reads `IRC_CHANNEL` on its own.
The join, the message target and the cache key all get the bare name.

Tests are in `tests/irc/test_source.py` (main and audiobook channel) and
a new `tests/irc/test_handler.py`. With the source change reverted all
three fail (`assert '#ebooks' == 'ebooks'`). `make python-checks` is
clean.

One thing I left alone: the download handler always uses `IRC_CHANNEL`,
never `IRC_AUDIOBOOK_CHANNEL`, so an audiobook found in a separate
audiobook channel is requested in the main one.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* IRC channel settings now work consistently whether entered with or
without a leading `#`. Surrounding whitespace is also handled, helping
searches, audiobook routing, and downloads use the intended channel.
* Downloads connect using the normalized channel name while keeping the
channel request correctly formatted.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-11-15 18:27:14 -05:00

28 lines
1.3 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")